-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper_test.go
177 lines (146 loc) · 4.03 KB
/
scraper_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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package scraper
import (
"fmt"
"log"
"os"
"testing"
)
func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func TestScrapeWithoutPromptAPIToken(t *testing.T) {
if os.Getenv("PROMPTAPI_TOKEN") != "" {
t.Skip("skipping test...")
}
s := new(PromptAPI)
params := new(Params)
result := new(Result)
extraHeaders := []*ExtraHeader{}
if err := s.Scrape(params, extraHeaders, result); err.Error() != "You need to set PROMPTAPI_TOKEN environment variable" {
t.Errorf("got: %v", err)
}
}
func TestScrapeBasicRequest(t *testing.T) {
if os.Getenv("PROMPTAPI_TOKEN") == "" {
t.Skip("skipping test...")
}
s := new(PromptAPI)
params := &Params{
URL: "https://pypi.org/classifiers/",
}
result := new(Result)
extraHeaders := []*ExtraHeader{}
if err := s.Scrape(params, extraHeaders, result); err != nil {
t.Errorf("got: %v", err)
}
if result.URL != params.URL {
t.Errorf("want: %s, got: %s", params.URL, result.URL)
}
if len(result.Data) < 300000 {
t.Errorf("fetced data length should be greater than 300000 bytes, got: %d", len(result.Data))
}
}
func TestScrapeBasicRequestWithSave(t *testing.T) {
if os.Getenv("PROMPTAPI_TOKEN") == "" {
t.Skip("skipping test...")
}
s := new(PromptAPI)
params := &Params{
URL: "https://pypi.org/classifiers/",
}
result := new(Result)
extraHeaders := []*ExtraHeader{}
if err := s.Scrape(params, extraHeaders, result); err != nil {
t.Errorf("got: %v", err)
}
if result.URL != params.URL {
t.Errorf("want: %s, got: %s", params.URL, result.URL)
}
if len(result.Data) < 300000 {
t.Errorf("fetced data length should be greater than 300000 bytes, got: %d", len(result.Data))
}
fileSize, err := s.Save("/tmp/test.html", result)
if err != nil {
t.Errorf("got: %v", err)
}
if fileSize <= 0 {
t.Errorf("saved file size should be greater than 0 bytes, got: %d", fileSize)
}
}
func TestScrapeComplexRequest(t *testing.T) {
if os.Getenv("PROMPTAPI_TOKEN") == "" {
t.Skip("skipping test...")
}
s := new(PromptAPI)
params := &Params{
URL: "https://pypi.org/classifiers/",
Country: "EE",
Selector: "ul li button[data-clipboard-text]",
}
result := new(Result)
extraHeaders := []*ExtraHeader{}
if err := s.Scrape(params, extraHeaders, result); err != nil {
t.Errorf("got: %v", err)
}
if result.URL != params.URL {
t.Errorf("want: %s, got: %s", params.URL, result.URL)
}
if len(result.DataSelector) < 700 {
t.Errorf("fetced array length should be greater than 700 items, got: %d", len(result.DataSelector))
}
}
func ExamplePromptAPI_Scrape() {
s := new(PromptAPI)
params := &Params{
URL: "https://pypi.org/classifiers/",
}
result := new(Result)
extraHeaders := []*ExtraHeader{}
err := s.Scrape(params, extraHeaders, result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Length of incoming data: %d\n", len(result.Data))
fmt.Printf("Response headers: %v\n", result.Headers)
fmt.Printf("Content-Length: %v\n", result.Headers["Content-Length"])
}
func ExamplePromptAPI_Scrape_with_extra_headers() {
s := new(PromptAPI)
params := &Params{
URL: "https://pypi.org/classifiers/",
}
result := new(Result)
extraHeaders := []*ExtraHeader{
&ExtraHeader{
Name: "X-Referer",
Value: "https://www.google.com",
},
}
err := s.Scrape(params, extraHeaders, result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Length of incoming data: %d\n", len(result.Data))
fmt.Printf("Response headers: %v\n", result.Headers)
fmt.Printf("Content-Length: %v\n", result.Headers["Content-Length"])
}
func ExamplePromptAPI_Save() {
s := new(PromptAPI)
params := &Params{
URL: "https://pypi.org/classifiers/",
}
result := new(Result)
extraHeaders := []*ExtraHeader{}
err := s.Scrape(params, extraHeaders, result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Length of incoming data: %d\n", len(result.Data))
fmt.Printf("Response headers: %v\n", result.Headers)
fmt.Printf("Content-Length: %v\n", result.Headers["Content-Length"])
fileSize, err := s.Save("/tmp/test.html", result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Size of /tmp/test.html -> %d bytes\n", fileSize)
}