-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
56 lines (45 loc) · 1.21 KB
/
context_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
package middleware
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/v3nom/pipes"
)
func TestContextMiddleware(t *testing.T) {
req, _ := http.NewRequest("GET", "/test", nil)
Context(nil, nil, req, func(ctx context.Context) {
if ctx == nil {
t.Fatalf("Expected context to not be nil")
}
})
}
func TestAddContextMiddleware(t *testing.T) {
aKey := pipes.ContextKey("akey")
type NewContextKey string
bKey := NewContextKey("bkey")
options := map[interface{}]interface{}{
aKey: "a",
bKey: "b",
}
pipeline := pipes.New().Use(Context).Use(AddOptions(options)).Use(func(ctx context.Context, w http.ResponseWriter, r *http.Request, next pipes.Next) {
a := ctx.Value(aKey).(string)
b := ctx.Value(bKey).(string)
if a+b == "ab" {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
next(ctx)
})
// Request
req, _ := http.NewRequest("GET", "/test", nil)
// Request handling
handler := http.HandlerFunc(pipeline.Build())
// Record
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if recorder.Code == http.StatusInternalServerError {
t.Fatalf("Expected status code: %v, Actual: %v", http.StatusOK, recorder.Code)
}
}