-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscraperapi_test.go
69 lines (55 loc) · 1.56 KB
/
scraperapi_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
package scraperapi
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
func TestClient_Post(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("ok"))
h1Expected := "application/x-www-form-urlencoded"
h1Actual := r.Header.Get("Content-Type")
h2Expected := "special value"
h2Actual := r.Header.Get("X-My-Header")
apiKeyExpected := "very-secret-api-key"
apiKeyActual := r.URL.Query().Get("api_key")
sdkExpected := "go"
sdkActual := r.URL.Query().Get("scraper_sdk")
r.ParseForm()
fExpected := "bar"
fActual := r.Form["foo"][0]
if h1Expected != h1Actual {
t.Errorf("expected Content-Type %s, got %s", h1Expected, h1Actual)
}
if h2Expected != h2Actual {
t.Errorf("expected X-My-Header %s, got %s", h2Expected, h2Actual)
}
if apiKeyExpected != apiKeyActual {
t.Errorf("expected api_key %s, got %s", apiKeyExpected, apiKeyActual)
}
if sdkExpected != sdkActual {
t.Errorf("expected scraper_sdk %s, got %s", sdkExpected, sdkActual)
}
if fExpected != fActual {
t.Errorf("expected form value %s, got %s", fExpected, fActual)
}
}))
defer srv.Close()
c := New("very-secret-api-key")
c.HTTPClient = srv.Client()
c.BaseURL = srv.URL
form := url.Values{}
form.Add("foo", "bar")
_, err := c.Post(
"http://httpbin.org/anything",
strings.NewReader(form.Encode()),
WithHeader("Content-Type", "application/x-www-form-urlencoded"),
WithHeader("X-My-Header", "special value"),
)
if err != nil {
t.Error(err)
}
}