@@ -68,11 +68,11 @@ func TestRouter(t *testing.T) {
6868}
6969
7070type handlerStruct struct {
71- handeled * bool
71+ handled * bool
7272}
7373
7474func (h handlerStruct ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
75- * h .handeled = true
75+ * h .handled = true
7676}
7777
7878func TestRouterAPI (t * testing.T ) {
@@ -216,20 +216,127 @@ func TestRouterChaining(t *testing.T) {
216216 }
217217}
218218
219+ func TestRouterOPTIONS (t * testing.T ) {
220+ handlerFunc := func (_ http.ResponseWriter , _ * http.Request , _ Params ) {}
221+
222+ router := New ()
223+ router .POST ("/path" , handlerFunc )
224+
225+ // test not allowed
226+ // * (server)
227+ r , _ := http .NewRequest ("OPTIONS" , "*" , nil )
228+ w := httptest .NewRecorder ()
229+ router .ServeHTTP (w , r )
230+ if ! (w .Code == http .StatusOK ) {
231+ t .Errorf ("OPTIONS handling failed: Code=%d, Header=%v" , w .Code , w .Header ())
232+ } else if allow := w .Header ().Get ("Allow" ); allow != "POST, OPTIONS" {
233+ t .Error ("unexpected Allow header value: " + allow )
234+ }
235+
236+ // path
237+ r , _ = http .NewRequest ("OPTIONS" , "/path" , nil )
238+ w = httptest .NewRecorder ()
239+ router .ServeHTTP (w , r )
240+ if ! (w .Code == http .StatusOK ) {
241+ t .Errorf ("OPTIONS handling failed: Code=%d, Header=%v" , w .Code , w .Header ())
242+ } else if allow := w .Header ().Get ("Allow" ); allow != "POST, OPTIONS" {
243+ t .Error ("unexpected Allow header value: " + allow )
244+ }
245+
246+ r , _ = http .NewRequest ("OPTIONS" , "/doesnotexist" , nil )
247+ w = httptest .NewRecorder ()
248+ router .ServeHTTP (w , r )
249+ if ! (w .Code == http .StatusNotFound ) {
250+ t .Errorf ("OPTIONS handling failed: Code=%d, Header=%v" , w .Code , w .Header ())
251+ }
252+
253+ // add another method
254+ router .GET ("/path" , handlerFunc )
255+
256+ // test again
257+ // * (server)
258+ r , _ = http .NewRequest ("OPTIONS" , "*" , nil )
259+ w = httptest .NewRecorder ()
260+ router .ServeHTTP (w , r )
261+ if ! (w .Code == http .StatusOK ) {
262+ t .Errorf ("OPTIONS handling failed: Code=%d, Header=%v" , w .Code , w .Header ())
263+ } else if allow := w .Header ().Get ("Allow" ); allow != "POST, GET, OPTIONS" && allow != "GET, POST, OPTIONS" {
264+ t .Error ("unexpected Allow header value: " + allow )
265+ }
266+
267+ // path
268+ r , _ = http .NewRequest ("OPTIONS" , "/path" , nil )
269+ w = httptest .NewRecorder ()
270+ router .ServeHTTP (w , r )
271+ if ! (w .Code == http .StatusOK ) {
272+ t .Errorf ("OPTIONS handling failed: Code=%d, Header=%v" , w .Code , w .Header ())
273+ } else if allow := w .Header ().Get ("Allow" ); allow != "POST, GET, OPTIONS" && allow != "GET, POST, OPTIONS" {
274+ t .Error ("unexpected Allow header value: " + allow )
275+ }
276+
277+ // custom handler
278+ var custom bool
279+ router .OPTIONS ("/path" , func (w http.ResponseWriter , r * http.Request , _ Params ) {
280+ custom = true
281+ })
282+
283+ // test again
284+ // * (server)
285+ r , _ = http .NewRequest ("OPTIONS" , "*" , nil )
286+ w = httptest .NewRecorder ()
287+ router .ServeHTTP (w , r )
288+ if ! (w .Code == http .StatusOK ) {
289+ t .Errorf ("OPTIONS handling failed: Code=%d, Header=%v" , w .Code , w .Header ())
290+ } else if allow := w .Header ().Get ("Allow" ); allow != "POST, GET, OPTIONS" && allow != "GET, POST, OPTIONS" {
291+ t .Error ("unexpected Allow header value: " + allow )
292+ }
293+ if custom {
294+ t .Error ("custom handler called on *" )
295+ }
296+
297+ // path
298+ r , _ = http .NewRequest ("OPTIONS" , "/path" , nil )
299+ w = httptest .NewRecorder ()
300+ router .ServeHTTP (w , r )
301+ if ! (w .Code == http .StatusOK ) {
302+ t .Errorf ("OPTIONS handling failed: Code=%d, Header=%v" , w .Code , w .Header ())
303+ }
304+ if ! custom {
305+ t .Error ("custom handler not called" )
306+ }
307+ }
308+
219309func TestRouterNotAllowed (t * testing.T ) {
220310 handlerFunc := func (_ http.ResponseWriter , _ * http.Request , _ Params ) {}
221311
222312 router := New ()
223313 router .POST ("/path" , handlerFunc )
224314
225- // Test not allowed
315+ // test not allowed
226316 r , _ := http .NewRequest ("GET" , "/path" , nil )
227317 w := httptest .NewRecorder ()
228318 router .ServeHTTP (w , r )
229319 if ! (w .Code == http .StatusMethodNotAllowed ) {
230320 t .Errorf ("NotAllowed handling failed: Code=%d, Header=%v" , w .Code , w .Header ())
321+ } else if allow := w .Header ().Get ("Allow" ); allow != "POST, OPTIONS" {
322+ t .Error ("unexpected Allow header value: " + allow )
323+ }
324+
325+ // add another method
326+ router .DELETE ("/path" , handlerFunc )
327+ router .OPTIONS ("/path" , handlerFunc ) // must be ignored
328+
329+ // test again
330+ r , _ = http .NewRequest ("GET" , "/path" , nil )
331+ w = httptest .NewRecorder ()
332+ router .ServeHTTP (w , r )
333+ if ! (w .Code == http .StatusMethodNotAllowed ) {
334+ t .Errorf ("NotAllowed handling failed: Code=%d, Header=%v" , w .Code , w .Header ())
335+ } else if allow := w .Header ().Get ("Allow" ); allow != "POST, DELETE, OPTIONS" && allow != "DELETE, POST, OPTIONS" {
336+ t .Error ("unexpected Allow header value: " + allow )
231337 }
232338
339+ // test custom handler
233340 w = httptest .NewRecorder ()
234341 responseText := "custom method"
235342 router .MethodNotAllowed = http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
@@ -243,6 +350,9 @@ func TestRouterNotAllowed(t *testing.T) {
243350 if w .Code != http .StatusTeapot {
244351 t .Errorf ("unexpected response code %d want %d" , w .Code , http .StatusTeapot )
245352 }
353+ if allow := w .Header ().Get ("Allow" ); allow != "POST, DELETE, OPTIONS" && allow != "DELETE, POST, OPTIONS" {
354+ t .Error ("unexpected Allow header value: " + allow )
355+ }
246356}
247357
248358func TestRouterNotFound (t * testing.T ) {
0 commit comments