mirror of
https://github.com/gin-gonic/gin.git
synced 2026-09-04 22:53:34 +08:00
Merge 3e6682d0f191d6491b550b9c80f91ebb79597de8 into dcaa4296d111981ffb31ac3eba90bb63e1eb5ab9
This commit is contained in:
commit
b96e362ada
@ -7,6 +7,7 @@ package gin
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
@ -157,6 +158,27 @@ func TestMarshalXMLforHSuccess(t *testing.T) {
|
||||
assert.Contains(t, string(data), "<key2>123</key2>")
|
||||
}
|
||||
|
||||
// errXMLWriter always fails, to exercise encoder write-error paths.
|
||||
type errXMLWriter struct{}
|
||||
|
||||
func (errXMLWriter) Write(_ []byte) (int, error) { return 0, errors.New("write failed") }
|
||||
|
||||
// TestMarshalXMLEncodeTokenError covers the branch where the opening
|
||||
// EncodeToken(start) fails. xml.Encoder buffers its output, so the failing
|
||||
// writer is only reached once the buffer overflows; we prime it with a large
|
||||
// token first, which puts the encoder into an error state. MarshalXML's very
|
||||
// first EncodeToken then returns that cached write error.
|
||||
func TestMarshalXMLEncodeTokenError(t *testing.T) {
|
||||
enc := xml.NewEncoder(errXMLWriter{})
|
||||
|
||||
// Overflow the encoder's internal buffer so it flushes to the failing
|
||||
// writer and latches the error.
|
||||
require.Error(t, enc.EncodeToken(xml.CharData(bytes.Repeat([]byte("a"), 8192))))
|
||||
|
||||
err := H{"key": "value"}.MarshalXML(enc, xml.StartElement{})
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestIsASCII(t *testing.T) {
|
||||
assert.True(t, isASCII("test"))
|
||||
assert.False(t, isASCII("🧡💛💚💙💜"))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user