Created
July 7, 2018 19:56
-
-
Save gufranmirza/0009e85fd3ff140b13db81978a49dab9 to your computer and use it in GitHub Desktop.
This file contains 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 TestRouter(t *testing.T) { | |
// Instantiate the router using the constructor function that | |
// we defined previously | |
r := newRouter() | |
// Create a new server using the "httptest" libraries `NewServer` method | |
// Documentation : https://golang.org/pkg/net/http/httptest/#NewServer | |
mockServer := httptest.NewServer(r) | |
// The mock server we created runs a server and exposes its location in the | |
// URL attribute | |
// We make a GET request to the "hello" route we defined in the router | |
resp, err := http.Get(mockServer.URL + "/hello") | |
// Handle any unexpected error | |
if err != nil { | |
t.Fatal(err) | |
} | |
// We want our status to be 200 (ok) | |
if resp.StatusCode != http.StatusOK { | |
t.Errorf("Status should be ok, got %d", resp.StatusCode) | |
} | |
// In the next few lines, the response body is read, and converted to a string | |
defer resp.Body.Close() | |
// read the body into a bunch of bytes (b) | |
b, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
t.Fatal(err) | |
} | |
// convert the bytes to a string | |
respString := string(b) | |
expected := "Hello World!" | |
// We want our response to match the one defined in our handler. | |
// If it does happen to be "Hello world!", then it confirms, that the | |
// route is correct | |
if respString != expected { | |
t.Errorf("Response should be %s, got %s", expected, respString) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment