forked from rcrowley/go-tigertonic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
47 lines (44 loc) · 940 Bytes
/
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
package tigertonic
import (
"net/http"
"sync"
"testing"
)
func TestContext(t *testing.T) {
ch := make(chan bool)
wg := &sync.WaitGroup{}
mux := NewTrieServeMux()
mux.HandleFunc("GET", "/1", func(w http.ResponseWriter, r *http.Request) {
if 1 != len(contexts) {
t.Error(contexts)
}
<-ch
w.WriteHeader(http.StatusNoContent)
})
mux.HandleFunc("GET", "/2", func(w http.ResponseWriter, r *http.Request) {
if 2 != len(contexts) {
t.Error(contexts)
}
ch <- true
w.WriteHeader(http.StatusNoContent)
})
handler := WithContext(mux, testContext{})
wg.Add(2)
go func() {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/1", nil)
handler.ServeHTTP(w, r)
wg.Done()
}()
go func() {
w := &testResponseWriter{}
r, _ := http.NewRequest("GET", "http://example.com/2", nil)
handler.ServeHTTP(w, r)
wg.Done()
}()
wg.Wait()
}
type testContext struct {
Foo string
Bar int
}