Skip to content
Myr

feat(render): add PDF renderer and tests (#4491)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* render: add PDF renderer and tests * render: update PDF renderer copyright header * test: add PDF rendering tests including 204 No Content in context_test.go

authored by Aum Patel · committed by GitHub · parent ff00c01 · Browse files · 5 files+76 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+76 added−0 removed

  • other:+76 added−0 removed

5 files+76 added−0 removed

added:render/pdf.go+26 added
1// Copyright 2026 Gin Core Team. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package render67import "net/http"89// PDF contains the given PDF binary data.10type PDF struct {11 Data []byte12}1314var pdfContentType = []string{"application/pdf"}1516// Render (PDF) writes PDF data with custom ContentType.17func (r PDF) Render(w http.ResponseWriter) error {18 r.WriteContentType(w)19 _, err := w.Write(r.Data)20 return err21}2223// WriteContentType (PDF) writes PDF ContentType for response.24func (r PDF) WriteContentType(w http.ResponseWriter) {25 writeContentType(w, pdfContentType)26}
modified:render/render_test.go+16 added
464 unmodified lines
465 require.Error(t, err)466 assert.Contains(t, err.Error(), "xml: unsupported type: chan int")467}468469func TestRenderPDF(t *testing.T) {470 w := httptest.NewRecorder()471 data := []byte("%Test pdf content")472473 pdf := PDF{data}474475 pdf.WriteContentType(w)476 assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))477478 err := pdf.Render(w)479 require.NoError(t, err)480481 assert.Equal(t, data, w.Body.Bytes())482 assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))483}484485func TestRenderRedirect(t *testing.T) {486 req, err := http.NewRequest(http.MethodGet, "/test-redirect", nil)487 require.NoError(t, err)488
319 unmodified lines
modified:render/render.go+1 added
29 unmodified lines
30 _ Render = (*Reader)(nil)31 _ Render = (*AsciiJSON)(nil)32 _ Render = (*ProtoBuf)(nil)33 _ Render = (*TOML)(nil)34 _ Render = (*PDF)(nil)35)3637func writeContentType(w http.ResponseWriter, value []string) {38 header := w.Header()
4 unmodified lines
modified:context_test.go+27 added
1318 unmodified lines
1319 assert.Empty(t, w.Body.String())1320 assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))1321}13221323// TestContextRenderPDF tests that the response is serialized as PDF1324// and Content-Type is set to application/pdf1325func TestContextRenderPDF(t *testing.T) {1326 w := httptest.NewRecorder()1327 c, _ := CreateTestContext(w)13281329 data := []byte("%Test pdf content")1330 c.PDF(http.StatusCreated, data)13311332 assert.Equal(t, http.StatusCreated, w.Code)1333 assert.Equal(t, data, w.Body.Bytes())1334 assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))1335}13361337// Tests that no PDF is rendered if code is 2041338func TestContextRenderNoContentPDF(t *testing.T) {1339 w := httptest.NewRecorder()1340 c, _ := CreateTestContext(w)13411342 data := []byte("%Test pdf content")1343 c.PDF(http.StatusNoContent, data)13441345 assert.Equal(t, http.StatusNoContent, w.Code)1346 assert.Empty(t, w.Body.Bytes())1347 assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))1348}13491350// TestContextRenderString tests that the response is returned1351// with Content-Type set to text/plain1352func TestContextRenderString(t *testing.T) {1353 w := httptest.NewRecorder()
2457 unmodified lines
modified:context.go+6 added
1222 unmodified lines
1223func (c *Context) XML(code int, obj any) {1224 c.Render(code, render.XML{Data: obj})1225}12261227// PDF writes the given PDF binary data into the response body.1228// It also sets the Content-Type as "application/pdf".1229func (c *Context) PDF(code int, data []byte) {1230 c.Render(code, render.PDF{Data: data})1231}12321233// YAML serializes the given struct as YAML into the response body.1234func (c *Context) YAML(code int, obj any) {1235 c.Render(code, render.YAML{Data: obj})1236}
253 unmodified lines