mockhttp
A simple and expressive HTTP server mocking library for end-to-end tests in Go.
Installation
go get -d github.com/americanas-go/mockhttp
Example
The following example shows a Book API as the application under test, which calls an external ISBN database exposed with an REST interface.
Since mockhttp
only starts the server, you must have a way to inject the address of the mocked server into your application. For example, the Book API may use an environment variable BOOK_API_ISBN_URL
that is set to http://localhost:12000/
during the end-to-end test execution.
package e2e
import (
"net/url"
"strings"
"testing"
"github.com/americanas-go/mockhttp"
"net/http"
)
func TestCreateBook(t *testing.T) {
mockServer := mockhttp.NewMockServer(mockhttp.WithPort(12000))
mockServer.
Get("/isbn", mockhttp.MatchQueryParams(url.Values{"title": []string{"Foundation"}})).
Return(mockhttp.JSONBody(`{"isbn": "9780345317988"}`))
mockServer.Start(t)
defer mockServer.Teardown()
bookBody := strings.NewReader(`{"title": "Foundation"}`)
response, err := http.Post("http://localhost:9000/book", "application/json", bookBody)
if err != nil {
t.Error(err.Error())
return
}
if response.StatusCode != http.StatusOK {
t.Errorf("unexpected status code: %d", response.StatusCode)
}
mockServer.AssertExpectations()
}
Contributing
Every help is always welcome. Feel free do throw us a pull request, we'll do our best to check it out as soon as possible. But before that, let us establish some guidelines:
- This is an open source project so please do not add any proprietary code or infringe any copyright of any sort.
- Avoid unnecessary dependencies or messing up go.mod file.
- Be aware of golang coding style. Use a lint to help you out.
- Add tests to cover your contribution.
- Use meaningful messages to your commits.
- Use pull requests.
- At last, but also important, be kind and polite with the community.
Any submitted issue which disrespect one or more guidelines above, will be discarded and closed.
Released under the MIT License.