...
Source file
src/goji.io/pat/match_test.go
1 package pat
2
3 import (
4 "context"
5 "net/http"
6 "reflect"
7 "testing"
8
9 "goji.io/pattern"
10 )
11
12 func TestExistingContext(t *testing.T) {
13 t.Parallel()
14
15 pat := New("/hi/:c/:a/:r/:l")
16 req, err := http.NewRequest("GET", "/hi/foo/bar/baz/quux", nil)
17 if err != nil {
18 panic(err)
19 }
20 ctx := context.Background()
21 ctx = pattern.SetPath(ctx, req.URL.EscapedPath())
22 ctx = context.WithValue(ctx, pattern.AllVariables, map[pattern.Variable]interface{}{
23 "hello": "world",
24 "c": "nope",
25 })
26 ctx = context.WithValue(ctx, pattern.Variable("user"), "carl")
27
28 req = req.WithContext(ctx)
29 req = pat.Match(req)
30 if req == nil {
31 t.Fatalf("expected pattern to match")
32 }
33 ctx = req.Context()
34
35 expected := map[pattern.Variable]interface{}{
36 "c": "foo",
37 "a": "bar",
38 "r": "baz",
39 "l": "quux",
40 }
41 for k, v := range expected {
42 if p := Param(req, string(k)); p != v {
43 t.Errorf("expected %s=%q, got %q", k, v, p)
44 }
45 }
46
47 expected["hello"] = "world"
48 all := ctx.Value(pattern.AllVariables).(map[pattern.Variable]interface{})
49 if !reflect.DeepEqual(all, expected) {
50 t.Errorf("expected %v, got %v", expected, all)
51 }
52
53 if path := pattern.Path(ctx); path != "" {
54 t.Errorf("expected path=%q, got %q", "", path)
55 }
56
57 if user := ctx.Value(pattern.Variable("user")); user != "carl" {
58 t.Errorf("expected user=%q, got %q", "carl", user)
59 }
60 }
61
View as plain text