Skip to content

Instantly share code, notes, and snippets.

@cep21
Last active June 30, 2016 17:50
Show Gist options
  • Save cep21/5ec4544740cf10652fcd50ad331e5e95 to your computer and use it in GitHub Desktop.
Save cep21/5ec4544740cf10652fcd50ad331e5e95 to your computer and use it in GitHub Desktop.
Example client library test
func ItemTest(t *testing.T) {
// Common setup. Abstract this out
// This allows each test to create its own handler by changing handler variable
handler := http.NotFound
hs := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
handler(rw, req)
}))
defer hs.Close()
// Notice I set the base URL of the client to the httptest server
c := Client{
BaseURL: hs.URL,
}
// Code specific to this test
handler = func(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/v1/smite/item/sword" {
t.Error("Bad path!")
}
io.WriteString(rw, `{"type":"sword"}`)
}
item, err := c.GetItem(ctx, "sword")
if err != nil {
t.Error("Got error sending item")
}
if item.Type != "sword" {
t.Error("Did not get a sword!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment