Skip to content

Commit

Permalink
[Specs] Use VRC gem for network tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiopelosin committed Dec 2, 2013
1 parent 1b77aa3 commit 2f028e0
Show file tree
Hide file tree
Showing 8 changed files with 541 additions and 53 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ group :development do
gem "mocha-on-bacon"
gem "rake"
gem 'prettybacon', :git => 'https://github.com/irrationalfab/PrettyBacon.git', :branch => 'master'
gem 'vcr'
gem 'webmock'
if RUBY_VERSION >= '1.9.3'
gem 'rubocop'
end
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GEM
activesupport (3.2.15)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
addressable (2.3.5)
ast (1.1.0)
awesome_print (1.2.0)
bacon (1.2.0)
Expand All @@ -40,6 +41,8 @@ GEM
simplecov (>= 0.7)
term-ansicolor
thor
crack (0.4.1)
safe_yaml (~> 0.9.0)
docile (1.1.1)
ffi (1.9.3)
fuzzy_match (2.0.4)
Expand Down Expand Up @@ -86,6 +89,7 @@ GEM
parser (~> 2.0)
powerpack (~> 0.0.6)
rainbow (>= 1.1.4)
safe_yaml (0.9.7)
simplecov (0.8.2)
docile (~> 1.1.0)
multi_json
Expand All @@ -96,6 +100,10 @@ GEM
tins (~> 0.8)
thor (0.18.1)
tins (0.13.1)
vcr (2.5.0)
webmock (1.8.11)
addressable (>= 2.2.7)
crack (>= 0.1.7)
yajl-ruby (1.1.0)
yard (0.8.7.3)

Expand All @@ -119,4 +127,6 @@ DEPENDENCIES
rb-fsevent
redcarpet
rubocop
vcr
webmock
yard
12 changes: 10 additions & 2 deletions lib/cocoapods-core/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,16 @@ def self.repo_id_from_url(url)
def self.peform_request(url)
require 'rest'
require 'json'
response = REST.get(url)
JSON.parse(response.body)
headers = { "User-Agent" => "CocoaPods" }
response = REST.get(url, headers)
body = JSON.parse(response.body)
if response.ok?
body
else
CoreUI.warn "Request to #{url} failed - #{response.status_code}"
CoreUI.warn body['message']
nil
end
end

#-------------------------------------------------------------------------#
Expand Down
300 changes: 300 additions & 0 deletions spec/fixtures/vcr_cassettes/GitHub.yml

Large diffs are not rendered by default.

159 changes: 159 additions & 0 deletions spec/fixtures/vcr_cassettes/Specification_Set_Statistics.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 45 additions & 47 deletions spec/github_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,80 @@
module Pod
describe GitHub do

def stub_github_request(url, response_body)
require 'rest'
response = mock(:body => response_body)
REST.expects(:get).with(url).returns(response)
end

describe "In general" do
it "returns the information of a user" do
stub_github_request('https://api.github.com/users/CocoaPods', '{"login":"CocoaPods"}')
user = GitHub.user("CocoaPods")
user['login'].should == 'CocoaPods'
VCR.use_cassette('GitHub', :record => :new_episodes) do
user = GitHub.user("CocoaPods")
user['login'].should == 'CocoaPods'
end
end

it "returns the information of a repo" do
stub_github_request('https://api.github.com/repos/CocoaPods/CocoaPods', '{"name":"CocoaPods"}')
repo = GitHub.repo("https://github.com/CocoaPods/CocoaPods")
repo['name'].should == 'CocoaPods'
VCR.use_cassette('GitHub', :record => :new_episodes) do
repo = GitHub.repo("https://github.com/CocoaPods/CocoaPods")
repo['name'].should == 'CocoaPods'
end
end

it "returns the tags of a repo" do
stub_github_request('https://api.github.com/repos/CocoaPods/CocoaPods/tags', '[{"name":"0.20.2"}]')
tags = GitHub.tags("https://github.com/CocoaPods/CocoaPods")
tags.find { |t| t["name"] == "0.20.2" }.should.not.be.nil
VCR.use_cassette('GitHub', :record => :new_episodes) do
tags = GitHub.tags("https://github.com/CocoaPods/CocoaPods")
tags.find { |t| t["name"] == "0.20.2" }.should.not.be.nil
end
end

it "returns the branches of a repo" do
stub_github_request('https://api.github.com/repos/CocoaPods/CocoaPods/branches', '[{"name":"master"}]')
branches = GitHub.branches("https://github.com/CocoaPods/CocoaPods")
branches.find { |t| t["name"] == "master" }.should.not.be.nil
VCR.use_cassette('GitHub', :record => :new_episodes) do
branches = GitHub.branches("https://github.com/CocoaPods/CocoaPods")
branches.find { |t| t["name"] == "master" }.should.not.be.nil
end
end
end

#-------------------------------------------------------------------------#

describe "Private helpers" do

describe "#normalized_repo_id" do

it "returns a repo id as it is" do
id = GitHub.send(:normalized_repo_id, "CocoaPods/CocoaPods")
id.should == "CocoaPods/CocoaPods"
describe "Additional behaviour" do
it "sets the user agent" do
VCR.use_cassette('GitHub', :record => :new_episodes) do
url = 'https://api.github.com/repos/CocoaPods/CocoaPods'
headers = {'User-Agent' => 'CocoaPods'}
response = stub(:body => '{}', :ok? => true)
REST.expects(:get).with(url, headers).returns(response)
repo = GitHub.repo("http://github.com/CocoaPods/CocoaPods")
end
end

it "convertns an url to a repo id" do
id = GitHub.send(:normalized_repo_id, "https://github.com/CocoaPods/CocoaPods")
id.should == "CocoaPods/CocoaPods"
it "supports URLs with the `http` protocol" do
VCR.use_cassette('GitHub', :record => :new_episodes) do
repo = GitHub.repo("http://github.com/CocoaPods/CocoaPods")
repo['name'].should == 'CocoaPods'
end

end

#----------------------------------------#

describe "#repo_id_from_url" do

it "returns the repo id from a given github URL" do
id = GitHub.send(:repo_id_from_url, "https://github.com/CocoaPods/CocoaPods")
id.should == "CocoaPods/CocoaPods"
it "supports the GitHub identifier instead of the URL" do
VCR.use_cassette('GitHub', :record => :new_episodes) do
repo = GitHub.repo("CocoaPods/CocoaPods")
repo['name'].should == 'CocoaPods'
end
end

it "returns the repo id from a given github https git URL" do
id = GitHub.send(:repo_id_from_url, "https://github.com/CocoaPods/CocoaPods.git")
id.should == "CocoaPods/CocoaPods"
it "returns nil if a requests fails" do
VCR.use_cassette('GitHub', :record => :new_episodes) do
repo = GitHub.repo("https://github.com/CocoaPods/Missing_Repo")
repo.should.be.nil
end
end

it "returns nil if the given url is not a valid github URL" do
id = GitHub.send(:repo_id_from_url, "CocoaPods/CocoaPods")
id.should.be.nil
it "prints a warning for failed requests" do
VCR.use_cassette('GitHub', :record => :new_episodes) do
GitHub.repo("https://github.com/CocoaPods/Missing_Repo")
CoreUI.warnings.should.match /Request to https:.*Missing_Repo failed - 404/
CoreUI.warnings.should.match /Not Found/
end

end

#----------------------------------------#
#-----------------------------------------------------------------------#

end

#-------------------------------------------------------------------------#

end
end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ def copy_fixture_to_pod(name, pod)
FileUtils.cp_r(path, pod.root)
end

# VCR
#--------------------------------------#

require 'vcr'
VCR.configure do |c|
c.cassette_library_dir = ROOT + 'spec/fixtures/vcr_cassettes'
c.hook_into :webmock
end

# Silence the output
#--------------------------------------#

Expand Down
10 changes: 6 additions & 4 deletions spec/specification/set/statistics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ module Pod
end

it "returns nil for GitHub based methods if the Pod is not hosted by GitHub" do
Specification.any_instance.stubs(:source).returns({:git => 'example.com/repo.git'})
@stats.github_watchers(@set).should == nil
@stats.github_forks(@set).should == nil
@stats.github_pushed_at(@set).should == nil
VCR.use_cassette('Specification::Set::Statistics', :record => :new_episodes) do
Specification.any_instance.stubs(:source).returns({:git => 'example.com/repo.git'})
@stats.github_watchers(@set).should == nil
@stats.github_forks(@set).should == nil
@stats.github_pushed_at(@set).should == nil
end
end
end

Expand Down

0 comments on commit 2f028e0

Please sign in to comment.