Skip to content

HealthCheck.new should accept domains and URI's #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/github-pages-health-check.rb
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ class HealthCheck
}

def initialize(domain)
@domain = domain
@domain = host_from_uri(domain)
end

def cloudflare_ip?
@@ -260,5 +260,24 @@ def scheme
def uri
@uri ||= Addressable::URI.new(:host => domain, :scheme => scheme, :path => "/").normalize
end

# Parse the URI. Accept either domain names or full URI's.
# Used by the initializer so we can be more flexible with inputs.
#
# domain - a URI or domain name.
#
# Examples
#
# host_from_uri("benbalter.github.com")
# # => 'benbalter.github.com'
# host_from_uri("https://benbalter.github.com")
# # => 'benbalter.github.com'
# host_from_uri("benbalter.github.com/help-me-im-a-path/")
# # => 'benbalter.github.com'
#
# Return the hostname.
def host_from_uri(domain)
Addressable::URI.parse(domain).host || Addressable::URI.parse("http://#{domain}").host
end
end
end
2 changes: 1 addition & 1 deletion script/cibuild
Original file line number Diff line number Diff line change
@@ -4,6 +4,6 @@ set -ex

script/bootstrap

bundle exec rspec
script/test
script/check-cloudflare-ips
bundle exec gem build github-pages-health-check.gemspec
2 changes: 2 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
bundle exec rspec $@
24 changes: 24 additions & 0 deletions spec/github_pages_health_check_spec.rb
Original file line number Diff line number Diff line change
@@ -6,6 +6,30 @@ def make_health_check(domain="foo.github.io")
GitHubPages::HealthCheck.new(domain)
end

context "constructor" do
let(:expected) { "foo.github.io" }
before(:each) do
stub_request(:head, expected).
to_return(:status => 200, :headers => {:server => "GitHub.com"})
end

it "can handle bare domains" do
expect(make_health_check("foo.github.io").domain).to eql(expected)
end

it "can handle URI's" do
expect(make_health_check("https://foo.github.io").domain).to eql(expected)
expect(make_health_check("http://foo.github.io").domain).to eql(expected)
expect(make_health_check("ftp://foo.github.io").domain).to eql(expected)
end

it "can handle paths" do
expect(make_health_check("foo.github.io/im-a-path/").domain).to eql(expected)
expect(make_health_check("foo.github.io/im-a-path").domain).to eql(expected)
expect(make_health_check("foo.github.io/index.html").domain).to eql(expected)
end
end

def a_packet(ip)
Net::DNS::RR::A.new(:name => "pages.invalid", :address => ip, :ttl => 1000)
end