mirror of
https://github.com/gin-gonic/gin.git
synced 2026-09-04 14:49:27 +08:00
test(utils): cover EncodeToken error path in H.MarshalXML
H.MarshalXML returns early when the opening e.EncodeToken(start) call fails, but no test covered that error path. Because xml.Encoder buffers its output, the underlying writer is not called until the buffer flushes. The test writes an 8KB token first to push the encoder into an error state, then calls MarshalXML and asserts the write error is propagated.
This commit is contained in:
parent
34dac209ff
commit
3e6682d0f1
@ -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