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

Function TestServerBaseContext

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

Source from the content-addressed store, hash-verified

1953}
1954
1955func TestServerBaseContext(t *testing.T) {
1956 r := NewRouter()
1957 r.Get("/", func(w http.ResponseWriter, r *http.Request) {
1958 baseYes := r.Context().Value(ctxKey{"base"}).(string)
1959 if _, ok := r.Context().Value(http.ServerContextKey).(*http.Server); !ok {
1960 panic("missing server context")
1961 }
1962 if _, ok := r.Context().Value(http.LocalAddrContextKey).(net.Addr); !ok {
1963 panic("missing local addr context")
1964 }
1965 w.Write([]byte(baseYes))
1966 })
1967
1968 // Setup http Server with a base context
1969 ctx := context.WithValue(context.Background(), ctxKey{"base"}, "yes")
1970 ts := httptest.NewUnstartedServer(r)
1971 ts.Config.BaseContext = func(_ net.Listener) context.Context {
1972 return ctx
1973 }
1974 ts.Start()
1975
1976 defer ts.Close()
1977
1978 if _, body := testRequest(t, ts, "GET", "/", nil); body != "yes" {
1979 t.Fatal(body)
1980 }
1981}
1982
1983func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io.Reader) (*http.Response, string) {
1984 req, err := http.NewRequest(method, ts.URL+path, body)

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected