In normal operation, we expect one access log entry, and one data collector entry. Let's assume both will succeed. We should return a HTTP 200 status.
(t *testing.T)
| 16 | // and one data collector entry. Let's assume both will succeed. |
| 17 | // We should return a HTTP 200 status. |
| 18 | func TestCollectSuccessfully(t *testing.T) { |
| 19 | dataCollectorMock := mocks.NewSyncProducer(t, nil) |
| 20 | dataCollectorMock.ExpectSendMessageAndSucceed() |
| 21 | |
| 22 | accessLogProducerMock := mocks.NewAsyncProducer(t, nil) |
| 23 | accessLogProducerMock.ExpectInputAndSucceed() |
| 24 | |
| 25 | // Now, use dependency injection to use the mocks. |
| 26 | s := &Server{ |
| 27 | DataCollector: dataCollectorMock, |
| 28 | AccessLogProducer: accessLogProducerMock, |
| 29 | } |
| 30 | |
| 31 | // The Server's Close call is important; it will call Close on |
| 32 | // the two mock producers, which will then validate whether all |
| 33 | // expectations are resolved. |
| 34 | defer safeClose(t, s) |
| 35 | |
| 36 | req, err := http.NewRequest("GET", "http://example.com/?data", nil) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | res := httptest.NewRecorder() |
| 41 | s.Handler().ServeHTTP(res, req) |
| 42 | |
| 43 | if res.Code != 200 { |
| 44 | t.Errorf("Expected HTTP status 200, found %d", res.Code) |
| 45 | } |
| 46 | |
| 47 | if res.Body.String() != "Your data is stored with unique identifier important/0/1" { |
| 48 | t.Error("Unexpected response body", res.Body) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Now, let's see if we handle the case of not being able to produce |
| 53 | // to the data collector properly. In this case we should return a 500 status. |
nothing calls this directly
no test coverage detected