{"id":15965199,"url":"https://github.com/bwplotka/go-httpt","last_synced_at":"2025-09-05T08:31:30.795Z","repository":{"id":57487352,"uuid":"91459178","full_name":"bwplotka/go-httpt","owner":"bwplotka","description":"Awesome, quick golang HTTP client mocking! :sparkles:","archived":false,"fork":false,"pushed_at":"2019-10-11T05:53:08.000Z","size":16,"stargazers_count":5,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-15T08:03:54.251Z","etag":null,"topics":["game","golang","http-client","httptest","mock","roundtripper","testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bwplotka.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2017-05-16T13:03:57.000Z","updated_at":"2023-03-03T16:55:07.000Z","dependencies_parsed_at":"2022-08-28T18:01:06.815Z","dependency_job_id":null,"html_url":"https://github.com/bwplotka/go-httpt","commit_stats":null,"previous_names":["bplotka/go-httpt"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwplotka%2Fgo-httpt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwplotka%2Fgo-httpt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwplotka%2Fgo-httpt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bwplotka%2Fgo-httpt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bwplotka","download_url":"https://codeload.github.com/bwplotka/go-httpt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230582837,"owners_count":18248671,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["game","golang","http-client","httptest","mock","roundtripper","testing"],"created_at":"2024-10-07T17:40:22.817Z","updated_at":"2024-12-31T21:32:47.523Z","avatar_url":"https://github.com/bwplotka.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-httpt\n\n[![Build Status](https://travis-ci.org/Bplotka/go-httpt.svg?branch=master)](https://travis-ci.org/Bplotka/go-httpt) [![Go Report Card](https://goreportcard.com/badge/github.com/Bplotka/go-httpt)](https://goreportcard.com/report/github.com/Bplotka/go-httpt)\n\nStandard httptest package is great, but there is lot of boilerplate needed to mock your single HTTP response.\n\n*httpt* is a small library that provides quick request-response mock for your Golang tests!\n\n## Usage\n\n*httpt* provides `Server` struct. Using that you can specify any round trip scenario (on what request what round trip you want).\n\nFor example inside unit-test:\n```go\npackage somepackage\n\nimport (\n    \"net/http\"\n    \"testing\"\n    \n    \"github.com/Bplotka/go-httpt\"\n    \"github.com/Bplotka/go-httpt/rt\"\n    \"github.com/stretchr/testify/assert\"\n    \"github.com/stretchr/testify/require\"\n)\n\n\nfunc TestSomething(t *testing.T) {\n    request, err := http.NewRequest(...)\n    // handle err...\n            \n    s := httpt.NewServer(t)\n    s.On(httpt.GET, \"/test/path\").Push(rt.StringResponseFunc(http.StatusBadRequest, \"really_bad_request\"))\n    s.On(httpt.POST, httpt.AnyPath).Push(rt.JSONResponseFunc(http.StatusOK, []byte(`{\"error\": \"really_bad_request\"}`)))\n\n    testClient := s.HTTPClient()\n    // Pass testClient to your components for mocked HTTP calls...\n}\n```\n\nHaving the scenario we can pass mocked HTTP client anywhere. It is common for complex libraries to not use interface but \nrather enabling custom clients from context.Context e.g standard oauth2 package. In this case, we can just pass our httpt.Server's client:\n\n```go\nctx = context.WithValue(ctx, oauth2.HTTPClient, s.HTTPClient())\n\n// Pass ctx into oauth component...\n```\n\nUsing that pattern is really convenient. Imagine now using such libraries (that take custom clients and make lots of HTTP requests internally) within\nHTTP Handler itself. To properly test your Server with HTTP handlers in your unit test you can use this approach:\n\n```go\npackage somepackage\n\nimport (\n    \"context\"\n    \"net/http\"\n    \"net/http/httptest\"\n    \"testing\"\n    \n    \"github.com/Bplotka/go-httpt\"\n    \"github.com/Bplotka/go-httpt/rt\"\n    \"github.com/stretchr/testify/assert\"\n    \"github.com/stretchr/testify/require\"\n    \"golang.org/x/oauth2\"\n)\n\n\nfunc TestYourServer(t *testing.T) {\n    request, err := http.NewRequest(...)\n    // handle err...\n    \n    s := httpt.NewServer(t)\n    s.On(httpt.GET, \"/test/path\").Push(rt.StringResponseFunc(http.StatusBadRequest, \"really_bad_request\"))\n    s.On(httpt.POST, httpt.AnyPath).Push(rt.JSONResponseFunc(http.StatusOK, []byte(`{\"error\": \"really_bad_request\"}`)))\n\n    rec := httptest.NewRecorder()\n    yourServer.ServeHTTP(\n        rec,\n        // Pass test HTTP client.\n        request.WithContext(\n            context.WithValue(context.TODO(), oauth2.HTTPClient, s.HTTPClient()),\n        ),\n    )\n    // ...\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbwplotka%2Fgo-httpt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbwplotka%2Fgo-httpt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbwplotka%2Fgo-httpt/lists"}