Skip to content
Myr

render_test.go

This file

Path

render/render_test.go

593 lines · 15.1 KiB · Go

Last changed by

test(render): increased unit tests coverage (#3691)Leonardo de Araujobefore adoptionbb2d8cf

History of this file

Take it

The walk stopped after 200 commits: lines older than a550c56 point at it.

chore: update go.mod and remove space from copyright (#3158)thinkeroubefore adoption4b68a5f

line 1why, one step back
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 2–10why, one step back
2// Use of this source code is governed by a MIT style
3// license that can be found in the LICENSE file.45package render67import (8 "encoding/xml"9 "errors"10 "html/template"

test(TOML): Add some tests for the TOML render (#3401)mstmdevbefore adoptione868fd1

line 11why, one step back
11 "net"

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 12–17why, one step back
12 "net/http"
13 "net/http/httptest"14 "strconv"15 "strings"16 "testing"17

improve render code coverage (#3525)ccprobefore adoption6a0556e

line 18why, one step back
18 "github.com/gin-gonic/gin/internal/json"

golangci(lint) : more linters (#2870)Matthieu MORELbefore adoption71f7087

line 19why, one step back
19 testdata "github.com/gin-gonic/gin/testdata/protoexample"

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 20–28why, one step back
20 "github.com/stretchr/testify/assert"
21 "google.golang.org/protobuf/proto"22)2324// TODO unit tests25// test errors2627func TestRenderJSON(t *testing.T) {28 w := httptest.NewRecorder()

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 29why, one step back
29 data := map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 30–43why, one step back
30 "foo": "bar",
31 "html": "<b>",32 }3334 (JSON{data}).WriteContentType(w)35 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))3637 err := (JSON{data}).Render(w)3839 assert.NoError(t, err)40 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())41 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))42}43

Stop useless panicking in context and render (#2150)Vladislav Dmitriyevbefore adoption0c96a20

line 44why, one step back
44func TestRenderJSONError(t *testing.T) {

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 45–48why, one step back
45 w := httptest.NewRecorder()
46 data := make(chan int)4748 // json: unsupported type: chan int

Stop useless panicking in context and render (#2150)Vladislav Dmitriyevbefore adoption0c96a20

line 49why, one step back
49 assert.Error(t, (JSON{data}).Render(w))

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 50–53why, one step back
50}
5152func TestRenderIndentedJSON(t *testing.T) {53 w := httptest.NewRecorder()

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 54why, one step back
54 data := map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 55–76why, one step back
55 "foo": "bar",
56 "bar": "foo",57 }5859 err := (IndentedJSON{data}).Render(w)6061 assert.NoError(t, err)62 assert.Equal(t, "{\n \"bar\": \"foo\",\n \"foo\": \"bar\"\n}", w.Body.String())63 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))64}6566func TestRenderIndentedJSONPanics(t *testing.T) {67 w := httptest.NewRecorder()68 data := make(chan int)6970 // json: unsupported type: chan int71 err := (IndentedJSON{data}).Render(w)72 assert.Error(t, err)73}7475func TestRenderSecureJSON(t *testing.T) {76 w1 := httptest.NewRecorder()

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 77why, one step back
77 data := map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 78–90why, one step back
78 "foo": "bar",
79 }8081 (SecureJSON{"while(1);", data}).WriteContentType(w1)82 assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))8384 err1 := (SecureJSON{"while(1);", data}).Render(w1)8586 assert.NoError(t, err1)87 assert.Equal(t, "{\"foo\":\"bar\"}", w1.Body.String())88 assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))8990 w2 := httptest.NewRecorder()

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 91why, one step back
91 datas := []map[string]any{{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 92–113why, one step back
92 "foo": "bar",
93 }, {94 "bar": "foo",95 }}9697 err2 := (SecureJSON{"while(1);", datas}).Render(w2)98 assert.NoError(t, err2)99 assert.Equal(t, "while(1);[{\"foo\":\"bar\"},{\"bar\":\"foo\"}]", w2.Body.String())100 assert.Equal(t, "application/json; charset=utf-8", w2.Header().Get("Content-Type"))101}102103func TestRenderSecureJSONFail(t *testing.T) {104 w := httptest.NewRecorder()105 data := make(chan int)106107 // json: unsupported type: chan int108 err := (SecureJSON{"while(1);", data}).Render(w)109 assert.Error(t, err)110}111112func TestRenderJsonpJSON(t *testing.T) {113 w1 := httptest.NewRecorder()

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 114why, one step back
114 data := map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 115–127why, one step back
115 "foo": "bar",
116 }117118 (JsonpJSON{"x", data}).WriteContentType(w1)119 assert.Equal(t, "application/javascript; charset=utf-8", w1.Header().Get("Content-Type"))120121 err1 := (JsonpJSON{"x", data}).Render(w1)122123 assert.NoError(t, err1)124 assert.Equal(t, "x({\"foo\":\"bar\"});", w1.Body.String())125 assert.Equal(t, "application/javascript; charset=utf-8", w1.Header().Get("Content-Type"))126127 w2 := httptest.NewRecorder()

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 128why, one step back
128 datas := []map[string]any{{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 129–137why, one step back
129 "foo": "bar",
130 }, {131 "bar": "foo",132 }}133134 err2 := (JsonpJSON{"x", datas}).Render(w2)135 assert.NoError(t, err2)136 assert.Equal(t, "x([{\"foo\":\"bar\"},{\"bar\":\"foo\"}]);", w2.Body.String())137 assert.Equal(t, "application/javascript; charset=utf-8", w2.Header().Get("Content-Type"))

improve render code coverage (#3525)ccprobefore adoption6a0556e

lines 138–182why, one step back
138}
139140type errorWriter struct {141 bufString string142 *httptest.ResponseRecorder143}144145var _ http.ResponseWriter = (*errorWriter)(nil)146147func (w *errorWriter) Write(buf []byte) (int, error) {148 if string(buf) == w.bufString {149 return 0, errors.New(`write "` + w.bufString + `" error`)150 }151 return w.ResponseRecorder.Write(buf)152}153154func TestRenderJsonpJSONError(t *testing.T) {155 ew := &errorWriter{156 ResponseRecorder: httptest.NewRecorder(),157 }158159 jsonpJSON := JsonpJSON{160 Callback: "foo",161 Data: map[string]string{162 "foo": "bar",163 },164 }165166 cb := template.JSEscapeString(jsonpJSON.Callback)167 ew.bufString = cb168 err := jsonpJSON.Render(ew) // error was returned while writing callback169 assert.Equal(t, `write "`+cb+`" error`, err.Error())170171 ew.bufString = `(`172 err = jsonpJSON.Render(ew)173 assert.Equal(t, `write "`+`(`+`" error`, err.Error())174175 data, _ := json.Marshal(jsonpJSON.Data) // error was returned while writing data176 ew.bufString = string(data)177 err = jsonpJSON.Render(ew)178 assert.Equal(t, `write "`+string(data)+`" error`, err.Error())179180 ew.bufString = `);`181 err = jsonpJSON.Render(ew)182 assert.Equal(t, `write "`+`);`+`" error`, err.Error())

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 183–186why, one step back
183}
184185func TestRenderJsonpJSONError2(t *testing.T) {186 w := httptest.NewRecorder()

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 187why, one step back
187 data := map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 188–210why, one step back
188 "foo": "bar",
189 }190 (JsonpJSON{"", data}).WriteContentType(w)191 assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))192193 e := (JsonpJSON{"", data}).Render(w)194 assert.NoError(t, e)195196 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())197 assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))198}199200func TestRenderJsonpJSONFail(t *testing.T) {201 w := httptest.NewRecorder()202 data := make(chan int)203204 // json: unsupported type: chan int205 err := (JsonpJSON{"x", data}).Render(w)206 assert.Error(t, err)207}208209func TestRenderAsciiJSON(t *testing.T) {210 w1 := httptest.NewRecorder()

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 211why, one step back
211 data1 := map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 212–222why, one step back
212 "lang": "GO语言",
213 "tag": "<br>",214 }215216 err := (AsciiJSON{data1}).Render(w1)217218 assert.NoError(t, err)219 assert.Equal(t, "{\"lang\":\"GO\\u8bed\\u8a00\",\"tag\":\"\\u003cbr\\u003e\"}", w1.Body.String())220 assert.Equal(t, "application/json", w1.Header().Get("Content-Type"))221222 w2 := httptest.NewRecorder()

refactor(struct): Remove redundant type conversions (#3345)hopehookbefore adoption33ab0fc

line 223why, one step back
223 data2 := 3.1415926

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 224–239why, one step back
224
225 err = (AsciiJSON{data2}).Render(w2)226 assert.NoError(t, err)227 assert.Equal(t, "3.1415926", w2.Body.String())228}229230func TestRenderAsciiJSONFail(t *testing.T) {231 w := httptest.NewRecorder()232 data := make(chan int)233234 // json: unsupported type: chan int235 assert.Error(t, (AsciiJSON{data}).Render(w))236}237238func TestRenderPureJSON(t *testing.T) {239 w := httptest.NewRecorder()

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 240why, one step back
240 data := map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 241–249why, one step back
241 "foo": "bar",
242 "html": "<b>",243 }244 err := (PureJSON{data}).Render(w)245 assert.NoError(t, err)246 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())247 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))248}249

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 250why, one step back
250type xmlmap map[string]any

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 251–286why, one step back
251
252// Allows type H to be used with xml.Marshal253func (h xmlmap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {254 start.Name = xml.Name{255 Space: "",256 Local: "map",257 }258 if err := e.EncodeToken(start); err != nil {259 return err260 }261 for key, value := range h {262 elem := xml.StartElement{263 Name: xml.Name{Space: "", Local: key},264 Attr: []xml.Attr{},265 }266 if err := e.EncodeElement(value, elem); err != nil {267 return err268 }269 }270271 return e.EncodeToken(xml.EndElement{Name: start.Name})272}273274func TestRenderYAML(t *testing.T) {275 w := httptest.NewRecorder()276 data := `277a : Easy!278b:279 c: 2280 d: [3, 4]281 `282 (YAML{data}).WriteContentType(w)283 assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))284285 err := (YAML{data}).Render(w)286 assert.NoError(t, err)

chore(yaml): upgrade dependency to v3 version (#3456)thinkeroubefore adoptionc9b2724

line 287why, one step back
287 assert.Equal(t, "|4-\n a : Easy!\n b:\n \tc: 2\n \td: [3, 4]\n \t\n", w.Body.String())

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 288–293why, one step back
288 assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))
289}290291type fail struct{}292293// Hook MarshalYAML

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 294why, one step back
294func (ft *fail) MarshalYAML() (any, error) {

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 295–300why, one step back
295 return nil, errors.New("fail")
296}297298func TestRenderYAMLFail(t *testing.T) {299 w := httptest.NewRecorder()300 err := (YAML{&fail{}}).Render(w)

test(TOML): Add some tests for the TOML render (#3401)mstmdevbefore adoptione868fd1

lines 301–321why, one step back
301 assert.Error(t, err)
302}303304func TestRenderTOML(t *testing.T) {305 w := httptest.NewRecorder()306 data := map[string]any{307 "foo": "bar",308 "html": "<b>",309 }310 (TOML{data}).WriteContentType(w)311 assert.Equal(t, "application/toml; charset=utf-8", w.Header().Get("Content-Type"))312313 err := (TOML{data}).Render(w)314 assert.NoError(t, err)315 assert.Equal(t, "foo = 'bar'\nhtml = '<b>'\n", w.Body.String())316 assert.Equal(t, "application/toml; charset=utf-8", w.Header().Get("Content-Type"))317}318319func TestRenderTOMLFail(t *testing.T) {320 w := httptest.NewRecorder()321 err := (TOML{net.IPv4bcast}).Render(w)

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 322–428why, one step back
322 assert.Error(t, err)
323}324325// test Protobuf rendering326func TestRenderProtoBuf(t *testing.T) {327 w := httptest.NewRecorder()328 reps := []int64{int64(1), int64(2)}329 label := "test"330 data := &testdata.Test{331 Label: &label,332 Reps: reps,333 }334335 (ProtoBuf{data}).WriteContentType(w)336 protoData, err := proto.Marshal(data)337 assert.NoError(t, err)338 assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))339340 err = (ProtoBuf{data}).Render(w)341342 assert.NoError(t, err)343 assert.Equal(t, string(protoData), w.Body.String())344 assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))345}346347func TestRenderProtoBufFail(t *testing.T) {348 w := httptest.NewRecorder()349 data := &testdata.Test{}350 err := (ProtoBuf{data}).Render(w)351 assert.Error(t, err)352}353354func TestRenderXML(t *testing.T) {355 w := httptest.NewRecorder()356 data := xmlmap{357 "foo": "bar",358 }359360 (XML{data}).WriteContentType(w)361 assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))362363 err := (XML{data}).Render(w)364365 assert.NoError(t, err)366 assert.Equal(t, "<map><foo>bar</foo></map>", w.Body.String())367 assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))368}369370func TestRenderRedirect(t *testing.T) {371 req, err := http.NewRequest("GET", "/test-redirect", nil)372 assert.NoError(t, err)373374 data1 := Redirect{375 Code: http.StatusMovedPermanently,376 Request: req,377 Location: "/new/location",378 }379380 w := httptest.NewRecorder()381 err = data1.Render(w)382 assert.NoError(t, err)383384 data2 := Redirect{385 Code: http.StatusOK,386 Request: req,387 Location: "/new/location",388 }389390 w = httptest.NewRecorder()391 assert.PanicsWithValue(t, "Cannot redirect with status code 200", func() {392 err := data2.Render(w)393 assert.NoError(t, err)394 })395396 data3 := Redirect{397 Code: http.StatusCreated,398 Request: req,399 Location: "/new/location",400 }401402 w = httptest.NewRecorder()403 err = data3.Render(w)404 assert.NoError(t, err)405406 // only improve coverage407 data2.WriteContentType(w)408}409410func TestRenderData(t *testing.T) {411 w := httptest.NewRecorder()412 data := []byte("#!PNG some raw data")413414 err := (Data{415 ContentType: "image/png",416 Data: data,417 }).Render(w)418419 assert.NoError(t, err)420 assert.Equal(t, "#!PNG some raw data", w.Body.String())421 assert.Equal(t, "image/png", w.Header().Get("Content-Type"))422}423424func TestRenderString(t *testing.T) {425 w := httptest.NewRecorder()426427 (String{428 Format: "hello %s %d",

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 429why, one step back
429 Data: []any{},

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 430–434why, one step back
430 }).WriteContentType(w)
431 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))432433 err := (String{434 Format: "hola %s %d",

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 435why, one step back
435 Data: []any{"manu", 2},

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 436–447why, one step back
436 }).Render(w)
437438 assert.NoError(t, err)439 assert.Equal(t, "hola manu 2", w.Body.String())440 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))441}442443func TestRenderStringLenZero(t *testing.T) {444 w := httptest.NewRecorder()445446 err := (String{447 Format: "hola %s %d",

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 448why, one step back
448 Data: []any{},

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 449–460why, one step back
449 }).Render(w)
450451 assert.NoError(t, err)452 assert.Equal(t, "hola %s %d", w.Body.String())453 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))454}455456func TestRenderHTMLTemplate(t *testing.T) {457 w := httptest.NewRecorder()458 templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))459460 htmlRender := HTMLProduction{Template: templ}

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 461why, one step back
461 instance := htmlRender.Instance("t", map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 462–476why, one step back
462 "name": "alexandernyquist",
463 })464465 err := instance.Render(w)466467 assert.NoError(t, err)468 assert.Equal(t, "Hello alexandernyquist", w.Body.String())469 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))470}471472func TestRenderHTMLTemplateEmptyName(t *testing.T) {473 w := httptest.NewRecorder()474 templ := template.Must(template.New("").Parse(`Hello {{.name}}`))475476 htmlRender := HTMLProduction{Template: templ}

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 477why, one step back
477 instance := htmlRender.Instance("", map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 478–495why, one step back
478 "name": "alexandernyquist",
479 })480481 err := instance.Render(w)482483 assert.NoError(t, err)484 assert.Equal(t, "Hello alexandernyquist", w.Body.String())485 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))486}487488func TestRenderHTMLDebugFiles(t *testing.T) {489 w := httptest.NewRecorder()490 htmlRender := HTMLDebug{491 Files: []string{"../testdata/template/hello.tmpl"},492 Glob: "",493 Delims: Delims{Left: "{[{", Right: "}]}"},494 FuncMap: nil,495 }

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 496why, one step back
496 instance := htmlRender.Instance("hello.tmpl", map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 497–514why, one step back
497 "name": "thinkerou",
498 })499500 err := instance.Render(w)501502 assert.NoError(t, err)503 assert.Equal(t, "<h1>Hello thinkerou</h1>", w.Body.String())504 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))505}506507func TestRenderHTMLDebugGlob(t *testing.T) {508 w := httptest.NewRecorder()509 htmlRender := HTMLDebug{510 Files: nil,511 Glob: "../testdata/template/hello*",512 Delims: Delims{Left: "{[{", Right: "}]}"},513 FuncMap: nil,514 }

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 515why, one step back
515 instance := htmlRender.Instance("hello.tmpl", map[string]any{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 516–580why, one step back
516 "name": "thinkerou",
517 })518519 err := instance.Render(w)520521 assert.NoError(t, err)522 assert.Equal(t, "<h1>Hello thinkerou</h1>", w.Body.String())523 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))524}525526func TestRenderHTMLDebugPanics(t *testing.T) {527 htmlRender := HTMLDebug{528 Files: nil,529 Glob: "",530 Delims: Delims{"{{", "}}"},531 FuncMap: nil,532 }533 assert.Panics(t, func() { htmlRender.Instance("", nil) })534}535536func TestRenderReader(t *testing.T) {537 w := httptest.NewRecorder()538539 body := "#!PNG some raw data"540 headers := make(map[string]string)541 headers["Content-Disposition"] = `attachment; filename="filename.png"`542 headers["x-request-id"] = "requestId"543544 err := (Reader{545 ContentLength: int64(len(body)),546 ContentType: "image/png",547 Reader: strings.NewReader(body),548 Headers: headers,549 }).Render(w)550551 assert.NoError(t, err)552 assert.Equal(t, body, w.Body.String())553 assert.Equal(t, "image/png", w.Header().Get("Content-Type"))554 assert.Equal(t, strconv.Itoa(len(body)), w.Header().Get("Content-Length"))555 assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition"))556 assert.Equal(t, headers["x-request-id"], w.Header().Get("x-request-id"))557}558559func TestRenderReaderNoContentLength(t *testing.T) {560 w := httptest.NewRecorder()561562 body := "#!PNG some raw data"563 headers := make(map[string]string)564 headers["Content-Disposition"] = `attachment; filename="filename.png"`565 headers["x-request-id"] = "requestId"566567 err := (Reader{568 ContentLength: -1,569 ContentType: "image/png",570 Reader: strings.NewReader(body),571 Headers: headers,572 }).Render(w)573574 assert.NoError(t, err)575 assert.Equal(t, body, w.Body.String())576 assert.Equal(t, "image/png", w.Header().Get("Content-Type"))577 assert.NotContains(t, "Content-Length", w.Header())578 assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition"))579 assert.Equal(t, headers["x-request-id"], w.Header().Get("x-request-id"))580}

test(render): increased unit tests coverage (#3691)Leonardo de Araujobefore adoptionbb2d8cf

lines 581–593why, one step back
581
582func TestRenderWriteError(t *testing.T) {583 data := []interface{}{"value1", "value2"}584 prefix := "my-prefix:"585 r := SecureJSON{Data: data, Prefix: prefix}586 ew := &errorWriter{587 bufString: prefix,588 ResponseRecorder: httptest.NewRecorder(),589 }590 err := r.Render(ew)591 assert.NotNil(t, err)592 assert.Equal(t, `write "my-prefix:" error`, err.Error())593}