package main import ( "encoding/json" "fmt" "io" "net/http" "net/http/httptest" "reflect" "testing" "git.andreafazzi.eu/andrea/testhub/logger" "git.andreafazzi.eu/andrea/testhub/models" "git.andreafazzi.eu/andrea/testhub/store" "github.com/remogatto/prettytest" ) type testSuite struct { prettytest.Suite } type TestResponse struct { Status string `json:"success"` Content []*models.Quiz `json:"content"` } type StubTestHubCollectorStore struct { tests []*models.Quiz } func (store *StubTestHubCollectorStore) CreateQuiz(test *store.CreateQuizRequest) *models.Quiz { return nil } func (store *StubTestHubCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) { return store.tests, nil } func TestRunner(t *testing.T) { prettytest.Run( t, new(testSuite), new(integrationTestSuite), ) } func (t *testSuite) BeforeAll() { logger.SetLevel(logger.Disabled) } func (t *testSuite) TestGETQuestions() { expectedResult := &TestResponse{ Status: "success", Content: []*models.Quiz{ { Question: &models.Question{ID: "1", Text: "Question 1"}, Answers: []*models.Answer{{}, {}, {}}, }, }, } store := &StubTestHubCollectorStore{[]*models.Quiz{ { Question: &models.Question{ID: "1", Text: "Question 1"}, Answers: []*models.Answer{{}, {}, {}}, }, }} server := NewQuizHubCollectorServer(store) request, _ := http.NewRequest(http.MethodGet, "/tests", nil) response := httptest.NewRecorder() server.ServeHTTP(response, request) result := getResponse(response.Body) t.True(result.Status == expectedResult.Status) t.True(testsAreEqual(result, expectedResult)) t.Equal(http.StatusOK, response.Code) } func getResponse(body io.Reader) (response *TestResponse) { err := json.NewDecoder(body).Decode(&response) if err != nil { panic(fmt.Errorf("Unable to parse response from server %q into slice of Test, '%v'", body, err)) } return } func testsAreEqual(got, want *TestResponse) bool { return reflect.DeepEqual(got, want) }