-
Notifications
You must be signed in to change notification settings - Fork 1
/
middlewares_test.go
129 lines (115 loc) · 3.5 KB
/
middlewares_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
package lit_test
import (
"bytes"
"log"
"net/http"
"net/http/httptest"
"os"
"regexp"
"testing"
"github.com/jvcoutinho/lit"
"github.com/stretchr/testify/require"
)
func TestRecover(t *testing.T) {
t.Parallel()
// Arrange
r := lit.NewRequest(
httptest.NewRequest(http.MethodGet, "/users", nil),
)
panicHandler := lit.Handler(func(r *lit.Request) lit.Response {
panic("scary!")
})
recorder := httptest.NewRecorder()
// Act
response := lit.Recover(panicHandler)(r)
response.Write(recorder)
// Assert
require.Equal(t, http.StatusInternalServerError, recorder.Code)
require.Equal(t, "scary!\n", recorder.Body.String())
require.Equal(t, http.Header{
"Content-Type": {"text/plain; charset=utf-8"},
"X-Content-Type-Options": {"nosniff"},
}, recorder.Header())
}
func TestLog(t *testing.T) {
tests := []struct {
description string
writer http.ResponseWriter
response lit.Response
expectedContent *regexp.Regexp
}{
{
description: "WhenResponseIsNil_ShouldNotLog",
writer: lit.NewRecorder(httptest.NewRecorder()),
response: nil,
expectedContent: regexp.MustCompile("^$"),
},
{
description: "WhenStatusCodeIs2xx_ShouldLogGreen",
writer: lit.NewRecorder(httptest.NewRecorder()),
response: lit.ResponseFunc(func(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("log"))
}),
expectedContent: regexp.MustCompile(
"^\n\u001B\\[97;1;42m>> GET /users\u001B\\[0m\n> 200 OK\n> Start Time: .+\n> Remote Address: .+\n> Duration: .+\n> Content-Length: 3\n$",
),
},
{
description: "WhenStatusCodeIs3xx_ShouldLogBlue",
writer: lit.NewRecorder(httptest.NewRecorder()),
response: lit.ResponseFunc(func(w http.ResponseWriter) {
w.WriteHeader(http.StatusPermanentRedirect)
w.Write([]byte("log"))
}),
expectedContent: regexp.MustCompile(
"^\n\u001B\\[97;1;104m>> GET /users\u001B\\[0m\n> 308 Permanent Redirect\n> Start Time: .+\n> Remote Address: .+\n> Duration: .+\n> Content-Length: 3\n$",
),
},
{
description: "WhenStatusCodeIs4xx_ShouldLogYellow",
writer: lit.NewRecorder(httptest.NewRecorder()),
response: lit.ResponseFunc(func(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("log"))
}),
expectedContent: regexp.MustCompile(
"^\n\u001B\\[97;1;43m>> GET /users\u001B\\[0m\n> 404 Not Found\n> Start Time: .+\n> Remote Address: .+\n> Duration: .+\n> Content-Length: 3\n$",
),
},
{
description: "WhenStatusCodeIs5xx_ShouldLogRed",
writer: lit.NewRecorder(httptest.NewRecorder()),
response: lit.ResponseFunc(func(w http.ResponseWriter) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("log"))
}),
expectedContent: regexp.MustCompile(
"^\n\u001B\\[97;1;41m>> GET /users\u001B\\[0m\n> 500 Internal Server Error\n> Start Time: .+\n> Remote Address: .+\n> Duration: .+\n> Content-Length: 3\n$",
),
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
// Arrange
r := lit.NewRequest(
httptest.NewRequest(http.MethodGet, "/users", nil),
)
handler := lit.Handler(func(r *lit.Request) lit.Response {
return test.response
})
var output bytes.Buffer
log.SetOutput(&output)
t.Cleanup(func() {
log.SetOutput(os.Stderr)
})
// Act
response := lit.Log(handler)(r)
if response != nil {
response.Write(httptest.NewRecorder())
}
// Assert
require.Regexp(t, test.expectedContent, output.String())
})
}
}