Browse Source

replace deprecated HeaderMap with Header() (#1585)

andriikushch 7 năm trước cách đây
mục cha
commit
6ab50f944c
5 tập tin đã thay đổi với 44 bổ sung44 xóa
  1. 2 2
      auth_test.go
  2. 1 1
      context_17_test.go
  3. 32 32
      context_test.go
  4. 3 3
      render/render_test.go
  5. 6 6
      routes_test.go

+ 2 - 2
auth_test.go

@@ -122,7 +122,7 @@ func TestBasicAuth401(t *testing.T) {
 
 	assert.False(t, called)
 	assert.Equal(t, http.StatusUnauthorized, w.Code)
-	assert.Equal(t, "Basic realm=\"Authorization Required\"", w.HeaderMap.Get("WWW-Authenticate"))
+	assert.Equal(t, "Basic realm=\"Authorization Required\"", w.Header().Get("WWW-Authenticate"))
 }
 
 func TestBasicAuth401WithCustomRealm(t *testing.T) {
@@ -142,5 +142,5 @@ func TestBasicAuth401WithCustomRealm(t *testing.T) {
 
 	assert.False(t, called)
 	assert.Equal(t, http.StatusUnauthorized, w.Code)
-	assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.HeaderMap.Get("WWW-Authenticate"))
+	assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.Header().Get("WWW-Authenticate"))
 }

+ 1 - 1
context_17_test.go

@@ -23,5 +23,5 @@ func TestContextRenderPureJSON(t *testing.T) {
 	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.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
 }

+ 32 - 32
context_test.go

@@ -628,7 +628,7 @@ func TestContextRenderJSON(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\\u003cb\\u003e\"}", w.Body.String())
-	assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that the response is serialized as JSONP
@@ -642,7 +642,7 @@ func TestContextRenderJSONP(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "x({\"foo\":\"bar\"})", w.Body.String())
-	assert.Equal(t, "application/javascript; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/javascript; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that the response is serialized as JSONP
@@ -656,7 +656,7 @@ func TestContextRenderJSONPWithoutCallback(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
-	assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that no JSON is rendered if code is 204
@@ -668,7 +668,7 @@ func TestContextRenderNoContentJSON(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that the response is serialized as JSON
@@ -682,7 +682,7 @@ func TestContextRenderAPIJSON(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
-	assert.Equal(t, "application/vnd.api+json", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/vnd.api+json", w.Header().Get("Content-Type"))
 }
 
 // Tests that no Custom JSON is rendered if code is 204
@@ -695,7 +695,7 @@ func TestContextRenderNoContentAPIJSON(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, w.HeaderMap.Get("Content-Type"), "application/vnd.api+json")
+	assert.Equal(t, w.Header().Get("Content-Type"), "application/vnd.api+json")
 }
 
 // Tests that the response is serialized as JSON
@@ -708,7 +708,7 @@ func TestContextRenderIndentedJSON(t *testing.T) {
 
 	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.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that no Custom JSON is rendered if code is 204
@@ -720,7 +720,7 @@ func TestContextRenderNoContentIndentedJSON(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that the response is serialized as Secure JSON
@@ -734,7 +734,7 @@ func TestContextRenderSecureJSON(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "&&&START&&&[\"foo\",\"bar\"]", w.Body.String())
-	assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that no Custom JSON is rendered if code is 204
@@ -746,7 +746,7 @@ func TestContextRenderNoContentSecureJSON(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 func TestContextRenderNoContentAsciiJSON(t *testing.T) {
@@ -757,7 +757,7 @@ func TestContextRenderNoContentAsciiJSON(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, "application/json", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
 }
 
 // Tests that the response executes the templates
@@ -773,7 +773,7 @@ func TestContextRenderHTML(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "Hello alexandernyquist", w.Body.String())
-	assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 func TestContextRenderHTML2(t *testing.T) {
@@ -797,7 +797,7 @@ func TestContextRenderHTML2(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "Hello alexandernyquist", w.Body.String())
-	assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that no HTML is rendered if code is 204
@@ -811,7 +811,7 @@ func TestContextRenderNoContentHTML(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // TestContextXML tests that the response is serialized as XML
@@ -824,7 +824,7 @@ func TestContextRenderXML(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "<map><foo>bar</foo></map>", w.Body.String())
-	assert.Equal(t, "application/xml; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that no XML is rendered if code is 204
@@ -836,7 +836,7 @@ func TestContextRenderNoContentXML(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, "application/xml; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // TestContextString tests that the response is returned
@@ -849,7 +849,7 @@ func TestContextRenderString(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "test string 2", w.Body.String())
-	assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that no String is rendered if code is 204
@@ -861,7 +861,7 @@ func TestContextRenderNoContentString(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // TestContextString tests that the response is returned
@@ -875,7 +875,7 @@ func TestContextRenderHTMLString(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "<html>string 3</html>", w.Body.String())
-	assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // Tests that no HTML String is rendered if code is 204
@@ -888,7 +888,7 @@ func TestContextRenderNoContentHTMLString(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // TestContextData tests that the response can be written from `bytesting`
@@ -901,7 +901,7 @@ func TestContextRenderData(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "foo,bar", w.Body.String())
-	assert.Equal(t, "text/csv", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/csv", w.Header().Get("Content-Type"))
 }
 
 // Tests that no Custom Data is rendered if code is 204
@@ -913,7 +913,7 @@ func TestContextRenderNoContentData(t *testing.T) {
 
 	assert.Equal(t, http.StatusNoContent, w.Code)
 	assert.Empty(t, w.Body.String())
-	assert.Equal(t, "text/csv", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/csv", w.Header().Get("Content-Type"))
 }
 
 func TestContextRenderSSE(t *testing.T) {
@@ -942,7 +942,7 @@ func TestContextRenderFile(t *testing.T) {
 
 	assert.Equal(t, http.StatusOK, w.Code)
 	assert.Contains(t, w.Body.String(), "func New() *Engine {")
-	assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // TestContextRenderYAML tests that the response is serialized as YAML
@@ -955,7 +955,7 @@ func TestContextRenderYAML(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, "foo: bar\n", w.Body.String())
-	assert.Equal(t, "application/x-yaml; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/x-yaml; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // TestContextRenderProtoBuf tests that the response is serialized as ProtoBuf
@@ -979,7 +979,7 @@ func TestContextRenderProtoBuf(t *testing.T) {
 
 	assert.Equal(t, http.StatusCreated, w.Code)
 	assert.Equal(t, string(protoData), w.Body.String())
-	assert.Equal(t, "application/x-protobuf", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))
 }
 
 func TestContextHeaders(t *testing.T) {
@@ -1062,7 +1062,7 @@ func TestContextNegotiationWithJSON(t *testing.T) {
 
 	assert.Equal(t, http.StatusOK, w.Code)
 	assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
-	assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 func TestContextNegotiationWithXML(t *testing.T) {
@@ -1077,7 +1077,7 @@ func TestContextNegotiationWithXML(t *testing.T) {
 
 	assert.Equal(t, http.StatusOK, w.Code)
 	assert.Equal(t, "<map><foo>bar</foo></map>", w.Body.String())
-	assert.Equal(t, "application/xml; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 func TestContextNegotiationWithHTML(t *testing.T) {
@@ -1095,7 +1095,7 @@ func TestContextNegotiationWithHTML(t *testing.T) {
 
 	assert.Equal(t, http.StatusOK, w.Code)
 	assert.Equal(t, "Hello gin", w.Body.String())
-	assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 func TestContextNegotiationNotSupport(t *testing.T) {
@@ -1131,7 +1131,7 @@ func TestContextNegotiationFormatWithAccept(t *testing.T) {
 	assert.Empty(t, c.NegotiateFormat(MIMEJSON))
 }
 
-func TestContextNegotiationFormatCustum(t *testing.T) {
+func TestContextNegotiationFormatCustom(t *testing.T) {
 	c, _ := CreateTestContext(httptest.NewRecorder())
 	c.Request, _ = http.NewRequest("POST", "/", nil)
 	c.Request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
@@ -1627,9 +1627,9 @@ func TestContextRenderDataFromReader(t *testing.T) {
 
 	assert.Equal(t, http.StatusOK, w.Code)
 	assert.Equal(t, body, w.Body.String())
-	assert.Equal(t, contentType, w.HeaderMap.Get("Content-Type"))
-	assert.Equal(t, fmt.Sprintf("%d", contentLength), w.HeaderMap.Get("Content-Length"))
-	assert.Equal(t, extraHeaders["Content-Disposition"], w.HeaderMap.Get("Content-Disposition"))
+	assert.Equal(t, contentType, w.Header().Get("Content-Type"))
+	assert.Equal(t, fmt.Sprintf("%d", contentLength), w.Header().Get("Content-Length"))
+	assert.Equal(t, extraHeaders["Content-Disposition"], w.Header().Get("Content-Disposition"))
 }
 
 type TestResponseRecorder struct {

+ 3 - 3
render/render_test.go

@@ -480,7 +480,7 @@ func TestRenderReader(t *testing.T) {
 
 	assert.NoError(t, err)
 	assert.Equal(t, body, w.Body.String())
-	assert.Equal(t, "image/png", w.HeaderMap.Get("Content-Type"))
-	assert.Equal(t, strconv.Itoa(len(body)), w.HeaderMap.Get("Content-Length"))
-	assert.Equal(t, headers["Content-Disposition"], w.HeaderMap.Get("Content-Disposition"))
+	assert.Equal(t, "image/png", w.Header().Get("Content-Type"))
+	assert.Equal(t, strconv.Itoa(len(body)), w.Header().Get("Content-Length"))
+	assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition"))
 }

+ 6 - 6
routes_test.go

@@ -267,7 +267,7 @@ func TestRouteStaticFile(t *testing.T) {
 	assert.Equal(t, w, w2)
 	assert.Equal(t, http.StatusOK, w.Code)
 	assert.Equal(t, "Gin Web Framework", w.Body.String())
-	assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
 
 	w3 := performRequest(router, "HEAD", "/using_static/"+filename)
 	w4 := performRequest(router, "HEAD", "/result")
@@ -285,7 +285,7 @@ func TestRouteStaticListingDir(t *testing.T) {
 
 	assert.Equal(t, http.StatusOK, w.Code)
 	assert.Contains(t, w.Body.String(), "gin.go")
-	assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+	assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
 }
 
 // TestHandleHeadToDir - ensure the root/sub dir handles properly
@@ -312,10 +312,10 @@ func TestRouterMiddlewareAndStatic(t *testing.T) {
 
 	assert.Equal(t, http.StatusOK, w.Code)
 	assert.Contains(t, w.Body.String(), "package gin")
-	assert.Equal(t, "text/plain; charset=utf-8", w.HeaderMap.Get("Content-Type"))
-	assert.NotEqual(t, w.HeaderMap.Get("Last-Modified"), "Mon, 02 Jan 2006 15:04:05 MST")
-	assert.Equal(t, "Mon, 02 Jan 2006 15:04:05 MST", w.HeaderMap.Get("Expires"))
-	assert.Equal(t, "Gin Framework", w.HeaderMap.Get("x-GIN"))
+	assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
+	assert.NotEqual(t, w.Header().Get("Last-Modified"), "Mon, 02 Jan 2006 15:04:05 MST")
+	assert.Equal(t, "Mon, 02 Jan 2006 15:04:05 MST", w.Header().Get("Expires"))
+	assert.Equal(t, "Gin Framework", w.Header().Get("x-GIN"))
 }
 
 func TestRouteNotAllowedEnabled(t *testing.T) {