-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_test.go
100 lines (93 loc) · 2.63 KB
/
ext_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gospider
import (
"bytes"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/zhshch2002/goreq"
"net/http"
"testing"
)
func TestWithDeduplicate(t *testing.T) {
s := NewSpider(WithDeduplicate())
got := false
s.SeedTask(goreq.Get("https://httpbin.org/get").AddParam("a", "a").AddCookie(&http.Cookie{
Name: "b",
Value: "b",
}).SetRawBody([]byte("c=c")).AddHeader("d", "d"), func(ctx *Context) {
got = true
})
s.SeedTask(goreq.Get("https://httpbin.org/get").AddParam("a", "a").AddCookie(&http.Cookie{
Name: "b",
Value: "b",
}).SetRawBody([]byte("c=c")).AddHeader("d", "d"), func(ctx *Context) {
t.Error("Deduplicate error")
})
s.Wait()
assert.True(t, got)
}
func TestWithRobotsTxt(t *testing.T) {
s := NewSpider(WithRobotsTxt("gospider"))
s.SeedTask(goreq.Get("https://github.com/gist/"), func(ctx *Context) {
t.Error("RobotsTxt error")
})
got := false
s.SeedTask( // unable to access according to https://github.com/robots.txt
goreq.Get("https://github.com/"),
func(ctx *Context) {
got = true
},
)
s.Wait()
assert.True(t, got)
}
func TestWithDepthLimit(t *testing.T) {
s := NewSpider(WithDepthLimit(2))
s.SeedTask(goreq.Get("https://httpbin.org/get"), func(ctx *Context) {
ctx.Println("Depth", ctx.Req.Context().Value("depth")) // 1
ctx.AddTask(goreq.Get("https://httpbin.org/get"), func(ctx *Context) {
ctx.Println("Depth", ctx.Req.Context().Value("depth")) // 2
ctx.AddTask(goreq.Get("https://httpbin.org/get"), func(ctx *Context) {
ctx.Println("Depth", ctx.Req.Context().Value("depth")) // 3
t.Error("Limiter error")
})
})
})
s.Wait()
}
func TestWithMaxReqLimit(t *testing.T) {
s := NewSpider(WithMaxReqLimit(2))
count := 0
s.SeedTask(goreq.Get("https://httpbin.org/get"), func(ctx *Context) {
count += 1
})
s.SeedTask(goreq.Get("https://httpbin.org/get"), func(ctx *Context) {
count += 1
})
s.SeedTask(goreq.Get("https://httpbin.org/get"), func(ctx *Context) {
count += 1
})
s.Wait()
assert.Equal(t, 2, count)
}
func TestWithErrorLog(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
s := NewSpider(WithErrorLog(buf))
s.SeedTask(goreq.Get("https://httpbin.org/get"), func(ctx *Context) {
ctx.AddItem(errors.New("test item error"))
panic("test panic error")
})
s.Wait()
fmt.Println(buf.String())
assert.True(t, buf.Len() > 0)
}
func TestWithCsvItemSaver(t *testing.T) {
buf := bytes.NewBuffer([]byte{})
s := NewSpider(WithCsvItemSaver(buf))
s.SeedTask(goreq.Get("https://httpbin.org/get"), func(ctx *Context) {
ctx.AddItem(CsvItem{ctx.Resp.Request.URL.String(), ctx.Resp.Status})
})
s.Wait()
fmt.Println(buf.String())
assert.True(t, buf.Len() > 0)
}