-
Notifications
You must be signed in to change notification settings - Fork 5
/
all_test.go
57 lines (49 loc) · 1.09 KB
/
all_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
package crawler
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type testController struct {
NopController
content chan []byte
}
func (t testController) Handle(r *Response, _ chan<- *url.URL) {
b, _ := ioutil.ReadAll(r.Body)
t.content <- b
}
func TestAll(t *testing.T) {
assert := assert.New(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprintln(w, `
<html>
<head></head>
<body>
<div class="foo">bar</div>
<div id="hello" key="value">Hello, world!</div>
</body>
</html>`)
}))
defer ts.Close()
ctrl := &testController{
content: make(chan []byte, 2),
}
cw := New(&Config{Controller: ctrl})
assert.Nil(cw.Crawl(ts.URL))
cw.Wait()
content := string(<-ctrl.content)
assert.Contains(content, "bar")
assert.Contains(content, "Hello, world!")
u, err := url.Parse(ts.URL)
assert.Nil(err)
uu, err := cw.store.Get(u)
assert.NoError(err)
assert.Equal(1, uu.NumVisit)
assert.True(uu.Last.After(time.Time{}))
}