package main import ( "encoding/json" "net/http" "net/http/httptest" "strings" "git.andreafazzi.eu/andrea/testhub/models" "git.andreafazzi.eu/andrea/testhub/store" "github.com/remogatto/prettytest" ) type integrationTestSuite struct { prettytest.Suite } func (t *integrationTestSuite) TestCreateAndGetTest() { server := NewTestHubCollectorServer(store.NewMemoryTestHubCollectorStore()) // 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, "/tests", strings.NewReader(payload)) response := httptest.NewRecorder() server.ServeHTTP(response, request) returnedTest := new(models.Test) 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, "/tests", nil) response = httptest.NewRecorder() server.ServeHTTP(response, request) returnedTests := make([]*models.Test, 0) err = json.Unmarshal(response.Body.Bytes(), &returnedTests) if err != nil { t.True(err == nil, err.Error()) } t.True(len(returnedTests) == 1, "Length of returned tests should be 1") t.Equal("Question 1", returnedTests[0].Question.Text) }