mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 19:44:53 +08:00
fix(render): do not send an empty Content-Type header
This commit is contained in:
parent
0f09c3a9b4
commit
cab8027a10
@ -26,6 +26,10 @@ func (r Data) Render(w http.ResponseWriter) (err error) {
|
||||
}
|
||||
|
||||
// WriteContentType (Data) writes custom ContentType.
|
||||
// An empty ContentType is not written, so that net/http detects it from the body.
|
||||
func (r Data) WriteContentType(w http.ResponseWriter) {
|
||||
if r.ContentType == "" {
|
||||
return
|
||||
}
|
||||
writeContentType(w, []string{r.ContentType})
|
||||
}
|
||||
|
||||
@ -33,7 +33,11 @@ func (r Reader) Render(w http.ResponseWriter) (err error) {
|
||||
}
|
||||
|
||||
// WriteContentType (Reader) writes custom ContentType.
|
||||
// An empty ContentType is not written, so that net/http detects it from the body.
|
||||
func (r Reader) WriteContentType(w http.ResponseWriter) {
|
||||
if r.ContentType == "" {
|
||||
return
|
||||
}
|
||||
writeContentType(w, []string{r.ContentType})
|
||||
}
|
||||
|
||||
|
||||
@ -815,3 +815,42 @@ func TestRenderWriteError(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "write error", err.Error())
|
||||
}
|
||||
|
||||
func TestRenderEmptyContentType(t *testing.T) {
|
||||
const page = "<html><body>hello</body></html>"
|
||||
|
||||
handlers := map[string]http.HandlerFunc{
|
||||
// What net/http does when the handler sets no Content-Type.
|
||||
"net/http": func(w http.ResponseWriter, _ *http.Request) {
|
||||
_, err := io.WriteString(w, page)
|
||||
assert.NoError(t, err)
|
||||
},
|
||||
"Data": func(w http.ResponseWriter, _ *http.Request) {
|
||||
assert.NoError(t, Data{Data: []byte(page)}.Render(w))
|
||||
},
|
||||
"Reader": func(w http.ResponseWriter, _ *http.Request) {
|
||||
assert.NoError(t, Reader{ContentLength: int64(len(page)), Reader: strings.NewReader(page)}.Render(w))
|
||||
},
|
||||
}
|
||||
|
||||
for name, handler := range handlers {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
srv := httptest.NewServer(handler)
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
resp, err := http.Get(srv.URL)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
assert.Equal(t, []string{"text/html; charset=utf-8"}, resp.Header.Values("Content-Type"))
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, page, string(body))
|
||||
})
|
||||
}
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
Data{}.WriteContentType(w)
|
||||
Reader{}.WriteContentType(w)
|
||||
assert.NotContains(t, w.Header(), "Content-Type")
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user