Unified · Split
| @@ -4,6 +4,10 @@ module Docs · 3 unmodified lines | |||
| 4 | code == 200 | 4 | code == 200 |
| 5 | end | 5 | end |
| 6 | 6 | ||
| 7 | def error? | ||
| 8 | code != 404 && code >= 400 && code <= 599 | ||
| 9 | end | ||
| 10 | |||
| 7 | def empty? | 11 | def empty? |
| 8 | body.empty? | 12 | body.empty? |
| 9 | end | 13 | end |
| @@ -29,6 +29,10 @@ module Docs · 28 unmodified lines | |||
| 29 | end | 29 | end |
| 30 | 30 | ||
| 31 | def process_response?(response) | 31 | def process_response?(response) |
| 32 | if response.error? | ||
| 33 | raise "Error status code (#{response.code}): #{response.url}" | ||
| 34 | end | ||
| 35 | |||
| 32 | response.success? && response.html? && base_url.contains?(response.effective_url) | 36 | response.success? && response.html? && base_url.contains?(response.effective_url) |
| 33 | end | 37 | end |
| 34 | 38 | ||
| @@ -29,6 +29,28 @@ class DocsResponseTest < MiniTest::Spec · 28 unmodified lines | |||
| 29 | end | 29 | end |
| 30 | end | 30 | end |
| 31 | 31 | ||
| 32 | describe "#error?" do | ||
| 33 | it "returns false when the code is 200" do | ||
| 34 | options.code = 200 | ||
| 35 | refute response.error? | ||
| 36 | end | ||
| 37 | |||
| 38 | it "returns false when the code is 404" do | ||
| 39 | options.code = 404 | ||
| 40 | refute response.error? | ||
| 41 | end | ||
| 42 | |||
| 43 | it "returns true when the code is 400" do | ||
| 44 | options.code = 400 | ||
| 45 | assert response.error? | ||
| 46 | end | ||
| 47 | |||
| 48 | it "returns true when the code is 500" do | ||
| 49 | options.code = 500 | ||
| 50 | assert response.error? | ||
| 51 | end | ||
| 52 | end | ||
| 53 | |||
| 32 | describe "#empty?" do | 54 | describe "#empty?" do |
| 33 | it "returns true when the body is empty" do | 55 | it "returns true when the body is empty" do |
| 34 | options.body = '' | 56 | options.body = '' |
| @@ -89,13 +89,18 @@ class DocsUrlScraperTest < MiniTest::Spec · 88 unmodified lines | |||
| 89 | 89 | ||
| 90 | describe "#process_response?" do | 90 | describe "#process_response?" do |
| 91 | let :response do | 91 | let :response do |
| 92 | OpenStruct.new success?: true, html?: true, effective_url: scraper.root_url | 92 | OpenStruct.new success?: true, html?: true, effective_url: scraper.root_url, error?: false |
| 93 | end | 93 | end |
| 94 | 94 | ||
| 95 | let :result do | 95 | let :result do |
| 96 | scraper.send :process_response?, response | 96 | scraper.send :process_response?, response |
| 97 | end | 97 | end |
| 98 | 98 | ||
| 99 | it "raises when the response is an error" do | ||
| 100 | response.send 'error?=', true | ||
| 101 | assert_raises(RuntimeError) { result } | ||
| 102 | end | ||
| 103 | |||
| 99 | it "returns false when the response isn't successful" do | 104 | it "returns false when the response isn't successful" do |
| 100 | response.send 'success?=', false | 105 | response.send 'success?=', false |
| 101 | refute result | 106 | refute result |