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

Function TestCustomHTTPMethod

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

Source from the content-addressed store, hash-verified

1759}
1760
1761func TestCustomHTTPMethod(t *testing.T) {
1762 // first we must register this method to be accepted, then we
1763 // can define method handlers on the router below
1764 RegisterMethod("BOO")
1765
1766 r := NewRouter()
1767 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
1768 w.Write([]byte("."))
1769 })
1770
1771 // note the custom BOO method for route /hi
1772 r.MethodFunc("BOO", "/hi", func(w http.ResponseWriter, r *http.Request) {
1773 w.Write([]byte("custom method"))
1774 })
1775
1776 ts := httptest.NewServer(r)
1777 defer ts.Close()
1778
1779 if _, body := testRequest(t, ts, "GET", "/", nil); body != "." {
1780 t.Fatal(body)
1781 }
1782 if _, body := testRequest(t, ts, "BOO", "/hi", nil); body != "custom method" {
1783 t.Fatal(body)
1784 }
1785
1786 var expectRoutes = map[string]string{
1787 "GET": "/",
1788 "BOO": "/hi",
1789 }
1790 Walk(r, func(method string, route string, handler http.Handler, _ ...func(http.Handler) http.Handler) error {
1791 r, ok := expectRoutes[method]
1792 if !ok {
1793 t.Fatalf("unexpected method %s", method)
1794 }
1795 if r != route {
1796 t.Fatalf("expected route %s, got %s", r, route)
1797 }
1798 delete(expectRoutes, method)
1799
1800 return nil
1801 })
1802 if len(expectRoutes) != 0 {
1803 t.Fatalf("missing expected methods: %v", expectRoutes)
1804 }
1805}
1806
1807func TestMuxMatch(t *testing.T) {
1808 r := NewRouter()

Callers

nothing calls this directly

Calls 8

GetMethod · 0.95
MethodFuncMethod · 0.95
RegisterMethodFunction · 0.85
NewRouterFunction · 0.85
WalkFunction · 0.85
CloseMethod · 0.80
testRequestFunction · 0.70
WriteMethod · 0.65

Tested by

no test coverage detected