mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
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:
parent
cef83efd4e
commit
3bbc90ec24
@ -39,7 +39,7 @@ func (rw *RemoteWriter) Write(report *StatReport) error {
|
|||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: httpTimeout,
|
Timeout: httpTimeout,
|
||||||
}
|
}
|
||||||
resp, err := client.Post(rw.endpoint, jsonContentType, bytes.NewBuffer(bs))
|
resp, err := client.Post(rw.endpoint, jsonContentType, bytes.NewReader(bs))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/core/lang"
|
"github.com/zeromicro/go-zero/core/lang"
|
||||||
"github.com/zeromicro/go-zero/core/mapping"
|
"github.com/zeromicro/go-zero/core/mapping"
|
||||||
"github.com/zeromicro/go-zero/rest/httpc/internal"
|
"github.com/zeromicro/go-zero/rest/httpc/internal"
|
||||||
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
)
|
)
|
||||||
|
|
||||||
var interceptors = []internal.Interceptor{
|
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])
|
req.URL.RawQuery = buildFormQuery(u, val[formKey])
|
||||||
fillHeader(req, val[headerKey])
|
fillHeader(req, val[headerKey])
|
||||||
if hasJsonBody {
|
if hasJsonBody {
|
||||||
req.Header.Set(contentType, applicationJson)
|
req.Header.Set(header.ContentType, header.JsonContentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
return req, nil
|
return req, nil
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/zeromicro/go-zero/rest/httpx"
|
"github.com/zeromicro/go-zero/rest/httpx"
|
||||||
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
"github.com/zeromicro/go-zero/rest/router"
|
"github.com/zeromicro/go-zero/rest/router"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ func TestDoRequest_NotFound(t *testing.T) {
|
|||||||
defer svr.Close()
|
defer svr.Close()
|
||||||
req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
|
req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set(header.ContentType, header.JsonContentType)
|
||||||
resp, err := DoRequest(req)
|
resp, err := DoRequest(req)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/mapping"
|
"github.com/zeromicro/go-zero/core/mapping"
|
||||||
"github.com/zeromicro/go-zero/rest/internal/encoding"
|
"github.com/zeromicro/go-zero/rest/internal/encoding"
|
||||||
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse parses the response.
|
// Parse parses the response.
|
||||||
@ -32,5 +33,5 @@ func ParseJsonBody(resp *http.Response, val interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func withJsonBody(r *http.Response) bool {
|
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)
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
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) {
|
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("foo", "bar")
|
w.Header().Set("foo", "bar")
|
||||||
w.Header().Set(contentType, applicationJson)
|
w.Header().Set(header.ContentType, header.JsonContentType)
|
||||||
w.Write([]byte(`{"name":"kevin","value":100}`))
|
w.Write([]byte(`{"name":"kevin","value":100}`))
|
||||||
}))
|
}))
|
||||||
defer svr.Close()
|
defer svr.Close()
|
||||||
@ -36,7 +37,7 @@ func TestParseHeaderError(t *testing.T) {
|
|||||||
}
|
}
|
||||||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("foo", "bar")
|
w.Header().Set("foo", "bar")
|
||||||
w.Header().Set(contentType, applicationJson)
|
w.Header().Set(header.ContentType, header.JsonContentType)
|
||||||
}))
|
}))
|
||||||
defer svr.Close()
|
defer svr.Close()
|
||||||
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
|
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) {
|
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("foo", "bar")
|
w.Header().Set("foo", "bar")
|
||||||
w.Header().Set(contentType, applicationJson)
|
w.Header().Set(header.ContentType, header.JsonContentType)
|
||||||
}))
|
}))
|
||||||
defer svr.Close()
|
defer svr.Close()
|
||||||
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
|
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNamedService_DoRequest(t *testing.T) {
|
func TestNamedService_DoRequest(t *testing.T) {
|
||||||
@ -43,7 +44,7 @@ func TestNamedService_DoRequestPost(t *testing.T) {
|
|||||||
service := NewService("foo")
|
service := NewService("foo")
|
||||||
req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
|
req, err := http.NewRequest(http.MethodPost, svr.URL, nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set(header.ContentType, header.JsonContentType)
|
||||||
resp, err := service.DoRequest(req)
|
resp, err := service.DoRequest(req)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
|
||||||
|
@ -3,14 +3,12 @@ package httpc
|
|||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
pathKey = "path"
|
pathKey = "path"
|
||||||
formKey = "form"
|
formKey = "form"
|
||||||
headerKey = "header"
|
headerKey = "header"
|
||||||
jsonKey = "json"
|
jsonKey = "json"
|
||||||
slash = "/"
|
slash = "/"
|
||||||
colon = ':'
|
colon = ':'
|
||||||
contentType = "Content-Type"
|
|
||||||
applicationJson = "application/json; charset=utf-8"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrGetWithBody indicates that GET request with body.
|
// ErrGetWithBody indicates that GET request with body.
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/mapping"
|
"github.com/zeromicro/go-zero/core/mapping"
|
||||||
"github.com/zeromicro/go-zero/rest/internal/encoding"
|
"github.com/zeromicro/go-zero/rest/internal/encoding"
|
||||||
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
"github.com/zeromicro/go-zero/rest/pathvar"
|
"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 {
|
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)
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
"github.com/zeromicro/go-zero/rest/pathvar"
|
"github.com/zeromicro/go-zero/rest/pathvar"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -204,7 +205,7 @@ func TestParseJsonBody(t *testing.T) {
|
|||||||
|
|
||||||
body := `{"name":"kevin", "age": 18}`
|
body := `{"name":"kevin", "age": 18}`
|
||||||
r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
|
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.Nil(t, Parse(r, &v))
|
||||||
assert.Equal(t, "kevin", v.Name)
|
assert.Equal(t, "kevin", v.Name)
|
||||||
@ -267,7 +268,7 @@ func TestParseHeaders(t *testing.T) {
|
|||||||
request.Header.Add("addrs", "addr2")
|
request.Header.Add("addrs", "addr2")
|
||||||
request.Header.Add("X-Forwarded-For", "10.0.10.11")
|
request.Header.Add("X-Forwarded-For", "10.0.10.11")
|
||||||
request.Header.Add("x-real-ip", "10.0.11.10")
|
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)
|
err = ParseHeaders(request, &v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -277,7 +278,7 @@ func TestParseHeaders(t *testing.T) {
|
|||||||
assert.Equal(t, []string{"addr1", "addr2"}, v.Addrs)
|
assert.Equal(t, []string{"addr1", "addr2"}, v.Addrs)
|
||||||
assert.Equal(t, "10.0.10.11", v.XForwardedFor)
|
assert.Equal(t, "10.0.10.11", v.XForwardedFor)
|
||||||
assert.Equal(t, "10.0.11.10", v.XRealIP)
|
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) {
|
func TestParseHeaders_Error(t *testing.T) {
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/logx"
|
"github.com/zeromicro/go-zero/core/logx"
|
||||||
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -67,7 +68,7 @@ func WriteJson(w http.ResponseWriter, code int, v interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set(ContentType, ApplicationJson)
|
w.Header().Set(ContentType, header.JsonContentType)
|
||||||
w.WriteHeader(code)
|
w.WriteHeader(code)
|
||||||
|
|
||||||
if n, err := w.Write(bs); err != nil {
|
if n, err := w.Write(bs); err != nil {
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
package httpx
|
package httpx
|
||||||
|
|
||||||
|
import "github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// ApplicationJson means application/json.
|
|
||||||
ApplicationJson = "application/json; charset=utf-8"
|
|
||||||
// ContentEncoding means Content-Encoding.
|
// ContentEncoding means Content-Encoding.
|
||||||
ContentEncoding = "Content-Encoding"
|
ContentEncoding = "Content-Encoding"
|
||||||
// ContentSecurity means X-Content-Security.
|
// ContentSecurity means X-Content-Security.
|
||||||
ContentSecurity = "X-Content-Security"
|
ContentSecurity = "X-Content-Security"
|
||||||
// ContentType means Content-Type.
|
// ContentType means Content-Type.
|
||||||
ContentType = "Content-Type"
|
ContentType = header.ContentType
|
||||||
|
// JsonContentType means application/json.
|
||||||
|
JsonContentType = header.JsonContentType
|
||||||
// KeyField means key.
|
// KeyField means key.
|
||||||
KeyField = "key"
|
KeyField = "key"
|
||||||
// SecretField means secret.
|
// SecretField means secret.
|
||||||
|
10
rest/internal/header/headers.go
Normal file
10
rest/internal/header/headers.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package header
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ApplicationJson stands for application/json.
|
||||||
|
ApplicationJson = "application/json"
|
||||||
|
// ContentType is the header key for Content-Type.
|
||||||
|
ContentType = "Content-Type"
|
||||||
|
// JsonContentType is the content type for JSON.
|
||||||
|
JsonContentType = "application/json; charset=utf-8"
|
||||||
|
)
|
@ -11,13 +11,11 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/zeromicro/go-zero/rest/httpx"
|
"github.com/zeromicro/go-zero/rest/httpx"
|
||||||
|
"github.com/zeromicro/go-zero/rest/internal/header"
|
||||||
"github.com/zeromicro/go-zero/rest/pathvar"
|
"github.com/zeromicro/go-zero/rest/pathvar"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const contentLength = "Content-Length"
|
||||||
applicationJsonWithUtf8 = "application/json; charset=utf-8"
|
|
||||||
contentLength = "Content-Length"
|
|
||||||
)
|
|
||||||
|
|
||||||
type mockedResponseWriter struct {
|
type mockedResponseWriter struct {
|
||||||
code int
|
code int
|
||||||
@ -167,7 +165,7 @@ func TestParseJsonPost(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
||||||
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
|
r.Header.Set(httpx.ContentType, httpx.JsonContentType)
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
|
||||||
@ -199,7 +197,7 @@ func TestParseJsonPostWithIntSlice(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
|
||||||
bytes.NewBufferString(`{"ages": [1, 2], "years": [3, 4]}`))
|
bytes.NewBufferString(`{"ages": [1, 2], "years": [3, 4]}`))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
|
r.Header.Set(httpx.ContentType, httpx.JsonContentType)
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(
|
||||||
@ -227,7 +225,7 @@ func TestParseJsonPostError(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
||||||
bytes.NewBufferString(payload))
|
bytes.NewBufferString(payload))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
|
r.Header.Set(httpx.ContentType, httpx.JsonContentType)
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
||||||
@ -255,7 +253,7 @@ func TestParseJsonPostInvalidRequest(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/",
|
||||||
bytes.NewBufferString(payload))
|
bytes.NewBufferString(payload))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
|
r.Header.Set(httpx.ContentType, httpx.JsonContentType)
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
err = router.Handle(http.MethodPost, "/", http.HandlerFunc(
|
err = router.Handle(http.MethodPost, "/", http.HandlerFunc(
|
||||||
@ -277,7 +275,7 @@ func TestParseJsonPostRequired(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
|
||||||
bytes.NewBufferString(`{"location": "shanghai"`))
|
bytes.NewBufferString(`{"location": "shanghai"`))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
|
r.Header.Set(httpx.ContentType, httpx.JsonContentType)
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
||||||
@ -455,7 +453,7 @@ func TestParsePtrInRequest(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
|
||||||
bytes.NewBufferString(`{"audio": {"volume": 100}}`))
|
bytes.NewBufferString(`{"audio": {"volume": 100}}`))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
|
r.Header.Set(httpx.ContentType, httpx.JsonContentType)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Request struct {
|
Request struct {
|
||||||
@ -603,7 +601,7 @@ func TestParseWrappedRequest(t *testing.T) {
|
|||||||
func TestParseWrappedGetRequestWithJsonHeader(t *testing.T) {
|
func TestParseWrappedGetRequestWithJsonHeader(t *testing.T) {
|
||||||
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil)
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017", nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
|
r.Header.Set(httpx.ContentType, header.JsonContentType)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Request struct {
|
Request struct {
|
||||||
@ -636,7 +634,7 @@ func TestParseWrappedGetRequestWithJsonHeader(t *testing.T) {
|
|||||||
func TestParseWrappedHeadRequestWithJsonHeader(t *testing.T) {
|
func TestParseWrappedHeadRequestWithJsonHeader(t *testing.T) {
|
||||||
r, err := http.NewRequest(http.MethodHead, "http://hello.com/kevin/2017", nil)
|
r, err := http.NewRequest(http.MethodHead, "http://hello.com/kevin/2017", nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
|
r.Header.Set(httpx.ContentType, header.JsonContentType)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Request struct {
|
Request struct {
|
||||||
@ -702,7 +700,7 @@ func TestParseWithAll(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
||||||
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
|
r.Header.Set(httpx.ContentType, httpx.JsonContentType)
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -733,7 +731,7 @@ func TestParseWithAllUtf8(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
||||||
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
bytes.NewBufferString(`{"location": "shanghai", "time": 20170912}`))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
|
r.Header.Set(httpx.ContentType, header.JsonContentType)
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
||||||
@ -923,7 +921,7 @@ func TestParseWithMissingAllPaths(t *testing.T) {
|
|||||||
func TestParseGetWithContentLengthHeader(t *testing.T) {
|
func TestParseGetWithContentLengthHeader(t *testing.T) {
|
||||||
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil)
|
r, err := http.NewRequest(http.MethodGet, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000", nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, httpx.ApplicationJson)
|
r.Header.Set(httpx.ContentType, header.JsonContentType)
|
||||||
r.Header.Set(contentLength, "1024")
|
r.Header.Set(contentLength, "1024")
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
@ -951,7 +949,7 @@ func TestParseJsonPostWithTypeMismatch(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017?nickname=whatever&zipcode=200000",
|
||||||
bytes.NewBufferString(`{"time": "20170912"}`))
|
bytes.NewBufferString(`{"time": "20170912"}`))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
|
r.Header.Set(httpx.ContentType, header.JsonContentType)
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
||||||
@ -977,7 +975,7 @@ func TestParseJsonPostWithInt2String(t *testing.T) {
|
|||||||
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
|
r, err := http.NewRequest(http.MethodPost, "http://hello.com/kevin/2017",
|
||||||
bytes.NewBufferString(`{"time": 20170912}`))
|
bytes.NewBufferString(`{"time": 20170912}`))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
r.Header.Set(httpx.ContentType, applicationJsonWithUtf8)
|
r.Header.Set(httpx.ContentType, header.JsonContentType)
|
||||||
|
|
||||||
router := NewRouter()
|
router := NewRouter()
|
||||||
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
err = router.Handle(http.MethodPost, "/:name/:year", http.HandlerFunc(
|
||||||
|
@ -149,7 +149,7 @@ Future _apiRequest(String method, String path, dynamic data,
|
|||||||
r = await client.getUrl(Uri.parse('https://' + serverHost + path));
|
r = await client.getUrl(Uri.parse('https://' + serverHost + path));
|
||||||
}
|
}
|
||||||
|
|
||||||
r.headers.set('Content-Type', 'application/json');
|
r.headers.set('Content-Type', 'application/json; charset=utf-8');
|
||||||
if (tokens != null) {
|
if (tokens != null) {
|
||||||
r.headers.set('Authorization', tokens.accessToken);
|
r.headers.set('Authorization', tokens.accessToken);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user