1
1
package transformers
2
2
3
3
import (
4
+ "io/ioutil"
4
5
"net/http"
6
+ "net/http/httptest"
7
+ "strings"
5
8
"testing"
9
+ "testing/iotest"
6
10
7
11
"github.com/stretchr/testify/assert"
8
12
"github.com/stretchr/testify/mock"
13
+ "github.com/thewizardplusplus/go-crawler/extractors"
9
14
)
10
15
11
16
func TestTransformerGroup_TransformLinks (test * testing.T ) {
@@ -22,7 +27,162 @@ func TestTransformerGroup_TransformLinks(test *testing.T) {
22
27
wantLinks []string
23
28
wantErr assert.ErrorAssertionFunc
24
29
}{
25
- // TODO: Add test cases.
30
+ {
31
+ name : "success without transformers" ,
32
+ transformers : nil ,
33
+ args : args {
34
+ links : []string {"http://example.com/1" , "http://example.com/2" },
35
+ response : & http.Response {
36
+ Body : ioutil .NopCloser (strings .NewReader (`
37
+ <ul>
38
+ <li><a href="http://example.com/1">1</a></li>
39
+ <li><a href="http://example.com/2">2</a></li>
40
+ </ul>
41
+ ` )),
42
+ Request : httptest .NewRequest (http .MethodGet , "http://example.com/" , nil ),
43
+ },
44
+ responseContent : []byte (`
45
+ <ul>
46
+ <li><a href="http://example.com/1">1</a></li>
47
+ <li><a href="http://example.com/2">2</a></li>
48
+ </ul>
49
+ ` ),
50
+ },
51
+ wantLinks : []string {"http://example.com/1" , "http://example.com/2" },
52
+ wantErr : assert .NoError ,
53
+ },
54
+ {
55
+ name : "success with transformers" ,
56
+ transformers : TransformerGroup {
57
+ func () extractors.LinkTransformer {
58
+ links := []string {"http://example.com/1" , "http://example.com/2" }
59
+ transformedLinks := []string {
60
+ "http://example.com/1/transformed/1" ,
61
+ "http://example.com/2/transformed/1" ,
62
+ }
63
+
64
+ responseContent := `
65
+ <ul>
66
+ <li><a href="http://example.com/1">1</a></li>
67
+ <li><a href="http://example.com/2">2</a></li>
68
+ </ul>
69
+ `
70
+ response := & http.Response {
71
+ Body : ioutil .NopCloser (strings .NewReader (responseContent )),
72
+ Request : httptest .NewRequest (http .MethodGet , "http://example.com/" , nil ),
73
+ }
74
+
75
+ transformer := new (MockLinkTransformer )
76
+ transformer .
77
+ On ("TransformLinks" , links , response , []byte (responseContent )).
78
+ Return (transformedLinks , nil )
79
+
80
+ return transformer
81
+ }(),
82
+ func () extractors.LinkTransformer {
83
+ links := []string {
84
+ "http://example.com/1/transformed/1" ,
85
+ "http://example.com/2/transformed/1" ,
86
+ }
87
+ transformedLinks := []string {
88
+ "http://example.com/1/transformed/2" ,
89
+ "http://example.com/2/transformed/2" ,
90
+ }
91
+
92
+ responseContent := `
93
+ <ul>
94
+ <li><a href="http://example.com/1">1</a></li>
95
+ <li><a href="http://example.com/2">2</a></li>
96
+ </ul>
97
+ `
98
+ response := & http.Response {
99
+ Body : ioutil .NopCloser (strings .NewReader (responseContent )),
100
+ Request : httptest .NewRequest (http .MethodGet , "http://example.com/" , nil ),
101
+ }
102
+
103
+ transformer := new (MockLinkTransformer )
104
+ transformer .
105
+ On ("TransformLinks" , links , response , []byte (responseContent )).
106
+ Return (transformedLinks , nil )
107
+
108
+ return transformer
109
+ }(),
110
+ },
111
+ args : args {
112
+ links : []string {"http://example.com/1" , "http://example.com/2" },
113
+ response : & http.Response {
114
+ Body : ioutil .NopCloser (strings .NewReader (`
115
+ <ul>
116
+ <li><a href="http://example.com/1">1</a></li>
117
+ <li><a href="http://example.com/2">2</a></li>
118
+ </ul>
119
+ ` )),
120
+ Request : httptest .NewRequest (http .MethodGet , "http://example.com/" , nil ),
121
+ },
122
+ responseContent : func () []byte {
123
+ return []byte (`
124
+ <ul>
125
+ <li><a href="http://example.com/1">1</a></li>
126
+ <li><a href="http://example.com/2">2</a></li>
127
+ </ul>
128
+ ` )
129
+ }(),
130
+ },
131
+ wantLinks : []string {
132
+ "http://example.com/1/transformed/2" ,
133
+ "http://example.com/2/transformed/2" ,
134
+ },
135
+ wantErr : assert .NoError ,
136
+ },
137
+ {
138
+ name : "error" ,
139
+ transformers : TransformerGroup {
140
+ func () extractors.LinkTransformer {
141
+ links := []string {"http://example.com/1" , "http://example.com/2" }
142
+
143
+ responseContent := `
144
+ <ul>
145
+ <li><a href="http://example.com/1">1</a></li>
146
+ <li><a href="http://example.com/2">2</a></li>
147
+ </ul>
148
+ `
149
+ response := & http.Response {
150
+ Body : ioutil .NopCloser (strings .NewReader (responseContent )),
151
+ Request : httptest .NewRequest (http .MethodGet , "http://example.com/" , nil ),
152
+ }
153
+
154
+ transformer := new (MockLinkTransformer )
155
+ transformer .
156
+ On ("TransformLinks" , links , response , []byte (responseContent )).
157
+ Return (nil , iotest .ErrTimeout )
158
+
159
+ return transformer
160
+ }(),
161
+ new (MockLinkTransformer ),
162
+ },
163
+ args : args {
164
+ links : []string {"http://example.com/1" , "http://example.com/2" },
165
+ response : & http.Response {
166
+ Body : ioutil .NopCloser (strings .NewReader (`
167
+ <ul>
168
+ <li><a href="http://example.com/1">1</a></li>
169
+ <li><a href="http://example.com/2">2</a></li>
170
+ </ul>
171
+ ` )),
172
+ Request : httptest .NewRequest (http .MethodGet , "http://example.com/" , nil ),
173
+ },
174
+ responseContent : func () []byte {
175
+ return []byte (`
176
+ <ul>
177
+ <li><a href="http://example.com/1">1</a></li>
178
+ <li><a href="http://example.com/2">2</a></li>
179
+ </ul>
180
+ ` )
181
+ }(),
182
+ },
183
+ wantLinks : nil ,
184
+ wantErr : assert .Error ,
185
+ },
26
186
} {
27
187
test .Run (data .name , func (test * testing.T ) {
28
188
gotLinks , gotErr := data .transformers .TransformLinks (
0 commit comments