Skip to content

Commit 6ee791c

Browse files
authored
Updates: Improve test coverage env (#21)
* Set & Get value from context methods * middleware, handle methods tests
1 parent b8efe09 commit 6ee791c

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

context.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pulse
22

33
import (
4+
"context"
45
"encoding/json"
56
"net/http"
67
"strings"
@@ -72,14 +73,14 @@ func (c *Context) String(value string) {
7273
}
7374
}
7475

75-
// SetData sets the http header value to the given key.
76-
func (c *Context) SetData(key string, value interface{}) {
77-
c.SetResponseHeader(key, value.(string))
76+
// SetValue create a middleware that adds a value to the context
77+
func (c *Context) SetValue(key interface{}, value interface{}) {
78+
c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), key, value))
7879
}
7980

80-
// GetData returns the http header value for the given key.
81-
func (c *Context) GetData(key string) string {
82-
return string(c.Request.Header.Get(key))
81+
// GetValue returns the value for the given key.
82+
func (c *Context) GetValue(key string) string {
83+
return c.Request.Context().Value(key).(string)
8384
}
8485

8586
// Next calls the next handler in the chain.

context_test.go

+8-30
Original file line numberDiff line numberDiff line change
@@ -165,39 +165,14 @@ func TestContext_Header(t *testing.T) {
165165
}
166166
}
167167

168-
func TestContext_SetData(t *testing.T) {
169-
w := httptest.NewRecorder()
170-
ctx := NewContext(w, nil)
171-
172-
key := "custom-key"
173-
value := "custom-value"
174-
175-
ctx.SetData(key, value)
176-
177-
if w.Header().Get(key) != value {
178-
t.Errorf("Response header does not match expected value. Expected: %s, got: %s", value, w.Header().Get(key))
179-
}
180-
}
181-
182-
func TestContext_GetData(t *testing.T) {
183-
r, _ := http.NewRequest(http.MethodGet, "/", nil)
184-
r.Header.Set("custom-key", "custom-value")
185-
186-
w := httptest.NewRecorder()
187-
ctx := NewContext(w, r)
188-
189-
key := "custom-key"
190-
191-
if ctx.GetData(key) != "custom-value" {
192-
t.Errorf("Request header does not match expected value. Expected: custom-value, got: %s", ctx.GetData(key))
193-
}
194-
}
195-
196168
func TestContext_Next(t *testing.T) {
197169
w := httptest.NewRecorder()
198170
ctx := NewContext(w, nil)
199171

200-
ctx.Next()
172+
err := ctx.Next()
173+
if err != nil {
174+
return
175+
}
201176
}
202177

203178
func TestContext_Reset(t *testing.T) {
@@ -224,7 +199,10 @@ func TestContext_JSON(t *testing.T) {
224199
w := httptest.NewRecorder()
225200
ctx := NewContext(w, nil)
226201

227-
ctx.JSON(200, map[string]string{"test": "test"})
202+
_, err := ctx.JSON(200, map[string]string{"test": "test"})
203+
if err != nil {
204+
return
205+
}
228206
}
229207

230208
func TestContext_SetContentType(t *testing.T) {

middleware_test.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,41 @@ import (
66
"testing"
77
)
88

9-
func TestRouter_Use(t *testing.T) {
9+
func TestMiddlewareFunc_Middleware(t *testing.T) {
10+
// create a mock handler
11+
mockHandler := func(ctx *Context) error { return nil }
12+
13+
// create a middleware that adds a value to the context
14+
middleware := MiddlewareFunc(func(handler Handler) Handler {
15+
return func(ctx *Context) error {
16+
ctx.SetValue("key", "value")
17+
return handler(ctx)
18+
}
19+
})
20+
21+
// call the Middleware method with the mock handler
22+
newHandler := middleware.Middleware(mockHandler)
23+
24+
// create a new context instance
25+
req := httptest.NewRequest(http.MethodGet, "/test", nil)
26+
w := httptest.NewRecorder()
27+
ctx := NewContext(w, req)
28+
29+
// call the new handler with the context
30+
err := newHandler(ctx)
31+
32+
// check if the context value was set correctly
33+
if val := ctx.GetValue("key"); val != "value" {
34+
t.Errorf("Expected context value for key \"key\" to be \"value\", but got %v", val)
35+
}
36+
37+
// check if the original handler was called with the context
38+
if err != nil {
39+
t.Errorf("Expected err to be nil, but got %v", err)
40+
}
41+
}
42+
43+
func TestMiddleware_Use(t *testing.T) {
1044
// Create a new router.
1145
r := NewRouter()
1246

@@ -75,3 +109,34 @@ func TestCORSMiddleware(t *testing.T) {
75109
t.Errorf("Expected handler to return no error, but got %v", err)
76110
}
77111
}
112+
113+
func TestMiddlewareFunc_Handle(t *testing.T) {
114+
req := httptest.NewRequest(http.MethodGet, "/test", nil)
115+
w := httptest.NewRecorder()
116+
117+
ctx := NewContext(w, req)
118+
119+
// create a mock handler
120+
mockHandler := func(ctx *Context) error { return nil }
121+
122+
// create a middleware that adds a value to the context
123+
middleware := MiddlewareFunc(func(handler Handler) Handler {
124+
return func(ctx *Context) error {
125+
ctx.SetValue("key", "value")
126+
return handler(ctx)
127+
}
128+
})
129+
130+
// call the Handle method with the mock handler as the next handler
131+
err := middleware.Handle(ctx, mockHandler)
132+
133+
// check if the context value was set correctly
134+
if val := ctx.GetValue("key"); val != "value" {
135+
t.Errorf("Expected context value for key \"key\" to be \"value\", but got %v", val)
136+
}
137+
138+
// check if the next handler was called with the original context
139+
if err != nil {
140+
t.Errorf("Expected err to be nil, but got %v", err)
141+
}
142+
}

0 commit comments

Comments
 (0)