MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestHandler_ServeHTTP

Function TestHandler_ServeHTTP

pkg/frontend/transport/handler_test.go:179–453  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

177}
178
179func TestHandler_ServeHTTP(t *testing.T) {
180 roundTripper := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
181 return &http.Response{
182 StatusCode: http.StatusOK,
183 Body: io.NopCloser(strings.NewReader("{}")),
184 }, nil
185 })
186 userID := "12345"
187 tenantFederationCfg := tenantfederation.Config{}
188 for _, tt := range []struct {
189 name string
190 cfg HandlerConfig
191 expectedMetrics int
192 expectedStatusCode int
193 roundTripperFunc roundTripperFunc
194 additionalMetricsCheckFunc func(h *Handler)
195 }{
196 {
197 name: "test handler with stats enabled",
198 cfg: HandlerConfig{QueryStatsEnabled: true},
199 expectedMetrics: 6,
200 roundTripperFunc: roundTripper,
201 expectedStatusCode: http.StatusOK,
202 },
203 {
204 name: "test handler with stats disabled",
205 cfg: HandlerConfig{QueryStatsEnabled: false},
206 expectedMetrics: 0,
207 roundTripperFunc: roundTripper,
208 expectedStatusCode: http.StatusOK,
209 },
210 {
211 name: "test handler with reasonResponseTooLarge",
212 cfg: HandlerConfig{QueryStatsEnabled: true},
213 expectedMetrics: 6,
214 roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
215 return &http.Response{
216 StatusCode: http.StatusRequestEntityTooLarge,
217 Body: io.NopCloser(strings.NewReader("{}")),
218 }, nil
219 }),
220 additionalMetricsCheckFunc: func(h *Handler) {
221 v := promtest.ToFloat64(h.rejectedQueries.WithLabelValues(reasonResponseBodySizeExceeded, requestmeta.SourceAPI, userID))
222 assert.Equal(t, float64(1), v)
223 },
224 expectedStatusCode: http.StatusRequestEntityTooLarge,
225 },
226 {
227 name: "test handler with reasonTooManyRequests",
228 cfg: HandlerConfig{QueryStatsEnabled: true},
229 expectedMetrics: 6,
230 roundTripperFunc: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
231 return &http.Response{
232 StatusCode: http.StatusTooManyRequests,
233 Body: io.NopCloser(strings.NewReader("{}")),
234 }, nil
235 }),
236 additionalMetricsCheckFunc: func(h *Handler) {

Callers

nothing calls this directly

Calls 6

ServeHTTPMethod · 0.95
roundTripperFuncFuncType · 0.85
NewHandlerFunction · 0.85
EqualMethod · 0.65
RunMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected