-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker_test.go
116 lines (95 loc) · 2.7 KB
/
worker_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
package crawler
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/require"
)
//go:generate goautomock Queue
func TestWorkerStopsOnContextCancelation(t *testing.T) {
r := require.New(t)
s := httptest.NewServer(http.FileServer(http.Dir("testdata")))
defer s.Close()
ctx, cancel := context.WithCancel(context.Background())
q := NewQueueMock()
q.PopFrontFunc = func() (*Request, error) {
return NewRequest(s.URL)
}
q.PushBackFunc = func(*Request) error { return nil }
w, err := NewWorker(func(url string, res *Response, err error) error {
if q.PopFrontTotalCalls() == 2 {
cancel()
}
return err
})
r.NoError(err)
err = w.Run(ctx, q)
r.Equal(context.Canceled, err)
r.Equal(2, q.PopFrontTotalCalls())
}
func TestWorkerCancelsHTTPRequest(t *testing.T) {
r := require.New(t)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
select {
case <-ctx.Done():
r.Equal(context.Canceled, ctx.Err())
case <-time.After(time.Second):
http.Error(w, "should have cancelled", http.StatusInternalServerError)
}
}))
defer s.Close()
ctx, cancel := context.WithCancel(context.Background())
q := NewQueueMock()
q.PopFrontFunc = func() (*Request, error) {
cancel()
return NewRequest(s.URL)
}
q.PushBackFunc = func(*Request) error { return nil }
w, err := NewWorker(func(url string, res *Response, err error) error {
return err
})
r.NoError(err)
err = w.Run(ctx, q)
r.Equal(context.Canceled, err)
r.Equal(1, q.PopFrontTotalCalls())
}
func TestFetch(t *testing.T) {
sf := http.FileServer(http.Dir("testdata"))
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Log(r.URL.Path)
if r.URL.Path == "/redirect" {
t.Log("redirect")
http.Redirect(w, r, "/ok", http.StatusFound)
} else {
sf.ServeHTTP(w, r)
}
}))
defer s.Close()
c := &http.Client{
CheckRedirect: skipRedirects,
}
tests := []struct {
path string
expectedURL string
expectedRedirect string
}{
{path: "", expectedURL: s.URL + "/"},
{path: "/", expectedURL: s.URL + "/"},
{path: "/index.html", expectedURL: s.URL + "/index.html", expectedRedirect: s.URL + "/"},
{path: "/#with-fragment", expectedURL: s.URL + "/"},
{path: "/redirect", expectedURL: s.URL + "/redirect", expectedRedirect: s.URL + "/ok"},
}
for _, test := range tests {
t.Run(test.path, func(t *testing.T) {
req, err := NewRequest(s.URL + test.path)
require.NoError(t, err)
res, err := fetch(context.Background(), c, req)
require.NoError(t, err)
require.Equal(t, test.expectedURL, res.URL)
require.Equal(t, test.expectedRedirect, res.RedirectTo)
})
}
}