-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest_test.go
61 lines (48 loc) · 1.63 KB
/
request_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
49
50
51
52
53
54
55
56
57
58
59
60
61
package tegenaria
import (
"errors"
"testing"
"github.com/agiledragon/gomonkey/v2"
jsoniter "github.com/json-iterator/go"
"github.com/smartystreets/goconvey/convey"
)
func TestRequestWithBodyError(t *testing.T) {
convey.Convey("request body read error,should be panic", t, func() {
body := map[string]interface{}{}
body["key"] = "value"
request := NewRequest("http://www.example.com", GET, testParser)
opt := RequestWithRequestBody(body)
patch := gomonkey.ApplyFunc(jsoniter.Marshal, func(_ interface{}) ([]byte, error) {
return nil, errors.New("marshal error")
})
defer patch.Reset()
f := func() {
opt(request)
}
convey.So(f, convey.ShouldPanic)
})
}
func TestRequestToMap(t *testing.T) {
convey.Convey("request to map", t, func() {
body := map[string]interface{}{}
body["key"] = "value"
bytesBody := `{
"key":"value"
}`
request := NewRequest("http://www.example.com", GET, testParser)
r, err := request.ToMap()
convey.So(err, convey.ShouldBeNil)
convey.So(r["url"], convey.ShouldContainSubstring, "http://www.example.com")
newReq := RequestFromMap(r, RequestWithRequestBytesBody([]byte(bytesBody)))
convey.So(newReq.Url, convey.ShouldContainSubstring, "http://www.example.com")
})
}
func TestRequestWithParser(t *testing.T) {
convey.Convey("RequestWithParser", t, func() {
testSpider = &TestSpider{
NewBaseSpider("tWithParserSpider", []string{"https://www.example.com"}),
}
request := NewRequest("http://www.example.com", GET, testSpider.Parser, RequestWithParser(testSpider.Parser))
convey.So(request.Parser, convey.ShouldContainSubstring, GetFunctionName(testSpider.Parser))
})
}