Refactor Hbc::UrlChecker test.

This commit is contained in:
Markus Reiter 2016-10-23 19:00:33 +02:00
parent 0c140f0970
commit c7a535de22
4 changed files with 42 additions and 63 deletions

View File

@ -1,4 +1,5 @@
require "hbc/checkable" require "hbc/checkable"
require "hbc/fetcher"
module Hbc module Hbc
class UrlChecker class UrlChecker

View File

@ -2,32 +2,50 @@ require "test_helper"
describe Hbc::UrlChecker do describe Hbc::UrlChecker do
describe "request processing" do describe "request processing" do
it "adds an error if response is empty" do let(:cask) { Hbc.load("basic-cask") }
cask = Hbc.load("basic-cask") let(:checker) { Hbc::UrlChecker.new(cask) }
Hbc::FakeFetcher.fake_response_for(cask.url, "") let(:with_stubbed_fetcher) {
checker = Hbc::UrlChecker.new(cask, Hbc::FakeFetcher) lambda { |&block|
Hbc::Fetcher.stub(:head, response) do
checker.run checker.run
checker.errors.must_include "timeout while requesting #{cask.url}" instance_eval(&block)
end
}
}
describe "with an empty response" do
let(:response) { "" }
it "adds an error" do
with_stubbed_fetcher.call do
expect(checker.errors).must_include("timeout while requesting #{cask.url}")
end
end
end end
it "properly populates the response code and headers from an http response" do describe "with a valid http response" do
cask = Hbc.load("basic-cask") let(:response) {
<<-EOS.undent
Hbc::FakeFetcher.fake_response_for(cask.url, <<-EOS.undent)
HTTP/1.1 200 OK HTTP/1.1 200 OK
Content-Type: application/x-apple-diskimage Content-Type: application/x-apple-diskimage
ETag: "b4208f3e84967be4b078ecaa03fba941" ETag: "b4208f3e84967be4b078ecaa03fba941"
Content-Length: 23726161 Content-Length: 23726161
Last-Modified: Sun, 12 Aug 2012 21:17:21 GMT Last-Modified: Sun, 12 Aug 2012 21:17:21 GMT
EOS EOS
}
checker = Hbc::UrlChecker.new(cask, Hbc::FakeFetcher) it "properly populates the response code and headers" do
checker.run with_stubbed_fetcher.call do
checker.response_status.must_equal "HTTP/1.1 200 OK" expect(checker.errors).must_be_empty
checker.headers.must_equal("Content-Type" => "application/x-apple-diskimage", expect(checker.response_status).must_equal("HTTP/1.1 200 OK")
expect(checker.headers).must_equal(
"Content-Type" => "application/x-apple-diskimage",
"ETag" => '"b4208f3e84967be4b078ecaa03fba941"', "ETag" => '"b4208f3e84967be4b078ecaa03fba941"',
"Content-Length" => "23726161", "Content-Length" => "23726161",
"Last-Modified" => "Sun, 12 Aug 2012 21:17:21 GMT") "Last-Modified" => "Sun, 12 Aug 2012 21:17:21 GMT"
)
end
end
end end
end end
end end

View File

@ -1,39 +0,0 @@
module Hbc
class FakeFetcher
def self.fake_response_for(url, response)
@responses[url] = response
end
def self.head(url)
@responses ||= {}
raise("no response faked for #{url.inspect}") unless @responses.key?(url)
@responses[url]
end
def self.init
@responses = {}
end
def self.clear
@responses = {}
end
end
end
module FakeFetcherHooks
def before_setup
super
Hbc::FakeFetcher.init
end
def after_teardown
super
Hbc::FakeFetcher.clear
end
end
module MiniTest
class Spec
include FakeFetcherHooks
end
end

View File

@ -100,7 +100,6 @@ require "support/shared_examples"
require "support/shared_examples/dsl_base.rb" require "support/shared_examples/dsl_base.rb"
require "support/shared_examples/staged.rb" require "support/shared_examples/staged.rb"
require "support/fake_fetcher"
require "support/fake_dirs" require "support/fake_dirs"
require "support/fake_system_command" require "support/fake_system_command"
require "support/cleanup" require "support/cleanup"