|
|
|
@ -20,16 +20,40 @@ func assertResponseBody(t testing.TB, got, want string) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertStatus(t testing.TB, got, want int) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
if got != want {
|
|
|
|
|
t.Errorf("Status error, got %d want %d", got, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MockEnclosureStore struct {
|
|
|
|
|
enclosures map[string]string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *MockEnclosureStore) GetEnclosure(name string) string {
|
|
|
|
|
e := s.enclosures[name]
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGETEnclosures(t *testing.T) {
|
|
|
|
|
mockEnclosureStore := MockEnclosureStore{
|
|
|
|
|
map[string]string{
|
|
|
|
|
"1337": "1337",
|
|
|
|
|
"7331": "7331"},
|
|
|
|
|
}
|
|
|
|
|
server := &BugBoxServer{EnclosureStore: &mockEnclosureStore}
|
|
|
|
|
|
|
|
|
|
t.Run("returns enclosure 1337", func(t *testing.T) {
|
|
|
|
|
// nil is used since we are not providing a request body
|
|
|
|
|
request := newGETEnclosureRequest("1337")
|
|
|
|
|
// response is a ResponseRecorder used for spying on response
|
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
BugBoxServer(response, request)
|
|
|
|
|
server.ServeHTTP(response, request)
|
|
|
|
|
|
|
|
|
|
assertResponseBody(t, response.Body.String(), "1337")
|
|
|
|
|
assertStatus(t, response.Code, http.StatusOK)
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
t.Run("returns enclosure 7331", func(t *testing.T) {
|
|
|
|
@ -38,8 +62,18 @@ func TestGETEnclosures(t *testing.T) {
|
|
|
|
|
// response is a ResponseRecorder used for spying on response
|
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
BugBoxServer(response, request)
|
|
|
|
|
server.ServeHTTP(response, request)
|
|
|
|
|
|
|
|
|
|
assertResponseBody(t, response.Body.String(), "7331")
|
|
|
|
|
assertStatus(t, response.Code, http.StatusOK)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("returns 404 when enclosure missing", func(t *testing.T) {
|
|
|
|
|
request := newGETEnclosureRequest("9000")
|
|
|
|
|
response := httptest.NewRecorder()
|
|
|
|
|
|
|
|
|
|
server.ServeHTTP(response, request)
|
|
|
|
|
|
|
|
|
|
assertStatus(t, response.Code, http.StatusNotFound)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|