func (h *mockResolveHandler) Method() string {
	return http.MethodGet
}
Method returns the HTTP method.
package httpserver
import (
	"bytes"
	"context"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"
	"github.com/stretchr/testify/require"
	"github.com/trustbloc/sidetree-core-go/pkg/restapi/common"
)
const (
	url       = "localhost:8080"
	clientURL = "http://" + url
	samplePath = "/sample"
)
func TestServer_Start(t *testing.T) {
	s := New(url,
		"",
		"",
		&mockUpdateHandler{},
		&mockResolveHandler{},
	)
	require.NoError(t, s.Start())
	require.Error(t, s.Start())
	time.Sleep(time.Second)
	t.Run("success - sample operation ", func(t *testing.T) {
		resp, err := httpPut(t, clientURL+samplePath, []byte(""))
		require.NoError(t, err)
		require.NotNil(t, resp)
	})
	t.Run("success - sample resolution", func(t *testing.T) {
		resp, err := httpGet(t, clientURL+samplePath+"/id")
		require.NoError(t, err)
		require.NotNil(t, resp)
	})
	t.Run("success - health check", func(t *testing.T) {
		b := &httptest.ResponseRecorder{}
		healthCheckHandler(b, nil)
		require.Equal(t, http.StatusOK, b.Code)
	})
	t.Run("Stop", func(t *testing.T) {
		require.NoError(t, s.Stop(context.Background()))
		require.Error(t, s.Stop(context.Background()))
	})
}
func httpPut(t *testing.T, url string, req []byte) ([]byte, error) {
	t.Helper()
	client := &http.Client{}
	httpReq, err := http.NewRequest("POST", url, bytes.NewReader(req))
	require.NoError(t, err)
	httpReq.Header.Set("Content-Type", "application/json")
	resp, err := invokeWithRetry(
		func() (response *http.Response, e error) {
			return client.Do(httpReq)
		},
	)
	require.NoError(t, err)
	return handleHTTPResp(resp)
}
func httpGet(t *testing.T, url string) ([]byte, error) {
	t.Helper()
	client := &http.Client{}
	httpReq, err := http.NewRequest(http.MethodGet, url, nil)
	require.NoError(t, err)
	resp, err := invokeWithRetry(
		func() (response *http.Response, e error) {
			return client.Do(httpReq)
		},
	)
	require.NoError(t, err)
	return handleHTTPResp(resp)
}
func handleHTTPResp(resp *http.Response) ([]byte, error) {
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("reading response body failed: %w", err)
	}
	if status := resp.StatusCode; status != http.StatusOK {
		return nil, fmt.Errorf(string(body))
	}
	return body, nil
}
func invokeWithRetry(invoke func() (*http.Response, error)) (*http.Response, error) {
	remainingAttempts := 20
	for {
		resp, err := invoke()
		if err == nil {
			return resp, nil
		}
		remainingAttempts--
		if remainingAttempts == 0 {
			return nil, err
		}
		time.Sleep(100 * time.Millisecond)
	}
}
type mockUpdateHandler struct{}
func (h *mockUpdateHandler) Path() string {
	return samplePath
}
func (h *mockUpdateHandler) Method() string {
	return http.MethodPost
}
func (h *mockUpdateHandler) Handler() common.HTTPRequestHandler {
	return func(writer http.ResponseWriter, request *http.Request) {
	}
}
type mockResolveHandler struct{}
func (h *mockResolveHandler) Path() string {
	return samplePath + "/{id}"
}