Skip to content

Commit 6ce7654

Browse files
committed
Add tests for handling redirects
1 parent b00bc4b commit 6ce7654

File tree

7 files changed

+146
-2
lines changed

7 files changed

+146
-2
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,6 @@ group :development, :test do
8080

8181
# Create hashes that look like classes with methods
8282
gem 'hash_dot'
83+
84+
gem 'webmock'
8385
end

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ GEM
3838
tzinfo (~> 1.1)
3939
activesupport-json_encoder (1.1.0)
4040
activesupport (>= 4.1.0, < 5.0)
41+
addressable (2.4.0)
4142
arel (6.0.3)
4243
builder (3.2.2)
4344
byebug (8.2.2)
@@ -51,6 +52,8 @@ GEM
5152
coffee-script-source (1.10.0)
5253
concurrent-ruby (1.0.1)
5354
connection_pool (2.2.0)
55+
crack (0.4.3)
56+
safe_yaml (~> 1.0.0)
5457
debug_inspector (0.0.2)
5558
diff-lcs (1.2.5)
5659
erubis (2.7.0)
@@ -69,6 +72,7 @@ GEM
6972
globalid (0.3.6)
7073
activesupport (>= 4.1.0)
7174
hash_dot (2.0.2)
75+
hashdiff (0.3.0)
7276
i18n (0.7.0)
7377
jbuilder (2.4.1)
7478
activesupport (>= 3.0.0, < 5.1)
@@ -146,6 +150,7 @@ GEM
146150
rspec-support (~> 3.4.0)
147151
rspec-support (3.4.1)
148152
rufus-scheduler (3.2.0)
153+
safe_yaml (1.0.4)
149154
sass (3.4.21)
150155
sass-rails (5.0.4)
151156
railties (>= 4.0.0, < 5.0)
@@ -198,6 +203,10 @@ GEM
198203
activemodel (>= 4.2)
199204
debug_inspector
200205
railties (>= 4.2)
206+
webmock (2.0.2)
207+
addressable (>= 2.3.6)
208+
crack (>= 0.3.2)
209+
hashdiff
201210

202211
PLATFORMS
203212
ruby
@@ -230,6 +239,7 @@ DEPENDENCIES
230239
turbolinks
231240
uglifier (>= 2.7.2)
232241
web-console (~> 3.1.1)
242+
webmock
233243

234244
BUNDLED WITH
235245
1.11.2

lib/github_api.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def self.request_raw(uri, github_token, params = nil, headers = {})
7777
break
7878
end
7979

80-
return request_raw(uri, github_token, params, headers) if response == Net::HTTPRedirection
80+
if response.kind_of?(Net::HTTPRedirection)
81+
new_url = response['location']
82+
return request_raw(new_url, github_token, params, headers)
83+
end
8184

8285
unless response.code.to_i == 200
8386
raise "Request failed with #{response.code}: #{response.read_body}"

spec/lib/github_api_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=begin
2+
Copyright 2016 SourceClear Inc
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
=end
16+
17+
require 'rails_helper'
18+
#require 'spec_helper'
19+
20+
describe 'GitHubAPI' do
21+
describe '.request_raw' do
22+
#https://api.github.com/repositories/5625/commits?since=2016-05-01T12%3A45%3A06%2B00%3A00
23+
let(:url) { 'https://api.github.com/repositories/5625/commits_redirect?since=2016-05-01T12%3A45%3A06%2B00%3A00' }
24+
let(:github_token) { 'token' }
25+
subject { GitHubAPI.request_raw(url, github_token) }
26+
27+
#self.request_raw(uri, github_token, params = nil, headers = {})
28+
it { should_not be nil }
29+
end
30+
end

spec/spec_helper.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'webmock/rspec'
2+
require_relative 'support/fake_github.rb'
3+
14
RSpec.configure do |config|
25
# rspec-expectations config goes here. You can use an alternate
36
# assertion/expectation library such as wrong or the stdlib/minitest
@@ -23,6 +26,6 @@
2326
end
2427

2528
config.before(:each) do
26-
stub_request(:any, /api.github.com/).to_rack(FakeGitHub)
29+
stub_request(:any, /api\.github\.com/).to_rack(FakeGitHub)
2730
end
2831
end

spec/support/fake_github.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'sinatra/base'
2+
3+
class FakeGitHub < Sinatra::Base
4+
get '/repositories/:organization/:project/contributors' do
5+
json_response 200, 'contributors.json'
6+
end
7+
8+
#https://api.github.com/repositories/5625/commits?since=2016-05-01T12%3A45%3A06%2B00%3A00
9+
get '/repositories/:project_id/commits_redirect' do
10+
redirect to("/repositories/#{params['project_id']}/commits")
11+
end
12+
13+
get '/repositories/:project_id/commits' do
14+
json_response 200, 'commits.json'
15+
end
16+
17+
private
18+
19+
def json_response(response_code, file_name)
20+
content_type :json
21+
status response_code
22+
File.open("#{File.dirname(__FILE__)}/fixtures/#{file_name}", 'rb').read
23+
end
24+
end

spec/support/fixtures/commits.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[
2+
{
3+
"sha": "d6dcaa7203e859037bfaa1222f85111feb3dbe93",
4+
"commit": {
5+
"author": {
6+
"name": "Caleb Fenton",
7+
"email": "[email protected]",
8+
"date": "2016-02-23T04:03:40Z"
9+
},
10+
"committer": {
11+
"name": "Caleb Fenton",
12+
"email": "[email protected]",
13+
"date": "2016-02-23T04:03:40Z"
14+
},
15+
"message": "Prepare 1.0 release, update gradlew",
16+
"tree": {
17+
"sha": "31af61b96fd652305d4a8874cc1ddb27fafc59a2",
18+
"url": "https://api.github.com/repos/CalebFenton/simplify/git/trees/31af61b96fd652305d4a8874cc1ddb27fafc59a2"
19+
},
20+
"url": "https://api.github.com/repos/CalebFenton/simplify/git/commits/d6dcaa7203e859037bfaa1222f85111feb3dbe93",
21+
"comment_count": 4
22+
},
23+
"url": "https://api.github.com/repos/CalebFenton/simplify/commits/d6dcaa7203e859037bfaa1222f85111feb3dbe93",
24+
"html_url": "https://github.com/CalebFenton/simplify/commit/d6dcaa7203e859037bfaa1222f85111feb3dbe93",
25+
"comments_url": "https://api.github.com/repos/CalebFenton/simplify/commits/d6dcaa7203e859037bfaa1222f85111feb3dbe93/comments",
26+
"author": {
27+
"login": "CalebFenton",
28+
"id": 1356658,
29+
"avatar_url": "https://avatars.githubusercontent.com/u/1356658?v=3",
30+
"gravatar_id": "",
31+
"url": "https://api.github.com/users/CalebFenton",
32+
"html_url": "https://github.com/CalebFenton",
33+
"followers_url": "https://api.github.com/users/CalebFenton/followers",
34+
"following_url": "https://api.github.com/users/CalebFenton/following{/other_user}",
35+
"gists_url": "https://api.github.com/users/CalebFenton/gists{/gist_id}",
36+
"starred_url": "https://api.github.com/users/CalebFenton/starred{/owner}{/repo}",
37+
"subscriptions_url": "https://api.github.com/users/CalebFenton/subscriptions",
38+
"organizations_url": "https://api.github.com/users/CalebFenton/orgs",
39+
"repos_url": "https://api.github.com/users/CalebFenton/repos",
40+
"events_url": "https://api.github.com/users/CalebFenton/events{/privacy}",
41+
"received_events_url": "https://api.github.com/users/CalebFenton/received_events",
42+
"type": "User",
43+
"site_admin": false
44+
},
45+
"committer": {
46+
"login": "CalebFenton",
47+
"id": 1356658,
48+
"avatar_url": "https://avatars.githubusercontent.com/u/1356658?v=3",
49+
"gravatar_id": "",
50+
"url": "https://api.github.com/users/CalebFenton",
51+
"html_url": "https://github.com/CalebFenton",
52+
"followers_url": "https://api.github.com/users/CalebFenton/followers",
53+
"following_url": "https://api.github.com/users/CalebFenton/following{/other_user}",
54+
"gists_url": "https://api.github.com/users/CalebFenton/gists{/gist_id}",
55+
"starred_url": "https://api.github.com/users/CalebFenton/starred{/owner}{/repo}",
56+
"subscriptions_url": "https://api.github.com/users/CalebFenton/subscriptions",
57+
"organizations_url": "https://api.github.com/users/CalebFenton/orgs",
58+
"repos_url": "https://api.github.com/users/CalebFenton/repos",
59+
"events_url": "https://api.github.com/users/CalebFenton/events{/privacy}",
60+
"received_events_url": "https://api.github.com/users/CalebFenton/received_events",
61+
"type": "User",
62+
"site_admin": false
63+
},
64+
"parents": [
65+
{
66+
"sha": "5d8997a715edefdd4d5404f5ac53fc44b1fe2f64",
67+
"url": "https://api.github.com/repos/CalebFenton/simplify/commits/5d8997a715edefdd4d5404f5ac53fc44b1fe2f64",
68+
"html_url": "https://github.com/CalebFenton/simplify/commit/5d8997a715edefdd4d5404f5ac53fc44b1fe2f64"
69+
}
70+
]
71+
}
72+
]

0 commit comments

Comments
 (0)