-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcolly_test.go
176 lines (155 loc) · 4.74 KB
/
colly_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
170
171
172
173
174
175
176
package crawlab
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/crawlab-team/crawlab-go-sdk/constants"
"github.com/crawlab-team/crawlab-go-sdk/entity"
"github.com/gocolly/colly/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCollyOnHTMLOne(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
tests := []struct {
name string
html string
goqueryString string
expectedItem map[string]any
}{
{
name: "simple div extraction",
html: `<html><body><div class="item">Test Content</div></body></html>`,
goqueryString: "div.item",
expectedItem: map[string]any{"content": "Test Content"},
},
{
name: "div with attributes",
html: `<html><body><div class="item" data-id="123">Test Content</div></body></html>`,
goqueryString: "div.item",
expectedItem: map[string]any{"content": "Test Content", "id": "123"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(tt.html))
}))
defer ts.Close()
// Create collector
c := colly.NewCollector()
// Set up the HTML callback
CollyOnHTMLOne(c, tt.goqueryString, func(e *colly.HTMLElement) map[string]any {
if tt.name == "div with attributes" {
return map[string]any{
"content": e.Text,
"id": e.Attr("data-id"),
}
}
return map[string]any{
"content": e.Text,
}
})
// Capture stdout and visit the test server
output := captureOutput(func() {
err := c.Visit(ts.URL)
require.NoError(err)
})
// Verify output format
var msg entity.IPCMessage
err := json.Unmarshal([]byte(output), &msg)
require.NoError(err, "Should unmarshal JSON without error")
assert.Equal(constants.IPCMessageTypeData, msg.Type, "Message type should be 'data'")
assert.True(msg.IPC, "IPC flag should be true")
// Verify items
items, ok := msg.Payload.([]any)
assert.True(ok, "Payload should be an array")
require.Len(items, 1, "Should have exactly 1 item")
// Convert and compare item
actualItem, ok := items[0].(map[string]any)
assert.True(ok, "Item should be a map")
for k, v := range tt.expectedItem {
assert.Equal(v, actualItem[k], "Item %s should match", k)
}
})
}
}
func TestCollyOnHTMLMany(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
tests := []struct {
name string
html string
goqueryString string
expectedItems []map[string]any
}{
{
name: "single item extraction",
html: `<html><body><div class="item">Test Content</div></body></html>`,
goqueryString: "div.item",
expectedItems: []map[string]any{
{"content": "Test Content"},
},
},
{
name: "multiple items extraction",
html: `<html><body>
<div class="item">First Item</div>
<div class="item">Second Item</div>
</body></html>`,
goqueryString: "div.item",
expectedItems: []map[string]any{
{"content": "First Item"},
{"content": "Second Item"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create test server
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(tt.html))
}))
defer ts.Close()
// Create collector
c := colly.NewCollector()
// Set up the HTML callback
CollyOnHTMLMany(c, tt.goqueryString, func(e *colly.HTMLElement) []map[string]any {
return []map[string]any{
{"content": e.Text},
}
})
// Capture stdout and visit the test server
output := captureOutput(func() {
err := c.Visit(ts.URL)
require.NoError(err)
})
// Verify output format
outputLines := strings.Split(strings.TrimSpace(output), "\n")
assert.Len(outputLines, len(tt.expectedItems), "Should have expected number of output lines")
for i, line := range outputLines {
var msg entity.IPCMessage
err := json.Unmarshal([]byte(line), &msg)
require.NoError(err, "Should unmarshal JSON without error")
assert.Equal(constants.IPCMessageTypeData, msg.Type, "Message type should be 'data'")
assert.True(msg.IPC, "IPC flag should be true")
// Verify item
expectedItem := tt.expectedItems[i]
payloadItems, ok := msg.Payload.([]interface{})
require.True(ok)
actualItem, ok := payloadItems[0].(map[string]any)
require.True(ok)
assert.True(ok, "Payload should be a map")
for k, v := range expectedItem {
assert.Equal(v, actualItem[k], "Item %s should match", k)
}
}
})
}
}