...
1 package middleware
2
3 import (
4 "context"
5 "net/http"
6 "testing"
7 )
8
9 type testPattern bool
10
11 func (t testPattern) Match(r *http.Request) *http.Request {
12 if t {
13 return r
14 }
15 return nil
16 }
17
18 type testHandler struct{}
19
20 func (t testHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
21
22 func TestPattern(t *testing.T) {
23 t.Parallel()
24
25 pat := testPattern(true)
26 ctx := SetPattern(context.Background(), pat)
27 if pat2 := Pattern(ctx); pat2 != pat {
28 t.Errorf("got ctx=%v, expected %v", pat2, pat)
29 }
30
31 if pat2 := Pattern(context.Background()); pat2 != nil {
32 t.Errorf("got ctx=%v, expecte nil", pat2)
33 }
34 }
35
36 func TestHandler(t *testing.T) {
37 t.Parallel()
38
39 h := testHandler{}
40 ctx := SetHandler(context.Background(), h)
41 if h2 := Handler(ctx); h2 != h {
42 t.Errorf("got handler=%v, expected %v", h2, h)
43 }
44
45 if h2 := Handler(context.Background()); h2 != nil {
46 t.Errorf("got handler=%v, expected nil", h2)
47 }
48 }
49
View as plain text