-
Notifications
You must be signed in to change notification settings - Fork 26
/
context_16_test.go
127 lines (94 loc) · 2.62 KB
/
context_16_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// +build !go1.7
package lars
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"golang.org/x/net/context"
. "gopkg.in/go-playground/assert.v1"
)
// NOTES:
// - Run "go test" to run tests
// - Run "gocov test | gocov report" to report on test converage by file
// - Run "gocov test | gocov annotate -" to report on all code and functions, those ,marked with "MISS" were never called
//
// or
//
// -- may be a good idea to change to output path to somewherelike /tmp
// go test -coverprofile cover.out && go tool cover -html=cover.out -o cover.html
//
func TestContext(t *testing.T) {
l := New()
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
c := NewContext(l)
var varParams []Param
// Parameter
param1 := Param{
Key: "userID",
Value: "507f191e810c19729de860ea",
}
varParams = append(varParams, param1)
c.params = varParams
c.netContext = context.Background()
c.request = r
//Request
NotEqual(t, c.request, nil)
//Response
NotEqual(t, c.response, nil)
//Paramter by name
bsonValue := c.Param("userID")
NotEqual(t, len(bsonValue), 0)
Equal(t, "507f191e810c19729de860ea", bsonValue)
//Store
ctx := c.Context()
ctx = context.WithValue(ctx, "publicKey", "U|ydN3SX)B(hI8SV1R;(")
c.WithContext(ctx)
value, exists := c.Get("publicKey")
//Get
Equal(t, true, exists)
Equal(t, "U|ydN3SX)B(hI8SV1R;(", value)
c.WithValue("User", "Alice")
value, exists = c.Value("User").(string)
Equal(t, true, exists)
Equal(t, "Alice", value)
value, exists = c.Get("UserName")
NotEqual(t, true, exists)
NotEqual(t, "Alice", value)
c.Set("Information", []string{"Alice", "Bob", "40.712784", "-74.005941"})
value, exists = c.Get("Information")
Equal(t, true, exists)
vString := value.([]string)
Equal(t, "Alice", vString[0])
Equal(t, "Bob", vString[1])
Equal(t, "40.712784", vString[2])
Equal(t, "-74.005941", vString[3])
// Reset
c.RequestStart(w, r)
//Request
NotEqual(t, c.request, nil)
//Response
NotEqual(t, c.response, nil)
//Set
Equal(t, c.Value("test"), nil)
// Index
Equal(t, c.index, -1)
// Handlers
Equal(t, c.handlers, nil)
cancelFunc := c.WithCancel()
Equal(t, reflect.TypeOf(cancelFunc).String(), "context.CancelFunc")
dt := time.Now().Add(time.Minute)
cancelFunc = c.WithDeadline(dt)
Equal(t, reflect.TypeOf(cancelFunc).String(), "context.CancelFunc")
cancelFunc = c.WithTimeout(time.Minute)
Equal(t, reflect.TypeOf(cancelFunc).String(), "context.CancelFunc")
deadline, ok := c.Deadline()
Equal(t, ok, true)
Equal(t, deadline, dt)
dc := c.Done()
Equal(t, reflect.TypeOf(dc).String(), "<-chan struct {}")
err := c.Err()
Equal(t, err, nil)
}