This repository has been archived by the owner on Feb 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
169 lines (151 loc) · 3.8 KB
/
client_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
package scrapeninja
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestNew(t *testing.T) {
apiKey := "test-api-key"
timeout := time.Second * Timeout
client := New(apiKey)
if client.apiKey != apiKey {
t.Errorf("got: %s, wanted: %s", client.apiKey, apiKey)
}
if client.httpClient.Timeout != timeout {
t.Errorf("got: %q, wanted: %q", client.httpClient.Timeout, timeout)
}
}
func TestScrapeRequest_MarshalJSON(t *testing.T) {
tests := map[string]struct {
url string
proxy string
headers map[string]string
method string
want string
}{
"url is empty": {
"",
"test-proxy",
nil,
"GET",
"{\"url\":\"\",\"method\":\"GET\",\"geo\":\"test-proxy\",\"headers\":[]}",
},
"proxy is empty": {
"https://test",
"",
nil,
"GET",
"{\"url\":\"https://test\",\"method\":\"GET\",\"headers\":[]}",
},
"method is empty": {
"https://test",
"test-proxy",
nil,
"",
"{\"url\":\"https://test\",\"method\":\"\",\"geo\":\"test-proxy\",\"headers\":[]}",
},
"headers is nil": {
"https://test",
"test-proxy",
nil,
"GET",
"{\"url\":\"https://test\",\"method\":\"GET\",\"geo\":\"test-proxy\",\"headers\":[]}",
},
"headers has one element": {
"https://test",
"test-proxy",
map[string]string{"test-header": "test-value"},
"GET",
"{\"url\":\"https://test\",\"method\":\"GET\",\"geo\":\"test-proxy\",\"headers\":[\"test-header: test-value\"]}",
},
"headers has multiple elements": {
"https://test",
"test-proxy",
map[string]string{
"test-header": "test-value",
"test-header2": "test-value2",
"test-header3": "test-value3",
},
"GET",
"{\"url\":\"https://test\",\"method\":\"GET\",\"geo\":\"test-proxy\",\"headers\":[\"test-header: test-value\",\"test-header2: test-value2\",\"test-header3: test-value3\"]}",
},
}
for name, data := range tests {
t.Run(name, func(t *testing.T) {
request := ScrapeRequest{
Url: data.url,
Proxy: data.proxy,
Headers: data.headers,
Method: data.method,
}
got, err := request.MarshalJSON()
if err != nil {
t.Fatal(err)
}
if string(got) != data.want {
t.Errorf("got: %s, wanted: %s", string(got), data.want)
}
})
}
}
func TestClient_Scrape(t *testing.T) {
apiKey := "test-api-key"
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := json.NewEncoder(w).Encode(&ScrapeResponse{
Info: struct {
Version string `json:"version"`
StatusCode int `json:"statusCode"`
StatusMessage string `json:"statusMessage"`
Headers map[string]string `json:"headers"`
}{
"test-version",
200,
"test-message",
nil,
},
Body: "test-body",
})
if err != nil {
t.Fatal(err)
}
t.Run("path is correct", func(t *testing.T) {
got := r.URL.Path
if got != "/scrape" {
t.Errorf("got: %s, wanted: %s", got, "scrape")
}
})
t.Run("X-RapidAPI-Key header is set correctly", func(t *testing.T) {
got := r.Header.Get("X-RapidAPI-Key")
if got != apiKey {
t.Errorf("got: %s, wanted: %s", got, apiKey)
}
})
t.Run("X-RapidAPI-Host header is set correctly", func(t *testing.T) {
got := r.Header.Get("X-RapidAPI-Host")
if got != RapidAPIHost {
t.Errorf("got: %s, wanted: %s", got, RapidAPIHost)
}
})
t.Run("Content-Type header is set correctly", func(t *testing.T) {
got := r.Header.Get("Content-Type")
if got != ContentType {
t.Errorf("got: %s, wanted: %s", got, ContentType)
}
})
}))
defer srv.Close()
// Overwrite base url with test server url
BaseUrl = srv.URL
client := New(apiKey)
response, _ := client.Scrape(&ScrapeRequest{
Url: "",
Proxy: "",
Headers: nil,
Method: "",
})
if response.Body != "test-body" {
t.Errorf("Oh no!")
}
}