Total Lines | 29 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package middleware |
||
2 | |||
3 | import ( |
||
4 | "context" |
||
5 | "net/http" |
||
6 | "net/http/httptest" |
||
7 | "testing" |
||
8 | |||
9 | httpmock "github.com/muonsoft/openapi-mock/test/mocks/http" |
||
10 | "github.com/sirupsen/logrus/hooks/test" |
||
11 | "github.com/stretchr/testify/mock" |
||
12 | ) |
||
13 | |||
14 | func TestContextualLoggerHandler_ServeHTTP_RequestIdInContext_LoggerSetToContext(t *testing.T) { |
||
15 | request, _ := http.NewRequest("GET", "/", nil) |
||
16 | ctx := context.WithValue(context.Background(), requestIDKey, "00000000-0000-0000-0000-000000000000") |
||
17 | request = request.WithContext(ctx) |
||
18 | recorder := httptest.NewRecorder() |
||
19 | logger, _ := test.NewNullLogger() |
||
20 | nextHandler := httpmock.Handler{} |
||
21 | handler := NewContextLogger(logger, &nextHandler) |
||
22 | nextHandler. |
||
23 | On("ServeHTTP", mock.Anything, mock.Anything). |
||
24 | Return(nil). |
||
25 | Once() |
||
26 | |||
27 | handler.ServeHTTP(recorder, request) |
||
28 | |||
29 | nextHandler.AssertExpectations(t) |
||
30 | } |
||
31 |