Skip to content

Commit 005c9e5

Browse files
committed
Merge pull request #24 from github/stub-requests
Stub net requests when testing
2 parents a0788b7 + 2e6f2e5 commit 005c9e5

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

github-pages-health-check.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ Gem::Specification.new do |s|
3030
s.add_development_dependency("rspec", "~> 3.0")
3131
s.add_development_dependency("pry", "~> 0.10")
3232
s.add_development_dependency("gem-release", "~> 0.7")
33+
s.add_development_dependency("webmock", "~> 1.21")
3334
end

lib/github-pages-health-check.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,15 @@ def to_hash
139139
:reason => reason
140140
}
141141
end
142+
alias_method :to_h, :to_hash
142143

143144
def served_by_pages?
144145
response = Typhoeus.head(uri, TYPHOEUS_OPTIONS)
146+
# Workaround for webmock not playing nicely with Typhoeus redirects
147+
# See https://github.com/bblimke/webmock/issues/237
148+
if response.mock? && response.headers["Location"]
149+
response = Typhoeus.head(response.headers["Location"], TYPHOEUS_OPTIONS)
150+
end
145151
response.success? && response.headers["Server"] == "GitHub.com"
146152
end
147153

spec/github_pages_health_check_spec.rb

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,28 @@ def cname_packet(domain)
136136
expect(health_check.valid_domain?).to be(false)
137137
end
138138

139-
it "returns valid json" do
140-
data = JSON.parse GitHubPages::HealthCheck.new("benbalter.com").to_json
141-
expect(data.length).to eql(14)
142-
data.each { |key, value| expect([true,false,nil].include?(value)).to eql(true) }
143-
end
139+
context "served by pages" do
140+
it "returns valid json" do
141+
stub_request(:head, "benbalter.com").
142+
to_return(:status => 200, :headers => {:server => "GitHub.com"})
144143

145-
it "return the error" do
146-
check = GitHubPages::HealthCheck.new "developer.facebook.com"
147-
expect(check.valid?).to eql(false)
148-
expect(check.reason.class).to eql(GitHubPages::HealthCheck::InvalidCNAME)
149-
expect(check.reason.message).to eql("CNAME does not point to GitHub Pages")
150-
end
144+
data = JSON.parse GitHubPages::HealthCheck.new("benbalter.com").to_json
145+
expect(data.length).to eql(14)
146+
data.each { |key, value| expect([true,false,nil].include?(value)).to eql(true) }
147+
end
151148

152-
context "served by pages" do
153149
it "knows when a domain is served by pages" do
150+
stub_request(:head, "http://choosealicense.com").
151+
to_return(:status => 200, :headers => {:server => "GitHub.com"})
152+
154153
check = GitHubPages::HealthCheck.new "choosealicense.com"
155154
expect(check.served_by_pages?).to eql(true)
156155
end
157156

158157
it "knows when a GitHub domain is served by pages" do
158+
stub_request(:head, "https://mac.github.com").
159+
to_return(:status => 200, :headers => {:server => "GitHub.com"})
160+
159161
check = GitHubPages::HealthCheck.new "mac.github.com"
160162
expect(check.served_by_pages?).to eql(true)
161163
end
@@ -165,21 +167,45 @@ def cname_packet(domain)
165167
# › curl -I http://getbootstrap.com/
166168
# HTTP/1.1 302 Found
167169
# Location: /
170+
stub_request(:head, "http://getbootstrap.com").
171+
to_return(:status => 302, :headers => {:location => "/"})
172+
173+
stub_request(:head, "http://getbootstrap.com/").
174+
to_return(:status => 200, :headers => {:server => "GitHub.com"})
175+
168176
check = GitHubPages::HealthCheck.new "getbootstrap.com"
169177
expect(check.served_by_pages?).to eql(true)
170178
end
171179

172180
it "knows when a domain with a redirect is served by pages" do
181+
stub_request(:head, "http://management.cio.gov").
182+
to_return(:status => 302, :headers => {:location => "https://management.cio.gov"})
183+
184+
stub_request(:head, "https://management.cio.gov").
185+
to_return(:status => 200, :headers => {:server => "GitHub.com"})
186+
173187
check = GitHubPages::HealthCheck.new "management.cio.gov"
174188
expect(check.served_by_pages?).to eql(true)
175189
end
190+
end
191+
192+
context "not served by pages" do
176193

177194
it "knows when a domain isn't served by pages" do
195+
stub_request(:head, "http://google.com").to_return(:status => 200, :headers => {})
178196
check = GitHubPages::HealthCheck.new "google.com"
179197
expect(check.served_by_pages?).to eql(false)
180198
expect(check.reason.class).to eql(GitHubPages::HealthCheck::NotServedByPages)
181199
expect(check.reason.message).to eql("Domain does not resolve to the GitHub Pages server")
182200
end
201+
202+
it "returns the error" do
203+
stub_request(:head, "http://developers.facebook.com").to_return(:status => 200, :headers => {})
204+
check = GitHubPages::HealthCheck.new "developers.facebook.com"
205+
expect(check.valid?).to eql(false)
206+
expect(check.reason.class).to eql(GitHubPages::HealthCheck::InvalidCNAME)
207+
expect(check.reason.message).to eql("CNAME does not point to GitHub Pages")
208+
end
183209
end
184210

185211
context "proxies" do
@@ -199,11 +225,15 @@ def cname_packet(domain)
199225
end
200226

201227
it "detects proxied sites" do
228+
stub_request(:head, "http://management.cio.gov").
229+
to_return(:status => 200, :headers => {:server => "GitHub.com"})
230+
202231
check = GitHubPages::HealthCheck.new "management.cio.gov"
203232
expect(check.proxied?).to eql(true)
204233
end
205234

206235
it "knows a site not served by pages isn't proxied" do
236+
stub_request(:head, "http://google.com").to_return(:status => 200, :headers => {})
207237
check = GitHubPages::HealthCheck.new "google.com"
208238
expect(check.proxied?).to eql(false)
209239
end

spec/spec_helper.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
require File.expand_path("../../lib/github-pages-health-check.rb", __FILE__)
1+
require "bundler/setup"
2+
require 'webmock/rspec'
3+
require_relative "../lib/github-pages-health-check"
4+
5+
WebMock.disable_net_connect!
26

37
RSpec.configure do |config|
48
config.raise_errors_for_deprecations!

0 commit comments

Comments
 (0)