/
rawler_spec.rb
331 lines (243 loc) · 9.86 KB
/
rawler_spec.rb
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# encoding: UTF-8
require 'spec_helper'
describe Rawler::Base do
let(:output) { double('output').as_null_object }
let(:rawler) { Rawler::Base.new('http://example.com', output) }
before(:each) do
Rawler.stub(:output).and_return(output)
register('http://example.com', site)
end
describe "logfile" do
it "should have default value" do
url = 'http://example.com'
Rawler::Base.new(url, output)
Rawler.logfile.should == Rawler::Base::DEFAULT_LOGFILE
end
it "should honor logfile option" do
url = 'http://example.com'
logfile = 'custom_logfile'.freeze
Rawler::Base.new(url, output, :logfile => logfile)
Rawler.logfile.should == logfile
end
end
describe "log" do
it "should be turned off by default" do
url = 'http://example.com'
Rawler::Base.new(url, output)
Rawler.log.should == false
end
it "should be turned on when assigning custom logfile" do
url = 'http://example.com'
logfile = 'custom_logfile'
Rawler::Base.new(url, output, :logfile => logfile)
Rawler.log.should == true
end
end
describe "url encoding" do
it "should encode url" do
original = 'http://example.com/写程序容易出现的几个不好的地方'
expected = 'http://example.com/%E5%86%99%E7%A8%8B%E5%BA%8F%E5%AE%B9%E6%98%93%E5%87%BA%E7%8E%B0%E7%9A%84%E5%87%A0%E4%B8%AA%E4%B8%8D%E5%A5%BD%E7%9A%84%E5%9C%B0%E6%96%B9'
Rawler::Base.new(original, output)
Rawler.url.should == expected
end
it "should auto prepend http" do
original = 'example.com'
expected = 'http://example.com'
Rawler::Base.new(original, output)
Rawler.url.should == expected
end
it "should not auto prepend http when already http" do
original = 'http://example.com'
expected = 'http://example.com'
Rawler::Base.new(original, output)
Rawler.url.should == expected
end
it "should not auto prepend http when https" do
original = 'https://example.com'
expected = 'https://example.com'
Rawler::Base.new(original, output)
Rawler.url.should == expected
end
end
describe "validate_links" do
it "should validate links recursively" do
register('http://example.com/foo1', '<a href="http://external.com/foo">x</a>')
register('http://example.com/foo2', '')
register('http://external.com', '')
register('http://external.com/foo', '')
rawler.validate
rawler.responses['http://example.com/foo1'].should_not be_nil
rawler.responses['http://example.com/foo2'].should_not be_nil
rawler.responses['http://external.com'].should_not be_nil
rawler.responses['http://external.com/foo'].should_not be_nil
end
it "should not validate links on external pages" do
register('http://example.com/', '<a href="http://external.com/foo">x</a>')
register('http://example.com/foo', '<a href="http://external.com/foo">x</a>')
register('http://external.com/foo', '<a href="http://external.com/bar">x</a>')
register('http://external.com/bar', '')
rawler.validate
rawler.responses['http://external.com/foo'].should_not be_nil
rawler.responses['http://external.com/bar'].should be_nil
end
it "should output results" do
register('http://example.com/foo1', '<a href="http://external.com/foo">x</a>')
register('http://example.com/foo2', '')
register('http://external.com', '')
register('http://external.com/foo', '', 301)
output.should_receive(:info).with('200 - http://example.com/foo1')
output.should_receive(:info).with('200 - http://example.com/foo2')
output.should_receive(:info).with('200 - http://external.com')
output.should_receive(:warn).with('301 - http://external.com/foo - Called from: http://example.com/foo1')
rawler.validate
end
it "should follow redirections but inform about them" do
register('http://example.com', '<a href="/foo">foo</a>')
register('http://example.com/foo', '', 301, :location => 'http://example.com/bar')
register('http://example.com/bar', '')
output.should_receive(:warn).with('301 - http://example.com/foo - Called from: http://example.com - Following redirection to: http://example.com/bar')
output.should_receive(:info).with('200 - http://example.com/bar')
rawler.validate
end
it "should handle circular redirections" do
register('http://example.com', '<a href="/foo">foo</a>')
register('http://example.com/foo', '', 301, :location => 'http://example.com/foo')
output.should_receive(:warn).with('301 - http://example.com/foo - Called from: http://example.com - Following redirection to: http://example.com/foo')
rawler.validate
end
end
describe "get_status_code" do
it "should add to 200 links" do
url = 'http://example.com/foo'
from = 'http://other.com'
register(url, '', 200)
rawler.send(:add_status_code, url, from)
rawler.responses[url][:status].should == 200
end
it "should add to 301 links" do
url = 'http://example.com/foo'
from = 'http://other.com'
register(url, '', 301)
rawler.send(:add_status_code, url, from)
rawler.responses[url][:status].should == 301
end
it "should save username and password" do
rawler = Rawler::Base.new('http://example.com', output, {:username => 'my_user', :password => 'secret'})
Rawler.username.should == 'my_user'
Rawler.password.should == 'secret'
end
it "should save wait" do
rawler = Rawler::Base.new('http://example.com', output, {:wait => 5})
Rawler.wait.should == 5
end
it "should rescue from Errno::ECONNREFUSED" do
url = 'http://example.com'
from = 'http://other.com'
Rawler::Request.should_receive(:get).and_raise Errno::ECONNREFUSED
output.should_receive(:error).with("Connection refused - #{url} - Called from: #{from}")
rawler.send(:add_status_code, url, from)
end
[Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ETIMEDOUT, EOFError,
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, SocketError].each do |error|
it "should rescue from #{error}" do
url = 'http://example.com'
from = 'http://other.com'
Rawler::Request.should_receive(:get).and_raise error
output.should_receive(:error).with("Connection problems - #{url} - Called from: #{from}")
rawler.send(:add_status_code, url, from)
end
end
it "should rescue from general errors" do
url = 'http://example.com'
from = 'http://other.com'
Rawler::Request.should_receive(:get).and_raise
output.should_receive(:error).with("Unknown error - #{url} - Called from: #{from}")
rawler.send(:add_status_code, url, from)
end
end
describe "record_response" do
let(:link) { 'http://foo.com' }
let(:from) { 'http://bar.com' }
context "response code 100" do
%w!100, 150, 199!.each do |code|
it "logger should receive info" do
output.should_receive(:info).with("#{code} - #{link}")
rawler.send(:record_response, code, link, from)
end
end
end
context "response code 200" do
%w!200, 250, 299!.each do |code|
it "logger should receive info" do
output.should_receive(:info).with("#{code} - #{link}")
rawler.send(:record_response, code, link, from)
end
end
end
context "response code 300" do
%w!300, 350, 399!.each do |code|
it "logger should receive warn" do
output.should_receive(:warn).with("#{code} - #{link} - Called from: #{from}")
rawler.send(:record_response, code, link, from)
end
end
end
context "response code 400" do
%w!400, 450, 499!.each do |code|
it "logger should receive info" do
output.should_receive(:error).with("#{code} - #{link} - Called from: #{from}")
rawler.send(:record_response, code, link, from)
end
end
end
context "response code 500" do
%w!400, 550, 599!.each do |code|
it "logger should receive info" do
output.should_receive(:error).with("#{code} - #{link} - Called from: #{from}")
rawler.send(:record_response, code, link, from)
end
end
end
context "response code invalid" do
let(:code) { 600 }
it "logger should receive eror" do
output.should_receive(:error).with("Unknown code #{code} - #{link} - Called from: #{from}")
rawler.send(:record_response, code, link, from)
end
end
end
describe "error introspection" do
context "no error codes occured" do
before do
rawler.responses["http://example.com/"] = { :status => 100 }
rawler.responses["http://example.com/foo"] = { :status => 200 }
rawler.responses["http://example.com/bar"] = { :status => 300 }
end
it "should give all occurring errors" do
rawler.errors.should be_empty
end
end
context "error codes occured" do
before do
rawler.responses["http://example.com/"] = { :status => 100 }
rawler.responses["http://example.com/foo"] = { :status => 400 }
rawler.responses["http://example.com/bar"] = { :status => 500 }
end
it "should give all occurring errors" do
rawler.errors.count.should eq 2
end
end
end
private
def site
<<-site
<html>
<body>
<a href="http://example.com/foo1">foo1</a>
<a href="http://example.com/foo2">foo2</a>
<a href="http://external.com">external</a>
</body>
</html>
site
end
end