~samwhited/mux

ref: v0.0.4 mux/params_test.go -rw-r--r-- 3.7 KiB
fee92349Sam Whited mux: add changelog and release v0.0.4 10 months ago
                                                                                
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package mux_test

import (
	"net/http"
	"net/http/httptest"
	"strconv"
	"testing"

	"code.soquee.net/mux"
)

var paramsTests = [...]struct {
	routes  []string
	path    string
	params  []mux.ParamInfo
	noMatch bool
	panics  bool
}{
	0: {
		routes: []string{"/user/{account uint}/{user int}/{name string}/{f float}"},
		path:   "/user/123/-11/me/1.123",
		params: []mux.ParamInfo{
			{
				Value: uint64(123),
				Raw:   "123",
				Name:  "account",
				Type:  "uint",
			},
			{
				Value: int64(-11),
				Raw:   "-11",
				Name:  "user",
				Type:  "int",
			},
			{
				Value: "me",
				Raw:   "me",
				Name:  "name",
				Type:  "string",
			},
			{
				Value: float64(1.123),
				Raw:   "1.123",
				Name:  "f",
				Type:  "float",
			},
		},
	},
	1: {
		routes:  []string{"/{bad float}"},
		path:    "/notfloat",
		noMatch: true,
	},
	2: {
		routes: []string{"/one/{other path}"},
		path:   "/one/two/three",
		params: []mux.ParamInfo{
			{
				Value: "two/three",
				Raw:   "two/three",
				Name:  "other",
				Type:  "path",
			},
		},
	},
	3: {
		routes:  []string{"/a"},
		path:    "/b",
		noMatch: true,
	},
	4: {
		routes: []string{"/{}"},
		path:   "/b",
	},
	5: {
		routes: []string{"/{badtype}"},
		panics: true,
	},
	6: {
		routes: []string{"not/rooted"},
		panics: true,
	},
	7: {
		routes: []string{"unclean/./path"},
		panics: true,
	},
	8: {
		routes: []string{"unclean/../path"},
		panics: true,
	},
	9: {
		routes: []string{"/{}/"},
		path:   "/b/",
	},
}

// Used as an HTTP status code code to make sure the test path matches at
// least one of the routes. This is just a sanity check on the tests
// themselves.
const (
	testStatusCode     = 42
	notFoundStatusCode = 43
)

func paramsHandler(t *testing.T, params []mux.ParamInfo) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		p, err := mux.Path(r)
		if err != nil {
			t.Errorf("Error while generating canonical path: %v", err)
		}
		if p != r.URL.Path {
			t.Errorf("Unexpected path generated from context: want=%q, got=%q", r.URL.Path, p)
		}

		w.WriteHeader(testStatusCode)
		for _, v := range params {
			pinfo := mux.Param(r, v.Name)
			if pinfo.Value == nil {
				t.Errorf("No such parameter found %q", v.Name)
				continue
			}
			if pinfo.Value != v.Value {
				t.Errorf("Param values do not match: want=%v, got=%v)", v.Value, pinfo.Value)
			}
			if pinfo.Raw != v.Raw {
				t.Errorf("Param raw values do not match: want=%q, got=%q)", v.Raw, pinfo.Raw)
			}
			if pinfo.Name != v.Name {
				t.Errorf("Param names do not match: want=%q, got=%q)", v.Name, pinfo.Name)
			}
			if pinfo.Type != v.Type {
				t.Errorf("Param types do not match: want=%s, got=%s)", v.Type, pinfo.Type)
			}
		}
	}
}

func TestParams(t *testing.T) {
	for i, tc := range paramsTests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			if tc.panics {
				defer func() {
					if r := recover(); r == nil {
						t.Errorf("Expected test to panic")
					}
				}()
			}

			var opts []mux.Option
			for _, route := range tc.routes {
				opts = append(opts, mux.HandleFunc("GET", route, paramsHandler(t, tc.params)))
			}
			opts = append(opts, mux.NotFound(codeHandler(t, notFoundStatusCode)))

			m := mux.New(opts...)
			rec := httptest.NewRecorder()
			m.ServeHTTP(rec, httptest.NewRequest("GET", tc.path, nil))
			switch {
			case tc.noMatch && rec.Code != notFoundStatusCode:
				t.Fatalf("Expected path to not be found, got code %d", rec.Code)
			case !tc.noMatch && rec.Code != testStatusCode:
				t.Fatalf("Test path (%q) did not match any route!", tc.path)
			}
		})
	}
}

func TestParamNotFound(t *testing.T) {
	pinfo := mux.Param(httptest.NewRequest("GET", "/", nil), "badparam")
	if pinfo.Value != nil {
		t.Errorf("Did not expect to find param but got %+v", pinfo)
	}
}