From a3ccdd9f491c20771cfda343ebebd4b5d0ea6602 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 18 Aug 2026 13:40:20 +0200 Subject: test coverage for microprom --- internal/accept/accept.go | 7 ++++++- internal/accept/accept_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'internal') diff --git a/internal/accept/accept.go b/internal/accept/accept.go index 5ed6e66..c23905a 100644 --- a/internal/accept/accept.go +++ b/internal/accept/accept.go @@ -103,6 +103,11 @@ func (h Header) Negotiate(mediaTypes ...string) Option[string] { return None[string]() } + // if nothing was offered, we default to our own preferred option + if len(h.options) == 0 { + return Some(offers[0].OriginalValue) + } + // NOTE: ParseHeader() sorts options by descending weight, so the first match wins. for _, opt := range h.options { MEDIATYPE: @@ -128,5 +133,5 @@ func (h Header) Negotiate(mediaTypes ...string) Option[string] { } } - return Some(offers[0].OriginalValue) + return None[string]() } diff --git a/internal/accept/accept_test.go b/internal/accept/accept_test.go index 90b6934..edfef42 100644 --- a/internal/accept/accept_test.go +++ b/internal/accept/accept_test.go @@ -12,6 +12,7 @@ import ( ) 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( @@ -38,6 +39,18 @@ func TestAcceptWithHeader(t *testing.T) { "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) { -- cgit v1.3.1