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

@@ -7,6 +7,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"
"github.com/zeromicro/go-zero/rest/pathvar"
)
@@ -114,5 +115,5 @@ func ParsePath(r *http.Request, v interface{}) error {
}
func withJsonBody(r *http.Request) 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

@@ -8,6 +8,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/rest/internal/header"
"github.com/zeromicro/go-zero/rest/pathvar"
)
@@ -204,7 +205,7 @@ func TestParseJsonBody(t *testing.T) {
body := `{"name":"kevin", "age": 18}`
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
r.Header.Set(ContentType, ApplicationJson)
r.Header.Set(ContentType, header.JsonContentType)
assert.Nil(t, Parse(r, &v))
assert.Equal(t, "kevin", v.Name)
@@ -267,7 +268,7 @@ func TestParseHeaders(t *testing.T) {
request.Header.Add("addrs", "addr2")
request.Header.Add("X-Forwarded-For", "10.0.10.11")
request.Header.Add("x-real-ip", "10.0.11.10")
request.Header.Add("Accept", "application/json")
request.Header.Add("Accept", header.JsonContentType)
err = ParseHeaders(request, &v)
if err != nil {
t.Fatal(err)
@@ -277,7 +278,7 @@ func TestParseHeaders(t *testing.T) {
assert.Equal(t, []string{"addr1", "addr2"}, v.Addrs)
assert.Equal(t, "10.0.10.11", v.XForwardedFor)
assert.Equal(t, "10.0.11.10", v.XRealIP)
assert.Equal(t, "application/json", v.Accept)
assert.Equal(t, header.JsonContentType, v.Accept)
}
func TestParseHeaders_Error(t *testing.T) {

View File

@@ -6,6 +6,7 @@ import (
"sync"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/internal/header"
)
var (
@@ -67,7 +68,7 @@ func WriteJson(w http.ResponseWriter, code int, v interface{}) {
return
}
w.Header().Set(ContentType, ApplicationJson)
w.Header().Set(ContentType, header.JsonContentType)
w.WriteHeader(code)
if n, err := w.Write(bs); err != nil {

View File

@@ -1,14 +1,16 @@
package httpx
import "github.com/zeromicro/go-zero/rest/internal/header"
const (
// ApplicationJson means application/json.
ApplicationJson = "application/json; charset=utf-8"
// ContentEncoding means Content-Encoding.
ContentEncoding = "Content-Encoding"
// ContentSecurity means X-Content-Security.
ContentSecurity = "X-Content-Security"
// ContentType means Content-Type.
ContentType = "Content-Type"
ContentType = header.ContentType
// JsonContentType means application/json.
JsonContentType = header.JsonContentType
// KeyField means key.
KeyField = "key"
// SecretField means secret.