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
Provenance
- Commit
052d1a7- Tree
8b2a755- Author
Aum Patel <150163017+Aum-Patel1234@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
ff00c01
5 files changed+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.4+5+package render6+7+import "net/http"8+9+// PDF contains the given PDF binary data.10+type PDF struct {11+ Data []byte12+}13+14+var pdfContentType = []string{"application/pdf"}15+16+// Render (PDF) writes PDF data with custom ContentType.17+func (r PDF) Render(w http.ResponseWriter) error {18+ r.WriteContentType(w)19+ _, err := w.Write(r.Data)20+ return err21+}22+23+// WriteContentType (PDF) writes PDF ContentType for response.24+func (r PDF) WriteContentType(w http.ResponseWriter) {25+ writeContentType(w, pdfContentType)26+}modified:render/render_test.go+16 added
464 unmodified lines465 require.Error(t, err)466 assert.Contains(t, err.Error(), "xml: unsupported type: chan int")467 }468 469+func TestRenderPDF(t *testing.T) {470+ w := httptest.NewRecorder()471+ data := []byte("%Test pdf content")472+473+ pdf := PDF{data}474+475+ pdf.WriteContentType(w)476+ assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))477+478+ err := pdf.Render(w)479+ require.NoError(t, err)480+481+ assert.Equal(t, data, w.Body.Bytes())482+ assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))483+}484+485 func TestRenderRedirect(t *testing.T) {486 req, err := http.NewRequest(http.MethodGet, "/test-redirect", nil)487 require.NoError(t, err)488 319 unmodified linesmodified:render/render.go+1 added
29 unmodified lines30 _ Render = (*Reader)(nil)31 _ Render = (*AsciiJSON)(nil)32 _ Render = (*ProtoBuf)(nil)33 _ Render = (*TOML)(nil)34+ _ Render = (*PDF)(nil)35 )36 37 func writeContentType(w http.ResponseWriter, value []string) {38 header := w.Header()4 unmodified linesmodified:context_test.go+27 added
1318 unmodified lines1319 assert.Empty(t, w.Body.String())1320 assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))1321 }1322 1323+// TestContextRenderPDF tests that the response is serialized as PDF1324+// and Content-Type is set to application/pdf1325+func TestContextRenderPDF(t *testing.T) {1326+ w := httptest.NewRecorder()1327+ c, _ := CreateTestContext(w)1328+1329+ data := []byte("%Test pdf content")1330+ c.PDF(http.StatusCreated, data)1331+1332+ 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+}1336+1337+// Tests that no PDF is rendered if code is 2041338+func TestContextRenderNoContentPDF(t *testing.T) {1339+ w := httptest.NewRecorder()1340+ c, _ := CreateTestContext(w)1341+1342+ data := []byte("%Test pdf content")1343+ c.PDF(http.StatusNoContent, data)1344+1345+ 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+}1349+1350 // TestContextRenderString tests that the response is returned1351 // with Content-Type set to text/plain1352 func TestContextRenderString(t *testing.T) {1353 w := httptest.NewRecorder()2457 unmodified linesmodified:context.go+6 added
1222 unmodified lines1223 func (c *Context) XML(code int, obj any) {1224 c.Render(code, render.XML{Data: obj})1225 }1226 1227+// PDF writes the given PDF binary data into the response body.1228+// It also sets the Content-Type as "application/pdf".1229+func (c *Context) PDF(code int, data []byte) {1230+ c.Render(code, render.PDF{Data: data})1231+}1232+1233 // YAML serializes the given struct as YAML into the response body.1234 func (c *Context) YAML(code int, obj any) {1235 c.Render(code, render.YAML{Data: obj})1236 }253 unmodified lines