-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttplogger_test.go
160 lines (148 loc) · 3.93 KB
/
httplogger_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package rmhttp
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// ------------------------------------------------------------------------------------------------
// HTTP LOGGER TESTS
// ------------------------------------------------------------------------------------------------
type TestLogEntry struct {
Level string `json:"level"`
Status int `json:"status"`
}
// Test_HTTPLoggerMiddleware tests that requests and errors are logged as expected by the
// HTTP Logger middleware
func Test_HTTPLoggerMiddleware(t *testing.T) {
testPattern := "/test"
testBody := "test body"
// ErrSentinel := errors.New("sentinel error")
tests := []struct {
name string
expectedCode int
errorExpected bool
route *Route
}{
{
"an error is logged when response status is 400 and no error is returned from handler",
http.StatusBadRequest,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(createTestHandlerFunc(http.StatusBadRequest, testBody, nil)),
),
},
{
"an error is logged when response status is 500 and no error is returned from handler",
http.StatusInternalServerError,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(createTestHandlerFunc(http.StatusInternalServerError, testBody, nil)),
),
},
{
"an error is not logged when response status is 200 and no error is returned from handler",
http.StatusOK,
false,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(createTestHandlerFunc(http.StatusOK, testBody, nil)),
),
},
{
"an error is logged when response status is 400 and an error is returned from handler",
http.StatusBadRequest,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusBadRequest, testBody, errors.New("error!")),
),
),
},
{
"an error is logged when response status is 200 and an error is returned from handler",
http.StatusInternalServerError,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(http.StatusOK, testBody, errors.New("error!")),
),
),
},
{
"an error is logged when response status is 200 and an HTTPError is returned from handler",
http.StatusBadRequest,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(
http.StatusOK,
testBody,
NewHTTPError(errors.New("error!"), http.StatusBadRequest),
),
),
),
},
{
"an error is logged when response status is 500 and an HTTPError is returned from handler",
http.StatusBadRequest,
true,
NewRoute(
http.MethodGet,
testPattern,
HandlerFunc(
createTestHandlerFunc(
http.StatusInternalServerError,
testBody,
NewHTTPError(errors.New("error!"), http.StatusBadRequest),
),
),
),
},
}
out := &bytes.Buffer{}
logger := slog.New(slog.NewJSONHandler(out, nil))
for _, test := range tests {
out.Reset()
t.Run(test.name, func(t *testing.T) {
test.route.Handler = applyMiddleware(
test.route.Handler,
[]MiddlewareFunc{HTTPLoggerMiddleware(logger)},
)
url := fmt.Sprintf("http://%s%s", testAddress, testPattern)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Errorf("failed to create request: %v", err)
}
w := httptest.NewRecorder()
_ = test.route.Handler.ServeHTTPWithError(w, req)
// fmt.Println("log: ", out.String(), " :END")
log := TestLogEntry{}
if err = json.Unmarshal(out.Bytes(), &log); err != nil {
t.Errorf("cannot unmarshal log entry JSON: %v", err.Error())
}
if test.errorExpected {
assert.Equal(t, test.expectedCode, log.Status, "they should be equal")
assert.Equal(t, "ERROR", log.Level, "they should be equal")
} else {
assert.Equal(t, "INFO", log.Level, "they should be equal")
}
})
}
}