Skip to content

Stub net requests when testing #24

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
Aug 12, 2015
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions github-pages-health-check.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ Gem::Specification.new do |s|
s.add_development_dependency("rspec", "~> 3.0")
s.add_development_dependency("pry", "~> 0.10")
s.add_development_dependency("gem-release", "~> 0.7")
s.add_development_dependency("webmock", "~> 1.21")
end
6 changes: 6 additions & 0 deletions lib/github-pages-health-check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,15 @@ def to_hash
:reason => reason
}
end
alias_method :to_h, :to_hash

def served_by_pages?
response = Typhoeus.head(uri, TYPHOEUS_OPTIONS)
# Workaround for webmock not playing nicely with Typhoeus redirects
# See https://github.com/bblimke/webmock/issues/237
if response.mock? && response.headers["Location"]
response = Typhoeus.head(response.headers["Location"], TYPHOEUS_OPTIONS)
end
response.success? && response.headers["Server"] == "GitHub.com"
end

Expand Down
54 changes: 42 additions & 12 deletions spec/github_pages_health_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,28 @@ def cname_packet(domain)
expect(health_check.valid_domain?).to be(false)
end

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

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

context "served by pages" do
it "knows when a domain is served by pages" do
stub_request(:head, "http://choosealicense.com").
to_return(:status => 200, :headers => {:server => "GitHub.com"})

check = GitHubPages::HealthCheck.new "choosealicense.com"
expect(check.served_by_pages?).to eql(true)
end

it "knows when a GitHub domain is served by pages" do
stub_request(:head, "https://mac.github.com").
to_return(:status => 200, :headers => {:server => "GitHub.com"})

check = GitHubPages::HealthCheck.new "mac.github.com"
expect(check.served_by_pages?).to eql(true)
end
Expand All @@ -165,21 +167,45 @@ def cname_packet(domain)
# › curl -I http://getbootstrap.com/
# HTTP/1.1 302 Found
# Location: /
stub_request(:head, "http://getbootstrap.com").
to_return(:status => 302, :headers => {:location => "/"})

stub_request(:head, "http://getbootstrap.com/").
to_return(:status => 200, :headers => {:server => "GitHub.com"})

check = GitHubPages::HealthCheck.new "getbootstrap.com"
expect(check.served_by_pages?).to eql(true)
end

it "knows when a domain with a redirect is served by pages" do
stub_request(:head, "http://management.cio.gov").
to_return(:status => 302, :headers => {:location => "https://management.cio.gov"})

stub_request(:head, "https://management.cio.gov").
to_return(:status => 200, :headers => {:server => "GitHub.com"})

check = GitHubPages::HealthCheck.new "management.cio.gov"
expect(check.served_by_pages?).to eql(true)
end
end

context "not served by pages" do

it "knows when a domain isn't served by pages" do
stub_request(:head, "http://google.com").to_return(:status => 200, :headers => {})
check = GitHubPages::HealthCheck.new "google.com"
expect(check.served_by_pages?).to eql(false)
expect(check.reason.class).to eql(GitHubPages::HealthCheck::NotServedByPages)
expect(check.reason.message).to eql("Domain does not resolve to the GitHub Pages server")
end

it "returns the error" do
stub_request(:head, "http://developers.facebook.com").to_return(:status => 200, :headers => {})
check = GitHubPages::HealthCheck.new "developers.facebook.com"
expect(check.valid?).to eql(false)
expect(check.reason.class).to eql(GitHubPages::HealthCheck::InvalidCNAME)
expect(check.reason.message).to eql("CNAME does not point to GitHub Pages")
end
end

context "proxies" do
Expand All @@ -199,11 +225,15 @@ def cname_packet(domain)
end

it "detects proxied sites" do
stub_request(:head, "http://management.cio.gov").
to_return(:status => 200, :headers => {:server => "GitHub.com"})

check = GitHubPages::HealthCheck.new "management.cio.gov"
expect(check.proxied?).to eql(true)
end

it "knows a site not served by pages isn't proxied" do
stub_request(:head, "http://google.com").to_return(:status => 200, :headers => {})
check = GitHubPages::HealthCheck.new "google.com"
expect(check.proxied?).to eql(false)
end
Expand Down
6 changes: 5 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require File.expand_path("../../lib/github-pages-health-check.rb", __FILE__)
require "bundler/setup"
require 'webmock/rspec'
require_relative "../lib/github-pages-health-check"

WebMock.disable_net_connect!

RSpec.configure do |config|
config.raise_errors_for_deprecations!
Expand Down