From 22ae924b10f959da8fcd360b84281553f03f633a Mon Sep 17 00:00:00 2001 From: kkoehler Date: Fri, 27 Jun 2025 09:54:49 +0200 Subject: [PATCH] added test for protobuf negotiation logic and for the registration of custom contentType --- context_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/context_test.go b/context_test.go index fe3ccd69..27c404de 100644 --- a/context_test.go +++ b/context_test.go @@ -1542,6 +1542,44 @@ func TestContextNegotiationWithHTML(t *testing.T) { assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type")) } +func TestContextNegotiationWithProto(t *testing.T) { + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + c.Request, _ = http.NewRequest(http.MethodPost, "", nil) + + c.Negotiate(http.StatusOK, Negotiate{ + Offered: []string{binding.MIMEPROTOBUF}, + Data: &testdata.Test{}, + }) + + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, 0, w.Body.Len()) + assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type")) +} + +func TestContextNegotiationWithCustom(t *testing.T) { + + contentType := "application/whatever" + + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + c.Request, _ = http.NewRequest(http.MethodPost, "", nil) + + AddNegotiationRenderMapping(contentType, func(status int, config Negotiate, c *Context) { + data := config.Data.([]byte) + c.Data(status, contentType, data) + }) + + c.Negotiate(http.StatusOK, Negotiate{ + Offered: []string{contentType}, + Data: []byte("Hello World"), + }) + + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "Hello World", w.Body.String()) + assert.Equal(t, contentType, w.Header().Get("Content-Type")) +} + func TestContextNegotiationNotSupport(t *testing.T) { w := httptest.NewRecorder() c, _ := CreateTestContext(w)