{"id":19680126,"url":"https://github.com/drewolson/testflight","last_synced_at":"2025-03-17T15:12:47.891Z","repository":{"id":5843397,"uuid":"7059676","full_name":"drewolson/testflight","owner":"drewolson","description":"Painless http testing in Go","archived":false,"fork":false,"pushed_at":"2020-04-02T07:46:00.000Z","size":35,"stargazers_count":146,"open_issues_count":1,"forks_count":14,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-03T23:51:17.847Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/drewolson/testflight","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/drewolson.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":"2012-12-07T20:47:15.000Z","updated_at":"2025-03-02T17:48:55.000Z","dependencies_parsed_at":"2022-09-18T05:47:32.780Z","dependency_job_id":null,"html_url":"https://github.com/drewolson/testflight","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewolson%2Ftestflight","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewolson%2Ftestflight/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewolson%2Ftestflight/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drewolson%2Ftestflight/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drewolson","download_url":"https://codeload.github.com/drewolson/testflight/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244056425,"owners_count":20390719,"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":[],"created_at":"2024-11-11T18:04:05.264Z","updated_at":"2025-03-17T15:12:47.684Z","avatar_url":"https://github.com/drewolson.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# testflight\n\n[![Build Status](https://travis-ci.org/drewolson/testflight.png?branch=master)](https://travis-ci.org/drewolson/testflight)\n\n## Installation\n\n```bash\ngo get github.com/drewolson/testflight\n```\n\n```go\nimport \"github.com/drewolson/testflight\"\n```\n\n## Usage\n\ntestflight makes it simple to test your http servers in Go. Suppose you're using [pat](https://github.com/bmizerany/pat) to create a simple http handler, like so:\n\n```go\nfunc Handler() http.Handler {\n\tm := pat.New()\n\n\tm.Get(\"/hello/:name\", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {\n\t\tio.WriteString(w, \"hello, \"+req.URL.Query().Get(\":name\"))\n\t}))\n\n\tm.Post(\"/post/form\", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {\n\t\treq.ParseForm()\n\t\tname := req.Form.Get(\"name\")\n\t\tw.WriteHeader(201)\n\t\tio.WriteString(w, name+\" created\")\n\t}))\n\n\treturn m\n}\n```\n\nLet's use testflight to test our handler. Keep in mind that testflight is doing full-stack http tests. We're also using assert for test assertions.\n\n```go\nfunc TestGet(t *testing.T) {\n\ttestflight.WithServer(Handler(), func(r *testflight.Requester) {\n\t\tresponse := r.Get(\"/hello/drew\")\n\n\t\tassert.Equal(t, 200, response.StatusCode)\n\t\tassert.Equal(t, \"hello, drew\", response.Body)\n\t})\n}\n\nfunc TestPostWithForm(t *testing.T) {\n\ttestflight.WithServer(Handler(), func(r *testflight.Requester) {\n\t\tresponse := r.Post(\"/post/form\", testflight.FORM_ENCODED, \"name=Drew\")\n\n\t\tassert.Equal(t, 201, response.StatusCode)\n\t\tassert.Equal(t, \"Drew created\", response.Body)\n\t})\n}\n```\n\nThe testflight.Requester struct has the following methods: Get, Post, Put, Delete and Do. Do accepts an *http.Request for times when you need more explicit control of your request. See testflight_test.go for more usage information.\n\n## Testing Websockets\n\nTestflight also allows you to perform full-stack testing of websockets. You'll want to import both the testflight and testflight/ws packages.\n\n```go\nimport (\n    \"github.com/drewolson/testflight\"\n    \"github.com/drewolson/testflight/ws\"\n)\n```\n\nNow, let's make a handler with a websocket route.\n\n```go\nfunc Handler() http.Handler {\n\tmux := http.NewServeMux()\n\n\tmux.Handle(\"/websocket\", websocket.Handler(func(ws *websocket.Conn) {\n\t\tvar name string\n\t\twebsocket.Message.Receive(ws, \u0026name)\n\t\twebsocket.Message.Send(ws, \"Hello, \"+name)\n\t}))\n\n\treturn mux\n}\n```\n\nFinally, let's write the test.\n\n```go\nfunc TestWebSocket(t *testing.T) {\n    testflight.WithServer(Handler(), func(r *testflight.Requester) {\n        connection := ws.Connect(r, \"/websocket\")\n\n        connection.SendMessage(\"Drew\")\n        message, _ := connection.ReceiveMessage()\n        assert.Equal(t, \"Hello, Drew\", message)\n    })\n}\n```\n\n## Contributing\n\nRequires go `\u003e= 1.11`.\n\nRun the tests.\n\n```bash\ngo test -v ./...\n```\n\nNow write new tests, fix them and send me a pull request!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrewolson%2Ftestflight","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrewolson%2Ftestflight","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrewolson%2Ftestflight/lists"}