func TestMiddleware_ServeHTTP(t *testing.T) { mw, rec, req := setupServeHTTP(t) mw.ServeHTTP(rec, req, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(418) }) lines := strings.Split(strings.TrimSpace(mw.Logger.Out.(*bytes.Buffer).String()), "\n") assert.Len(t, lines, 2) assert.JSONEq(t, fmt.Sprintf(`{"level":"info","method":"GET","msg":"started handling request",`+ `"remote":"10.10.10.10","request":"http://example.com/stuff?rly=ya",`+ `"request_id":"22035D08-98EF-413C-BBA0-C4E66A11B28D","time":"%s"}`, nowToday), lines[0]) assert.JSONEq(t, fmt.Sprintf(`{"level":"info","method":"GET","msg":"completed handling request",`+ `"remote":"10.10.10.10","request":"http://example.com/stuff?rly=ya",`+ `"measure#web.elapsed":"0.010ms","count#status4XX":1,"text_status":"I'm a teapot",`+ `"status":418,"request_id":"22035D08-98EF-413C-BBA0-C4E66A11B28D","time":"%s"}`, nowToday), lines[1]) }
// JSONEq asserts that two JSON strings are equivalent. // // assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) // // Returns whether the assertion was successful (true) or not (false). func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) { if !assert.JSONEq(t, expected, actual, msgAndArgs...) { t.FailNow() } }