{"id":37101318,"url":"https://github.com/oupo1337/httpmock","last_synced_at":"2026-01-14T12:18:39.973Z","repository":{"id":64301182,"uuid":"556459600","full_name":"oupo1337/httpmock","owner":"oupo1337","description":"Easily mock your HTTP clients in your Go code.","archived":false,"fork":false,"pushed_at":"2023-05-16T07:15:19.000Z","size":55,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-06-20T10:06:36.497Z","etag":null,"topics":["go","golang","http","http-client","http-requests","mock","mocking","mocks","test","test-driven-development","testing","testing-library","testing-tools","tests"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oupo1337.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-23T22:28:52.000Z","updated_at":"2024-03-19T09:05:41.000Z","dependencies_parsed_at":"2024-06-20T09:18:19.379Z","dependency_job_id":"5b95de5c-ec89-4512-ad2e-29c46f46b98c","html_url":"https://github.com/oupo1337/httpmock","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/oupo1337/httpmock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oupo1337%2Fhttpmock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oupo1337%2Fhttpmock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oupo1337%2Fhttpmock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oupo1337%2Fhttpmock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oupo1337","download_url":"https://codeload.github.com/oupo1337/httpmock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oupo1337%2Fhttpmock/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28420030,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T10:47:48.104Z","status":"ssl_error","status_checked_at":"2026-01-14T10:46:19.031Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","http","http-client","http-requests","mock","mocking","mocks","test","test-driven-development","testing","testing-library","testing-tools","tests"],"created_at":"2026-01-14T12:18:39.300Z","updated_at":"2026-01-14T12:18:39.951Z","avatar_url":"https://github.com/oupo1337.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# httpmock\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/oupo1337/httpmock)](https://goreportcard.com/report/github.com/oupo1337/httpmock)\n\nHTTPMock is a library to easily mock your http clients and describe their behavior.\n\nIt's designed to be as simple as possible, first you describe the client behavior:\n - How many calls should it make? On which path?\n - What HTTP status codes should it return?\n - Should it receive headers? Query parameters? A body?\n\nYou can now use the mock client as a regular HTTP client in your code.\n\nFinally, you check that all expected calls were done with the mock client by the tested code.\n\n## Examples\n\n### Basic\n\n```go\nfunc Test_basic(t *testing.T) {\n    // We declare a mock variable and its behavior.\n    // It's an HTTP client waiting for a GET request on /path.\n    // It will return a 200 status code.\n    mock := httpmock.New(t).\n        WithRequest(http.MethodGet, \"/path\",\n            httpmock.ReturnStatus(http.StatusOK),\n        )\n\n    // Do something with mock variable (It's a regular http.Client object).\n    doSomething(mock)\n\n    // We check that all expected requests were done.\n    mock.AssertExpectations()\n}\n\n// Or you can declare your mock in a different way without using option functions\n// similar way to the testify/mock package.\nfunc Test_basic_bis(t *testing.T) {\n\tmock := httpmock.New(t)\n\tmock.On(http.MethodGet, \"/path\").ReturnStatus(http.StatusOK)\n\n\tdoSomething(mock)\n\tmock.AssertExpectations()\n}\n```\n\n### Advanced\n\n```go\nfunc Test_advanced(t *testing.T) {\n    // We declare a mock variable and its behavior.\n    // It's an HTTP client waiting for a POST request on /form.\n    // It will return a 201 status code.\n    // It expects a body, an authorization header and will return a body.\n    //\n    // It's also waiting for a DELETE request on /route\n    // returning a 204 status code.\n    // It expects a query param.\n    mock := httpmock.New(t).\n        WithRequest(http.MethodPost, \"/form\",\n            httpmock.ExpectBody(`{\"some\": \"data\"}`),\n            httpmock.ExpectHeader(\"Authorization\", []string{\"Bearer token\"}),\n            httpmock.ReturnStatus(http.StatusCreated),\n            httpmock.ReturnBody(`{\"a\": \"response\"}`),\n        ).\n        WithRequest(http.MethodDelete, \"/route\",\n            httpmock.ExpectQueryParam(\"param\", \"value\"),\n            httpmock.ReturnStatus(http.StatusNoContent),\n        )\n\n    // Do something with the mock variable (It's a regular http.Client object).\n    doSomething(mock)\n\n    // We check that all expected requests were made.\n    mock.AssertExpectations()\n}\n\n// Or you can declare your mock in a different way without using option functions\n// similar way to the testify/mock package.\nfunc Test_advanced_bis(t *testing.T) {\n\tmock := httpmock.New(t)\n\tmock.On(http.MethodPost, \"/form\").\n\t\tExpectBody(`{\"some\": \"data\"}`).\n\t\tExpectHeader(\"Authorization\", []string{\"Bearer token\"}).\n\t\tReturnStatus(http.StatusCreated).\n\t\tReturnBody(`{\"a\": \"response\"}`)\n\n\tmock.On(http.MethodDelete, \"/route\").\n\t\tExpectQueryParam(\"param\", \"value\").\n\t\tReturnStatus(http.StatusNoContent)\n\n\tdoSomething(mock)\n\tmock.AssertExpectations()\n}\n```\n\n### More examples\n\nSee example file [here](examples/example_test.go)\n\n## Documentation\n\n### Options functions\n\n| Name                   | Description                                                                                      | Type             |\n|------------------------|--------------------------------------------------------------------------------------------------|------------------|\n| ReturnStatus           | Sets the http status code returned by the request.                                               | int              |\n| ReturnBodyRaw          | Sets the body returned by the request.                                                           | string           |\n| ReturnBodyFromObject   | Sets the body returned by the request from an object. (Using json.Marshal function)              | interface{}      |\n| ReturnHeader           | Sets an header to be returned by the request.                                                    | string, []string |\n| ReturnError            | Sets an error returned by the http client.                                                       | error            |\n| ExpectBody             | Will expect a body in the received request and asserts that strings are equal.                   | string           |\n| ExpectJSON             | Will expect a body in the received request and asserts that the JSONs are equal.                 | string           |\n| ExpectHeader           | Will expect a header in the received request and asserts that the name and value are equal.      | string, string   |\n| ExpectQueryParamValues | Will expect a query param in the received request and assert that the name and values are equal. | string, []string |\n| ExpectQueryParam       | Will expect a query param in the received request and assert that the name and values are equal. | string, string   |\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foupo1337%2Fhttpmock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foupo1337%2Fhttpmock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foupo1337%2Fhttpmock/lists"}