-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
56 lines (39 loc) · 1.2 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 twitter
import (
"context"
"github.com/stretchr/testify/require"
"testing"
)
func TestGetUserIDFromContext(t *testing.T) {
t.Run("get user id from context", func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, userIDKey, "123")
userID, err := GetUserIDFromContext(ctx)
require.NoError(t, err)
require.Equal(t, "123", userID)
})
t.Run("return error when user id is not in context", func(t *testing.T) {
ctx := context.Background()
userID, err := GetUserIDFromContext(ctx)
require.Error(t, err)
require.ErrorIs(t, err, ErrNoUserIdInContext)
require.Empty(t, userID)
})
t.Run("return error when user id is not a string", func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, userIDKey, 123)
userID, err := GetUserIDFromContext(ctx)
require.Error(t, err)
require.ErrorIs(t, err, ErrNoUserIdInContext)
require.Empty(t, userID)
})
}
func TestPutUserIDIntoContext(t *testing.T) {
t.Run("put user id into context", func(t *testing.T) {
ctx := context.Background()
ctx = PutUserIDIntoContext(ctx, "123")
userID, err := GetUserIDFromContext(ctx)
require.NoError(t, err)
require.Equal(t, "123", userID)
})
}