refactor: move json related header vars to internal (#1840)

* refactor: move json related header vars to internal

* refactor: use header.ContentType
This commit is contained in:
Kevin Wan
2022-04-28 15:12:04 +08:00
committed by GitHub
parent cef83efd4e
commit 3bbc90ec24
14 changed files with 58 additions and 42 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/zeromicro/go-zero/core/lang"
"github.com/zeromicro/go-zero/core/mapping"
"github.com/zeromicro/go-zero/rest/httpc/internal"
"github.com/zeromicro/go-zero/rest/internal/header"
)
var interceptors = []internal.Interceptor{
@@ -98,7 +99,7 @@ func buildRequest(ctx context.Context, method, url string, data interface{}) (*h
req.URL.RawQuery = buildFormQuery(u, val[formKey])
fillHeader(req, val[headerKey])
if hasJsonBody {
req.Header.Set(contentType, applicationJson)
req.Header.Set(header.ContentType, header.JsonContentType)
}
return req, nil

View File

@@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zeromicro/go-zero/rest/internal/header"
"github.com/zeromicro/go-zero/rest/router"
)
@@ -27,7 +28,7 @@ func TestDoRequest_NotFound(t *testing.T) {
defer svr.Close()
req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
assert.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set(header.ContentType, header.JsonContentType)
resp, err := DoRequest(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)

View File

@@ -6,6 +6,7 @@ import (
"github.com/zeromicro/go-zero/core/mapping"
"github.com/zeromicro/go-zero/rest/internal/encoding"
"github.com/zeromicro/go-zero/rest/internal/header"
)
// Parse parses the response.
@@ -32,5 +33,5 @@ func ParseJsonBody(resp *http.Response, val interface{}) error {
}
func withJsonBody(r *http.Response) bool {
return r.ContentLength > 0 && strings.Contains(r.Header.Get(contentType), applicationJson)
return r.ContentLength > 0 && strings.Contains(r.Header.Get(header.ContentType), header.ApplicationJson)
}

View File

@@ -6,6 +6,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/rest/internal/header"
)
func TestParse(t *testing.T) {
@@ -16,7 +17,7 @@ func TestParse(t *testing.T) {
}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", "bar")
w.Header().Set(contentType, applicationJson)
w.Header().Set(header.ContentType, header.JsonContentType)
w.Write([]byte(`{"name":"kevin","value":100}`))
}))
defer svr.Close()
@@ -36,7 +37,7 @@ func TestParseHeaderError(t *testing.T) {
}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", "bar")
w.Header().Set(contentType, applicationJson)
w.Header().Set(header.ContentType, header.JsonContentType)
}))
defer svr.Close()
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
@@ -52,7 +53,7 @@ func TestParseNoBody(t *testing.T) {
}
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", "bar")
w.Header().Set(contentType, applicationJson)
w.Header().Set(header.ContentType, header.JsonContentType)
}))
defer svr.Close()
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)

View File

@@ -7,6 +7,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/rest/internal/header"
)
func TestNamedService_DoRequest(t *testing.T) {
@@ -43,7 +44,7 @@ func TestNamedService_DoRequestPost(t *testing.T) {
service := NewService("foo")
req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
assert.Nil(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set(header.ContentType, header.JsonContentType)
resp, err := service.DoRequest(req)
assert.Nil(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)

View File

@@ -3,14 +3,12 @@ package httpc
import "errors"
const (
pathKey = "path"
formKey = "form"
headerKey = "header"
jsonKey = "json"
slash = "/"
colon = ':'
contentType = "Content-Type"
applicationJson = "application/json; charset=utf-8"
pathKey = "path"
formKey = "form"
headerKey = "header"
jsonKey = "json"
slash = "/"
colon = ':'
)
// ErrGetWithBody indicates that GET request with body.