Skip to content

Commit

Permalink
Add additional specs for redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
pengwynn committed Jun 6, 2015
1 parent d97eab7 commit 2c5740a
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions spec/octokit/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,18 @@
assert_requested redirect_request
end

it "follows redirect for 302 response" do
client = oauth_client

original_request = stub_get("/foo").
to_return(:status => 302, :headers => { "Location" => "/bar" })
redirect_request = stub_get("/bar").to_return(:status => 200)

client.get("/foo")
assert_requested original_request
assert_requested redirect_request
end

it "follows redirect for 307 response" do
client = oauth_client

Expand All @@ -449,6 +461,93 @@
assert_requested original_request
assert_requested redirect_request
end

it "follows redirects for supported HTTP methods" do
client = oauth_client

http_methods = [:head, :get, :post, :put, :patch, :delete]

http_methods.each do |http|
original_request = stub_request(http, github_url("/foo")).
to_return(:status => 301, :headers => { "Location" => "/bar" })
redirect_request = stub_request(http, github_url("/bar")).
to_return(:status => 200)

client.send(http, "/foo")
assert_requested original_request
assert_requested redirect_request
end
end

it "does not change HTTP method when following a redirect" do
client = oauth_client

original_request = stub_get("/foo").
to_return(:status => 301, :headers => { "Location" => "/bar" })
redirect_request = stub_get("/bar").to_return(:status => 200)

client.get("/foo")
assert_requested original_request
assert_requested redirect_request

other_methods = [:head, :post, :put, :patch, :delete]
other_methods.each do |http|
assert_not_requested http, github_url("/bar")
end
end

it "keeps authentication info when redirecting to the same host" do
client = oauth_client

original_request = stub_get("/foo").
with(:headers => {"Authorization" => "token #{test_github_token}"}).
to_return(:status => 301, :headers => { "Location" => "/bar" })
redirect_request = stub_get("/bar").
with(:headers => {"Authorization" => "token #{test_github_token}"}).
to_return(:status => 200)

client.get("/foo")
assert_requested original_request
assert_requested redirect_request
end

it "drops authentication info when redirecting to a different host" do
client = oauth_client

original_request = stub_request(:get, github_url("/foo")).
with(:headers => {"Authorization" => "token #{test_github_token}"}).
to_return(:status => 301, :headers => { "Location" => "https://example.com/bar" })
redirect_request = stub_request(:get, "https://example.com/bar").
to_return(:status => 200)

client.get("/foo")

assert_requested original_request
assert_requested(:get, "https://example.com/bar") { |req|
req.headers["Authorization"].nil?
}
end

it "follows at most 3 consecutive redirects" do
client = oauth_client

original_request = stub_get("/a").
to_return(:status => 302, :headers => { "Location" => "/b" })
first_redirect = stub_get("/b").
to_return(:status => 302, :headers => { "Location" => "/c" })
second_redirect = stub_get("/c").
to_return(:status => 302, :headers => { "Location" => "/d" })
third_redirect = stub_get("/d").
to_return(:status => 302, :headers => { "Location" => "/e" })
fourth_redirect = stub_get("/e").to_return(:status => 200)

expect { client.get("/a") }.to raise_error(Octokit::Middleware::RedirectLimitReached)
assert_requested original_request
assert_requested first_redirect
assert_requested second_redirect
assert_requested third_redirect
assert_not_requested fourth_redirect
end
end

describe "auto pagination", :vcr do
Expand Down

0 comments on commit 2c5740a

Please sign in to comment.