package main import ( "encoding/json" "net/http" "net/http/httptest" "strings" "git.andreafazzi.eu/andrea/testhub/client" "git.andreafazzi.eu/andrea/testhub/models" "git.andreafazzi.eu/andrea/testhub/store" "github.com/remogatto/prettytest" ) type integrationTestSuite struct { prettytest.Suite } func (t *integrationTestSuite) TestQuizCreateAndReadAll() { server := NewQuizHubCollectorServer(store.NewMemoryQuizHubCollectorStore()) // POST a new question using a JSON payload payload := ` { "question": {"text": "Question 1"}, "answers": [ {"text": "Text of the answer 1", "correct": true}, {"text": "Text of the answer 2"}, {"text": "Text of the answer 3"}, {"text": "Text of the answer 4"} ] } ` request, _ := http.NewRequest(http.MethodPost, "/quizzes", strings.NewReader(payload)) response := httptest.NewRecorder() server.ServeHTTP(response, request) returnedTest := new(models.Quiz) err := json.Unmarshal(response.Body.Bytes(), returnedTest) if err != nil { t.True(err == nil, err.Error()) } t.Equal("Question 1", returnedTest.Question.Text) t.Equal("Text of the answer 1", returnedTest.Answers[0].Text) t.Equal("Text of the answer 1", returnedTest.Correct.Text) t.True(returnedTest.ID != 0, "Test ID should not be 0") t.True(returnedTest.Question.ID != "", "Question ID should not be empty") t.True(returnedTest.Answers[0].ID != "", "Answer ID should not be empty") request, _ = http.NewRequest(http.MethodGet, "/quizzes", nil) response = httptest.NewRecorder() server.ServeHTTP(response, request) decodedResponse := new(client.QuizReadAllResponse) err = json.Unmarshal(response.Body.Bytes(), &decodedResponse) if err != nil { t.True(err == nil, err.Error()) } t.True(len(decodedResponse.Content) == 1, "Length of returned tests should be 1") t.Equal("Question 1", decodedResponse.Content[0].Question.Text) }