{"id":23539854,"url":"https://github.com/cateiru/go-http-easy-test","last_synced_at":"2025-10-27T16:22:28.927Z","repository":{"id":104285926,"uuid":"513072816","full_name":"cateiru/go-http-easy-test","owner":"cateiru","description":"A package that wraps `net/http/httptest` and allows you to easily test HTTP Handlers.","archived":false,"fork":false,"pushed_at":"2023-03-23T14:14:18.000Z","size":453,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-01T13:05:32.371Z","etag":null,"topics":["go","golang","httptest"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/cateiru/go-http-easy-test/v2","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/cateiru.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-12T09:08:14.000Z","updated_at":"2023-03-21T08:52:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"72d6b4d6-1967-4aa7-91a8-0d77c0d163e0","html_url":"https://github.com/cateiru/go-http-easy-test","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/cateiru/go-http-easy-test","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cateiru%2Fgo-http-easy-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cateiru%2Fgo-http-easy-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cateiru%2Fgo-http-easy-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cateiru%2Fgo-http-easy-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cateiru","download_url":"https://codeload.github.com/cateiru/go-http-easy-test/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cateiru%2Fgo-http-easy-test/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262969885,"owners_count":23392529,"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":["go","golang","httptest"],"created_at":"2024-12-26T04:40:40.924Z","updated_at":"2025-10-15T05:32:25.186Z","avatar_url":"https://github.com/cateiru.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go http easy test\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/cateiru/go-http-easy-test.svg)](https://pkg.go.dev/github.com/cateiru/go-http-easy-test) [![Go Report Card](https://goreportcard.com/badge/github.com/cateiru/go-http-easy-test)](https://goreportcard.com/report/github.com/cateiru/go-http-easy-test) [![Go](https://github.com/cateiru/go-http-easy-test/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/cateiru/go-http-easy-test/actions/workflows/go.yml) [![codecov](https://codecov.io/gh/cateiru/go-http-easy-test/branch/main/graph/badge.svg?token=3yN9nRKyvb)](https://codecov.io/gh/cateiru/go-http-easy-test)\n\nA package that wraps `net/http/httptest` and allows you to easily test HTTP Handlers.\n\n✅ Easy\u003cbr/\u003e\n✅ Intuitive\u003cbr/\u003e\n✅ Support `application/json`\u003cbr/\u003e\n✅ Support `application/x-www-form-urlencoded`\u003cbr/\u003e\n✅ Support `multipart/form-data`\u003cbr/\u003e\n✅ Support [Echo package](https://echo.labstack.com/)\u003cbr/\u003e\n✅ Support cookie\u003cbr/\u003e\n\n## Install\n\n```bash\ngo get -u github.com/cateiru/go-http-easy-test/v2\n```\n\n## Mock\n\nThe user can choose from the following two options.\n\n- Actually start the server using `httptest.NewServer`\n- Mock the Handler arguments (`w http.ResponseWriter, r *http.Request`)\n\n### Actually start the server using `httptest.NewServer`\n\n```go\npackage main_test\n\nimport (\n    \"testing\"\n\n    \"github.com/cateiru/go-http-easy-test/easy\"\n)\n\nfunc Handler(w http.ResponseWriter, r *http.Request) {\n    ...do something\n}\n\nfunc TestHandler(t *testing.T) {\n    mux := http.NewServeMux()\n    mux.HandleFunc(\"/\", Handler)\n\n    // create server\n    s := easy.NewMockServer(mux)\n    // Start the server with TLS using:\n    // s := server.TestNewMockTLSServer(mux)\n    defer s.Close()\n\n    // Option: You can set cookies.\n    cookie := \u0026http.Cookie{\n        Name:  \"name\",\n        Value: \"value\",\n    }\n    s.Cookie([]*http.Cookie{\n        cookie,\n    })\n\n    // GET\n    resp := s.Get(t, \"/\")\n    resp := s.GetOK(t, \"/\")\n\n    // POST\n    resp := s.Post(t, \"/\", \"text/plain\", body)\n    resp := s.PostForm(t, \"/\", url) // application/x-www-form-urlencoded\n    resp := s.PostJson(t, \"/\", obj) // application/json\n    resp := s.PostString(t, \"/\", \"text/plain\", body)\n\n    // Easily build multipart/form-data\n    form := easy.NewMultipart()\n    form.Insert(\"key\", \"value\")\n    resp := s.PostFormData(t, \"/\", form)\n    resp := s.FormData(t, \"/\", \"[method]\", form)\n\n    // Other\n    resp := s.Do(t, \"/\", \"[method]\", body)\n\n    // The `resp` of all return values are easy to compare.\n    // Check status\n    resp.Ok(t)\n    resp.Status(t, 200)\n\n    // get body\n    body := resp.Body().String()\n\n    // Compare response body\n    resp.EqBody(t, body)\n    resp.EqJson(t, obj)\n\n    // prase response json\n    body := new(JsonType)\n    err := resp.Json(body)\n\n    // returns Set-Cookie headers\n    cookies := resp.SetCookies()\n}\n```\n\n### Mock the Handler arguments (`w http.ResponseWriter, r *http.Request`)\n\n```go\npackage main_test\n\nimport (\n    \"testing\"\n\n    \"github.com/cateiru/go-http-easy-test/easy\"\n)\n\nfunc Handler(w http.ResponseWriter, r *http.Request) {\n    ...do something\n}\n\nfunc EchoHandler(c echo.Context) error {\n    ...do something\n}\n\nfunc TestHandler(t *testing.T) {\n    // Default\n    m, err := easy.NewMock(body, http.MethodGet, \"/\")\n    m, err := easy.NewMockReader(reader, http.MethodGet, \"/\")\n\n    // GET\n    m, err := easy.NewGet(body, \"/\")\n\n    // POST or PUT send json\n    m, err := easy.NewJson(\"/\", data, http.MethodPost)\n\n    // POST or PUT send x-www-form-urlencoded\n    m, err := easy.NewURLEncoded(\"/\", url, http.MethodPost)\n\n    // POST or PUT send multipart/form-data\n    // Easily build multipart/form-data using the `contents` package.\n    m, err := easy.NewFormData(\"/\", multipart, http.MethodPost)\n\n\n    // Option: set remote addr\n    m.SetAddr(\"203.0.113.0\")\n\n    // Option: You can set cookies.\n    cookie := \u0026http.Cookie{\n        Name:  \"name\",\n        Value: \"value\",\n    }\n    m.Cookie([]*http.Cookie{\n        cookie,\n    })\n\n    // Set handler and run\n    m.Handler(Handler)\n\n    // Use echo package\n    echoCtx := m.Echo()\n    err := EchoHandler(echoCtx)\n\n    // check response\n    m.Ok(t)\n    m.Status(t, 200)\n\n    // Compare response body\n    m.EqBody(t, body)\n    m.EqJson(t, obj)\n\n        // prase response json\n    body := new(JsonType)\n    err := m.Json(body)\n\n    // returns Set-Cookie headers\n    cookies := m.SetCookies()\n\n    // Return http.Response\n    response := m.Response()\n\n    // set-cookie\n    cookie := m.FindCookie(\"name\")\n}\n```\n\n### multipart\n\nEasily create `multipart/form-data` requests.\u003cbr/\u003e\nThis method is used when submitting with `multipart/form-data`.\n\n```go\npackage main\n\nimport (\n    \"os\"\n\n    \"github.com/cateiru/go-http-easy-test/easy\"\n)\n\n\nfunc main() {\n    m := easy.NewMultipart()\n\n    // Add a string format form.\n    err := m.Insert(\"key\", \"value\")\n\n    // Add a file format form.\n    file, err := os.Open(\"path\")\n    err := m.InsertFile(\"key\", file)\n\n    // Outputs in the specified format.\n    body := m.Export()\n    contentType := m.ContentType()\n\n    // Use `handler` package\n    // Actually start the server using `httptest.NewServer`\n    s := server.NewMockServer(mux)\n    defer s.Close()\n    resp := s.PostFormData(t, \"/\", m)\n    // Mock the Handler arguments (`w http.ResponseWriter, r *http.Request`)\n    m, err := mock.NewFormData(\"/\", m, http.MethodPost)\n}\n\n```\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcateiru%2Fgo-http-easy-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcateiru%2Fgo-http-easy-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcateiru%2Fgo-http-easy-test/lists"}