-
Notifications
You must be signed in to change notification settings - Fork 41
/
context_test.go
56 lines (48 loc) · 1.06 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 session
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestContext(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type ctxKey struct{}
ctx := context.WithValue(context.Background(), ctxKey{}, "bar")
store, err := Start(ctx, w, r)
if err != nil {
t.Error(err)
return
}
ctxValue := store.Context().Value(ctxKey{})
if !reflect.DeepEqual(ctxValue, "bar") {
t.Error("Not expected value:", ctxValue)
return
}
req, ok := FromReqContext(store.Context())
if !ok || req.URL.Query().Get("foo") != "bar" {
t.Error("Not expected value:", req.URL.Query().Get("foo"))
return
}
res, ok := FromResContext(store.Context())
if !ok {
t.Error("Not expected value")
return
}
fmt.Fprint(res, "ok")
}))
defer ts.Close()
res, err := http.Get(ts.URL + "?foo=bar")
if err != nil {
t.Error(err)
return
}
buf, _ := io.ReadAll(res.Body)
res.Body.Close()
if string(buf) != "ok" {
t.Error("Not expected value:", string(buf))
}
}