MCPcopy
hub / github.com/julienschmidt/httprouter / TestRouterAPI

Function TestRouterAPI

router_test.go:78–165  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

76}
77
78func TestRouterAPI(t *testing.T) {
79 var get, head, options, post, put, patch, delete, handler, handlerFunc bool
80
81 httpHandler := handlerStruct{&handler}
82
83 router := New()
84 router.GET("/GET", func(w http.ResponseWriter, r *http.Request, _ Params) {
85 get = true
86 })
87 router.HEAD("/GET", func(w http.ResponseWriter, r *http.Request, _ Params) {
88 head = true
89 })
90 router.OPTIONS("/GET", func(w http.ResponseWriter, r *http.Request, _ Params) {
91 options = true
92 })
93 router.POST("/POST", func(w http.ResponseWriter, r *http.Request, _ Params) {
94 post = true
95 })
96 router.PUT("/PUT", func(w http.ResponseWriter, r *http.Request, _ Params) {
97 put = true
98 })
99 router.PATCH("/PATCH", func(w http.ResponseWriter, r *http.Request, _ Params) {
100 patch = true
101 })
102 router.DELETE("/DELETE", func(w http.ResponseWriter, r *http.Request, _ Params) {
103 delete = true
104 })
105 router.Handler(http.MethodGet, "/Handler", httpHandler)
106 router.HandlerFunc(http.MethodGet, "/HandlerFunc", func(w http.ResponseWriter, r *http.Request) {
107 handlerFunc = true
108 })
109
110 w := new(mockResponseWriter)
111
112 r, _ := http.NewRequest(http.MethodGet, "/GET", nil)
113 router.ServeHTTP(w, r)
114 if !get {
115 t.Error("routing GET failed")
116 }
117
118 r, _ = http.NewRequest(http.MethodHead, "/GET", nil)
119 router.ServeHTTP(w, r)
120 if !head {
121 t.Error("routing HEAD failed")
122 }
123
124 r, _ = http.NewRequest(http.MethodOptions, "/GET", nil)
125 router.ServeHTTP(w, r)
126 if !options {
127 t.Error("routing OPTIONS failed")
128 }
129
130 r, _ = http.NewRequest(http.MethodPost, "/POST", nil)
131 router.ServeHTTP(w, r)
132 if !post {
133 t.Error("routing POST failed")
134 }
135

Callers

nothing calls this directly

Calls 11

NewFunction · 0.85
GETMethod · 0.80
HEADMethod · 0.80
OPTIONSMethod · 0.80
POSTMethod · 0.80
PUTMethod · 0.80
PATCHMethod · 0.80
DELETEMethod · 0.80
HandlerMethod · 0.80
HandlerFuncMethod · 0.80
ServeHTTPMethod · 0.45

Tested by

no test coverage detected