|  |  |  | @ -1,14 +1,21 @@ | 
		
	
		
			
				|  |  |  |  | package bugbox | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | import ( | 
		
	
		
			
				|  |  |  |  | 	"bytes" | 
		
	
		
			
				|  |  |  |  | 	"fmt" | 
		
	
		
			
				|  |  |  |  | 	"net/http" | 
		
	
		
			
				|  |  |  |  | 	"net/http/httptest" | 
		
	
		
			
				|  |  |  |  | 	"testing" | 
		
	
		
			
				|  |  |  |  | ) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | func newGETEnclosureRequest(name string) *http.Request { | 
		
	
		
			
				|  |  |  |  | 	request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/enclosure/%s", name), nil) | 
		
	
		
			
				|  |  |  |  | func newGETEnclosureRequest(id uint64) *http.Request { | 
		
	
		
			
				|  |  |  |  | 	request, _ := http.NewRequest(http.MethodGet, fmt.Sprintf("/enclosure/%d", id), nil) | 
		
	
		
			
				|  |  |  |  | 	return request | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | func newPOSTEventRequest(id uint64, value string) *http.Request { | 
		
	
		
			
				|  |  |  |  | 	body := []byte(value) | 
		
	
		
			
				|  |  |  |  | 	request, _ := http.NewRequest(http.MethodPost, fmt.Sprintf("/enclosure/%d/events", id), bytes.NewBuffer(body)) | 
		
	
		
			
				|  |  |  |  | 	return request | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | @ -28,25 +35,35 @@ func assertStatus(t testing.TB, got, want int) { | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | type MockEnclosureStore struct { | 
		
	
		
			
				|  |  |  |  | 	enclosures map[string]string | 
		
	
		
			
				|  |  |  |  | 	enclosures map[uint64]*Enclosure | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | func (s *MockEnclosureStore) GetEnclosure(id uint64) (enclosure *Enclosure, ok bool) { | 
		
	
		
			
				|  |  |  |  | 	enclosure, ok = s.enclosures[id] | 
		
	
		
			
				|  |  |  |  | 	return | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | func (s *MockEnclosureStore) GetEnclosure(name string) string { | 
		
	
		
			
				|  |  |  |  | 	e := s.enclosures[name] | 
		
	
		
			
				|  |  |  |  | 	return e | 
		
	
		
			
				|  |  |  |  | func (s *MockEnclosureStore) RecordEvent(id uint64, event string) error { | 
		
	
		
			
				|  |  |  |  | 	enc, ok := s.enclosures[id] | 
		
	
		
			
				|  |  |  |  | 	if !ok { | 
		
	
		
			
				|  |  |  |  | 		return fmt.Errorf("Failed to find enclosure %d", id) | 
		
	
		
			
				|  |  |  |  | 	} | 
		
	
		
			
				|  |  |  |  | 	enc.RecordEvent(event) | 
		
	
		
			
				|  |  |  |  | 	return nil | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | func TestGETEnclosures(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 	mockEnclosureStore := MockEnclosureStore{ | 
		
	
		
			
				|  |  |  |  | 		map[string]string{ | 
		
	
		
			
				|  |  |  |  | 			"1337": "1337", | 
		
	
		
			
				|  |  |  |  | 			"7331": "7331"}, | 
		
	
		
			
				|  |  |  |  | 		map[uint64]*Enclosure{ | 
		
	
		
			
				|  |  |  |  | 			1337: &Enclosure{Id: 1337}, | 
		
	
		
			
				|  |  |  |  | 			7331: &Enclosure{Id: 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") | 
		
	
		
			
				|  |  |  |  | 		request := newGETEnclosureRequest(1337) | 
		
	
		
			
				|  |  |  |  | 		// response is a ResponseRecorder used for spying on response
 | 
		
	
		
			
				|  |  |  |  | 		response := httptest.NewRecorder() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | @ -58,7 +75,7 @@ func TestGETEnclosures(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 	}) | 
		
	
		
			
				|  |  |  |  | 	t.Run("returns enclosure 7331", func(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 		// nil is used since we are not providing a request body
 | 
		
	
		
			
				|  |  |  |  | 		request := newGETEnclosureRequest("7331") | 
		
	
		
			
				|  |  |  |  | 		request := newGETEnclosureRequest(7331) | 
		
	
		
			
				|  |  |  |  | 		// response is a ResponseRecorder used for spying on response
 | 
		
	
		
			
				|  |  |  |  | 		response := httptest.NewRecorder() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | @ -69,7 +86,7 @@ func TestGETEnclosures(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 	}) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	t.Run("returns 404 when enclosure missing", func(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 		request := newGETEnclosureRequest("9000") | 
		
	
		
			
				|  |  |  |  | 		request := newGETEnclosureRequest(9000) | 
		
	
		
			
				|  |  |  |  | 		response := httptest.NewRecorder() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 		server.ServeHTTP(response, request) | 
		
	
	
		
			
				
					|  |  |  | @ -77,3 +94,24 @@ func TestGETEnclosures(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 		assertStatus(t, response.Code, http.StatusNotFound) | 
		
	
		
			
				|  |  |  |  | 	}) | 
		
	
		
			
				|  |  |  |  | } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | func TestStoreEnclosure(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 	testEnclosure := &Enclosure{Id: 2057} | 
		
	
		
			
				|  |  |  |  | 	store := MockEnclosureStore{ | 
		
	
		
			
				|  |  |  |  | 		map[uint64]*Enclosure{2057: testEnclosure}, | 
		
	
		
			
				|  |  |  |  | 	} | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	server := &BugBoxServer{EnclosureStore: &store} | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 	t.Run("it accepts a POST request for an event", func(t *testing.T) { | 
		
	
		
			
				|  |  |  |  | 		request := newPOSTEventRequest(2057, "dirp") | 
		
	
		
			
				|  |  |  |  | 		response := httptest.NewRecorder() | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 		server.ServeHTTP(response, request) | 
		
	
		
			
				|  |  |  |  | 		assertStatus(t, response.Code, http.StatusAccepted) | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  | 		if len(testEnclosure.Events) != 1 { | 
		
	
		
			
				|  |  |  |  | 			t.Errorf("Got %d calls to RecordEvent, want %d", len(testEnclosure.Events), 1) | 
		
	
		
			
				|  |  |  |  | 		} | 
		
	
		
			
				|  |  |  |  | 	}) | 
		
	
		
			
				|  |  |  |  | } | 
		
	
	
		
			
				
					|  |  |  | 
 |