mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-06 12:08:20 +08:00
Compare commits
4 Commits
eea692698f
...
8539f0b206
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8539f0b206 | ||
|
|
5f4f964325 | ||
|
|
cd9b91bb2e | ||
|
|
4a81d5807a |
2
.github/workflows/gin.yml
vendored
2
.github/workflows/gin.yml
vendored
@ -78,6 +78,6 @@ jobs:
|
|||||||
run: make test
|
run: make test
|
||||||
|
|
||||||
- name: Upload coverage to Codecov
|
- name: Upload coverage to Codecov
|
||||||
uses: codecov/codecov-action@v5
|
uses: codecov/codecov-action@v6
|
||||||
with:
|
with:
|
||||||
flags: ${{ matrix.os }},go-${{ matrix.go }},${{ matrix.test-tags }}
|
flags: ${{ matrix.os }},go-${{ matrix.go }},${{ matrix.test-tags }}
|
||||||
|
|||||||
4
.github/workflows/trivy-scan.yml
vendored
4
.github/workflows/trivy-scan.yml
vendored
@ -27,7 +27,7 @@ jobs:
|
|||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Run Trivy vulnerability scanner (source code)
|
- name: Run Trivy vulnerability scanner (source code)
|
||||||
uses: aquasecurity/trivy-action@0.35.0
|
uses: aquasecurity/trivy-action@v0.36.0
|
||||||
with:
|
with:
|
||||||
scan-type: "fs"
|
scan-type: "fs"
|
||||||
scan-ref: "."
|
scan-ref: "."
|
||||||
@ -44,7 +44,7 @@ jobs:
|
|||||||
sarif_file: "trivy-results.sarif"
|
sarif_file: "trivy-results.sarif"
|
||||||
|
|
||||||
- name: Run Trivy scanner (table output for logs)
|
- name: Run Trivy scanner (table output for logs)
|
||||||
uses: aquasecurity/trivy-action@0.35.0
|
uses: aquasecurity/trivy-action@v0.36.0
|
||||||
if: always()
|
if: always()
|
||||||
with:
|
with:
|
||||||
scan-type: "fs"
|
scan-type: "fs"
|
||||||
|
|||||||
@ -7,6 +7,7 @@ package binding
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -50,7 +51,12 @@ func decodeJSON(r io.Reader, obj any) error {
|
|||||||
decoder.DisallowUnknownFields()
|
decoder.DisallowUnknownFields()
|
||||||
}
|
}
|
||||||
if err := decoder.Decode(obj); err != nil {
|
if err := decoder.Decode(obj); err != nil {
|
||||||
|
if errors.Is(err, io.EOF) {
|
||||||
|
return fmt.Errorf("empty request body: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return validate(obj)
|
return validate(obj)
|
||||||
}
|
}
|
||||||
|
|||||||
39
binding/json_external_test.go
Normal file
39
binding/json_external_test.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package binding_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestJSONBindingEmptyBodyReturnsHelpfulError(t *testing.T) {
|
||||||
|
type Req struct {
|
||||||
|
Name string `json:"name" binding:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := gin.CreateTestContext(w)
|
||||||
|
|
||||||
|
req, err := http.NewRequest(http.MethodPost, "/", bytes.NewBuffer(nil))
|
||||||
|
require.NoError(t, err)
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
c.Request = req
|
||||||
|
|
||||||
|
var r Req
|
||||||
|
err = c.ShouldBindJSON(&r)
|
||||||
|
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
// Error message should be more descriptive than plain EOF,
|
||||||
|
// while still preserving io.EOF via wrapping.
|
||||||
|
assert.NotEqual(t, "EOF", err.Error())
|
||||||
|
assert.Contains(t, err.Error(), "empty request body")
|
||||||
|
assert.ErrorIs(t, err, io.EOF)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user