MCPcopy
hub / github.com/IBM/sarama / TestCollectSuccessfully

Function TestCollectSuccessfully

examples/http_server/http_server_test.go:18–50  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

16// and one data collector entry. Let's assume both will succeed.
17// We should return a HTTP 200 status.
18func 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.

Callers

nothing calls this directly

Calls 10

ExpectInputAndSucceedMethod · 0.95
HandlerMethod · 0.95
NewSyncProducerFunction · 0.92
NewAsyncProducerFunction · 0.92
FatalMethod · 0.80
safeCloseFunction · 0.70
ErrorfMethod · 0.65
ErrorMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected