-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
47 lines (37 loc) · 1.02 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
package httputil
import (
"fmt"
"net/http"
"testing"
"context"
"github.com/stretchr/testify/assert"
)
func withKeyValue(key interface{}, value interface{}) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
WithValue(ctx, key, value)
Next(ctx)
}
}
func Test_KeyValueMid(t *testing.T) {
ctx := WithHTTPContext(context.Background())
Use(ctx, withKeyValue("name", "tuhuayuan"))
assert.HTTPBodyContains(t, HandleFunc(ctx,
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
ctx := r.Context()
fmt.Println(ctx.Value("name"))
w.Write([]byte("ok"))
}), "GET", "/", map[string][]string{}, "ok")
}
func TestContextNil(t *testing.T) {
ctx := WithHTTPContext(nil)
Use(ctx, withKeyValue("name", "tuhuayuan"))
assert.HTTPBodyContains(t, HandleFunc(ctx,
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
ctx := r.Context()
fmt.Println(ctx.Value("name"))
w.Write([]byte("ok"))
}), "GET", "/", map[string][]string{}, "ok")
}