Skip to content
Myr

improve render code coverage (#3525)

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

authored by ccpro · committed by GitHub · parent eac2daa · Browse files · 1 file+46 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

1 file+46 added−0 removed

  • other:+46 added−0 removed

1 file+46 added−0 removed

modified:render/render_test.go+46 added
13 unmodified lines
14 "strconv"15 "strings"16 "testing"1718 "github.com/gin-gonic/gin/internal/json"19 testdata "github.com/gin-gonic/gin/testdata/protoexample"20 "github.com/stretchr/testify/assert"21 "google.golang.org/protobuf/proto"22)
113 unmodified lines
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"))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())183}184185func TestRenderJsonpJSONError2(t *testing.T) {186 w := httptest.NewRecorder()187 data := map[string]any{188 "foo": "bar",
392 unmodified lines