Skip to content

Commit 7e0b954

Browse files
committed
Merge pull request #36 from github/accept-uris
HealthCheck.new should accept domains and URI's
2 parents 0d20e51 + b557459 commit 7e0b954

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

lib/github-pages-health-check.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class HealthCheck
4747
}
4848

4949
def initialize(domain)
50-
@domain = domain
50+
@domain = host_from_uri(domain)
5151
end
5252

5353
def cloudflare_ip?
@@ -260,5 +260,24 @@ def scheme
260260
def uri
261261
@uri ||= Addressable::URI.new(:host => domain, :scheme => scheme, :path => "/").normalize
262262
end
263+
264+
# Parse the URI. Accept either domain names or full URI's.
265+
# Used by the initializer so we can be more flexible with inputs.
266+
#
267+
# domain - a URI or domain name.
268+
#
269+
# Examples
270+
#
271+
# host_from_uri("benbalter.github.com")
272+
# # => 'benbalter.github.com'
273+
# host_from_uri("https://benbalter.github.com")
274+
# # => 'benbalter.github.com'
275+
# host_from_uri("benbalter.github.com/help-me-im-a-path/")
276+
# # => 'benbalter.github.com'
277+
#
278+
# Return the hostname.
279+
def host_from_uri(domain)
280+
Addressable::URI.parse(domain).host || Addressable::URI.parse("http://#{domain}").host
281+
end
263282
end
264283
end

script/cibuild

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ set -ex
44

55
script/bootstrap
66

7-
bundle exec rspec
7+
script/test
88
script/check-cloudflare-ips
99
bundle exec gem build github-pages-health-check.gemspec

script/test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
bundle exec rspec $@

spec/github_pages_health_check_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ def make_health_check(domain="foo.github.io")
66
GitHubPages::HealthCheck.new(domain)
77
end
88

9+
context "constructor" do
10+
let(:expected) { "foo.github.io" }
11+
before(:each) do
12+
stub_request(:head, expected).
13+
to_return(:status => 200, :headers => {:server => "GitHub.com"})
14+
end
15+
16+
it "can handle bare domains" do
17+
expect(make_health_check("foo.github.io").domain).to eql(expected)
18+
end
19+
20+
it "can handle URI's" do
21+
expect(make_health_check("https://foo.github.io").domain).to eql(expected)
22+
expect(make_health_check("http://foo.github.io").domain).to eql(expected)
23+
expect(make_health_check("ftp://foo.github.io").domain).to eql(expected)
24+
end
25+
26+
it "can handle paths" do
27+
expect(make_health_check("foo.github.io/im-a-path/").domain).to eql(expected)
28+
expect(make_health_check("foo.github.io/im-a-path").domain).to eql(expected)
29+
expect(make_health_check("foo.github.io/index.html").domain).to eql(expected)
30+
end
31+
end
32+
933
def a_packet(ip)
1034
Net::DNS::RR::A.new(:name => "pages.invalid", :address => ip, :ttl => 1000)
1135
end

0 commit comments

Comments
 (0)