mirror of
https://github.com/gin-gonic/gin.git
synced 2025-04-06 03:57:46 +08:00
Merge ff59b49650ad9b570d96c23be2857945608eadaa into 8763f33c65f7df8be5b9fe7504ab7fcf20abb41d
This commit is contained in:
commit
5261a26678
@ -918,7 +918,7 @@ func TestContextRenderJSON(t *testing.T) {
|
||||
c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})
|
||||
|
||||
assert.Equal(t, http.StatusCreated, w.Code)
|
||||
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())
|
||||
assert.JSONEq(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())
|
||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
@ -946,7 +946,7 @@ func TestContextRenderJSONPWithoutCallback(t *testing.T) {
|
||||
c.JSONP(http.StatusCreated, H{"foo": "bar"})
|
||||
|
||||
assert.Equal(t, http.StatusCreated, w.Code)
|
||||
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
|
||||
assert.JSONEq(t, "{\"foo\":\"bar\"}", w.Body.String())
|
||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
@ -972,7 +972,7 @@ func TestContextRenderAPIJSON(t *testing.T) {
|
||||
c.JSON(http.StatusCreated, H{"foo": "bar"})
|
||||
|
||||
assert.Equal(t, http.StatusCreated, w.Code)
|
||||
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
|
||||
assert.JSONEq(t, "{\"foo\":\"bar\"}", w.Body.String())
|
||||
assert.Equal(t, "application/vnd.api+json", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
@ -998,7 +998,7 @@ func TestContextRenderIndentedJSON(t *testing.T) {
|
||||
c.IndentedJSON(http.StatusCreated, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})
|
||||
|
||||
assert.Equal(t, http.StatusCreated, w.Code)
|
||||
assert.Equal(t, "{\n \"bar\": \"foo\",\n \"foo\": \"bar\",\n \"nested\": {\n \"foo\": \"bar\"\n }\n}", w.Body.String())
|
||||
assert.JSONEq(t, "{\n \"bar\": \"foo\",\n \"foo\": \"bar\",\n \"nested\": {\n \"foo\": \"bar\"\n }\n}", w.Body.String())
|
||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
@ -1059,7 +1059,7 @@ func TestContextRenderPureJSON(t *testing.T) {
|
||||
c, _ := CreateTestContext(w)
|
||||
c.PureJSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})
|
||||
assert.Equal(t, http.StatusCreated, w.Code)
|
||||
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
|
||||
assert.JSONEq(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
|
||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
@ -1432,7 +1432,7 @@ func TestContextNegotiationWithJSON(t *testing.T) {
|
||||
})
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
|
||||
assert.JSONEq(t, "{\"foo\":\"bar\"}", w.Body.String())
|
||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
@ -1518,7 +1518,7 @@ func TestContextNegotiationFormat(t *testing.T) {
|
||||
c.Request, _ = http.NewRequest(http.MethodPost, "", nil)
|
||||
|
||||
assert.Panics(t, func() { c.NegotiateFormat() })
|
||||
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON, MIMEXML))
|
||||
assert.JSONEq(t, MIMEJSON, c.NegotiateFormat(MIMEJSON, MIMEXML))
|
||||
assert.Equal(t, MIMEHTML, c.NegotiateFormat(MIMEHTML, MIMEJSON))
|
||||
}
|
||||
|
||||
@ -1540,7 +1540,7 @@ func TestContextNegotiationFormatWithWildcardAccept(t *testing.T) {
|
||||
assert.Equal(t, "*/*", c.NegotiateFormat("*/*"))
|
||||
assert.Equal(t, "text/*", c.NegotiateFormat("text/*"))
|
||||
assert.Equal(t, "application/*", c.NegotiateFormat("application/*"))
|
||||
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
|
||||
assert.JSONEq(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
|
||||
assert.Equal(t, MIMEXML, c.NegotiateFormat(MIMEXML))
|
||||
assert.Equal(t, MIMEHTML, c.NegotiateFormat(MIMEHTML))
|
||||
|
||||
@ -1564,9 +1564,9 @@ func TestContextNegotiationFormatCustom(t *testing.T) {
|
||||
c.Accepted = nil
|
||||
c.SetAccepted(MIMEJSON, MIMEXML)
|
||||
|
||||
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON, MIMEXML))
|
||||
assert.JSONEq(t, MIMEJSON, c.NegotiateFormat(MIMEJSON, MIMEXML))
|
||||
assert.Equal(t, MIMEXML, c.NegotiateFormat(MIMEXML, MIMEHTML))
|
||||
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
|
||||
assert.JSONEq(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
|
||||
}
|
||||
|
||||
func TestContextNegotiationFormat2(t *testing.T) {
|
||||
@ -1634,7 +1634,7 @@ func TestContextAbortWithStatusJSON(t *testing.T) {
|
||||
_, err := buf.ReadFrom(w.Body)
|
||||
require.NoError(t, err)
|
||||
jsonStringBody := buf.String()
|
||||
assert.Equal(t, "{\"foo\":\"fooValue\",\"bar\":\"barValue\"}", jsonStringBody)
|
||||
assert.JSONEq(t, "{\"foo\":\"fooValue\",\"bar\":\"barValue\"}", jsonStringBody)
|
||||
}
|
||||
|
||||
func TestContextError(t *testing.T) {
|
||||
|
@ -34,7 +34,7 @@ func TestError(t *testing.T) {
|
||||
}, err.JSON())
|
||||
|
||||
jsonBytes, _ := json.Marshal(err)
|
||||
assert.Equal(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))
|
||||
assert.JSONEq(t, "{\"error\":\"test error\",\"meta\":\"some data\"}", string(jsonBytes))
|
||||
|
||||
err.SetMeta(H{ //nolint: errcheck
|
||||
"status": "200",
|
||||
@ -93,13 +93,13 @@ Error #03: third
|
||||
H{"error": "third", "status": "400"},
|
||||
}, errs.JSON())
|
||||
jsonBytes, _ := json.Marshal(errs)
|
||||
assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))
|
||||
assert.JSONEq(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))
|
||||
errs = errorMsgs{
|
||||
{Err: errors.New("first"), Type: ErrorTypePrivate},
|
||||
}
|
||||
assert.Equal(t, H{"error": "first"}, errs.JSON())
|
||||
jsonBytes, _ = json.Marshal(errs)
|
||||
assert.Equal(t, "{\"error\":\"first\"}", string(jsonBytes))
|
||||
assert.JSONEq(t, "{\"error\":\"first\"}", string(jsonBytes))
|
||||
|
||||
errs = errorMsgs{}
|
||||
assert.Nil(t, errs.Last())
|
||||
|
@ -371,11 +371,11 @@ func TestErrorLogger(t *testing.T) {
|
||||
|
||||
w := PerformRequest(router, http.MethodGet, "/error")
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "{\"error\":\"this is an error\"}", w.Body.String())
|
||||
assert.JSONEq(t, "{\"error\":\"this is an error\"}", w.Body.String())
|
||||
|
||||
w = PerformRequest(router, http.MethodGet, "/abort")
|
||||
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
||||
assert.Equal(t, "{\"error\":\"no authorized\"}", w.Body.String())
|
||||
assert.JSONEq(t, "{\"error\":\"no authorized\"}", w.Body.String())
|
||||
|
||||
w = PerformRequest(router, http.MethodGet, "/print")
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
|
@ -38,7 +38,7 @@ func TestRenderJSON(t *testing.T) {
|
||||
err := (JSON{data}).Render(w)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())
|
||||
assert.JSONEq(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())
|
||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
@ -60,7 +60,7 @@ func TestRenderIndentedJSON(t *testing.T) {
|
||||
err := (IndentedJSON{data}).Render(w)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "{\n \"bar\": \"foo\",\n \"foo\": \"bar\"\n}", w.Body.String())
|
||||
assert.JSONEq(t, "{\n \"bar\": \"foo\",\n \"foo\": \"bar\"\n}", w.Body.String())
|
||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
@ -85,7 +85,7 @@ func TestRenderSecureJSON(t *testing.T) {
|
||||
err1 := (SecureJSON{"while(1);", data}).Render(w1)
|
||||
|
||||
require.NoError(t, err1)
|
||||
assert.Equal(t, "{\"foo\":\"bar\"}", w1.Body.String())
|
||||
assert.JSONEq(t, "{\"foo\":\"bar\"}", w1.Body.String())
|
||||
assert.Equal(t, "application/json; charset=utf-8", w1.Header().Get("Content-Type"))
|
||||
|
||||
w2 := httptest.NewRecorder()
|
||||
@ -194,7 +194,7 @@ func TestRenderJsonpJSONError2(t *testing.T) {
|
||||
e := (JsonpJSON{"", data}).Render(w)
|
||||
require.NoError(t, e)
|
||||
|
||||
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
|
||||
assert.JSONEq(t, "{\"foo\":\"bar\"}", w.Body.String())
|
||||
assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
@ -217,7 +217,7 @@ func TestRenderAsciiJSON(t *testing.T) {
|
||||
err := (AsciiJSON{data1}).Render(w1)
|
||||
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "{\"lang\":\"GO\\u8bed\\u8a00\",\"tag\":\"\\u003cbr\\u003e\"}", w1.Body.String())
|
||||
assert.JSONEq(t, "{\"lang\":\"GO\\u8bed\\u8a00\",\"tag\":\"\\u003cbr\\u003e\"}", w1.Body.String())
|
||||
assert.Equal(t, "application/json", w1.Header().Get("Content-Type"))
|
||||
|
||||
w2 := httptest.NewRecorder()
|
||||
@ -244,7 +244,7 @@ func TestRenderPureJSON(t *testing.T) {
|
||||
}
|
||||
err := (PureJSON{data}).Render(w)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
|
||||
assert.JSONEq(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())
|
||||
assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user