Skip to content

Instantly share code, notes, and snippets.

@kpurdon
Last active April 28, 2016 03:17
Show Gist options
  • Save kpurdon/8afb944d0ff370fa8f19031cfb8b159d to your computer and use it in GitHub Desktop.
Save kpurdon/8afb944d0ff370fa8f19031cfb8b159d to your computer and use it in GitHub Desktop.
func TestGetReposHandler(t *testing.T) {
assert := assert.New(t)
tests := []struct {
description string
reposClient *ReposTestClient
url string
expectedStatusCode int
expectedBody string
}{
{
description: "missing argument user",
reposClient: &ReposTestClient{},
url: "/repos",
expectedStatusCode: 400,
expectedBody: "MISSING_ARG_USER\n",
}, {
description: "error getting repos",
reposClient: &ReposTestClient{
Repos: []repos.Repo{},
Err: errors.New("fake test error"),
},
url: "/repos?user=fakeuser",
expectedStatusCode: 500,
expectedBody: "INTERNAL_ERROR\n",
}, {
description: "no repos found",
reposClient: &ReposTestClient{
Repos: []repos.Repo{},
Err: nil,
},
url: "/repos?user=fakeuser",
expectedStatusCode: 200,
expectedBody: `[]`,
}, {
description: "succesfull query",
reposClient: &ReposTestClient{
Repos: []repos.Repo{
repos.Repo{Name: "test", Description: "a test"},
},
Err: nil,
},
url: "/repos?user=fakeuser",
expectedStatusCode: 200,
expectedBody: `[{"name":"test","description":"a test"}]`,
},
// TODO not all cases are covered
}
for _, tc := range tests {
app := &App{repos: tc.reposClient}
req, err := http.NewRequest("GET", tc.url, nil)
assert.NoError(err)
w := httptest.NewRecorder()
app.GetReposHandler(w, req)
assert.Equal(tc.expectedStatusCode, w.Code, tc.description)
assert.Equal(tc.expectedBody, w.Body.String(), tc.description)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment