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
Provenance
- Commit
6ab50f9- Tree
ebf6275- Author
andriikushch <andrii.kushch@gmail.com>- Committer
田欧 <thinkerou@gmail.com>- Parent
fbdcbd2
5 files changed+44 added−44 removed
modified:render/render_test.go+3 added-3 removed
478 unmodified lines479 }).Render(w)480 481 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 lines121 router.ServeHTTP(w, req)122 123 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 }127 128 func TestBasicAuth401WithCustomRealm(t *testing.T) {129 called := false11 unmodified lines141 router.ServeHTTP(w, req)142 143 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 lines22 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 lines627 c.JSON(http.StatusCreated, H{"foo": "bar", "html": "<b>"})628 629 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 }633 634 // Tests that the response is serialized as JSONP635 // and Content-Type is set to application/javascript5 unmodified lines641 c.JSONP(http.StatusCreated, H{"foo": "bar"})642 643 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 }647 648 // Tests that the response is serialized as JSONP649 // and Content-Type is set to application/json5 unmodified lines655 c.JSONP(http.StatusCreated, H{"foo": "bar"})656 657 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 }661 662 // Tests that no JSON is rendered if code is 204663 func TestContextRenderNoContentJSON(t *testing.T) {3 unmodified lines667 c.JSON(http.StatusNoContent, H{"foo": "bar"})668 669 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 }673 674 // Tests that the response is serialized as JSON675 // we change the content-type before5 unmodified lines681 c.JSON(http.StatusCreated, H{"foo": "bar"})682 683 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 }687 688 // Tests that no Custom JSON is rendered if code is 204689 func TestContextRenderNoContentAPIJSON(t *testing.T) {4 unmodified lines694 c.JSON(http.StatusNoContent, H{"foo": "bar"})695 696 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 }700 701 // Tests that the response is serialized as JSON702 // and Content-Type is set to application/json4 unmodified lines707 c.IndentedJSON(http.StatusCreated, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})708 709 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 }713 714 // Tests that no Custom JSON is rendered if code is 204715 func TestContextRenderNoContentIndentedJSON(t *testing.T) {3 unmodified lines719 c.IndentedJSON(http.StatusNoContent, H{"foo": "bar", "bar": "foo", "nested": H{"foo": "bar"}})720 721 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 }725 726 // Tests that the response is serialized as Secure JSON727 // and Content-Type is set to application/json5 unmodified lines733 c.SecureJSON(http.StatusCreated, []string{"foo", "bar"})734 735 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 }739 740 // Tests that no Custom JSON is rendered if code is 204741 func TestContextRenderNoContentSecureJSON(t *testing.T) {3 unmodified lines745 c.SecureJSON(http.StatusNoContent, []string{"foo", "bar"})746 747 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 }751 752 func TestContextRenderNoContentAsciiJSON(t *testing.T) {753 w := httptest.NewRecorder()2 unmodified lines756 c.AsciiJSON(http.StatusNoContent, []string{"lang", "Go语言"})757 758 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 }762 763 // Tests that the response executes the templates764 // and responds with Content-Type set to text/html7 unmodified lines772 c.HTML(http.StatusCreated, "t", H{"name": "alexandernyquist"})773 774 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 }778 779 func TestContextRenderHTML2(t *testing.T) {780 w := httptest.NewRecorder()15 unmodified lines796 c.HTML(http.StatusCreated, "t", H{"name": "alexandernyquist"})797 798 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 }802 803 // Tests that no HTML is rendered if code is 204804 func TestContextRenderNoContentHTML(t *testing.T) {5 unmodified lines810 c.HTML(http.StatusNoContent, "t", H{"name": "alexandernyquist"})811 812 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 }816 817 // TestContextXML tests that the response is serialized as XML818 // and Content-Type is set to application/xml4 unmodified lines823 c.XML(http.StatusCreated, H{"foo": "bar"})824 825 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 }829 830 // Tests that no XML is rendered if code is 204831 func TestContextRenderNoContentXML(t *testing.T) {3 unmodified lines835 c.XML(http.StatusNoContent, H{"foo": "bar"})836 837 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 }841 842 // TestContextString tests that the response is returned843 // with Content-Type set to text/plain4 unmodified lines848 c.String(http.StatusCreated, "test %s %d", "string", 2)849 850 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 }854 855 // Tests that no String is rendered if code is 204856 func TestContextRenderNoContentString(t *testing.T) {3 unmodified lines860 c.String(http.StatusNoContent, "test %s %d", "string", 2)861 862 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 }866 867 // TestContextString tests that the response is returned868 // with Content-Type set to text/html5 unmodified lines874 c.String(http.StatusCreated, "<html>%s %d</html>", "string", 3)875 876 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 }880 881 // Tests that no HTML String is rendered if code is 204882 func TestContextRenderNoContentHTMLString(t *testing.T) {4 unmodified lines887 c.String(http.StatusNoContent, "<html>%s %d</html>", "string", 3)888 889 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 }893 894 // TestContextData tests that the response can be written from `bytesting`895 // with specified MIME type4 unmodified lines900 c.Data(http.StatusCreated, "text/csv", []byte(`foo,bar`))901 902 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 }906 907 // Tests that no Custom Data is rendered if code is 204908 func TestContextRenderNoContentData(t *testing.T) {3 unmodified lines912 c.Data(http.StatusNoContent, "text/csv", []byte(`foo,bar`))913 914 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 }918 919 func TestContextRenderSSE(t *testing.T) {920 w := httptest.NewRecorder()20 unmodified lines941 c.File("./gin.go")942 943 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 }947 948 // TestContextRenderYAML tests that the response is serialized as YAML949 // and Content-Type is set to application/x-yaml4 unmodified lines954 c.YAML(http.StatusCreated, H{"foo": "bar"})955 956 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 }960 961 // TestContextRenderProtoBuf tests that the response is serialized as ProtoBuf962 // and Content-Type is set to application/x-protobuf15 unmodified lines978 assert.NoError(t, err)979 980 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 }984 985 func TestContextHeaders(t *testing.T) {986 c, _ := CreateTestContext(httptest.NewRecorder())74 unmodified lines1061 })1062 1063 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 }1067 1068 func TestContextNegotiationWithXML(t *testing.T) {1069 w := httptest.NewRecorder()6 unmodified lines1076 })1077 1078 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 }1082 1083 func TestContextNegotiationWithHTML(t *testing.T) {1084 w := httptest.NewRecorder()9 unmodified lines1094 })1095 1096 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 }1100 1101 func TestContextNegotiationNotSupport(t *testing.T) {1102 w := httptest.NewRecorder()27 unmodified lines1130 assert.Equal(t, MIMEHTML, c.NegotiateFormat(MIMEXML, MIMEHTML))1131 assert.Empty(t, c.NegotiateFormat(MIMEJSON))1132 }1133 1134-func TestContextNegotiationFormatCustum(t *testing.T) {1134+func 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 lines1626 c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)1627 1628 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 }1634 1635 type TestResponseRecorder struct {1636 *httptest.ResponseRecorder52 unmodified linesmodified:routes_test.go+6 added-6 removed
265 unmodified lines266 267 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"))271 272 w3 := performRequest(router, "HEAD", "/using_static/"+filename)273 w4 := performRequest(router, "HEAD", "/result")274 9 unmodified lines284 w := performRequest(router, "GET", "/")285 286 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 }290 291 // TestHandleHeadToDir - ensure the root/sub dir handles properly292 func TestRouteStaticNoListing(t *testing.T) {18 unmodified lines311 w := performRequest(router, "GET", "/gin.go")312 313 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 }320 321 func TestRouteNotAllowedEnabled(t *testing.T) {322 router := New()141 unmodified lines