feat: add httpc.Parse (#1698)
parent
0aeb49a6b0
commit
c1d9e6a00b
@ -0,0 +1,33 @@
|
||||
package httpc
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/mapping"
|
||||
"github.com/zeromicro/go-zero/rest/internal/encoding"
|
||||
)
|
||||
|
||||
func Parse(resp *http.Response, val interface{}) error {
|
||||
if err := ParseHeaders(resp, val); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return ParseJsonBody(resp, val)
|
||||
}
|
||||
|
||||
func ParseHeaders(resp *http.Response, val interface{}) error {
|
||||
return encoding.ParseHeaders(resp.Header, val)
|
||||
}
|
||||
|
||||
func ParseJsonBody(resp *http.Response, val interface{}) error {
|
||||
if withJsonBody(resp) {
|
||||
return mapping.UnmarshalJsonReader(resp.Body, val)
|
||||
}
|
||||
|
||||
return mapping.UnmarshalJsonMap(nil, val)
|
||||
}
|
||||
|
||||
func withJsonBody(r *http.Response) bool {
|
||||
return r.ContentLength > 0 && strings.Contains(r.Header.Get(contentType), applicationJson)
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package httpc
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
var val struct {
|
||||
Foo string `header:"foo"`
|
||||
Name string `json:"name"`
|
||||
Value int `json:"value"`
|
||||
}
|
||||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("foo", "bar")
|
||||
w.Header().Set(contentType, applicationJson)
|
||||
w.Write([]byte(`{"name":"kevin","value":100}`))
|
||||
}))
|
||||
defer svr.Close()
|
||||
resp, err := Get("foo", svr.URL)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, Parse(resp, &val))
|
||||
assert.Equal(t, "bar", val.Foo)
|
||||
assert.Equal(t, "kevin", val.Name)
|
||||
assert.Equal(t, 100, val.Value)
|
||||
}
|
||||
|
||||
func TestParseHeaderError(t *testing.T) {
|
||||
var val struct {
|
||||
Foo int `header:"foo"`
|
||||
}
|
||||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("foo", "bar")
|
||||
w.Header().Set(contentType, applicationJson)
|
||||
}))
|
||||
defer svr.Close()
|
||||
resp, err := Get("foo", svr.URL)
|
||||
assert.Nil(t, err)
|
||||
assert.NotNil(t, Parse(resp, &val))
|
||||
}
|
||||
|
||||
func TestParseNoBody(t *testing.T) {
|
||||
var val struct {
|
||||
Foo string `header:"foo"`
|
||||
}
|
||||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("foo", "bar")
|
||||
w.Header().Set(contentType, applicationJson)
|
||||
}))
|
||||
defer svr.Close()
|
||||
resp, err := Get("foo", svr.URL)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, Parse(resp, &val))
|
||||
assert.Equal(t, "bar", val.Foo)
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package httpc
|
||||
|
||||
const (
|
||||
contentType = "Content-Type"
|
||||
applicationJson = "application/json"
|
||||
)
|
@ -0,0 +1,27 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/textproto"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/mapping"
|
||||
)
|
||||
|
||||
const headerKey = "header"
|
||||
|
||||
var headerUnmarshaler = mapping.NewUnmarshaler(headerKey, mapping.WithStringValues(),
|
||||
mapping.WithCanonicalKeyFunc(textproto.CanonicalMIMEHeaderKey))
|
||||
|
||||
// ParseHeaders parses the headers request.
|
||||
func ParseHeaders(header http.Header, v interface{}) error {
|
||||
m := map[string]interface{}{}
|
||||
for k, v := range header {
|
||||
if len(v) == 1 {
|
||||
m[k] = v[0]
|
||||
} else {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return headerUnmarshaler.Unmarshal(m, v)
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package encoding
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseHeaders(t *testing.T) {
|
||||
var val struct {
|
||||
Foo string `header:"foo"`
|
||||
Baz int `header:"baz"`
|
||||
Qux bool `header:"qux,default=true"`
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "/any", nil)
|
||||
r.Header.Set("foo", "bar")
|
||||
r.Header.Set("baz", "1")
|
||||
assert.Nil(t, ParseHeaders(r.Header, &val))
|
||||
assert.Equal(t, "bar", val.Foo)
|
||||
assert.Equal(t, 1, val.Baz)
|
||||
assert.True(t, val.Qux)
|
||||
}
|
||||
|
||||
func TestParseHeadersMulti(t *testing.T) {
|
||||
var val struct {
|
||||
Foo []string `header:"foo"`
|
||||
Baz int `header:"baz"`
|
||||
Qux bool `header:"qux,default=true"`
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "/any", nil)
|
||||
r.Header.Set("foo", "bar")
|
||||
r.Header.Add("foo", "bar1")
|
||||
r.Header.Set("baz", "1")
|
||||
assert.Nil(t, ParseHeaders(r.Header, &val))
|
||||
assert.Equal(t, []string{"bar", "bar1"}, val.Foo)
|
||||
assert.Equal(t, 1, val.Baz)
|
||||
assert.True(t, val.Qux)
|
||||
}
|
Loading…
Reference in New Issue