From db85d1227e997a55862ff652ea9eaa28f4115d03 Mon Sep 17 00:00:00 2001 From: w169q169 Date: Thu, 26 Apr 2018 09:24:29 +0800 Subject: [PATCH] add jsonp test on the context --- context_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/context_test.go b/context_test.go index 9024cfc1..841d8af0 100644 --- a/context_test.go +++ b/context_test.go @@ -581,6 +581,20 @@ func TestContextRenderJSON(t *testing.T) { assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type")) } +// Tests that the response is serialized as JSONP +// and Content-Type is set to application/javascript +func TestContextRenderJSONP(t *testing.T) { + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + c.Request, _ = http.NewRequest("GET", "http://example.com/?callback=x", nil) + + c.JSONP(201, H{"foo": "bar"}) + + assert.Equal(t, 201, w.Code) + assert.Equal(t, "x({\"foo\":\"bar\"})", w.Body.String()) + assert.Equal(t, "application/javascript; charset=utf-8", w.HeaderMap.Get("Content-Type")) +} + // Tests that no JSON is rendered if code is 204 func TestContextRenderNoContentJSON(t *testing.T) { w := httptest.NewRecorder()