A fluent testing library for mocking http apis.
- Mock the external http apis your code is calling.
- Verify/assert the calls your code performed
Create the API mock
func TestApiCall(t *testing.T) {
api := mockhttp.Api(t)
defer func() { api.Close() }()
//...
}
Stub endpoints
api.
Stub(http.MethodGet, "/endpoint").
WithJson(http.StatusOK, jsonStub).
Stub(http.MethodPost, "/endpoint").
WithStatusCode(http.StatusCreated)
Call the mocked API
resultBasedOnMockedResponses, err := codeCallingTheApi(api.GetUrl())
Verify the API invocations
calls := api.
Verify(http.MethodPost, "/endpoint").
HasBeenCalled(2)
expectCall1 := calls[0]
expectCall1.WithPayload(expectedPayload1)
expectCall2 := calls[1]
expectCall2.WithPayload(expectedPayload2)
package main
import (
"github.com/le-yams/gomockhttp"
"fmt"
"net/http"
"testing"
)
type FooDto struct {
Foo string `json:"foo"`
}
func TestApiCall(t *testing.T) {
// Arrange
api := mockhttp.Api(t)
defer func() { api.Close() }()
api.
Stub(http.MethodGet, "/foo").
WithJson(http.StatusOK, &FooDto{Foo: "bar"})
token := "testToken"
//Act
fooService := NewFooService(api.GetUrl(), token)
foo := fooService.GetFoo() // the code actually making the http call to the api endpoint
// Assert
if foo != "bar" {
t.Errorf("unexpected value: %s\n", foo)
}
api.
Verify(http.MethodGet, "/foo").
HasBeenCalledOnce().
WithHeader("Authorization", fmt.Sprintf("Bearer %s", token))
}