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" "github.com/google/uuid" "github.com/remogatto/prettytest" ) type testSuite struct { prettytest.Suite } type StubTestHubCollectorStore struct { questions []*models.Question } func (store *StubTestHubCollectorStore) CreateQuestion(question *models.Question) *models.Question { return nil } func (store *StubTestHubCollectorStore) GetQuestions() []*models.Question { return store.questions } 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 := []*models.Question{ { Text: "Domanda 1", AnswerIDs: []uuid.UUID{{}}, }, } store := &StubTestHubCollectorStore{[]*models.Question{ { Text: "Domanda 1", AnswerIDs: []uuid.UUID{{}}, }, }} server := NewTestHubCollectorServer(store) request, _ := http.NewRequest(http.MethodGet, "/questions", nil) response := httptest.NewRecorder() server.ServeHTTP(response, request) result := getQuestionsFromResponse(response.Body) t.True(questionsAreEqual(expectedResult, result)) t.Equal(http.StatusOK, response.Code) } func getQuestionsFromResponse(body io.Reader) (questions []*models.Question) { err := json.NewDecoder(body).Decode(&questions) if err != nil { panic(fmt.Errorf("Unable to parse response from server %q into slice of Question, '%v'", body, err)) } return } func questionsAreEqual(got, want []*models.Question) bool { return reflect.DeepEqual(got, want) }