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

Function TestMuxSubroutes

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

Source from the content-addressed store, hash-verified

1175}
1176
1177func TestMuxSubroutes(t *testing.T) {
1178 hHubView1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1179 w.Write([]byte("hub1"))
1180 })
1181 hHubView2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1182 w.Write([]byte("hub2"))
1183 })
1184 hHubView3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1185 w.Write([]byte("hub3"))
1186 })
1187 hAccountView1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1188 w.Write([]byte("account1"))
1189 })
1190 hAccountView2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1191 w.Write([]byte("account2"))
1192 })
1193
1194 r := NewRouter()
1195 r.Get("/hubs/{hubID}/view", hHubView1)
1196 r.Get("/hubs/{hubID}/view/*", hHubView2)
1197
1198 sr := NewRouter()
1199 sr.Get("/", hHubView3)
1200 r.Mount("/hubs/{hubID}/users", sr)
1201 r.Get("/hubs/{hubID}/users/", func(w http.ResponseWriter, r *http.Request) {
1202 w.Write([]byte("hub3 override"))
1203 })
1204
1205 sr3 := NewRouter()
1206 sr3.Get("/", hAccountView1)
1207 sr3.Get("/hi", hAccountView2)
1208
1209 // var sr2 *Mux
1210 r.Route("/accounts/{accountID}", func(r Router) {
1211 _ = r.(*Mux) // sr2
1212 // r.Get("/", hAccountView1)
1213 r.Mount("/", sr3)
1214 })
1215
1216 // This is the same as the r.Route() call mounted on sr2
1217 // sr2 := NewRouter()
1218 // sr2.Mount("/", sr3)
1219 // r.Mount("/accounts/{accountID}", sr2)
1220
1221 ts := httptest.NewServer(r)
1222 defer ts.Close()
1223
1224 var body, expected string
1225
1226 _, body = testRequest(t, ts, "GET", "/hubs/123/view", nil)
1227 expected = "hub1"
1228 if body != expected {
1229 t.Fatalf("expected:%s got:%s", expected, body)
1230 }
1231 _, body = testRequest(t, ts, "GET", "/hubs/123/view/index.html", nil)
1232 expected = "hub2"
1233 if body != expected {
1234 t.Fatalf("expected:%s got:%s", expected, body)

Callers

nothing calls this directly

Calls 11

GetMethod · 0.95
MountMethod · 0.95
RouteMethod · 0.95
NewRouterFunction · 0.85
NewRouteContextFunction · 0.85
HandlerFuncMethod · 0.80
CloseMethod · 0.80
testRequestFunction · 0.70
WriteMethod · 0.65
ServeHTTPMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected