Skip to content
Myr

allow empty headers on DataFromReader (#2121)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Dmitry Kutakov · committed by thinkerou · parent 2ee0e96 · Browse files · 3 files+43 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+43 added−0 removed

  • other:+43 added−0 removed

3 files+43 added−0 removed

added:render/reader_test.go+23 added
1// Copyright 2019 Gin Core Team. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package render67import (8 "net/http/httptest"9 "strings"10 "testing"1112 "github.com/stretchr/testify/require"13)1415func TestReaderRenderNoHeaders(t *testing.T) {16 content := "test"17 r := Reader{18 ContentLength: int64(len(content)),19 Reader: strings.NewReader(content),20 }21 err := r.Render(httptest.NewRecorder())22 require.NoError(t, err)23}
modified:render/reader.go+3 added
20 unmodified lines
21// Render (Reader) writes data with custom ContentType and headers.22func (r Reader) Render(w http.ResponseWriter) (err error) {23 r.WriteContentType(w)24 if r.ContentLength >= 0 {25 if r.Headers == nil {26 r.Headers = map[string]string{}27 }28 r.Headers["Content-Length"] = strconv.FormatInt(r.ContentLength, 10)29 }30 r.writeHeaders(w, r.Headers)31 _, err = io.Copy(w, r.Reader)
17 unmodified lines
modified:context_test.go+17 added
1797 unmodified lines
1798 assert.Equal(t, fmt.Sprintf("%d", contentLength), w.Header().Get("Content-Length"))1799 assert.Equal(t, extraHeaders["Content-Disposition"], w.Header().Get("Content-Disposition"))1800}18011802func TestContextRenderDataFromReaderNoHeaders(t *testing.T) {1803 w := httptest.NewRecorder()1804 c, _ := CreateTestContext(w)18051806 body := "#!PNG some raw data"1807 reader := strings.NewReader(body)1808 contentLength := int64(len(body))1809 contentType := "image/png"18101811 c.DataFromReader(http.StatusOK, contentLength, contentType, reader, nil)18121813 assert.Equal(t, http.StatusOK, w.Code)1814 assert.Equal(t, body, w.Body.String())1815 assert.Equal(t, contentType, w.Header().Get("Content-Type"))1816 assert.Equal(t, fmt.Sprintf("%d", contentLength), w.Header().Get("Content-Length"))1817}18181819type TestResponseRecorder struct {1820 *httptest.ResponseRecorder1821 closeChannel chan bool1822}
85 unmodified lines