diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2026-08-18 13:40:30 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2026-08-18 13:40:30 +0200 |
| commit | 5cc0f9db288ffaf44d104974707338d52530cece (patch) | |
| tree | f0ad530dd072c55c6f46dfea23af993fb31333d4 /internal/accept/accept_test.go | |
| parent | ab5012e0083ff6623ffab78f52f9ea1120d80ffb (diff) | |
| parent | a3ccdd9f491c20771cfda343ebebd4b5d0ea6602 (diff) | |
| download | go-gg-5cc0f9db288ffaf44d104974707338d52530cece.tar.gz | |
Merge branch 'microprom'
Diffstat (limited to 'internal/accept/accept_test.go')
| -rw-r--r-- | internal/accept/accept_test.go | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/internal/accept/accept_test.go b/internal/accept/accept_test.go new file mode 100644 index 0000000..edfef42 --- /dev/null +++ b/internal/accept/accept_test.go @@ -0,0 +1,94 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net> +// SPDX-License-Identifier: Apache-2.0 + +package accept_test + +import ( + "testing" + + "go.xyrillian.de/gg/assert" + "go.xyrillian.de/gg/internal/accept" + . "go.xyrillian.de/gg/option" +) + +func TestAcceptWithHeader(t *testing.T) { + // asking for a wide range of formats, including wildcard matches + h := accept.ParseHeader([]string{"text/*;q=0.3, text/plain;format=flowed, text/plain;format=fixed;q=0.4, */*;q=0.5"}) + + assert.Equal(t, h.Negotiate( + "image/png", // matches with q=0.5 + "text/plain; format=fixed", // matches with q=0.4 + ), Some("image/png")) + + assert.Equal(t, h.Negotiate( + "image/png", // matches with q=0.5 + "text/plain; format=flowed", // matches with q=1.0 + ), Some("text/plain; format=flowed")) + + assert.Equal(t, h.Negotiate( + "text/plain", // matches with q=0.7 + "text/plain; format=flowed", // matches with q=1.0 + ), Some("text/plain; format=flowed")) + + assert.Equal(t, h.Negotiate( + "text/plain", // matches with q=0.7 + "text/plain; format=other", // matches with q=0.3 + ), Some("text/plain")) + + assert.Equal(t, h.Negotiate( + "text/markdown", // matches with q=0.3 + "text/plain", // matches with q=0.3 (but first wins) + ), Some("text/markdown")) + + // asking for specific formats only + h = accept.ParseHeader([]string{"image/png, image/jpeg"}) + + assert.Equal(t, h.Negotiate( + "text/plain", + "image/png", + ), Some("image/png")) + + assert.Equal(t, h.Negotiate( + "text/plain", + ), None[string]()) +} + +func TestAcceptWithoutHeader(t *testing.T) { + // Negotiate() will always pick the first option + h := accept.ParseHeader(nil) + + assert.Equal(t, h.Negotiate( + "image/png", + "image/jpeg", + ), Some("image/png")) + + assert.Equal(t, h.Negotiate(nil...), None[string]()) + + // malformed media types are ignored + assert.Equal(t, h.Negotiate( + "image/png/foo", + "image/jpeg", + ), Some("image/jpeg")) + + assert.Equal(t, h.Negotiate( + "image/png/foo", + "image/jpeg/foo", + ), None[string]()) +} + +func TestAcceptWithMalformedHeader(t *testing.T) { + for _, brokenHeader := range []string{ + "text/plain, text/markdown/foo", // malformed media type + "text/plain, image/png; q=high", // malformed q-value + "text/plain, image/jpeg; q=1.25", // q-value out of range + } { + h := accept.ParseHeader([]string{brokenHeader}) + + // broken headers are ignored completely, so the first option wins by default + assert.Equal(t, h.Negotiate( + "image/png", + "image/jpeg", + "text/plain", + ), Some("image/png")) + } +} |
