Archived Please visit https://github.com/subbuv26/mockhttpclient
A fake http client that acts a http client and serves requests as per the needs of tests.
To test code that handles different kinds of http responses, this fake client comes handy. As the client gets configured with the desired responses, the code that gets tested receives the same responses in the same order.
go get github.com/subbuv26/fakehttpclient
The function to be tested (createAndVerifyResource) has the below functionality
- CREATE resource using POST call, which returns http OK
- GET the resource using GET call, which returns http Service Unavailable (Server Busy)
- retry to GET the resource, which returns http OK
- On Success return true, otherwise false
import fakehc "github.com/subbuv26/fakehttpclient"
resp1 := &http.Response{
StatusCode: 200,
Header: http.Header{},
Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
}
resp2 := &http.Response{
StatusCode: 503,
Header: http.Header{},
Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
}
responseMap := make(fakehc.ResponseConfigMap)
responseMap[http.MethodPost] = &ResponseConfig{}
responseMap[http.MethodPost].Responses = []*http.Response{resp1}
responseMap[http.MethodGet] = &ResponseConfig{}
responseMap[http.MethodGet].Responses = []*http.Response{resp2, resp1}
// Create Client
client, _ := NewFakeHTTPClient(responseMap)
// myRESTClient is the client that is going to be tested
myRESTClient.client = client
// createAndVerifyResource creates a resource with POST and verifies with GET
// with myRESTClient.client
ok := myRESTClient.createAndVerifyResource(Resource{})
if ok {
// Success
} else {
// Failed
}
The function to be tested (createResources) creates N number of resources by calling continuous POST calls
import fakehc "github.com/subbuv26/fakehttpclient"
N := 5
resp1 := &http.Response{
StatusCode: 200,
Header: http.Header{},
Body: ioutil.NopCloser(bytes.NewReader([]byte("body"))),
}
responseMap := make(fakehc.ResponseConfigMap)
responseMap[http.MethodPost] = &ResponseConfig{}
responseMap[http.MethodPost].Responses = []*http.Response{resp1}
responseMap[http.MethodPost].MaxRun = N
// Create Client
client, _ := NewFakeHTTPClient(responseMap)
// myRESTClient is the client that is going to be tested
myRESTClient.client = client
// createResources makes post calls using myRESTClient.client
ok := myRESTClient.createResources([]Resources)
if ok {
// Success
} else {
// Failed
}