From 0a78782b01b80a46857c2b417dc93b6bcec760f8 Mon Sep 17 00:00:00 2001 From: Jeremy Shearer Date: Tue, 16 Jul 2013 14:51:11 -0500 Subject: [PATCH] update test suite so that it passes update rspec syntax to remove deprecation warnings Conflicts: Gemfile Rakefile spec/spec_helper.rb spec/support/custom_matchers.rb spec/wrest/components/translators/json_spec.rb spec/wrest/curl/response_spec.rb spec/wrest/http/response_spec.rb spec/wrest/http_codes_spec.rb spec/wrest/native/redirection_spec.rb spec/wrest/native/request_spec.rb spec/wrest/native/response_spec.rb spec/wrest/uri_spec.rb --- Gemfile | 6 +- Rakefile | 14 ++--- .../spec/lib/models/facebook_client_spec.rb | 20 +++--- .../spec/lib/models/facebook_user_spec.rb | 8 +-- lib/wrest/test/request_patches.rb | 2 +- spec/wrest/cache_proxy_spec.rb | 10 +-- .../container/alias_accessors_spec.rb | 2 +- spec/wrest/components/mutators/base_spec.rb | 2 +- .../wrest/components/translators/json_spec.rb | 26 ++++---- spec/wrest/components/translators/txt_spec.rb | 4 +- spec/wrest/components/translators/xml_spec.rb | 18 +++--- spec/wrest/curl/response_spec.rb | 26 ++++---- spec/wrest/http/response_spec.rb | 4 +- spec/wrest/http_codes_spec.rb | 10 +-- spec/wrest/native/redirection_spec.rb | 26 ++++---- spec/wrest/native/request_spec.rb | 62 +++++++++---------- spec/wrest/native/response_spec.rb | 20 +++--- spec/wrest/native/session_spec.rb | 18 +++--- spec/wrest/uri_spec.rb | 8 +-- 19 files changed, 143 insertions(+), 143 deletions(-) diff --git a/Gemfile b/Gemfile index 713eb32f..10911551 100644 --- a/Gemfile +++ b/Gemfile @@ -17,15 +17,15 @@ group :libcurl_support do end group :nokogiri do - gem 'nokogiri', '~> 1' + gem 'nokogiri' end group :libxml do platforms :ruby do - gem 'libxml-ruby', '~> 2' unless Object.const_defined?('RUBY_ENGINE') && RUBY_ENGINE =~ /rbx/ + gem 'libxml-ruby' unless Object.const_defined?('RUBY_ENGINE') && RUBY_ENGINE =~ /rbx/ end end - + group :jrexml do platforms :jruby do gem 'jrexml', '~> 0.5.3' diff --git a/Rakefile b/Rakefile index 9f897753..1b07a76e 100644 --- a/Rakefile +++ b/Rakefile @@ -47,7 +47,7 @@ namespace :rspec do ENV["wrest_functional_spec"] = "true" Rake::Task["rspec:spec_runner"].invoke end - + RSpec::Core::RakeTask.new(:spec_runner) do |task| task.pattern = 'spec/wrest/**/*_spec.rb' end @@ -91,7 +91,7 @@ namespace (:benchmark) do class Ooga < Wrest::Resource::Base end - class Booga < ActiveResource::Base + class Booga < ActiveResource::Base self.site='' end end @@ -217,8 +217,8 @@ namespace (:benchmark) do } end end - end - + end + desc "Benchmark keepalive connections (needs functional/sample_rails_app running with class-caching on and keep-alive enabled)" task :keep_alive => :setup_test_classes do n = 20 @@ -230,7 +230,7 @@ namespace (:benchmark) do 'http://localhost:3000/lead_bottles.xml?owner=Kai&type=bottle'.to_uri.get } end - + rpt.report("Keep-alive connection (Connection: Keep-Alive)") do Wrest::Native::Session.new('http://localhost:3000'.to_uri) do |session| n.times { @@ -246,7 +246,7 @@ namespace (:benchmark) do task :deserialise_xml => :setup_test_classes do |t| n = 100 puts "Deserialising using #{ActiveSupport::XmlMini.backend}" - + Benchmark.bmbm(10) do |rpt| rpt.report("Hash.from_xml") do n.times { @@ -255,7 +255,7 @@ namespace (:benchmark) do end end end - + def serialised_data <<-EOXML diff --git a/examples/facebook_auth/spec/lib/models/facebook_client_spec.rb b/examples/facebook_auth/spec/lib/models/facebook_client_spec.rb index 0ca54783..f4f915f4 100644 --- a/examples/facebook_auth/spec/lib/models/facebook_client_spec.rb +++ b/examples/facebook_auth/spec/lib/models/facebook_client_spec.rb @@ -12,34 +12,34 @@ params["redirect_uri"].should == "http://redirect_uri" params["client_id"].should_not be_nil end - + it "should exchange authentication code for the access token" do uri_string = "http://graph.facebook.com" - + FacebookClient::Config.should_receive(:[]).with("client_id").and_return("id") FacebookClient::Config.should_receive(:[]).with("client_secret").and_return("secret") FacebookClient::Config.should_receive(:[]).with("facebook_uri").and_return(uri_string) - - facebook_uri = mock(Wrest::Uri) - access_token_uri = mock(Wrest::Uri) + + facebook_uri = double(Wrest::Uri) + access_token_uri = double(Wrest::Uri) uri_string.should_receive(:to_uri).and_return(facebook_uri) facebook_uri.should_receive(:[]).with('/oauth/access_token').and_return(access_token_uri) - response = mock("Response", :body => 'access_token=access_token') + response = double("Response", :body => 'access_token=access_token') request_params = {:client_id => "id", :redirect_uri => "http://redirect_uri", :client_secret => "secret", :code => "auth_code"} access_token_uri.should_receive(:post_form).with(request_params).and_return(response) client.acquire_access_token("http://redirect_uri","auth_code").should == "access_token" end - + context "authorized access" do it "should get a resource at the given path using access token" do uri_string = "http://graph.facebook.com" FacebookClient::Config.should_receive(:[]).with("facebook_uri").and_return(uri_string) - facebook_uri = mock(Wrest::Uri) - get_uri = mock(Wrest::Uri) + facebook_uri = double(Wrest::Uri) + get_uri = double(Wrest::Uri) uri_string.should_receive(:to_uri).and_return(facebook_uri) facebook_uri.should_receive(:[]).with('/me').and_return(get_uri) - response = mock("Response", :body => 'body') + response = double("Response", :body => 'body') get_uri.should_receive(:get).with(:access_token => "access_token").and_return(response) client.authorized_get("/me", "access_token").body.should == 'body' end diff --git a/examples/facebook_auth/spec/lib/models/facebook_user_spec.rb b/examples/facebook_auth/spec/lib/models/facebook_user_spec.rb index be02f008..698a4bf1 100644 --- a/examples/facebook_auth/spec/lib/models/facebook_user_spec.rb +++ b/examples/facebook_auth/spec/lib/models/facebook_user_spec.rb @@ -1,24 +1,24 @@ require 'spec_helper' describe FacebookUser do - + context "authenticated" do it "should not be authenticated if access token is not available" do user = FacebookUser.new("") user.should_not be_authenticated end - + it "should be authenticate if access token is available" do user = FacebookUser.new("access_token") user.should be_authenticated end end - + it "should fetch profile using access token" do user = FacebookUser.new("access_token") client = FacebookClient.new FacebookClient.should_receive(:new).and_return(client) - response = mock("Response", :deserialise => {:name => "Booga"}) + response = double("Response", :deserialise => {:name => "Booga"}) client.should_receive(:authorized_get).with("/me", "access_token").and_return(response) profile = user.profile profile.name.should == "Booga" diff --git a/lib/wrest/test/request_patches.rb b/lib/wrest/test/request_patches.rb index 8358b342..d48ba01a 100644 --- a/lib/wrest/test/request_patches.rb +++ b/lib/wrest/test/request_patches.rb @@ -1,5 +1,5 @@ class Wrest::Native::Request def invoke - raise Wrest::Exceptions::RealRequestMadeInTestEnvironmet, 'A real HTTP request was made while running tests. Please avoid using live HTTP connections while testing and replace them with mocks.' + raise Wrest::Exceptions::RealRequestMadeInTestEnvironmet, 'A real HTTP request was made while running tests. Please avoid using live HTTP connections while testing and replace them with doubles.' end end diff --git a/spec/wrest/cache_proxy_spec.rb b/spec/wrest/cache_proxy_spec.rb index bf8ffe91..39ba18d1 100644 --- a/spec/wrest/cache_proxy_spec.rb +++ b/spec/wrest/cache_proxy_spec.rb @@ -127,7 +127,7 @@ # 304 is Not Modified it "should use the cached response if the server returns 304" do not_modified_response = @ok_response.clone - not_modified_response.should_receive(:code).any_number_of_times.and_return('304') + not_modified_response.stub(:code).and_return('304') @cache.should_receive(:[]).with(@get.hash).and_return(@cached_response) @cache_proxy.should_receive(:send_validation_request_for).and_return(not_modified_response) @@ -143,7 +143,7 @@ it "should call update_cache_headers" do not_modified_response = @ok_response.clone - not_modified_response.should_receive(:code).any_number_of_times.and_return('304') + not_modified_response.stub(:code).and_return('304') @cache.should_receive(:[]).with(@get.hash).and_return(@cached_response) @cache_proxy.should_receive(:send_validation_request_for).and_return(not_modified_response) @@ -178,10 +178,10 @@ end end end - + it "should use it if the server returns a new response" do new_response = Wrest::Native::Response.new(build_ok_response('', cacheable_headers())) - new_response.should_receive(:code).any_number_of_times.and_return('200') + new_response.stub(:code).and_return('200') @cache.should_receive(:[]).with(@get.hash).and_return(@cached_response) @cache_proxy.should_receive(:send_validation_request_for).and_return(new_response) @@ -191,7 +191,7 @@ it "should also cache it when the server returns a new response" do new_response = Wrest::Native::Response.new(build_ok_response('', cacheable_headers())) - new_response.should_receive(:code).any_number_of_times.and_return('200') + new_response.stub(:code).and_return('200') @cache.should_receive(:[]).with(@get.hash).and_return(@cached_response) @cache_proxy.should_receive(:send_validation_request_for).and_return(new_response) diff --git a/spec/wrest/components/container/alias_accessors_spec.rb b/spec/wrest/components/container/alias_accessors_spec.rb index 1538bfc0..a6287134 100644 --- a/spec/wrest/components/container/alias_accessors_spec.rb +++ b/spec/wrest/components/container/alias_accessors_spec.rb @@ -10,7 +10,7 @@ module Wrest::Components end it "should provide a macro to enable aliasing accessors" do - lambda{ @Demon.class_eval{ alias_accessors :shiriki => :chambala } }.should_not raise_error(NoMethodError) + lambda{ @Demon.class_eval{ alias_accessors :shiriki => :chambala } }.should_not raise_error() end describe 'aliasing' do diff --git a/spec/wrest/components/mutators/base_spec.rb b/spec/wrest/components/mutators/base_spec.rb index c7deace1..b34b2dd1 100644 --- a/spec/wrest/components/mutators/base_spec.rb +++ b/spec/wrest/components/mutators/base_spec.rb @@ -34,7 +34,7 @@ module Wrest::Components }]] ).should == ["result", {"publish_date" => "1240326000", "news_source" => {"online"=>"PC via News", "unique_id"=>1}}] end - + it "should register all subclasses in the registry" do class SomeMutator < Mutators::Base; end Mutators::REGISTRY[:some_mutator].should == SomeMutator diff --git a/spec/wrest/components/translators/json_spec.rb b/spec/wrest/components/translators/json_spec.rb index d2713564..eb9726bd 100644 --- a/spec/wrest/components/translators/json_spec.rb +++ b/spec/wrest/components/translators/json_spec.rb @@ -3,24 +3,24 @@ module Wrest::Components::Translators describe Json do let(:http_response) { double('Http Reponse') } - + it "should know how to convert json to a hashmap" do - http_response.should_receive(:body).and_return("{ - \"menu\": \"File\", - \"commands\": [ + http_response.should_receive(:body).and_return("{ + \"menu\": \"File\", + \"commands\": [ { - \"title\": \"New\", + \"title\": \"New\", \"action\":\"CreateDoc\" - }, + }, { - \"title\": \"Open\", + \"title\": \"Open\", \"action\": \"OpenDoc\" - }, + }, { \"title\": \"Close\", \"action\": \"CloseDoc\" } - ] + ] }") result = { "commands"=>[{"title"=>"New", "action"=>"CreateDoc"}, @@ -28,12 +28,12 @@ module Wrest::Components::Translators "menu"=>"File"} Json.deserialise(http_response).should eq(result) end - + it "should know how to convert json to a hashmap" do hash = { "menu"=>"File", "commands"=>[{ - "title"=>"New", + "title"=>"New", "action"=>"CreateDoc"}, { "title"=>"Open", @@ -43,12 +43,12 @@ module Wrest::Components::Translators Json.serialise(hash).should include("\"menu\":\"File\"") Json.serialise(hash).should include("\"commands\":[{\"title\":\"New\",\"action\":\"CreateDoc\"},{\"title\":\"Open\",\"action\":\"OpenDoc\"},{\"title\":\"Close\",\"action\":\"CloseDoc\"}]") end - + it "has #deserialize delegate to #deserialise" do Json.should_receive(:deserialise).with(http_response, :option => :something) Json.deserialize(http_response, :option => :something) end - + it "has #serialize delegate to #serialise" do Json.should_receive(:serialise).with({ :hash => :foo }, :option => :something) Json.serialize({:hash => :foo}, :option => :something) diff --git a/spec/wrest/components/translators/txt_spec.rb b/spec/wrest/components/translators/txt_spec.rb index caf0eb54..4ee38de4 100644 --- a/spec/wrest/components/translators/txt_spec.rb +++ b/spec/wrest/components/translators/txt_spec.rb @@ -12,12 +12,12 @@ module Wrest::Components::Translators it "should return string version of any object when serialise" do Txt.serialise({"ooga"=>{"age" => "12"}}).should == "{\"ooga\"=>{\"age\"=>\"12\"}}" end - + it "has #deserialize delegate to #deserialise" do Txt.should_receive(:deserialise).with(http_response, :option => :something) Txt.deserialize(http_response, :option => :something) end - + it "has #serialize delegate to #serialise" do Txt.should_receive(:serialise).with({ :hash => :foo }, :option => :something) Txt.serialize({:hash => :foo}, :option => :something) diff --git a/spec/wrest/components/translators/xml_spec.rb b/spec/wrest/components/translators/xml_spec.rb index 7b7bb7d1..266977cc 100644 --- a/spec/wrest/components/translators/xml_spec.rb +++ b/spec/wrest/components/translators/xml_spec.rb @@ -12,39 +12,39 @@ module Wrest::Components::Translators it "should know how to convert a hashmap to xml" do Xml.serialise({"ooga"=>{"age" => "12"}}).should == "\n\n \n 12\n \n\n" end - + it "should call filter only if xpath is specified" do http_response.should_receive(:body) ActiveSupport::XmlMini.should_receive(:filter) Xml.deserialise(http_response,{:xpath=>'//age'}) end - + it "should not call filter if xpath is not specified" do http_response.should_receive(:body).and_return("Nikhil
Bangalore
") Xml.should_not_receive(:filter) Xml.deserialise(http_response) end - - Helpers.xml_backends.each do |e| + + Helpers.xml_backends.each do |e| it "should be able to pull out desired elements from an xml response based on xpath and return an array of matching nodes" do ActiveSupport::XmlMini.backend = e - + http_response.should_receive(:body).and_return("Nikhil
Bangalore
") - + res_arr = Xml.deserialise(http_response,{:xpath=>'//Name'}) result = "" res_arr.each { |a| result+= a.to_s.gsub(/[\n]+/, "").gsub(/\s/, '')} - result.should == "NikhilBangalore" + result.should == "NikhilBangalore" end end - + it "has #deserialize delegate to #deserialise" do Xml.should_receive(:deserialise).with(http_response, :option => :something) Xml.deserialize(http_response, :option => :something) end - + it "has #serialize delegate to #serialise" do Xml.should_receive(:serialise).with({ :hash => :foo }, :option => :something) Xml.serialize({:hash => :foo}, :option => :something) diff --git a/spec/wrest/curl/response_spec.rb b/spec/wrest/curl/response_spec.rb index 93a0ec32..f6b3ffa4 100644 --- a/spec/wrest/curl/response_spec.rb +++ b/spec/wrest/curl/response_spec.rb @@ -6,19 +6,19 @@ module Wrest context 'Aliased methods' do it "has #deserialize delegate to #deserialise" do response = Wrest::Curl::Response.new(double('Response', :headers => {'Content-type' => 'text/xml;charset=utf-8'})) - + response.should_receive(:deserialise) response.deserialize end it "has #deserialize_using delegate to #deserialise_using" do response = Wrest::Curl::Response.new(double('Response', :headers => {'Content-type' => 'text/xml;charset=utf-8'})) - + response.should_receive(:deserialise_using) response.deserialize_using end end - + describe 'Headers' do it "should know how to retrieve content type irrespective of the casing" do http_response = double('Patron::Response') @@ -32,27 +32,27 @@ module Wrest http_response = double('response') http_response.stub(:code).and_return('200') http_response.should_receive(:body).and_return(<<-EOJS - { + { "menu": "File", - "commands": [ - { "title": "New", "action":"CreateDoc" }, - { "title": "Open", "action": "OpenDoc" }, - { "title": "Close", "action": "CloseDoc" } - ] + "commands": [ + { "title": "New", "action":"CreateDoc" }, + { "title": "Open", "action": "OpenDoc" }, + { "title": "Close", "action": "CloseDoc" } + ] } EOJS ) http_response.should_receive(:content_type).and_return('application/json') response = Native::Response.new(http_response) - + response.deserialise.should == { "commands"=>[{"title"=>"New", "action"=>"CreateDoc"}, {"title"=>"Open","action"=>"OpenDoc"},{"title"=>"Close", "action"=>"CloseDoc"}], "menu"=>"File"} end - - + + describe "cache deserialised body" do it "should return the catched deserialised body when deserialise is called more than once" do http_response = double('curl response') @@ -99,7 +99,7 @@ module Wrest @response.content_length.should == 172 end end - + it "should handle headers with multiple values where the values are made available in an array" do response = Wrest::Curl::Request.new('http://localhost:3000/multiple_response_headers'.to_uri, :get).invoke response.code.should == 200 diff --git a/spec/wrest/http/response_spec.rb b/spec/wrest/http/response_spec.rb index 8c895e2d..5bb0d550 100644 --- a/spec/wrest/http/response_spec.rb +++ b/spec/wrest/http/response_spec.rb @@ -3,7 +3,7 @@ module Wrest libraries = [Wrest::Native] libraries << Wrest::Curl unless RUBY_PLATFORM =~ /java/ - + libraries.each do |library| describe "For #{library}" do describe 'Response' do @@ -13,7 +13,7 @@ module Wrest http_response.stub(:headers).and_return({'Content-type' => 'application/xml'}) http_response.stub(:code).and_return('200') http_response.stub(:content_type).and_return('application/xml') - + response = library::Response.new(http_response) response.content_type.should == 'application/xml' end diff --git a/spec/wrest/http_codes_spec.rb b/spec/wrest/http_codes_spec.rb index 8ed30861..d475aa5a 100644 --- a/spec/wrest/http_codes_spec.rb +++ b/spec/wrest/http_codes_spec.rb @@ -4,25 +4,25 @@ module Wrest describe HttpCodes do http_backends = {"Wrest::Native::Response" => "Net::HTTPResponse"} http_backends["Wrest::Curl::Response"] = "Patron::Response" unless RUBY_PLATFORM =~ /java/ - - http_backends.each do |klass, mock_class| + + http_backends.each do |klass, double_class| {"200" => "OK", "201" => "CREATED", "202" => "ACCEPTED", "204" => "NO CONTENT", "301" => "MOVED PERMANENTLY", "302" => "FOUND", "303" => "SEE OTHER", "304" => "NOT MODIFIED", "307" => "TEMPORARY REDIRECT", "400" => "BAD REQUEST", "401" => "UNAUTHORIZED", "403" => "FORBIDDEN", "404" => "NOT FOUND", "405" => "METHOD_NOT_ALLOWED", "406" => "NOT_ACCEPTABLE", "422" => "UNPROCESSABLE ENTITY", "500" => "INTERNAL SERVER ERROR"}.each do |status, status_message| it "should know if the response code is HTTP #{status_message} for #{klass} object" do code = status method = (status_message.split.join('_').downcase + "?").to_sym - net_response = double(mock_class) + net_response = double(double_class) net_response.stub(:code).and_return(code) net_response.stub(:headers).and_return({}) net_response.stub(:status).and_return(code) - klass.constantize.send(:new, net_response).send(method).should be_true + klass.constantize.send(:new, net_response).send(method).should be_true end it "should know if the response code is not HTTP #{status_message} for #{klass} object" do code = (status.to_i + 1).to_s method = (status_message.split.join('_').downcase + "?").to_sym - net_response = double(mock_class) + net_response = double(double_class) net_response.stub(:code).and_return(code) net_response.stub(:headers).and_return({}) net_response.stub(:status).and_return(code) diff --git a/spec/wrest/native/redirection_spec.rb b/spec/wrest/native/redirection_spec.rb index 54735d4a..8ef8ddc1 100644 --- a/spec/wrest/native/redirection_spec.rb +++ b/spec/wrest/native/redirection_spec.rb @@ -1,26 +1,26 @@ require "spec_helper" describe Wrest::Native::Redirection do - + it "should make a request to the url in its location header and return the response" do - mock_net_http_response = double(Net::HTTPRedirection) + double_net_http_response = double(Net::HTTPRedirection) redirect_url = 'http://redirect.com' redirect_uri = redirect_url.to_uri - mock_net_http_response.should_receive(:to_hash).and_return('location' => redirect_url) - mock_net_http_response.should_receive(:code).and_return('200') - + double_net_http_response.should_receive(:to_hash).and_return('location' => redirect_url) + double_net_http_response.should_receive(:code).and_return('200') + Wrest::Uri.should_receive(:new).with(redirect_url, anything).and_return(redirect_uri) - + after_redirect_request = Wrest::Native::Get.new(redirect_uri) - final_mock_response = double(Wrest::Native::Response) - after_redirect_request.should_receive(:invoke).and_return(final_mock_response) - + final_double_response = double(Wrest::Native::Response) + after_redirect_request.should_receive(:invoke).and_return(final_double_response) + Wrest::Native::Get.should_receive(:new).with(redirect_uri, {}, {}, {:username=>nil, :password=>nil}).and_return(after_redirect_request) - - response = Wrest::Native::Redirection.new(mock_net_http_response) - response.follow(:follow_redirects_count => 0, :follow_redirects_limit => 5).should == final_mock_response + + response = Wrest::Native::Redirection.new(double_net_http_response) + response.follow(:follow_redirects_count => 0, :follow_redirects_limit => 5).should == final_double_response end - + it "should raise a Wrest::Exceptions::AutoRedirectLimitExceeded if there are more redirections than the limit" do # For n redirections, there will be n+1 Http requests. The last one being diff --git a/spec/wrest/native/request_spec.rb b/spec/wrest/native/request_spec.rb index 759aaeab..54ea717e 100644 --- a/spec/wrest/native/request_spec.rb +++ b/spec/wrest/native/request_spec.rb @@ -12,39 +12,39 @@ describe Wrest::Native::Request do it "should convert all symbols in header keys to strings" do Wrest::Native::Request.new( - 'http://localhost/foo'.to_uri, Net::HTTP::Get, {}, + 'http://localhost/foo'.to_uri, Net::HTTP::Get, {}, nil, 'Content-Type' => 'application/xml', :per_page => '10' ).headers.should == { 'Content-Type' => 'application/xml', 'per_page' => '10' } end - + it "should default the 'follow_redirects' option to true for a Get" do Wrest::Native::Get.new('http://localhost/foo'.to_uri).follow_redirects.should be_true end - + it "should default the 'follow_redirects_count' option to 0" do Wrest::Native::Get.new('http://localhost/foo'.to_uri).follow_redirects_count.should == 0 end - + it "should default the 'follow_redirects_limit' option to 5" do Wrest::Native::Get.new('http://localhost/foo'.to_uri).follow_redirects_limit.should == 5 end - + it "should allow Gets to disable redirect follows" do Wrest::Native::Get.new('http://localhost/foo'.to_uri, {}, {}, {:follow_redirects => false}).follow_redirects.should be_false end - + it "should increment the follow_redirects_count for every redirect leving the count unaffected in previous requests" do uri = 'http://localhost/foo'.to_uri request = Wrest::Native::Get.new(uri) redirect_location = 'http://coathangers.com' redirected_request = double('Request to http://coathangers.com') - mock_connection = double('Http Connection') - uri.stub(:create_connection).and_return(mock_connection) - + double_connection = double('Http Connection') + uri.stub(:create_connection).and_return(double_connection) + raw_response = double(Net::HTTPRedirection) raw_response.stub(:code).and_return('301') raw_response.stub(:message).and_return('') @@ -53,24 +53,24 @@ response = Wrest::Native::Redirection.new(raw_response) response.stub(:[]).with('location').and_return(redirect_location) - mock_connection.should_receive(:request).and_return(raw_response) - mock_connection.should_receive(:set_debug_output) - + double_connection.should_receive(:request).and_return(raw_response) + double_connection.should_receive(:set_debug_output) + Wrest::Native::Response.should_receive(:new).and_return(response) redirected_request.stub(:get) Wrest::Uri.should_receive(:new).with(redirect_location, hash_including(:follow_redirects_count=>1)).and_return(redirected_request) - + request.invoke request.follow_redirects_count.should == 0 end - + it "should not set basic authentication for request if either of username or password is nil" do uri = 'http://localhost/foo'.to_uri request = Wrest::Native::Get.new(uri) http_request = double(Net::HTTP::Get, :method => "GET", :hash => {}) http_request.should_not_receive(:basic_auth) - request.should_receive(:http_request).any_number_of_times.and_return(http_request) - request.should_receive(:do_request).and_return(double(Net::HTTPOK, :code => "200", :message => 'OK', :body => '', :to_hash => {})) + request.stub(:http_request).and_return(http_request) + request.stub(:do_request).and_return(double(Net::HTTPOK, :code => "200", :message => 'OK', :body => '', :to_hash => {})) request.invoke end @@ -79,8 +79,8 @@ request = Wrest::Native::Get.new(uri, {}, {}, {:username => "name", :password => "password"}) http_request = double(Net::HTTP::Get, :method => "GET", :hash => {}) http_request.should_receive(:basic_auth).with('name', 'password') - request.should_receive(:http_request).any_number_of_times.and_return(http_request) - request.should_receive(:do_request).and_return(double(Net::HTTPOK, :code => "200", :message => 'OK', :body => '', :to_hash => {})) + request.stub(:http_request).and_return(http_request) + request.stub(:do_request).and_return(double(Net::HTTPOK, :code => "200", :message => 'OK', :body => '', :to_hash => {})) request.invoke end @@ -89,39 +89,39 @@ Wrest::Native::Put.new('http://localhost/foo'.to_uri).follow_redirects.should_not be_true Wrest::Native::Delete.new('http://localhost/foo'.to_uri).follow_redirects.should_not be_true end - + context "SSL options" do let(:uri){ 'https://localhost/foo'.to_uri } let(:http_request){ double(Net::HTTP::Get, :method => "GET") } def setup_request_expectations(request) request.tap do |r| - request.should_receive(:http_request).any_number_of_times.and_return(http_request) - request.should_receive(:do_request).and_return(double(Net::HTTPOK, :code => "200", :message => 'OK', :body => '', :to_hash => {})) + request.stub(:http_request).and_return(http_request) + request.stub(:do_request).and_return(double(Net::HTTPOK, :code => "200", :message => 'OK', :body => '', :to_hash => {})) end end - + it "should have verification mode for https set to VERIFY_PEER by default" do request = setup_request_expectations(Wrest::Native::Get.new(uri, {}, {})) request.invoke request.connection.verify_mode.should == OpenSSL::SSL::VERIFY_PEER end - + it "should have verification mode for https set to VERIFY_NONE when passed as an option" do request = setup_request_expectations(Wrest::Native::Get.new(uri, {}, {}, :verify_mode => OpenSSL::SSL::VERIFY_NONE)) - + request.invoke request.connection.verify_mode.should == OpenSSL::SSL::VERIFY_NONE end it "should have the certificate authority path set when the ca_path option is passed" do request = setup_request_expectations(Wrest::Native::Get.new(uri, {}, {}, :ca_path => '/etc/ssl/certs')) - + request.invoke request.connection.ca_path.should == '/etc/ssl/certs' end end - + it "should not store response in cache if the original request was not GET" do cache = {} post = Wrest::Native::Post.new("http://localhost".to_uri, {}, {}, cacheable_headers, {:cache_store => cache}) @@ -133,7 +133,7 @@ def setup_request_expectations(request) context "callbacks" do it "should run the appropriate callbacks" do - cb = mock + cb = double cb.should_receive(:cb_204) cb.should_receive(:cb_200) cb.should_receive(:cb_range) @@ -167,17 +167,17 @@ def setup_request_expectations(request) it "should correctly use the detailed_http_logging option" do logger = double(Logger) logger.should_receive(:<<).at_least(:once).with {|detailed_log| detailed_log.include? "opening connection to"} - logger.should_receive(:<<).any_number_of_times + logger.stub(:<<) uri = "http://localhost:3000/glassware".to_uri :detailed_http_logging => logger uri.get end - + it "should raise a Wrest exception on timeout" do - lambda{ - Wrest::Native::Request.new('http://localhost:3000/two_seconds'.to_uri, Net::HTTP::Get, {}, '', {}, :timeout => 1).invoke + lambda{ + Wrest::Native::Request.new('http://localhost:3000/two_seconds'.to_uri, Net::HTTP::Get, {}, '', {}, :timeout => 1).invoke }.should raise_error(Wrest::Exceptions::Timeout) end end diff --git a/spec/wrest/native/response_spec.rb b/spec/wrest/native/response_spec.rb index a382de89..54134d68 100644 --- a/spec/wrest/native/response_spec.rb +++ b/spec/wrest/native/response_spec.rb @@ -5,14 +5,14 @@ module Wrest context 'Aliased methods' do it "has #deserialize delegate to #deserialise" do response = Wrest::Native::Response.new(double('Response', :code => '200')) - + response.should_receive(:deserialise) response.deserialize end it "has #deserialize_using delegate to #deserialise_using" do response = Wrest::Native::Response.new(double('Response', :code => '200')) - + response.should_receive(:deserialise_using) response.deserialize_using end @@ -56,24 +56,24 @@ module Wrest it "should build a Redirection instead of a normal response if the code is 301..303 or 305..3xx" do http_response = double(Net::HTTPRedirection) http_response.stub(:code).and_return('301') - + Native::Response.new(http_response).class.should == Wrest::Native::Redirection end it "should build a normal response if the code is 304" do http_response = double(Net::HTTPRedirection) http_response.stub(:code).and_return('304') - + Native::Response.new(http_response).class.should == Wrest::Native::Response end - + it "should build a normal Response for non 3xx codes" do http_response = double(Net::HTTPResponse) http_response.stub(:code).and_return('200') - + Native::Response.new(http_response).class.should == Wrest::Native::Response end - + it "should know how to delegate to a translator" do http_response = double('response') http_response.stub(:code).and_return('200') @@ -102,7 +102,7 @@ module Wrest http_response.should_receive(:content_type).and_return('application/json') response = Native::Response.new(http_response) - + response.deserialise.should == { "commands"=>[{"title"=>"New", "action"=>"CreateDoc"}, {"title"=>"Open","action"=>"OpenDoc"},{"title"=>"Close", @@ -159,7 +159,7 @@ module Wrest http_response = Native::Response.new(build_ok_response('', cacheable_headers.merge("Cache-Control" => "abc,test=100,max-age=20"))) http_response.cache_control_headers.should == ["abc", "test=100", "max-age=20"] end - + it "should parse the cache-control header when it has leading and trailing spaces" do http_response = Native::Response.new(build_ok_response('', cacheable_headers.merge("Cache-Control" => " abc, test=100 , max-age=20 "))) http_response.cache_control_headers.should == ["abc", "test=100", "max-age=20"] @@ -365,7 +365,7 @@ module Wrest response = Native::Response.new(build_ok_response('', @headers)) response.expires_not_in_our_past?.should == true end - + it "should say not expired for requests with Expires in the future" do response = Native::Response.new(build_ok_response('', @headers)) response.expired?.should == false diff --git a/spec/wrest/native/session_spec.rb b/spec/wrest/native/session_spec.rb index c0164089..603e853d 100644 --- a/spec/wrest/native/session_spec.rb +++ b/spec/wrest/native/session_spec.rb @@ -7,11 +7,11 @@ module Wrest uri = "http://localhost:3000" Native::Session.new(uri).uri.should == uri.to_uri end - + it "should accept a Wrest::Uri" do uri = "http://localhost:3000" Native::Session.new(uri.to_uri).uri.should == uri.to_uri - end + end end it "should know how to use the connection provided to make requests" do @@ -20,8 +20,8 @@ module Wrest http = double(Net::HTTP) Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http) - http.should_receive(:read_timeout=).with(60) - http.should_receive(:set_debug_output).any_number_of_times + http.stub(:read_timeout=).with(60) + http.stub(:set_debug_output) request_one = Net::HTTP::Get.new('/glassware?owner=Kai&type=bottle', {H::Connection=>T::KeepAlive}) request_two = Net::HTTP::Get.new('/bottles.xml', {H::Connection=>T::KeepAlive}) @@ -29,11 +29,11 @@ module Wrest Net::HTTP::Get.should_receive(:new).with('/glassware?owner=Kai&type=bottle', {H::Connection=>T::KeepAlive}).and_return(request_one) Net::HTTP::Get.should_receive(:new).with('/bottles.xml', {H::Connection=>T::KeepAlive}).and_return(request_two) - # TODO: Discuss whether these changes would be appropriate. Were made since the Headers responsibility was + # TODO: Discuss whether these changes would be appropriate. Were made since the Headers responsibility was # totally moved to Wrest::Native directly instead of using the componenet Net::HTTP response object. ok_response = build_ok_response('', {Native::StandardHeaders::Connection => Native::StandardTokens::KeepAlive}) #ok_response.should_receive(:[]).with(Native::StandardHeaders::Connection).twice.and_return(Native::StandardTokens::KeepAlive) - + http.should_receive(:request).with(request_one, nil).and_return(ok_response) http.should_receive(:request).with(request_two, nil).and_return(ok_response) @@ -42,7 +42,7 @@ module Wrest session.get '/bottles.xml' end end - + it "should destroy the current connection if a response is returned with a Connection: Close" do uri = "http://localhost:3000".to_uri uri.should_not be_https @@ -65,7 +65,7 @@ module Wrest ok_response_with_connection_close = build_ok_response('', {Native::StandardHeaders::Connection => Native::StandardTokens::Close}) #ok_response_with_connection_close.should_receive(:[]).with(Native::StandardHeaders::Connection).once.and_return(Native::StandardTokens::Close) - + http.should_receive(:request).with(request_one, nil).and_return(ok_response) http.should_receive(:request).with(request_two, nil).and_return(ok_response_with_connection_close) http.should_receive(:set_debug_output).twice @@ -77,7 +77,7 @@ module Wrest session.instance_variable_get('@connection').should be_nil end end - + context "functional", :functional => true do it "should know how to use the connection provided to make requests" def cont diff --git a/spec/wrest/uri_spec.rb b/spec/wrest/uri_spec.rb index 1e5973f6..50539aa7 100644 --- a/spec/wrest/uri_spec.rb +++ b/spec/wrest/uri_spec.rb @@ -237,7 +237,7 @@ def setup_http http_request = double(Net::HTTP::Get, :method => "GET", :hash => {}) http_request.should_receive(:basic_auth).with('ooga', 'bar') - request.should_receive(:http_request).any_number_of_times.and_return(http_request) + request.stub(:http_request).and_return(http_request) request.should_receive(:do_request).and_return(double(Net::HTTPOK, :code => "200", :message => 'OK', :body => '', :to_hash => {})) uri.get end @@ -431,9 +431,9 @@ def setup_http uri = "http://localhost:3000/glassware".to_uri http = double(Net::HTTP) - Net::HTTP.should_receive(:new).with('localhost', 3000).any_number_of_times.and_return(http) - http.should_receive(:read_timeout=).any_number_of_times.with(60) - http.should_receive(:set_debug_output).any_number_of_times + Net::HTTP.stub(:new).with('localhost', 3000).and_return(http) + http.stub(:read_timeout=).with(60) + http.stub(:set_debug_output) request_get = Net::HTTP::Get.new('/glassware?owner=Kai&type=bottle', {'page' => '2', 'per_page' => '5'}) Net::HTTP::Get.should_receive(:new).with('/glassware?owner=Kai&type=bottle', {'page' => '2', 'per_page' => '5'}).and_return(request_get)