Skip to content

Commit 6b2ea47

Browse files
committed
improve test coverage
1 parent 1a28a18 commit 6b2ea47

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

context_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,9 @@ func TestContextStream(t *testing.T) {
191191
}
192192

193193
func TestContextHTML(t *testing.T) {
194-
e := New()
195194
rec := httptest.NewRecorder()
196195
req := httptest.NewRequest(http.MethodGet, "/", nil)
197-
c := e.NewContext(req, rec)
196+
c := NewContext(req, rec)
198197

199198
err := c.HTML(http.StatusOK, "Hi, Jon Snow")
200199
if assert.NoError(t, err) {
@@ -205,10 +204,9 @@ func TestContextHTML(t *testing.T) {
205204
}
206205

207206
func TestContextHTMLBlob(t *testing.T) {
208-
e := New()
209207
rec := httptest.NewRecorder()
210208
req := httptest.NewRequest(http.MethodGet, "/", nil)
211-
c := e.NewContext(req, rec)
209+
c := NewContext(req, rec)
212210

213211
err := c.HTMLBlob(http.StatusOK, []byte("Hi, Jon Snow"))
214212
if assert.NoError(t, err) {
@@ -245,6 +243,21 @@ func TestContextJSONErrorsOut(t *testing.T) {
245243
assert.Empty(t, rec.Body.String()) // body must not be sent to the client
246244
}
247245

246+
func TestContextJSONWithNotEchoResponse(t *testing.T) {
247+
e := New()
248+
rec := httptest.NewRecorder()
249+
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
250+
c := e.NewContext(req, rec)
251+
252+
c.SetResponse(rec)
253+
254+
err := c.JSON(http.StatusOK, map[string]interface{}{"foo": "bar"})
255+
assert.EqualError(t, err, "json: response does not unwrap to *echo.Response")
256+
257+
assert.Equal(t, http.StatusOK, rec.Code) // status code must not be sent to the client
258+
assert.Empty(t, rec.Body.String()) // body must not be sent to the client
259+
}
260+
248261
func TestContextJSONPretty(t *testing.T) {
249262
e := New()
250263
rec := httptest.NewRecorder()

0 commit comments

Comments
 (0)