Last active
June 30, 2016 17:50
-
-
Save cep21/5ec4544740cf10652fcd50ad331e5e95 to your computer and use it in GitHub Desktop.
Example client library test
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
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