1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

Use 'double' instead of 'mock' about rspec-mock

'mock' is deprecated after rspec-mock 2.14.0.rc1(2013-05-27 released).
'mock' method may be removed for rspec-mock 3.0.0.

And, this fixes can remove following warnings.
DEPRECATION: mock is deprecated. Use double instead. Called from ...

(cherry picked from commit c0fd016dc3)

Conflicts:
	spec/request_spec.rb
This commit is contained in:
Kosuke Asami 2013-07-27 12:09:42 +09:00 committed by Larry Gilbert
parent d51cd69896
commit 4086123c31
6 changed files with 37 additions and 37 deletions

View file

@ -16,7 +16,7 @@ describe RestClient::AbstractResponse do
end
before do
@net_http_res = mock('net http response')
@net_http_res = double('net http response')
@response = MyAbstractResponse.new(@net_http_res, {})
end

View file

@ -35,7 +35,7 @@ end
describe RestClient::RequestFailed do
before do
@response = mock('HTTP Response', :code => '502')
@response = double('HTTP Response', :code => '502')
end
it "stores the http response on the exception" do

View file

@ -2,8 +2,8 @@ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
describe RestClient::RawResponse do
before do
@tf = mock("Tempfile", :read => "the answer is 42", :open => true)
@net_http_res = mock('net http response')
@tf = double("Tempfile", :read => "the answer is 42", :open => true)
@net_http_res = double('net http response')
@response = RestClient::RawResponse.new(@tf, @net_http_res, {})
end

View file

@ -7,13 +7,13 @@ describe RestClient::Request do
before do
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
@uri = mock("uri")
@uri = double("uri")
@uri.stub!(:request_uri).and_return('/resource')
@uri.stub!(:host).and_return('some')
@uri.stub!(:port).and_return(80)
@net = mock("net::http base")
@http = mock("net::http connection")
@net = double("net::http base")
@http = double("net::http connection")
Net::HTTP.stub!(:new).and_return(@net)
@net.stub!(:start).and_yield(@http)
@net.stub!(:use_ssl=)
@ -50,7 +50,7 @@ describe RestClient::Request do
end
it "processes a successful result" do
res = mock("result")
res = double("result")
res.stub!(:code).and_return("200")
res.stub!(:body).and_return('body')
res.stub!(:[]).with('content-encoding').and_return(nil)
@ -60,7 +60,7 @@ describe RestClient::Request do
it "doesn't classify successful requests as failed" do
203.upto(207) do |code|
res = mock("result")
res = double("result")
res.stub!(:code).and_return(code.to_s)
res.stub!(:body).and_return("")
res.stub!(:[]).with('content-encoding').and_return(nil)
@ -80,21 +80,21 @@ describe RestClient::Request do
describe "user - password" do
it "extracts the username and password when parsing http://user:password@example.com/" do
URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
URI.stub!(:parse).and_return(double('uri', :user => 'joe', :password => 'pass1'))
@request.parse_url_with_auth('http://joe:pass1@example.com/resource')
@request.user.should == 'joe'
@request.password.should == 'pass1'
end
it "extracts with escaping the username and password when parsing http://user:password@example.com/" do
URI.stub!(:parse).and_return(mock('uri', :user => 'joe%20', :password => 'pass1'))
URI.stub!(:parse).and_return(double('uri', :user => 'joe%20', :password => 'pass1'))
@request.parse_url_with_auth('http://joe%20:pass1@example.com/resource')
@request.user.should == 'joe '
@request.password.should == 'pass1'
end
it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
URI.stub!(:parse).and_return(double('uri', :user => nil, :password => nil))
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
@request.parse_url_with_auth('http://example.com/resource')
@request.user.should == 'beth'
@ -103,7 +103,7 @@ describe RestClient::Request do
end
it "correctly formats cookies provided to the constructor" do
URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
URI.stub!(:parse).and_return(double('uri', :user => nil, :password => nil))
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1', :user_id => "someone" })
@request.should_receive(:default_headers).and_return({'Foo' => 'bar'})
@request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1; user_id=someone'}
@ -173,7 +173,7 @@ describe RestClient::Request do
it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
@request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
klass = mock("net:http class")
klass = double("net:http class")
@request.should_receive(:net_http_request_class).with(:put).and_return(klass)
klass.should_receive(:new).and_return('result')
@request.should_receive(:transmit).with(@uri, 'result', kind_of(RestClient::Payload::Base))
@ -230,7 +230,7 @@ describe RestClient::Request do
it "does not attempt to send any credentials if user is nil" do
@request.stub!(:user).and_return(nil)
req = mock("request")
req = double("request")
req.should_not_receive(:basic_auth)
@request.setup_credentials(req)
end
@ -238,7 +238,7 @@ describe RestClient::Request do
it "setup credentials when there's a user" do
@request.stub!(:user).and_return('joe')
@request.stub!(:password).and_return('mypass')
req = mock("request")
req = double("request")
req.should_receive(:basic_auth).with('joe', 'mypass')
@request.setup_credentials(req)
end
@ -250,7 +250,7 @@ describe RestClient::Request do
end
it "class method execute wraps constructor" do
req = mock("rest request")
req = double("rest request")
RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
req.should_receive(:execute)
RestClient::Request.execute(1 => 2)
@ -258,24 +258,24 @@ describe RestClient::Request do
describe "exception" do
it "raises Unauthorized when the response is 401" do
res = mock('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
end
it "raises ResourceNotFound when the response is 404" do
res = mock('response', :code => '404', :[] => ['content-encoding' => ''], :body => '' )
res = double('response', :code => '404', :[] => ['content-encoding' => ''], :body => '' )
lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
end
it "raises RequestFailed otherwise" do
res = mock('response', :code => '500', :[] => ['content-encoding' => ''], :body => '' )
res = double('response', :code => '500', :[] => ['content-encoding' => ''], :body => '' )
lambda { @request.process_result(res) }.should raise_error(RestClient::InternalServerError)
end
end
describe "block usage" do
it "returns what asked to" do
res = mock('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
@request.process_result(res){|response, request| "foo"}.should == "foo"
end
end
@ -319,7 +319,7 @@ describe RestClient::Request do
it "logs a response including the status code, content type, and result body size in bytes" do
log = RestClient.log = []
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res.stub!(:[]).with('Content-type').and_return('text/html')
@request.log_response res
log[0].should == "# => 200 OK | text/html 4 bytes\n"
@ -327,7 +327,7 @@ describe RestClient::Request do
it "logs a response with a nil Content-type" do
log = RestClient.log = []
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res.stub!(:[]).with('Content-type').and_return(nil)
@request.log_response res
log[0].should == "# => 200 OK | 4 bytes\n"
@ -335,7 +335,7 @@ describe RestClient::Request do
it "logs a response with a nil body" do
log = RestClient.log = []
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => nil)
res = double('result', :code => '200', :class => Net::HTTPOK, :body => nil)
res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
@request.log_response res
log[0].should == "# => 200 OK | text/html 0 bytes\n"
@ -344,7 +344,7 @@ describe RestClient::Request do
it "strips the charset from the response content type" do
log = RestClient.log = []
res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
@request.log_response res
log[0].should == "# => 200 OK | text/html 4 bytes\n"

View file

@ -5,8 +5,8 @@ include WebMock::API
describe RestClient::Response do
before do
@net_http_res = mock('net http response', :to_hash => {"Status" => ["200 OK"]}, :code => 200)
@request = mock('http request', :user => nil, :password => nil)
@net_http_res = double('net http response', :to_hash => {"Status" => ["200 OK"]}, :code => 200)
@request = double('http request', :user => nil, :password => nil)
@response = RestClient::Response.create('abc', @net_http_res, {})
end
@ -27,14 +27,14 @@ describe RestClient::Response do
describe "cookie processing" do
it "should correctly deal with one Set-Cookie header with one cookie inside" do
net_http_res = mock('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]})
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]})
response = RestClient::Response.create('abc', net_http_res, {})
response.headers[:set_cookie].should == ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT"]
response.cookies.should == { "main_page" => "main_page_no_rewrite" }
end
it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
net_http_res = mock('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
response = RestClient::Response.create('abc', net_http_res, {})
response.headers[:set_cookie].should == ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT", "remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT", "user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]
response.cookies.should == {
@ -45,7 +45,7 @@ describe RestClient::Response do
end
it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
net_http_res = mock('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT, remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT, user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT, remember_me=; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT, user=somebody; path=/; expires=Thu, 01-Jan-1970 00:00:00 GMT"]})
response = RestClient::Response.create('abc', net_http_res, {})
response.cookies.should == {
"main_page" => "main_page_no_rewrite",
@ -58,7 +58,7 @@ describe RestClient::Response do
describe "exceptions processing" do
it "should return itself for normal codes" do
(200..206).each do |code|
net_http_res = mock('net http response', :code => '200')
net_http_res = double('net http response', :code => '200')
response = RestClient::Response.create('abc', net_http_res, {})
response.return! @request
end
@ -67,7 +67,7 @@ describe RestClient::Response do
it "should throw an exception for other codes" do
RestClient::Exceptions::EXCEPTIONS_MAP.each_key do |code|
unless (200..207).include? code
net_http_res = mock('net http response', :code => code.to_i)
net_http_res = double('net http response', :code => code.to_i)
response = RestClient::Response.create('abc', net_http_res, {})
lambda { response.return!}.should raise_error
end
@ -97,25 +97,25 @@ describe RestClient::Response do
end
it "doesn't follow a 301 when the request is a post" do
net_http_res = mock('net http response', :code => 301)
net_http_res = double('net http response', :code => 301)
response = RestClient::Response.create('abc', net_http_res, {:method => :post})
lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
end
it "doesn't follow a 302 when the request is a post" do
net_http_res = mock('net http response', :code => 302)
net_http_res = double('net http response', :code => 302)
response = RestClient::Response.create('abc', net_http_res, {:method => :post})
lambda { response.return!(@request)}.should raise_error(RestClient::Found)
end
it "doesn't follow a 307 when the request is a post" do
net_http_res = mock('net http response', :code => 307)
net_http_res = double('net http response', :code => 307)
response = RestClient::Response.create('abc', net_http_res, {:method => :post})
lambda { response.return!(@request)}.should raise_error(RestClient::TemporaryRedirect)
end
it "doesn't follow a redirection when the request is a put" do
net_http_res = mock('net http response', :code => 301)
net_http_res = double('net http response', :code => 301)
response = RestClient::Response.create('abc', net_http_res, {:method => :put})
lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
end

View file

@ -63,7 +63,7 @@ describe RestClient do
it "append the log to the requested filename" do
RestClient.log = '/tmp/restclient.log'
f = mock('file handle')
f = double('file handle')
File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
f.should_receive(:puts).with('xyz')
RestClient.log << 'xyz'