-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontext_test.go
49 lines (35 loc) · 1.07 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
package twitter
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetUserIDFromContext(t *testing.T) {
t.Run("get user id from context", func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, contextAuthIDKey, "123")
userID, err := GetUserIDFromContext(ctx)
require.NoError(t, err)
require.Equal(t, "123", userID)
})
t.Run("return error if no id", func(t *testing.T) {
ctx := context.Background()
_, err := GetUserIDFromContext(ctx)
require.ErrorIs(t, err, ErrNoUserIDInContext)
})
t.Run("return error if value is not a string", func(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, contextAuthIDKey, 123)
_, err := GetUserIDFromContext(ctx)
require.ErrorIs(t, err, ErrNoUserIDInContext)
})
}
func TestPutUserIDIntoContext(t *testing.T) {
t.Run("add 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)
})
}