Skip to content
Myr

replace deprecated HeaderMap with Header() (#1585)

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

authored by andriikushch · committed by 田欧 · parent fbdcbd2 · Browse files · 5 files+44 added−44 removed
This commit

State

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

Diff stats

5 files+44 added−44 removed

  • other:+44 added−44 removed

5 files+44 added−44 removed

modified:render/render_test.go+3 added-3 removed
478 unmodified lines
479 }).Render(w)480481 assert.NoError(t, err)482 assert.Equal(t, body, w.Body.String())483 assert.Equal(t, "image/png", w.HeaderMap.Get("Content-Type"))484 assert.Equal(t, strconv.Itoa(len(body)), w.HeaderMap.Get("Content-Length"))485 assert.Equal(t, headers["Content-Disposition"], w.HeaderMap.Get("Content-Disposition"))483 assert.Equal(t, "image/png", w.Header().Get("Content-Type"))484 assert.Equal(t, strconv.Itoa(len(body)), w.Header().Get("Content-Length"))485 assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition"))486}
modified:auth_test.go+2 added-2 removed
120 unmodified lines
121 router.ServeHTTP(w, req)122123 assert.False(t, called)124 assert.Equal(t, http.StatusUnauthorized, w.Code)125 assert.Equal(t, "Basic realm=\"Authorization Required\"", w.HeaderMap.Get("WWW-Authenticate"))125 assert.Equal(t, "Basic realm=\"Authorization Required\"", w.Header().Get("WWW-Authenticate"))126}127128func TestBasicAuth401WithCustomRealm(t *testing.T) {129 called := false
11 unmodified lines
141 router.ServeHTTP(w, req)142143 assert.False(t, called)144 assert.Equal(t, http.StatusUnauthorized, w.Code)145 assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.HeaderMap.Get("WWW-Authenticate"))145 assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.Header().Get("WWW-Authenticate"))146}
modified:context_17_test.go+1 added-1 removed
21 unmodified lines
22 c, _ := CreateTestContext(w)23 c.PureJSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})24 assert.Equal(t, http.StatusCreated, w.Code)25 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"<b>\"}\n", w.Body.String())26 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))26 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))27}
modified:context_test.go+32 added-32 removed
626 unmodified lines
627 c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})628629 assert.Equal(t, http.StatusCreated, w.Code)630 assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())631 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))631 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))632}633634// Tests that the response is serialized as JSONP635// and Content-Type is set to application/javascript
5 unmodified lines
641 c.JSONP(http.StatusCreated, H{"foo": "bar"})642643 assert.Equal(t, http.StatusCreated, w.Code)644 assert.Equal(t, "x({\"foo\":\"bar\"})", w.Body.String())645 assert.Equal(t, "application/javascript; charset=utf-8", w.HeaderMap.Get("Content-Type"))645 assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))646}647648// Tests that the response is serialized as JSONP649// and Content-Type is set to application/json
5 unmodified lines
655 c.JSONP(http.StatusCreated, H{"foo": "bar"})656657 assert.Equal(t, http.StatusCreated, w.Code)658 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())659 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))659 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))660}661662// Tests that no JSON is rendered if code is 204663func TestContextRenderNoContentJSON(t *testing.T) {
3 unmodified lines
667 c.JSON(http.StatusNoContent, H{"foo": "bar"})668669 assert.Equal(t, http.StatusNoContent, w.Code)670 assert.Empty(t, w.Body.String())671 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))671 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))672}673674// Tests that the response is serialized as JSON675// we change the content-type before
5 unmodified lines
681 c.JSON(http.StatusCreated, H{"foo": "bar"})682683 assert.Equal(t, http.StatusCreated, w.Code)684 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())685 assert.Equal(t, "application/vnd.api+json", w.HeaderMap.Get("Content-Type"))685 assert.Equal(t, "application/vnd.api+json", w.Header().Get("Content-Type"))686}687688// Tests that no Custom JSON is rendered if code is 204689func TestContextRenderNoContentAPIJSON(t *testing.T) {
4 unmodified lines
694 c.JSON(http.StatusNoContent, H{"foo": "bar"})695696 assert.Equal(t, http.StatusNoContent, w.Code)697 assert.Empty(t, w.Body.String())698 assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/vnd.api+json")698 assert.Equal(t, w.Header().Get("Content-Type"), "application/vnd.api+json")699}700701// Tests that the response is serialized as JSON702// and Content-Type is set to application/json
4 unmodified lines
707 c.IndentedJSON(http.StatusCreated, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})708709 assert.Equal(t, http.StatusCreated, w.Code)710 assert.Equal(t, "{\n \"bar\": \"foo\",\n \"foo\": \"bar\",\n \"nested\": {\n \"foo\": \"bar\"\n }\n}", w.Body.String())711 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))711 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))712}713714// Tests that no Custom JSON is rendered if code is 204715func TestContextRenderNoContentIndentedJSON(t *testing.T) {
3 unmodified lines
719 c.IndentedJSON(http.StatusNoContent, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})720721 assert.Equal(t, http.StatusNoContent, w.Code)722 assert.Empty(t, w.Body.String())723 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))723 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))724}725726// Tests that the response is serialized as Secure JSON727// and Content-Type is set to application/json
5 unmodified lines
733 c.SecureJSON(http.StatusCreated, []string{"foo", "bar"})734735 assert.Equal(t, http.StatusCreated, w.Code)736 assert.Equal(t, "&&&START&&&[\"foo\",\"bar\"]", w.Body.String())737 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))737 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))738}739740// Tests that no Custom JSON is rendered if code is 204741func TestContextRenderNoContentSecureJSON(t *testing.T) {
3 unmodified lines
745 c.SecureJSON(http.StatusNoContent, []string{"foo", "bar"})746747 assert.Equal(t, http.StatusNoContent, w.Code)748 assert.Empty(t, w.Body.String())749 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))749 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))750}751752func TestContextRenderNoContentAsciiJSON(t *testing.T) {753 w := httptest.NewRecorder()
2 unmodified lines
756 c.AsciiJSON(http.StatusNoContent, []string{"lang", "Go语言"})757758 assert.Equal(t, http.StatusNoContent, w.Code)759 assert.Empty(t, w.Body.String())760 assert.Equal(t, "application/json", w.HeaderMap.Get("Content-Type"))760 assert.Equal(t, "application/json", w.Header().Get("Content-Type"))761}762763// Tests that the response executes the templates764// and responds with Content-Type set to text/html
7 unmodified lines
772 c.HTML(http.StatusCreated, "t", H{"name": "alexandernyquist"})773774 assert.Equal(t, http.StatusCreated, w.Code)775 assert.Equal(t, "Hello alexandernyquist", w.Body.String())776 assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))776 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))777}778779func TestContextRenderHTML2(t *testing.T) {780 w := httptest.NewRecorder()
15 unmodified lines
796 c.HTML(http.StatusCreated, "t", H{"name": "alexandernyquist"})797798 assert.Equal(t, http.StatusCreated, w.Code)799 assert.Equal(t, "Hello alexandernyquist", w.Body.String())800 assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))800 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))801}802803// Tests that no HTML is rendered if code is 204804func TestContextRenderNoContentHTML(t *testing.T) {
5 unmodified lines
810 c.HTML(http.StatusNoContent, "t", H{"name": "alexandernyquist"})811812 assert.Equal(t, http.StatusNoContent, w.Code)813 assert.Empty(t, w.Body.String())814 assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))814 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))815}816817// TestContextXML tests that the response is serialized as XML818// and Content-Type is set to application/xml
4 unmodified lines
823 c.XML(http.StatusCreated, H{"foo": "bar"})824825 assert.Equal(t, http.StatusCreated, w.Code)826 assert.Equal(t, "<map><foo>bar</foo></map>", w.Body.String())827 assert.Equal(t, "application/xml; charset=utf-8", w.HeaderMap.Get("Content-Type"))827 assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))828}829830// Tests that no XML is rendered if code is 204831func TestContextRenderNoContentXML(t *testing.T) {
3 unmodified lines
835 c.XML(http.StatusNoContent, H{"foo": "bar"})836837 assert.Equal(t, http.StatusNoContent, w.Code)838 assert.Empty(t, w.Body.String())839 assert.Equal(t, "application/xml; charset=utf-8", w.HeaderMap.Get("Content-Type"))839 assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))840}841842// TestContextString tests that the response is returned843// with Content-Type set to text/plain
4 unmodified lines
848 c.String(http.StatusCreated, "test %s %d", "string", 2)849850 assert.Equal(t, http.StatusCreated, w.Code)851 assert.Equal(t, "test string 2", w.Body.String())852 assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))852 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))853}854855// Tests that no String is rendered if code is 204856func TestContextRenderNoContentString(t *testing.T) {
3 unmodified lines
860 c.String(http.StatusNoContent, "test %s %d", "string", 2)861862 assert.Equal(t, http.StatusNoContent, w.Code)863 assert.Empty(t, w.Body.String())864 assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))864 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))865}866867// TestContextString tests that the response is returned868// with Content-Type set to text/html
5 unmodified lines
874 c.String(http.StatusCreated, "<html>%s %d</html>", "string", 3)875876 assert.Equal(t, http.StatusCreated, w.Code)877 assert.Equal(t, "<html>string 3</html>", w.Body.String())878 assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))878 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))879}880881// Tests that no HTML String is rendered if code is 204882func TestContextRenderNoContentHTMLString(t *testing.T) {
4 unmodified lines
887 c.String(http.StatusNoContent, "<html>%s %d</html>", "string", 3)888889 assert.Equal(t, http.StatusNoContent, w.Code)890 assert.Empty(t, w.Body.String())891 assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))891 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))892}893894// TestContextData tests that the response can be written from `bytesting`895// with specified MIME type
4 unmodified lines
900 c.Data(http.StatusCreated, "text/csv", []byte(`foo,bar`))901902 assert.Equal(t, http.StatusCreated, w.Code)903 assert.Equal(t, "foo,bar", w.Body.String())904 assert.Equal(t, "text/csv", w.HeaderMap.Get("Content-Type"))904 assert.Equal(t, "text/csv", w.Header().Get("Content-Type"))905}906907// Tests that no Custom Data is rendered if code is 204908func TestContextRenderNoContentData(t *testing.T) {
3 unmodified lines
912 c.Data(http.StatusNoContent, "text/csv", []byte(`foo,bar`))913914 assert.Equal(t, http.StatusNoContent, w.Code)915 assert.Empty(t, w.Body.String())916 assert.Equal(t, "text/csv", w.HeaderMap.Get("Content-Type"))916 assert.Equal(t, "text/csv", w.Header().Get("Content-Type"))917}918919func TestContextRenderSSE(t *testing.T) {920 w := httptest.NewRecorder()
20 unmodified lines
941 c.File("./gin.go")942943 assert.Equal(t, http.StatusOK, w.Code)944 assert.Contains(t, w.Body.String(), "func New() *Engine {")945 assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))945 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))946}947948// TestContextRenderYAML tests that the response is serialized as YAML949// and Content-Type is set to application/x-yaml
4 unmodified lines
954 c.YAML(http.StatusCreated, H{"foo": "bar"})955956 assert.Equal(t, http.StatusCreated, w.Code)957 assert.Equal(t, "foo: bar\n", w.Body.String())958 assert.Equal(t, "application/x-yaml; charset=utf-8", w.HeaderMap.Get("Content-Type"))958 assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))959}960961// TestContextRenderProtoBuf tests that the response is serialized as ProtoBuf962// and Content-Type is set to application/x-protobuf
15 unmodified lines
978 assert.NoError(t, err)979980 assert.Equal(t, http.StatusCreated, w.Code)981 assert.Equal(t, string(protoData), w.Body.String())982 assert.Equal(t, "application/x-protobuf", w.HeaderMap.Get("Content-Type"))982 assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))983}984985func TestContextHeaders(t *testing.T) {986 c, _ := CreateTestContext(httptest.NewRecorder())
74 unmodified lines
1061 })10621063 assert.Equal(t, http.StatusOK, w.Code)1064 assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())1065 assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))1065 assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))1066}10671068func TestContextNegotiationWithXML(t *testing.T) {1069 w := httptest.NewRecorder()
6 unmodified lines
1076 })10771078 assert.Equal(t, http.StatusOK, w.Code)1079 assert.Equal(t, "<map><foo>bar</foo></map>", w.Body.String())1080 assert.Equal(t, "application/xml; charset=utf-8", w.HeaderMap.Get("Content-Type"))1080 assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))1081}10821083func TestContextNegotiationWithHTML(t *testing.T) {1084 w := httptest.NewRecorder()
9 unmodified lines
1094 })10951096 assert.Equal(t, http.StatusOK, w.Code)1097 assert.Equal(t, "Hello gin", w.Body.String())1098 assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))1098 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))1099}11001101func TestContextNegotiationNotSupport(t *testing.T) {1102 w := httptest.NewRecorder()
27 unmodified lines
1130 assert.Equal(t, MIMEHTML, c.NegotiateFormat(MIMEXML, MIMEHTML))1131 assert.Empty(t, c.NegotiateFormat(MIMEJSON))1132}11331134func TestContextNegotiationFormatCustum(t *testing.T) {1134func TestContextNegotiationFormatCustom(t *testing.T) {1135 c, _ := CreateTestContext(httptest.NewRecorder())1136 c.Request, _ = http.NewRequest("POST", "/", nil)1137 c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")1138
487 unmodified lines
1626 c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)16271628 assert.Equal(t, http.StatusOK, w.Code)1629 assert.Equal(t, body, w.Body.String())1630 assert.Equal(t, contentType, w.HeaderMap.Get("Content-Type"))1631 assert.Equal(t, fmt.Sprintf("%d", contentLength), w.HeaderMap.Get("Content-Length"))1632 assert.Equal(t, extraHeaders["Content-Disposition"], w.HeaderMap.Get("Content-Disposition"))1630 assert.Equal(t, contentType, w.Header().Get("Content-Type"))1631 assert.Equal(t, fmt.Sprintf("%d", contentLength), w.Header().Get("Content-Length"))1632 assert.Equal(t, extraHeaders["Content-Disposition"], w.Header().Get("Content-Disposition"))1633}16341635type TestResponseRecorder struct {1636 *httptest.ResponseRecorder
52 unmodified lines
modified:routes_test.go+6 added-6 removed
265 unmodified lines
266267 assert.Equal(t, w, w2)268 assert.Equal(t, http.StatusOK, w.Code)269 assert.Equal(t, "Gin Web Framework", w.Body.String())270 assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))270 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))271272 w3 := performRequest(router, "HEAD", "/using_static/"+filename)273 w4 := performRequest(router, "HEAD", "/result")274
9 unmodified lines
284 w := performRequest(router, "GET", "/")285286 assert.Equal(t, http.StatusOK, w.Code)287 assert.Contains(t, w.Body.String(), "gin.go")288 assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))288 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))289}290291// TestHandleHeadToDir - ensure the root/sub dir handles properly292func TestRouteStaticNoListing(t *testing.T) {
18 unmodified lines
311 w := performRequest(router, "GET", "/gin.go")312313 assert.Equal(t, http.StatusOK, w.Code)314 assert.Contains(t, w.Body.String(), "package gin")315 assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))316 assert.NotEqual(t, w.HeaderMap.Get("Last-Modified"), "Mon, 02 Jan 2006 15:04:05 MST")317 assert.Equal(t, "Mon, 02 Jan 2006 15:04:05 MST", w.HeaderMap.Get("Expires"))318 assert.Equal(t, "Gin Framework", w.HeaderMap.Get("x-GIN"))315 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))316 assert.NotEqual(t, w.Header().Get("Last-Modified"), "Mon, 02 Jan 2006 15:04:05 MST")317 assert.Equal(t, "Mon, 02 Jan 2006 15:04:05 MST", w.Header().Get("Expires"))318 assert.Equal(t, "Gin Framework", w.Header().Get("x-GIN"))319}320321func TestRouteNotAllowedEnabled(t *testing.T) {322 router := New()
141 unmodified lines