{"id":18251874,"url":"https://github.com/achiku/testsvr","last_synced_at":"2025-06-28T17:34:03.471Z","repository":{"id":57495452,"uuid":"62858690","full_name":"achiku/testsvr","owner":"achiku","description":"Make httptest generated test server aware of -v of go test","archived":false,"fork":false,"pushed_at":"2020-10-12T05:07:03.000Z","size":12,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T19:12:51.467Z","etag":null,"topics":["golang","http-client","testing"],"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/achiku.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":"2016-07-08T04:30:33.000Z","updated_at":"2024-08-24T11:03:13.000Z","dependencies_parsed_at":"2022-08-31T12:51:33.939Z","dependency_job_id":null,"html_url":"https://github.com/achiku/testsvr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/achiku/testsvr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/achiku%2Ftestsvr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/achiku%2Ftestsvr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/achiku%2Ftestsvr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/achiku%2Ftestsvr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/achiku","download_url":"https://codeload.github.com/achiku/testsvr/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/achiku%2Ftestsvr/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262469399,"owners_count":23316314,"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":["golang","http-client","testing"],"created_at":"2024-11-05T09:48:41.284Z","updated_at":"2025-06-28T17:34:03.451Z","avatar_url":"https://github.com/achiku.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# testsvr\n\n[![Build Status](https://travis-ci.org/achiku/testsvr.svg?branch=master)](https://travis-ci.org/achiku/testsvr)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/achiku/testsvr/master/LICENSE)\n[![Doc](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/achiku/testsvr)\n[![Go Report Card](https://goreportcard.com/badge/github.com/achiku/testsvr)](https://goreportcard.com/report/github.com/achiku/testsvr)\n\n## Description\n\nMake `httptest` generated test server aware of `-v` of `go test`, and reuse that code for stand alone mock server\n\n\n## Why created\n\nInteracting with external services becomes almost an requirement for modern Web service development. Golang has an awesome `httptest` standard library making it really easy to start and close mock server in test code. However,  this mock server, which is supposed to be a part of test code, is not aware of `-v` of `go test`, and this leads to verbose output if you put logging in mock server handlers. This tiny little library makes `httptest` generated mock server aware of `-v`, keeping your test output sane and clean by default, detailed and comprehensive if needed.\n\n\n## Installation\n\n```\ngo get -u github.com/achiku/testsvr\n```\n\n\n## Example\n\nFull example is in https://github.com/achiku/testsvr/tree/master/example\n\n###### mock server code\n\n```go\npackage example\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"github.com/achiku/testsvr\"\n)\n\n// DefaultHandlerMap default url and handler map\nvar DefaultHandlerMap = map[string]testsvr.CreateHandler{\n\t\"/hello\":   hello,\n\t\"/goodbye\": goodbye,\n}\n\nfunc hello(logger testsvr.Logger) http.HandlerFunc {\n\treturn func(w http.ResponseWriter, r *http.Request) {\n\t\tlogger.Logf(\"RawURL: %s\", r.URL)\n\t\tlogger.Logf(\"Header: %s\", r.Header)\n\t\tname := r.URL.Query().Get(\"name\")\n\t\tw.WriteHeader(http.StatusOK)\n\t\tfmt.Fprintf(w, \"hello! %s.\", name)\n\t}\n}\n\nfunc goodbye(logger testsvr.Logger) http.HandlerFunc {\n\treturn func(w http.ResponseWriter, r *http.Request) {\n\t\tlogger.Logf(\"RawURL: %s\", r.URL)\n\t\tlogger.Logf(\"Header: %s\", r.Header)\n\t\tw.WriteHeader(http.StatusOK)\n\t\tfmt.Fprintf(w, \"goodbye!\")\n\t}\n}\n```\n\n###### client test code\n\n```go\nfunc TestClientHello(t *testing.T) {\n\ts := httptest.NewServer(testsvr.NewMux(DefaultHandlerMap, t))\n\tdefer s.Close()\n\n\ttestData := []struct {\n\t\tname   string\n\t\tstatus int\n\t}{\n\t\t{\"moqada\", http.StatusOK},\n\t\t{\"8maki\", http.StatusOK},\n\t\t{\"achiku\", http.StatusOK},\n\t}\n\tc := NewClient(s.URL)\n\tfor _, d := range testData {\n\t\tstatus, resp, err := c.Hello(d.name)\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tif status != d.status {\n\t\t\tt.Errorf(\"want %d got %d\", http.StatusOK, status)\n\t\t}\n\t\texpResp := fmt.Sprintf(\"hello! %s.\", d.name)\n\t\tif resp != expResp {\n\t\t\tt.Errorf(\"want %s got %s\", expResp, resp)\n\t\t}\n\t}\n}\n```\n\nWhen you don't want verbose output.\n\n```\n$ go test\nPASS\nok      github.com/achiku/testsvr/example       0.016s\n```\n\n\nWhen you want detailed output.\n\n```\n$ go test -v\n=== RUN   TestNewClient\n--- PASS: TestNewClient (0.00s)\n=== RUN   TestClientHello\n--- PASS: TestClientHello (0.00s)\n        mock_server.go:18: RawURL: /hello?name=moqada\n        mock_server.go:19: Header: map[User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]\n        mock_server.go:18: RawURL: /hello?name=8maki\n        mock_server.go:19: Header: map[User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]\n        mock_server.go:18: RawURL: /hello?name=achiku\n        mock_server.go:19: Header: map[User-Agent:[Go-http-client/1.1] Accept-Encoding:[gzip]]\n=== RUN   TestClientHelloError\n--- PASS: TestClientHelloError (0.00s)\n        client_test.go:51: something went wrong\n        client_test.go:58: something went wrong\nPASS\nok      github.com/achiku/testsvr/example       0.018s\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fachiku%2Ftestsvr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fachiku%2Ftestsvr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fachiku%2Ftestsvr/lists"}