Created
June 23, 2014 23:09
-
-
Save jonathaningram/ae8f34685132160f111f to your computer and use it in GitHub Desktop.
Example integration test for API endpoints - tested using Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // +build integration | |
| package integration | |
| import ( | |
| "bytes" | |
| "encoding/json" | |
| "net/http" | |
| "testing" | |
| ) | |
| var endpoint = "http://local.yoursite.com:8080" | |
| func TestCompanyPost(t *testing.T) { | |
| body := &bytes.Buffer{} | |
| body.Write([]byte(`{ | |
| "name": "ACME" | |
| }`)) | |
| req, err := http.NewRequest("POST", endpoint + "/api/companies", body) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| res, err := http.DefaultClient.Do(req) | |
| if err != nil { | |
| t.Fatal(err) | |
| } | |
| defer res.Body.Close() | |
| if res.StatusCode != 201 { | |
| t.Errorf("got status %d, want %d", res.StatusCode, 201) | |
| } | |
| var apiRes map[string]interface{} | |
| if err := json.NewDecoder(res.Body).Decode(&apiRes); err != nil { | |
| t.Fatal(err) | |
| } | |
| _, ok := apiRes["company"].(map[string]interface{}) | |
| if !ok { | |
| t.Errorf(`expected response to contain "company" key`, res.StatusCode, 201) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With GAE you'd test it with