{"id":37213302,"url":"https://github.com/steinfletcher/gock","last_synced_at":"2026-01-15T00:36:19.067Z","repository":{"id":57591967,"uuid":"147719486","full_name":"steinfletcher/gock","owner":"steinfletcher","description":"Simple, expressive HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽","archived":false,"fork":true,"pushed_at":"2018-10-17T10:55:57.000Z","size":113,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T17:44:38.655Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"h2non/gock","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/steinfletcher.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-06T18:55:01.000Z","updated_at":"2024-06-20T17:44:38.656Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/steinfletcher/gock","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/steinfletcher/gock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Fgock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Fgock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Fgock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Fgock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/steinfletcher","download_url":"https://codeload.github.com/steinfletcher/gock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/steinfletcher%2Fgock/sbom","scorecard":{"id":850930,"data":{"date":"2025-08-11","repo":{"name":"github.com/steinfletcher/gock","commit":"e117ef66af56bb3b3a3b35a0b94063a5b4c2b3e3"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}}]},"last_synced_at":"2025-08-23T22:30:58.608Z","repository_id":57591967,"created_at":"2025-08-23T22:30:58.608Z","updated_at":"2025-08-23T22:30:58.608Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28439814,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-15T00:34:46.850Z","status":"ssl_error","status_checked_at":"2026-01-15T00:34:46.551Z","response_time":107,"last_error":"SSL_read: 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":[],"created_at":"2026-01-15T00:36:18.289Z","updated_at":"2026-01-15T00:36:19.059Z","avatar_url":"https://github.com/steinfletcher.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gock [![Build Status](https://travis-ci.org/h2non/gock.svg?branch=master)](https://travis-ci.org/h2non/gock) [![GitHub release](https://img.shields.io/badge/version-v1.0-orange.svg?style=flat)](https://github.com/h2non/gock/releases) [![GoDoc](https://godoc.org/github.com/h2non/gock?status.svg)](https://godoc.org/github.com/h2non/gock) [![Coverage Status](https://coveralls.io/repos/github/h2non/gock/badge.svg?branch=master)](https://coveralls.io/github/h2non/gock?branch=master) [![Go Report Card](https://img.shields.io/badge/go_report-A+-brightgreen.svg)](https://goreportcard.com/report/github.com/h2non/gock) [![license](https://img.shields.io/badge/license-MIT-blue.svg)]()\n\nVersatile HTTP mocking made easy in [Go](https://golang.org).\n\nHeavily inspired by [nock](https://github.com/node-nock/nock).\nSee also its Python port, [pook](https://github.com/h2non/pook).\n\nTo get started, take a look to the [examples](#examples).\n\n## Features\n\n- Simple, expressive, fluent API.\n- Semantic API DSL for declarative HTTP mock declarations.\n- Built-in helpers for easy JSON/XML mocking.\n- Supports persistent and volatile TTL-limited mocks.\n- Full regular expressions capable HTTP request mock matching.\n- Designed for both testing and runtime scenarios.\n- Match request by method, URL params, headers and bodies.\n- Extensible and pluggable HTTP matching rules.\n- Ability to switch between mock and real networking modes.\n- Ability to filter/map HTTP requests for accurate mock matching.\n- Supports map and filters to handle mocks easily.\n- Wide compatible HTTP interceptor using `http.RoundTripper` interface.\n- Works with any `net/http` compatible client, such as [gentleman](https://github.com/h2non/gentleman).\n- Network delay simulation (beta).\n- Extensible and hackable API.\n- Dependency free.\n\n## Installation\n\n```bash\ngo get -u gopkg.in/h2non/gock.v1\n```\n\n## API\n\nSee [godoc reference](https://godoc.org/github.com/h2non/gock) for detailed API documentation.\n\n## How it mocks\n\n1. Intercepts any HTTP outgoing request via `http.DefaultTransport` or custom `http.Transport` used by any `http.Client`.\n2. Matches outgoing HTTP requests against a pool of defined HTTP mock expectations in FIFO declaration order.\n3. If at least one mock matches, it will be used in order to compose the mock HTTP response.\n4. If no mock can be matched, it will resolve the request with an error, unless real networking mode is enable, in which case a real HTTP request will be performed.\n\n## Tips\n\n#### Testing\n\nDeclare your mocks before you start declaring the concrete test logic:\n\n```go\nfunc TestFoo(t *testing.T) {\n  defer gock.Off() // Flush pending mocks after test execution\n\n  gock.New(\"http://server.com\").\n    Get(\"/bar\").\n    Reply(200).\n    JSON(map[string]string{\"foo\": \"bar\"})\n\n  // Your test code starts here...\n}\n```\n\n#### Race conditions\n\nIf you're running concurrent code, be aware that your mocks are declared first to avoid unexpected\nrace conditions while configuring `gock` or intercepting custom HTTP clients.\n\n`gock` is not fully thread-safe, but sensible parts are.\nAny help making `gock` more reliable in this sense is appreciated.\n\n#### Define complex mocks first\n\nIf you're mocking a bunch of mocks in the same test suite, it's recommended to define the more\nconcrete mocks first, and then the generic ones.\n\nThis approach usually avoids matching unexpected generic mocks (e.g: specific header, body payload...) instead of the generic ones that performs less complex matches.\n\n#### Disable `gock` traffic interception once done\n\nIn other to minimize potential side effects within your test code, it's a good practice\ndisabling `gock` once you are done with your HTTP testing logic.\n\nA Go idiomatic approach for doing this can be using it in a `defer` statement, such as:\n\n```go\nfunction TestGock (t *testing.T) {\n\tdefer gock.Off()\n\n\t// ... my test code goes here\n}\n```\n\n#### Intercept an `http.Client` just once\n\nYou don't need to intercept multiple times the same `http.Client` instance.\n\nJust call `gock.InterceptClient(client)` once, typically at the beginning of your test scenarios.\n\n#### Restore an `http.Client` after interception\n\n**NOTE**: this is not required is you are using `http.DefaultClient` or `http.DefaultTransport`.\n\nAs a good testing pattern, you should call `gock.RestoreClient(client)` after running your test scenario, typically as after clean up hook.\n\nYou can also use a `defer` statement for doing it, as you do with `gock.Off()`, such as:\n\n```go\nfunction TestGock (t *testing.T) {\n\tdefer gock.Off()\n\tdefer gock.RestoreClient(client)\n\n\t// ... my test code goes here\n}\n```\n\n## Examples\n\nSee [examples](https://github.com/h2non/gock/tree/master/_examples) directory for more featured use cases.\n\n#### Simple mocking via tests\n\n```go\npackage test\n\nimport (\n  \"github.com/nbio/st\"\n  \"gopkg.in/h2non/gock.v1\"\n  \"io/ioutil\"\n  \"net/http\"\n  \"testing\"\n)\n\nfunc TestSimple(t *testing.T) {\n  defer gock.Off()\n\n  gock.New(\"http://foo.com\").\n    Get(\"/bar\").\n    Reply(200).\n    JSON(map[string]string{\"foo\": \"bar\"})\n\n  res, err := http.Get(\"http://foo.com/bar\")\n  st.Expect(t, err, nil)\n  st.Expect(t, res.StatusCode, 200)\n\n  body, _ := ioutil.ReadAll(res.Body)\n  st.Expect(t, string(body)[:13], `{\"foo\":\"bar\"}`)\n\n  // Verify that we don't have pending mocks\n  st.Expect(t, gock.IsDone(), true)\n}\n```\n\n#### Request headers matching\n\n```go\npackage test\n\nimport (\n  \"github.com/nbio/st\"\n  \"gopkg.in/h2non/gock.v1\"\n  \"io/ioutil\"\n  \"net/http\"\n  \"testing\"\n)\n\nfunc TestMatchHeaders(t *testing.T) {\n  defer gock.Off()\n\n  gock.New(\"http://foo.com\").\n    MatchHeader(\"Authorization\", \"^foo bar$\").\n    MatchHeader(\"API\", \"1.[0-9]+\").\n    HeaderPresent(\"Accept\").\n    Reply(200).\n    BodyString(\"foo foo\")\n\n  req, err := http.NewRequest(\"GET\", \"http://foo.com\", nil)\n  req.Header.Set(\"Authorization\", \"foo bar\")\n  req.Header.Set(\"API\", \"1.0\")\n  req.Header.Set(\"Accept\", \"text/plain\")\n\n  res, err := (\u0026http.Client{}).Do(req)\n  st.Expect(t, err, nil)\n  st.Expect(t, res.StatusCode, 200)\n  body, _ := ioutil.ReadAll(res.Body)\n  st.Expect(t, string(body), \"foo foo\")\n\n  // Verify that we don't have pending mocks\n  st.Expect(t, gock.IsDone(), true)\n}\n```\n\n#### JSON body matching and response\n\n```go\npackage test\n\nimport (\n  \"bytes\"\n  \"github.com/nbio/st\"\n  \"gopkg.in/h2non/gock.v1\"\n  \"io/ioutil\"\n  \"net/http\"\n  \"testing\"\n)\n\nfunc TestMockSimple(t *testing.T) {\n  defer gock.Off()\n\n  gock.New(\"http://foo.com\").\n    Post(\"/bar\").\n    MatchType(\"json\").\n    JSON(map[string]string{\"foo\": \"bar\"}).\n    Reply(201).\n    JSON(map[string]string{\"bar\": \"foo\"})\n\n  body := bytes.NewBuffer([]byte(`{\"foo\":\"bar\"}`))\n  res, err := http.Post(\"http://foo.com/bar\", \"application/json\", body)\n  st.Expect(t, err, nil)\n  st.Expect(t, res.StatusCode, 201)\n\n  resBody, _ := ioutil.ReadAll(res.Body)\n  st.Expect(t, string(resBody)[:13], `{\"bar\":\"foo\"}`)\n\n  // Verify that we don't have pending mocks\n  st.Expect(t, gock.IsDone(), true)\n}\n```\n\n#### Mocking a custom http.Client and http.RoundTripper\n\n```go\npackage test\n\nimport (\n  \"github.com/nbio/st\"\n  \"gopkg.in/h2non/gock.v1\"\n  \"io/ioutil\"\n  \"net/http\"\n  \"testing\"\n)\n\nfunc TestClient(t *testing.T) {\n  defer gock.Off()\n\n  gock.New(\"http://foo.com\").\n    Reply(200).\n    BodyString(\"foo foo\")\n\n  req, err := http.NewRequest(\"GET\", \"http://foo.com\", nil)\n  client := \u0026http.Client{Transport: \u0026http.Transport{}}\n  gock.InterceptClient(client)\n\n  res, err := client.Do(req)\n  st.Expect(t, err, nil)\n  st.Expect(t, res.StatusCode, 200)\n  body, _ := ioutil.ReadAll(res.Body)\n  st.Expect(t, string(body), \"foo foo\")\n\n  // Verify that we don't have pending mocks\n  st.Expect(t, gock.IsDone(), true)\n}\n```\n\n#### Enable real networking\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"gopkg.in/h2non/gock.v1\"\n  \"io/ioutil\"\n  \"net/http\"\n)\n\nfunc main() {\n  defer gock.Off()\n  defer gock.DisableNetworking()\n\n  gock.EnableNetworking()\n  gock.New(\"http://httpbin.org\").\n    Get(\"/get\").\n    Reply(201).\n    SetHeader(\"Server\", \"gock\")\n\n  res, err := http.Get(\"http://httpbin.org/get\")\n  if err != nil {\n    fmt.Errorf(\"Error: %s\", err)\n  }\n\n  // The response status comes from the mock\n  fmt.Printf(\"Status: %d\\n\", res.StatusCode)\n  // The server header comes from mock as well\n  fmt.Printf(\"Server header: %s\\n\", res.Header.Get(\"Server\"))\n  // Response body is the original\n  body, _ := ioutil.ReadAll(res.Body)\n  fmt.Printf(\"Body: %s\", string(body))\n\n  // Verify that we don't have pending mocks\n  st.Expect(t, gock.IsDone(), true)\n}\n```\n\n#### Debug intercepted http requests\n\n```go\npackage main\n\nimport (\n\t\"bytes\"\n\t\"gopkg.in/h2non/gock.v1\"\n\t\"net/http\"\n)\n\nfunc main() {\n\tdefer gock.Off()\n\tgock.Observe(gock.DumpRequest)\n\n\tgock.New(\"http://foo.com\").\n\t\tPost(\"/bar\").\n\t\tMatchType(\"json\").\n\t\tJSON(map[string]string{\"foo\": \"bar\"}).\n\t\tReply(200)\n\n\tbody := bytes.NewBuffer([]byte(`{\"foo\":\"bar\"}`))\n\thttp.Post(\"http://foo.com/bar\", \"application/json\", body)\n}\n\n```\n\n## Hacking it!\n\nYou can easily hack `gock` defining custom matcher functions with own matching rules.\n\nSee [add matcher functions](https://github.com/h2non/gock/blob/master/_examples/add_matchers/matchers.go) and [custom matching layer](https://github.com/h2non/gock/blob/master/_examples/custom_matcher/matcher.go) examples for further details.\n\n## License\n\nMIT - Tomas Aparicio\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteinfletcher%2Fgock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsteinfletcher%2Fgock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsteinfletcher%2Fgock/lists"}