MCPcopy
hub / github.com/go-chi/chi / TestMethodNotAllowed

Function TestMethodNotAllowed

mux_test.go:394–429  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

392}
393
394func TestMethodNotAllowed(t *testing.T) {
395 r := NewRouter()
396
397 r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
398 w.Write([]byte("hi, get"))
399 })
400
401 r.Head("/hi", func(w http.ResponseWriter, r *http.Request) {
402 w.Write([]byte("hi, head"))
403 })
404
405 ts := httptest.NewServer(r)
406 defer ts.Close()
407
408 t.Run("Registered Method", func(t *testing.T) {
409 resp, _ := testRequest(t, ts, "GET", "/hi", nil)
410 if resp.StatusCode != 200 {
411 t.Fatal(resp.Status)
412 }
413 if resp.Header.Values("Allow") != nil {
414 t.Fatal("allow should be empty when method is registered")
415 }
416 })
417
418 t.Run("Unregistered Method", func(t *testing.T) {
419 resp, _ := testRequest(t, ts, "POST", "/hi", nil)
420 if resp.StatusCode != 405 {
421 t.Fatal(resp.Status)
422 }
423 allowedMethods := resp.Header.Values("Allow")
424 if len(allowedMethods) != 2 || ((allowedMethods[0] != "GET" || allowedMethods[1] != "HEAD") &&
425 (allowedMethods[1] != "GET" || allowedMethods[0] != "HEAD")) {
426 t.Fatal("Allow header should contain 2 headers: GET, HEAD. Received: ", allowedMethods)
427 }
428 })
429}
430
431func TestMuxNestedMethodNotAllowed(t *testing.T) {
432 r := NewRouter()

Callers

nothing calls this directly

Calls 6

GetMethod · 0.95
HeadMethod · 0.95
NewRouterFunction · 0.85
CloseMethod · 0.80
testRequestFunction · 0.70
WriteMethod · 0.65

Tested by

no test coverage detected