Skip to content

Commit cf4820b

Browse files
test: add direct unit tests for acceptsJSON
Requested in review: acceptsJSON was previously only exercised indirectly through the handler-level table test.
1 parent b7b8929 commit cf4820b

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

pkg/loki/config_handler_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,52 @@ func TestConfigQueryHandler(t *testing.T) {
304304
}
305305
}
306306

307+
func TestAcceptsJSON(t *testing.T) {
308+
for _, tc := range []struct {
309+
name string
310+
accept string
311+
want bool
312+
}{
313+
{name: "plain application/json", accept: "application/json", want: true},
314+
{name: "application/json with q=1", accept: "application/json;q=1", want: true},
315+
{name: "application/json with a fractional q", accept: "application/json;q=0.5", want: true},
316+
{name: "application/json with q=0 is explicitly unacceptable", accept: "application/json;q=0", want: false},
317+
{name: "unrelated media type", accept: "text/html", want: false},
318+
{name: "lookalike media type doesn't false-positive", accept: "application/json-seq", want: false},
319+
{name: "empty Accept header", accept: "", want: false},
320+
{name: "media type is case-insensitive", accept: "APPLICATION/JSON", want: true},
321+
{
322+
name: "json present among several media ranges, in any position",
323+
accept: "text/html, application/json;q=0.9, */*;q=0.8",
324+
want: true,
325+
},
326+
{
327+
name: "an unparseable q falls back to unacceptable rather than defaulting to q=1",
328+
accept: "application/json;q=bogus",
329+
want: false,
330+
},
331+
{
332+
name: "a q above 1 falls back to unacceptable rather than defaulting to q=1",
333+
accept: "application/json;q=2",
334+
want: false,
335+
},
336+
{
337+
name: "a negative q falls back to unacceptable rather than defaulting to q=1",
338+
accept: "application/json;q=-1",
339+
want: false,
340+
},
341+
{
342+
name: "a later media range can still accept JSON after an earlier q=0 for it",
343+
accept: "application/json;q=0, application/json;q=0.5",
344+
want: true,
345+
},
346+
} {
347+
t.Run(tc.name, func(t *testing.T) {
348+
assert.Equal(t, tc.want, acceptsJSON(tc.accept))
349+
})
350+
}
351+
}
352+
307353
func TestLimitsDirectJSONMarshaling(t *testing.T) {
308354
// Test that validation.Limits can be directly marshaled to JSON
309355
// (it has proper json tags)

0 commit comments

Comments
 (0)