-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmiddleware_test.go
142 lines (111 loc) · 4.24 KB
/
middleware_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
package pulse
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestMiddlewareFunc_Middleware(t *testing.T) {
// create a mock handler
mockHandler := func(ctx *Context) error { return nil }
// create a middleware that adds a value to the context
middleware := MiddlewareFunc(func(handler Handler) Handler {
return func(ctx *Context) error {
ctx.SetValue("key", "value")
return handler(ctx)
}
})
// call the Middleware method with the mock handler
newHandler := middleware.Middleware(mockHandler)
// create a new context instance
req := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
ctx := NewContext(w, req)
// call the new handler with the context
err := newHandler(ctx)
// check if the context value was set correctly
if val := ctx.GetValue("key"); val != "value" {
t.Errorf("Expected context value for key \"key\" to be \"value\", but got %v", val)
}
// check if the original handler was called with the context
if err != nil {
t.Errorf("Expected err to be nil, but got %v", err)
}
}
func TestMiddleware_Use(t *testing.T) {
// Create a new router.
r := NewRouter()
// Define a simple handler that sets a custom response header.
handler := func(ctx *Context) error {
ctx.SetResponseHeader("X-Test-Header", "test")
return nil
}
// Add a CORS middleware to the router.
r.Use(http.MethodGet, CORSMiddleware())
// Add the simple handler to the router.
r.Add(http.MethodGet, "/", handler)
// Create an HTTP request to test the handler.
_ = httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
// Verify that the response has the expected status code and header.
if rec.Code != http.StatusOK {
t.Errorf("unexpected status code: %d", rec.Code)
}
}
func TestCORSMiddleware(t *testing.T) {
// create a test context
req := httptest.NewRequest("GET", "/test", nil)
w := httptest.NewRecorder()
ctx := NewContext(w, req)
// create a mock handler
mockHandler := func(ctx *Context) error {
return nil
}
// create the CORS middleware
corsMiddleware := CORSMiddleware()
// wrap the mock handler with the CORS middleware
handler := corsMiddleware(mockHandler)
// call the handler with the test context
err := handler(ctx)
// get the http.Response from the ResponseRecorder using Result()
res := w.Result()
// check if the Access-Control-Allow-Origin header was set to "*"
if header := res.Header.Get("Access-Control-Allow-Origin"); header != "*" {
t.Errorf("Expected Access-Control-Allow-Origin header to be \"*\", but got %q", header)
}
// check if the Access-Control-Allow-Methods header was set correctly
if header := res.Header.Get("Access-Control-Allow-Methods"); header != "POST, GET, OPTIONS, PUT, DELETE" {
t.Errorf("Expected Access-Control-Allow-Methods header to be \"POST, GET, OPTIONS, PUT, DELETE\", but got %q", header)
}
// check if the Access-Control-Allow-Headers header was set correctly
if header := res.Header.Get("Access-Control-Allow-Headers"); header != "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization" {
t.Errorf("Expected Access-Control-Allow-Headers header to be \"Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization\", but got %q", header)
}
// check if the handler returned no error
if err != nil {
t.Errorf("Expected handler to return no error, but got %v", err)
}
}
func TestMiddlewareFunc_Handle(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/test", nil)
w := httptest.NewRecorder()
ctx := NewContext(w, req)
// create a mock handler
mockHandler := func(ctx *Context) error { return nil }
// create a middleware that adds a value to the context
middleware := MiddlewareFunc(func(handler Handler) Handler {
return func(ctx *Context) error {
ctx.SetValue("key", "value")
return handler(ctx)
}
})
// call the Handle method with the mock handler as the next handler
err := middleware.Handle(ctx, mockHandler)
// check if the context value was set correctly
if val := ctx.GetValue("key"); val != "value" {
t.Errorf("Expected context value for key \"key\" to be \"value\", but got %v", val)
}
// check if the next handler was called with the original context
if err != nil {
t.Errorf("Expected err to be nil, but got %v", err)
}
}