Raise error and stop scraping on 4xx/5xx status code

pull/309/head
Thibaut 9 years ago
parent b4dcb63fa3
commit 3eb5ccb7ea

@ -4,6 +4,10 @@ module Docs
code == 200 code == 200
end end
def error?
code != 404 && code >= 400 && code <= 599
end
def empty? def empty?
body.empty? body.empty?
end end

@ -29,6 +29,10 @@ module Docs
end end
def process_response?(response) def process_response?(response)
if response.error?
raise "Error status code (#{response.code}): #{response.url}"
end
response.success? && response.html? && base_url.contains?(response.effective_url) response.success? && response.html? && base_url.contains?(response.effective_url)
end end

@ -29,6 +29,28 @@ class DocsResponseTest < MiniTest::Spec
end end
end end
describe "#error?" do
it "returns false when the code is 200" do
options.code = 200
refute response.error?
end
it "returns false when the code is 404" do
options.code = 404
refute response.error?
end
it "returns true when the code is 400" do
options.code = 400
assert response.error?
end
it "returns true when the code is 500" do
options.code = 500
assert response.error?
end
end
describe "#empty?" do describe "#empty?" do
it "returns true when the body is empty" do it "returns true when the body is empty" do
options.body = '' options.body = ''

@ -89,13 +89,18 @@ class DocsUrlScraperTest < MiniTest::Spec
describe "#process_response?" do describe "#process_response?" do
let :response do let :response do
OpenStruct.new success?: true, html?: true, effective_url: scraper.root_url OpenStruct.new success?: true, html?: true, effective_url: scraper.root_url, error?: false
end end
let :result do let :result do
scraper.send :process_response?, response scraper.send :process_response?, response
end end
it "raises when the response is an error" do
response.send 'error?=', true
assert_raises(RuntimeError) { result }
end
it "returns false when the response isn't successful" do it "returns false when the response isn't successful" do
response.send 'success?=', false response.send 'success?=', false
refute result refute result

Loading…
Cancel
Save