forked from philippta/flyscrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_test.go
55 lines (42 loc) · 1.25 KB
/
fetch_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
package flyscrape_test
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/philippta/flyscrape"
"github.com/stretchr/testify/require"
)
func TestFetchFetch(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("foobar"))
}))
fetch := flyscrape.Fetch()
html, err := fetch(srv.URL)
require.NoError(t, err)
require.Equal(t, html, "foobar")
}
func TestFetchCachedFetch(t *testing.T) {
numcalled := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
numcalled++
w.Write([]byte("foobar"))
}))
fetch := flyscrape.CachedFetch(flyscrape.Fetch())
html, err := fetch(srv.URL)
require.NoError(t, err)
require.Equal(t, html, "foobar")
html, err = fetch(srv.URL)
require.NoError(t, err)
require.Equal(t, html, "foobar")
require.Equal(t, 1, numcalled)
}
func TestFetchProxiedFetch(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, r.URL.String(), "http://example.com/foo")
w.Write([]byte("foobar"))
}))
fetch := flyscrape.ProxiedFetch(srv.URL)
html, err := fetch("http://example.com/foo")
require.NoError(t, err)
require.Equal(t, html, "foobar")
}