更新时间:2022-09-25 20:14:15
Setup test environment func TestStoryCreateAndGet(t *testing.T) { // Start MongoDB Docker container // // One of the most powerful features in Golang // is the ability to return multiple values from functions. // In this we get: // - containerID (type=ContainerID struct) // - ip (type=string) containerID, ip := dockertest.SetupMongoContainer(t) // defer schedules KillRemove(t) function call to run immediatelly // when TestStoryCreateAndGet(t) function is done, // so you can place resource clenup code close to resource allocation defer containerID.KillRemove(t) app := AppContext{} // Connect to Dockerized MongoDB mongoSession, err := mgo.Dial(ip) // Golang favors visible first hand error handling. // Main idea is that Errors are not exceptional so you should handle them if err != nil { Error.Printf("MongoDB connection failed, with address '%s'.", Configuration.MongoUrl) } // close MongoDB connections when we're finished defer mongoSession.Close() app.mongoSession = mongoSession // create test http server with applications route configuration ts := httptest.NewServer(app.createRoutes()) defer ts.Close() storyId := testCreate(ts, t) // run create test testGet(ts, storyId, t) // run get test for created story }
Post json document to http handler func testCreate(ts *httptest.Server, t *testing.T) string { postData := strings.NewReader("{\"text\":\"tekstiä\",\"subjectId\":\"k2j34\",\"subjectUrl\":\"www.fi/k2j34\"}") // create http POST with postData JSON res, err := http.Post(ts.URL+"/story", applicationJSON, postData) // read http response body data data, err := ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { t.Error(err) } id := string(data) // verify that we got correct http status code if res.StatusCode != http.StatusCreated { t.Fatalf("Non-expected status code: %v\n\tbody: %v, data:%s\n", http.StatusCreated, res.StatusCode, id) } // verify that we got valid lenght response data if res.ContentLength != 5 { t.Fatalf("Non-expected content length: %v != %v\n", res.ContentLength, 5) } return id }
Test that previously created story exists func testGet(ts *httptest.Server, storyId string, t *testing.T) { // create http GET request with correct path res, err := http.Get(ts.URL + "/story/" + storyId) data, err := ioutil.ReadAll(res.Body) res.Body.Close() if err != nil { t.Error(err) } body := string(data) // validate status code if res.StatusCode != http.StatusOK { t.Fatalf("Non-expected status code: %v\n\tbody: %v, data:%s\n", http.StatusCreated, res.StatusCode, body) } // validate that response has correct storyId if !strings.Contains(body, "{\"storyId\":\""+storyId+"\",") { t.Fatalf("Non-expected body content: %v", body) } // validate that content leght is what is should be if res.ContentLength < 163 && res.ContentLength > 165 { t.Fatalf("Non-expected content length: %v < %v, content:\n%v\n", res.ContentLength, 160, body) } }