-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontext_test.go
48 lines (39 loc) · 1.37 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
package fiber
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestContext(t *testing.T) {
type customType struct{}
var customTypeKey customType
httpCtx := Background()
httpCtx.WithValue("Hello", "world")
httpCtx.WithValue("Hi", "Goravel")
httpCtx.WithValue(customTypeKey, "halo")
httpCtx.WithValue(sessionKey, "session")
userContext := context.Background()
//nolint:all
userContext = context.WithValue(userContext, "user_a", "b")
httpCtx.WithContext(userContext)
httpCtx.WithValue(1, "one")
httpCtx.WithValue(2.2, "two point two")
assert.Equal(t, "world", httpCtx.Value("Hello"))
assert.Equal(t, "Goravel", httpCtx.Value("Hi"))
assert.Equal(t, "halo", httpCtx.Value(customTypeKey))
assert.Equal(t, "one", httpCtx.Value(1))
assert.Equal(t, "two point two", httpCtx.Value(2.2))
assert.Equal(t, "session", httpCtx.Value(sessionKey))
assert.NotNil(t, httpCtx.Value(contextKey))
// The value of UserContext can't be covered.
assert.Equal(t, "b", httpCtx.Value("user_a"))
ctx := httpCtx.Context()
assert.Equal(t, "world", ctx.Value("Hello"))
assert.Equal(t, "Goravel", ctx.Value("Hi"))
assert.Equal(t, "halo", ctx.Value(customTypeKey))
assert.Equal(t, "b", ctx.Value("user_a"))
assert.Equal(t, "one", ctx.Value(1))
assert.Equal(t, "two point two", ctx.Value(2.2))
assert.Nil(t, ctx.Value(sessionKey))
assert.Nil(t, ctx.Value(contextKey))
}