mirror of
https://github.com/gin-gonic/gin.git
synced 2026-03-13 09:21:45 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3e44fdc4d1 | ||
|
|
cb2b764cc8 | ||
|
|
a39670fb7b | ||
|
|
052d1a79aa | ||
|
|
ff00c01e67 |
4
.github/workflows/trivy-scan.yml
vendored
4
.github/workflows/trivy-scan.yml
vendored
@@ -27,7 +27,7 @@ jobs:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Run Trivy vulnerability scanner (source code)
|
||||
uses: aquasecurity/trivy-action@0.34.1
|
||||
uses: aquasecurity/trivy-action@0.34.2
|
||||
with:
|
||||
scan-type: "fs"
|
||||
scan-ref: "."
|
||||
@@ -44,7 +44,7 @@ jobs:
|
||||
sarif_file: "trivy-results.sarif"
|
||||
|
||||
- name: Run Trivy scanner (table output for logs)
|
||||
uses: aquasecurity/trivy-action@0.34.1
|
||||
uses: aquasecurity/trivy-action@0.34.2
|
||||
if: always()
|
||||
with:
|
||||
scan-type: "fs"
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Manuel Martínez-Almeida
|
||||
Copyright (c) 2014-present Manuel Martínez-Almeida
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
8
Makefile
8
Makefile
@@ -4,7 +4,7 @@ GO_VERSION=$(shell $(GO) version | cut -c 14- | cut -d' ' -f1 | cut -d'.' -f2)
|
||||
PACKAGES ?= $(shell $(GO) list ./...)
|
||||
VETPACKAGES ?= $(shell $(GO) list ./... | grep -v /examples/)
|
||||
GOFILES := $(shell find . -name "*.go")
|
||||
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'gin$$|binding$$|render$$' | grep -v examples)
|
||||
TESTFOLDER := $(shell $(GO) list ./... | grep -E 'gin$$|ginS$$|binding$$|render$$' | grep -v examples)
|
||||
TESTTAGS ?= ""
|
||||
|
||||
.PHONY: test
|
||||
@@ -12,7 +12,11 @@ TESTTAGS ?= ""
|
||||
test:
|
||||
echo "mode: count" > coverage.out
|
||||
for d in $(TESTFOLDER); do \
|
||||
$(GO) test $(TESTTAGS) -v -covermode=count -coverprofile=profile.out $$d > tmp.out; \
|
||||
if [ -n "$(TESTTAGS)" ]; then \
|
||||
$(GO) test $(TESTTAGS) -v -covermode=count -coverprofile=profile.out $$d > tmp.out; \
|
||||
else \
|
||||
$(GO) test -v -covermode=count -coverprofile=profile.out $$d > tmp.out; \
|
||||
fi; \
|
||||
cat tmp.out; \
|
||||
if grep -q "^--- FAIL" tmp.out; then \
|
||||
rm tmp.out; \
|
||||
|
||||
@@ -11,9 +11,9 @@
|
||||
[](https://www.codetriage.com/gin-gonic/gin)
|
||||
[](https://github.com/gin-gonic/gin/releases)
|
||||
|
||||
## 📰 [Announcing Gin 1.12.0!](https://gin-gonic.com/en/blog/news/gin-1-12-0-release-announcement/)
|
||||
## 📰 Gin 1.12.0 is now available!
|
||||
|
||||
Read about the latest features and improvements in Gin 1.11.0 on our official blog.
|
||||
We're excited to announce the release of **[Gin 1.12.0](https://gin-gonic.com/en/blog/news/gin-1-12-0-release-announcement/)**! This release brings new features, performance improvements, and important bug fixes. Check out the [release announcement](https://gin-gonic.com/en/blog/news/gin-1-12-0-release-announcement/) on our official blog for the full details.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1224,6 +1224,12 @@ func (c *Context) XML(code int, obj any) {
|
||||
c.Render(code, render.XML{Data: obj})
|
||||
}
|
||||
|
||||
// PDF writes the given PDF binary data into the response body.
|
||||
// It also sets the Content-Type as "application/pdf".
|
||||
func (c *Context) PDF(code int, data []byte) {
|
||||
c.Render(code, render.PDF{Data: data})
|
||||
}
|
||||
|
||||
// YAML serializes the given struct as YAML into the response body.
|
||||
func (c *Context) YAML(code int, obj any) {
|
||||
c.Render(code, render.YAML{Data: obj})
|
||||
|
||||
@@ -1320,6 +1320,33 @@ func TestContextRenderNoContentXML(t *testing.T) {
|
||||
assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
// TestContextRenderPDF tests that the response is serialized as PDF
|
||||
// and Content-Type is set to application/pdf
|
||||
func TestContextRenderPDF(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
data := []byte("%Test pdf content")
|
||||
c.PDF(http.StatusCreated, data)
|
||||
|
||||
assert.Equal(t, http.StatusCreated, w.Code)
|
||||
assert.Equal(t, data, w.Body.Bytes())
|
||||
assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
// Tests that no PDF is rendered if code is 204
|
||||
func TestContextRenderNoContentPDF(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
|
||||
data := []byte("%Test pdf content")
|
||||
c.PDF(http.StatusNoContent, data)
|
||||
|
||||
assert.Equal(t, http.StatusNoContent, w.Code)
|
||||
assert.Empty(t, w.Body.Bytes())
|
||||
assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
// TestContextRenderString tests that the response is returned
|
||||
// with Content-Type set to text/plain
|
||||
func TestContextRenderString(t *testing.T) {
|
||||
|
||||
26
render/pdf.go
Normal file
26
render/pdf.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright 2026 Gin Core Team. All rights reserved.
|
||||
// Use of this source code is governed by a MIT style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package render
|
||||
|
||||
import "net/http"
|
||||
|
||||
// PDF contains the given PDF binary data.
|
||||
type PDF struct {
|
||||
Data []byte
|
||||
}
|
||||
|
||||
var pdfContentType = []string{"application/pdf"}
|
||||
|
||||
// Render (PDF) writes PDF data with custom ContentType.
|
||||
func (r PDF) Render(w http.ResponseWriter) error {
|
||||
r.WriteContentType(w)
|
||||
_, err := w.Write(r.Data)
|
||||
return err
|
||||
}
|
||||
|
||||
// WriteContentType (PDF) writes PDF ContentType for response.
|
||||
func (r PDF) WriteContentType(w http.ResponseWriter) {
|
||||
writeContentType(w, pdfContentType)
|
||||
}
|
||||
@@ -31,6 +31,7 @@ var (
|
||||
_ Render = (*AsciiJSON)(nil)
|
||||
_ Render = (*ProtoBuf)(nil)
|
||||
_ Render = (*TOML)(nil)
|
||||
_ Render = (*PDF)(nil)
|
||||
)
|
||||
|
||||
func writeContentType(w http.ResponseWriter, value []string) {
|
||||
|
||||
@@ -466,6 +466,22 @@ func TestRenderXMLError(t *testing.T) {
|
||||
assert.Contains(t, err.Error(), "xml: unsupported type: chan int")
|
||||
}
|
||||
|
||||
func TestRenderPDF(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
data := []byte("%Test pdf content")
|
||||
|
||||
pdf := PDF{data}
|
||||
|
||||
pdf.WriteContentType(w)
|
||||
assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))
|
||||
|
||||
err := pdf.Render(w)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, data, w.Body.Bytes())
|
||||
assert.Equal(t, "application/pdf", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
func TestRenderRedirect(t *testing.T) {
|
||||
req, err := http.NewRequest(http.MethodGet, "/test-redirect", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
Reference in New Issue
Block a user