| 1953 | } |
| 1954 | |
| 1955 | func 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 | |
| 1983 | func 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) |