-
Notifications
You must be signed in to change notification settings - Fork 9
/
resp_test.go
48 lines (40 loc) · 1.05 KB
/
resp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package req
import (
"github.com/stretchr/testify/assert"
"testing"
"unicode/utf8"
)
var jsonResp *Response
var htmlResp *Response
func init() {
jsonResp = Get("https://httpbin.org/get").Do()
if jsonResp.Err != nil {
panic(jsonResp.Err)
}
htmlResp = Get("https://httpbin.org/").Do()
if htmlResp.Err != nil {
panic(htmlResp.Err)
}
}
func TestResponse_DecodeAndParse(t *testing.T) {
resp := Get("http://stock.10jqka.com.cn/zhuanti/hlw_list/").Do()
assert.False(t, utf8.Valid(resp.NoDecodeBody))
assert.True(t, utf8.Valid(resp.Body))
}
func TestResponse_HTML(t *testing.T) {
assert.True(t, htmlResp.IsHTML())
h, _ := htmlResp.HTML()
t.Log(h.Find("title").Text())
assert.NotEqual(t, 0, h.Find("title").Length())
}
func TestResponse_JSON(t *testing.T) {
assert.True(t, jsonResp.IsJSON())
t.Log(jsonResp.Text)
j, _ := jsonResp.JSON()
assert.Equal(t, "https://httpbin.org/get", j.Get("url").String())
var data struct {
Url string `json:"url"`
}
assert.Nil(t, jsonResp.BindJSON(&data))
assert.Equal(t, "https://httpbin.org/get", data.Url)
}