Skip to content

Instantly share code, notes, and snippets.

@jonathaningram
Created June 23, 2014 23:09
Show Gist options
  • Select an option

  • Save jonathaningram/ae8f34685132160f111f to your computer and use it in GitHub Desktop.

Select an option

Save jonathaningram/ae8f34685132160f111f to your computer and use it in GitHub Desktop.
Example integration test for API endpoints - tested using Go
// +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)
}
}
@jonathaningram
Copy link
Copy Markdown
Author

With GAE you'd test it with

goapp test ./integration -tags "integration"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment