mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
util: create IsJSON/IsHTML type helper
This commit is contained in:
parent
565086cc2a
commit
8663704d6e
|
@ -40,3 +40,19 @@ func (r *Response) DecodeJSON(o interface{}) error {
|
|||
func (r *Response) IsError() bool {
|
||||
return r.StatusCode >= 400
|
||||
}
|
||||
|
||||
func (r *Response) IsJSON() bool {
|
||||
switch r.Header.Get("content-type") {
|
||||
case "text/json", "application/json", "application/json; charset=utf-8":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *Response) IsHTML() bool {
|
||||
switch r.Header.Get("content-type") {
|
||||
case "text/html":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -42,3 +42,33 @@ func TestResponse_IsError(t *testing.T) {
|
|||
assert.Equal(t, isErr, resp.IsError())
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponse_IsJSON(t *testing.T) {
|
||||
cases := map[string]bool{
|
||||
"text/json": true,
|
||||
"application/json": true,
|
||||
"application/json; charset=utf-8": true,
|
||||
"text/html": false,
|
||||
}
|
||||
for k, v := range cases {
|
||||
resp := &Response{Response: &http.Response{}}
|
||||
resp.Header = http.Header{}
|
||||
resp.Header.Set("content-type", k)
|
||||
assert.Equal(t, v, resp.IsJSON())
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponse_IsHTML(t *testing.T) {
|
||||
cases := map[string]bool{
|
||||
"text/json": false,
|
||||
"application/json": false,
|
||||
"application/json; charset=utf-8": false,
|
||||
"text/html": true,
|
||||
}
|
||||
for k, v := range cases {
|
||||
resp := &Response{Response: &http.Response{}}
|
||||
resp.Header = http.Header{}
|
||||
resp.Header.Set("content-type", k)
|
||||
assert.Equal(t, v, resp.IsHTML())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user