Documentation
¶
Overview ¶
Package testify provide a tool to mock http calls.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrNilResponse = errors.New("response cannot be nil")
ErrNilResponse when the response is nil.
Functions ¶
This section is empty.
Types ¶
type Caller ¶
Caller type for call(s) http testing with a http.Client without execute on the network call You need set the ExpectedRequest field. This fields should be asserted to have equal values: - Body - ContentLength - Form - Header - Method - URL - context.Context.
type HTTPClient ¶
type HTTPClient interface {
// Client returns a http.Client.
Client() *http.Client
// AddCall adds a Caller to the list of callers
// The callers list is ordered, so the first caller added will be the first to be executed
// By default, if the caller does not have a response or error set,
// a default response will be returned.
// The default response return a 200 OK with a empty body
// and a Content-Type header set to application/json.
AddCall(call Caller)
// SetDefaultResponse sets a default response to be returned by the http.Client.
// If no response or error are set.
SetDefaultResponse(response *http.Response) error
// RoundTrip method to implement http.RoundTripper interface.
RoundTrip(req *http.Request) (*http.Response, error)
// ExpectedCalls asserts that all calls were executed
// You need to call this method at the end of your test to ensure that all calls were executed.
ExpectedCalls()
}
HTTPClient type for call(s) http testing with a http.Client without execute on the network call. It is useful to test a code that makes http calls.
func NewHTTPClient ¶
func NewHTTPClient(t *testing.T) HTTPClient
NewHTTPClient create a new mock HTTPClient.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"testing"
"github.com/gofast-pkg/http/testify"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
urlTestRequest1 = "http://example.com/request_1"
urlTestRequest2 = "http://example.com/request_2"
)
// testedCode simulates a code that makes two http requests.
func testedCode(ctx context.Context, c *http.Client) error {
var err error
var r1 *http.Request
var r2 *http.Request
var resp1 *http.Response
var resp2 *http.Response
if r1, err = http.NewRequestWithContext(ctx, http.MethodGet, urlTestRequest1, http.NoBody); err != nil {
return err
}
if r2, err = http.NewRequestWithContext(ctx, http.MethodGet, urlTestRequest2, http.NoBody); err != nil {
return err
}
if resp1, err = c.Do(r1); err != nil {
return err
}
defer func() {
if err = resp1.Body.Close(); err != nil {
fmt.Printf("fail to close body: %s", err.Error())
}
}()
if resp1.StatusCode != http.StatusOK {
return assert.AnError
}
if resp2, err = c.Do(r2); err != nil {
return err
}
defer func() {
if err := resp2.Body.Close(); err != nil {
fmt.Printf("fail to close body: %s", err.Error())
}
}()
if resp2.StatusCode != http.StatusCreated {
return assert.AnError
}
return nil
}
func main() {
var err error
var expectedR1 *http.Request
var expectedR2 *http.Request
t := new(testing.T)
client := testify.NewHTTPClient(t)
ctx := context.Background()
if expectedR1, err = http.NewRequestWithContext(
ctx,
http.MethodGet,
urlTestRequest1,
http.NoBody); err != nil {
t.Fatal(err)
}
if expectedR2, err = http.NewRequestWithContext(
ctx,
http.MethodGet,
urlTestRequest2,
http.NoBody); err != nil {
t.Fatal(err)
}
client.AddCall(testify.Caller{
ExpectedRequest: expectedR1,
})
client.AddCall(testify.Caller{
ExpectedRequest: expectedR2,
Response: &http.Response{
StatusCode: http.StatusCreated,
},
})
err = testedCode(ctx, client.Client())
fmt.Printf("%v", err)
require.NoError(t, err)
}
Output: <nil>
Click to show internal directories.
Click to hide internal directories.