Httpmock
Easily mock your HTTP clients in your Go code.
Install / Use
/learn @oupo1337/HttpmockREADME
httpmock
HTTPMock is a library to easily mock your http clients and describe their behavior.
It's designed to be as simple as possible, first you describe the client behavior:
- How many calls should it make? On which path?
- What HTTP status codes should it return?
- Should it receive headers? Query parameters? A body?
You can now use the mock client as a regular HTTP client in your code.
Finally, you check that all expected calls were done with the mock client by the tested code.
Examples
Basic
func Test_basic(t *testing.T) {
// We declare a mock variable and its behavior.
// It's an HTTP client waiting for a GET request on /path.
// It will return a 200 status code.
mock := httpmock.New(t).
WithRequest(http.MethodGet, "/path",
httpmock.ReturnStatus(http.StatusOK),
)
// Do something with mock variable (It's a regular http.Client object).
doSomething(mock)
// We check that all expected requests were done.
mock.AssertExpectations()
}
// Or you can declare your mock in a different way without using option functions
// similar way to the testify/mock package.
func Test_basic_bis(t *testing.T) {
mock := httpmock.New(t)
mock.On(http.MethodGet, "/path").ReturnStatus(http.StatusOK)
doSomething(mock)
mock.AssertExpectations()
}
Advanced
func Test_advanced(t *testing.T) {
// We declare a mock variable and its behavior.
// It's an HTTP client waiting for a POST request on /form.
// It will return a 201 status code.
// It expects a body, an authorization header and will return a body.
//
// It's also waiting for a DELETE request on /route
// returning a 204 status code.
// It expects a query param.
mock := httpmock.New(t).
WithRequest(http.MethodPost, "/form",
httpmock.ExpectBody(`{"some": "data"}`),
httpmock.ExpectHeader("Authorization", []string{"Bearer token"}),
httpmock.ReturnStatus(http.StatusCreated),
httpmock.ReturnBody(`{"a": "response"}`),
).
WithRequest(http.MethodDelete, "/route",
httpmock.ExpectQueryParam("param", "value"),
httpmock.ReturnStatus(http.StatusNoContent),
)
// Do something with the mock variable (It's a regular http.Client object).
doSomething(mock)
// We check that all expected requests were made.
mock.AssertExpectations()
}
// Or you can declare your mock in a different way without using option functions
// similar way to the testify/mock package.
func Test_advanced_bis(t *testing.T) {
mock := httpmock.New(t)
mock.On(http.MethodPost, "/form").
ExpectBody(`{"some": "data"}`).
ExpectHeader("Authorization", []string{"Bearer token"}).
ReturnStatus(http.StatusCreated).
ReturnBody(`{"a": "response"}`)
mock.On(http.MethodDelete, "/route").
ExpectQueryParam("param", "value").
ReturnStatus(http.StatusNoContent)
doSomething(mock)
mock.AssertExpectations()
}
More examples
See example file here
Documentation
Options functions
| Name | Description | Type | |------------------------|--------------------------------------------------------------------------------------------------|------------------| | ReturnStatus | Sets the http status code returned by the request. | int | | ReturnBodyRaw | Sets the body returned by the request. | string | | ReturnBodyFromObject | Sets the body returned by the request from an object. (Using json.Marshal function) | interface{} | | ReturnHeader | Sets an header to be returned by the request. | string, []string | | ReturnError | Sets an error returned by the http client. | error | | ExpectBody | Will expect a body in the received request and asserts that strings are equal. | string | | ExpectJSON | Will expect a body in the received request and asserts that the JSONs are equal. | string | | ExpectHeader | Will expect a header in the received request and asserts that the name and value are equal. | string, string | | ExpectQueryParamValues | Will expect a query param in the received request and assert that the name and values are equal. | string, []string | | ExpectQueryParam | Will expect a query param in the received request and assert that the name and values are equal. | string, string |
Related Skills
gh-issues
336.2kFetch GitHub issues, spawn sub-agents to implement fixes and open PRs, then monitor and address PR review comments. Usage: /gh-issues [owner/repo] [--label bug] [--limit 5] [--milestone v1.0] [--assignee @me] [--fork user/repo] [--watch] [--interval 5] [--reviews-only] [--cron] [--dry-run] [--model glm-5] [--notify-channel -1002381931352]
node-connect
336.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
336.2kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
82.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
