-
Notifications
You must be signed in to change notification settings - Fork 1
/
route_examples_test.go
182 lines (146 loc) · 5.06 KB
/
route_examples_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
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
174
175
176
177
178
179
180
181
182
package trout_test
import (
"fmt"
"net/http"
"os"
"darlinggo.co/trout/v2"
)
type exampleResponseWriter struct{}
func (e exampleResponseWriter) Header() http.Header {
return http.Header{}
}
func (e exampleResponseWriter) Write(in []byte) (int, error) {
n, err := os.Stdout.Write(in)
if err != nil {
return n, err
}
n2, err := os.Stdout.Write([]byte{'\n'})
return n + n2, err
}
func (e exampleResponseWriter) WriteHeader(statusCode int) {
}
func ExampleEndpoint_Handler() {
// usually your handler is defined elsewhere
// here we're defining a dummy for demo purposes
postsHandler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("matched"))
if err != nil {
panic(err)
}
})
var router trout.Router
router.Endpoint("/posts/{slug}/comments/{id}").Handler(postsHandler)
// all requests to /posts/FOO/comments/BAR will be routed to
// postsHandler
// normally this is done by passing router to http.ListenAndServe,
// or using http.Handle("/", router), but we don't want to run a
// server, we just want to call the router by hand right now
req, _ := http.NewRequest("GET",
"http://example.com/posts/foo/comments/bar", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
// the handler responds to any HTTP method
req, _ = http.NewRequest("POST",
"http://example.com/posts/foo/comments/bar", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
// routes that don't match return a 404
req, _ = http.NewRequest("GET", "http://example.com/posts/foo", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
req, _ = http.NewRequest("PUT", "http://example.com/users/bar", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
// endpoints don't match on prefix
req, _ = http.NewRequest("GET", "http://example.com/posts/foo/comments/bar/id/baz", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
// Output:
// matched
// matched
// 404 Page Not Found
// 404 Page Not Found
// 404 Page Not Found
}
func ExamplePrefix_Handler() {
// usually your handler is defined elsewhere
// here we're defining a dummy for demo purposes
postsHandler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("matched"))
if err != nil {
panic(err)
}
})
var router trout.Router
router.Prefix("/posts/{slug}").Handler(postsHandler)
// all requests that begin with /posts/FOO will be routed to
// postsHandler
// normally this is done by passing router to http.ListenAndServe,
// or using http.Handle("/", router), but we don't want to run a
// server, we just want to call the router by hand right now
// an exact match still works
req, _ := http.NewRequest("GET", "http://example.com/posts/foo", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
// but now anything using that prefix matches, too
req, _ = http.NewRequest("GET", "http://example.com/posts/foo/comments/bar", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
// Output:
// matched
// matched
}
func ExampleMethods_Handler() {
// usually your handler is defined elsewhere
// here we're defining a dummy for demo purposes
postsHandler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("matched"))
if err != nil {
panic(err)
}
})
var router trout.Router
router.Endpoint("/posts/{slug}").Methods("GET", "POST").Handler(postsHandler)
// only requests to /posts/FOO that are made with the GET or POST
// methods will be routed to postsHandler. Every other method will get
// a 405.
// normally this is done by passing router to http.ListenAndServe,
// or using http.Handle("/", router), but we don't want to run a
// server, we just want to call the router by hand right now
req, _ := http.NewRequest("GET", "http://example.com/posts/foo", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
req, _ = http.NewRequest("POST", "http://example.com/posts/foo", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
// this will return a 405
req, _ = http.NewRequest("PUT", "http://example.com/posts/foo", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
// Output:
// matched
// matched
// 405 Method Not Allowed
}
func ExampleRequestVars() {
postsHandler := http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
// RequestVars returns an http.Header object
vars := trout.RequestVars(r)
// you can use Get, but if a parameter name is
// repeated, you'll only get the first instance
// of it.
firstID := vars.Get("id")
// you can access all the instances of a parameter name
// using the map index. Just remember to use
// http.CanonicalHeaderKey.
allIDs := vars[http.CanonicalHeaderKey("id")]
secondID := allIDs[1]
_, err := w.Write([]byte(fmt.Sprintf("%s\n%v\n%s",
firstID, allIDs, secondID)))
if err != nil {
panic(err)
}
})
var router trout.Router
router.Endpoint("/posts/{id}/comments/{id}").Handler(postsHandler)
req, _ := http.NewRequest("GET", "http://example.com/posts/foo/comments/bar", nil)
router.ServeHTTP(exampleResponseWriter{}, req)
// Output:
// foo
// [foo bar]
// bar
}