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

Function TestRequest

pkg/querier/tripperware/queryrange/query_range_test.go:34–97  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

32}
33
34func TestRequest(t *testing.T) {
35 t.Parallel()
36 // Create a Copy parsedRequest to assign the expected headers to the request without affecting other tests using the global.
37 // The test below adds a Test-Header header to the request and expects it back once the encode/decode of request is done via PrometheusCodec
38 parsedRequestWithHeaders := *parsedRequest
39 parsedRequestWithHeaders.Headers = reqHeaders
40 for _, tc := range []struct {
41 url string
42 expected tripperware.Request
43 expectedErr error
44 }{
45 {
46 url: queryAll,
47 expected: &parsedRequestWithHeaders,
48 },
49 {
50 url: "api/v1/query_range?start=foo&stats=all",
51 expectedErr: httpgrpc.Errorf(http.StatusBadRequest, "invalid parameter \"start\"; cannot parse \"foo\" to a valid timestamp"),
52 },
53 {
54 url: "api/v1/query_range?start=123&end=bar",
55 expectedErr: httpgrpc.Errorf(http.StatusBadRequest, "invalid parameter \"end\"; cannot parse \"bar\" to a valid timestamp"),
56 },
57 {
58 url: "api/v1/query_range?start=123&end=0",
59 expectedErr: queryapi.ErrEndBeforeStart,
60 },
61 {
62 url: "api/v1/query_range?start=123&end=456&step=baz",
63 expectedErr: httpgrpc.Errorf(http.StatusBadRequest, "invalid parameter \"step\"; cannot parse \"baz\" to a valid duration"),
64 },
65 {
66 url: "api/v1/query_range?start=123&end=456&step=-1",
67 expectedErr: queryapi.ErrNegativeStep,
68 },
69 {
70 url: "api/v1/query_range?start=0&end=11001&step=1",
71 expectedErr: queryapi.ErrStepTooSmall,
72 },
73 } {
74 t.Run(tc.url, func(t *testing.T) {
75 t.Parallel()
76 r, err := http.NewRequest("POST", tc.url, http.NoBody)
77 require.NoError(t, err)
78 r.Header.Add("Test-Header", "test")
79
80 ctx := user.InjectOrgID(context.Background(), "1")
81
82 // Get a deep copy of the request with Context changed to ctx
83 r = r.Clone(ctx)
84
85 req, err := PrometheusCodec.DecodeRequest(ctx, r, []string{"Test-Header"})
86 if err != nil {
87 require.EqualValues(t, tc.expectedErr, err)
88 return
89 }
90 require.EqualValues(t, tc.expected, req)
91

Callers

nothing calls this directly

Calls 5

RunMethod · 0.65
CloneMethod · 0.65
DecodeRequestMethod · 0.65
EncodeRequestMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected