Fix up tests for encoding changes.

Add a `.to_hash => {}` for the Net HTTP response mocks.
This commit is contained in:
Andy Brody 2015-03-13 18:00:51 -07:00
parent 6d7818f517
commit 5ce0c6ebde
16 changed files with 56 additions and 31 deletions

3
.rspec
View File

@ -1 +1,2 @@
--colour --format progress --order random --color
--format progress

5
spec/helpers.rb Normal file
View File

@ -0,0 +1,5 @@
module Helpers
def response_double(opts={})
double('response', {:to_hash => {}}.merge(opts))
end
end

View File

@ -66,7 +66,7 @@ describe RestClient do
response.encoding.should eq Encoding::BINARY response.encoding.should eq Encoding::BINARY
lambda { lambda {
response.encode('utf-8') response.encode('utf-8')
}.should_raise(Encoding::UndefinedConversionError) }.should raise_error(Encoding::UndefinedConversionError)
response.valid_encoding?.should eq true response.valid_encoding?.should eq true
end end
end end

View File

@ -1,2 +1,20 @@
require 'webmock/rspec' require 'webmock/rspec'
require 'restclient' require 'rest-client'
require_relative './helpers'
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
# add helpers
config.include Helpers, :include_helpers
end

1
spec/unit/_lib.rb Normal file
View File

@ -0,0 +1 @@
require_relative '../spec_helper'

View File

@ -1,4 +1,4 @@
require 'spec_helper' require_relative '_lib'
describe RestClient::AbstractResponse do describe RestClient::AbstractResponse do

View File

@ -1,4 +1,4 @@
require 'spec_helper' require_relative '_lib'
describe RestClient::Exception do describe RestClient::Exception do
it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do

View File

@ -1,6 +1,6 @@
# encoding: binary # encoding: binary
require 'spec_helper' require_relative '_lib'
describe RestClient::Payload do describe RestClient::Payload do
context "A regular Payload" do context "A regular Payload" do

View File

@ -1,4 +1,4 @@
require 'spec_helper' require_relative '_lib'
describe RestClient::RawResponse do describe RestClient::RawResponse do
before do before do

View File

@ -1,4 +1,4 @@
require 'spec_helper' require_relative '_lib'
describe RestClient::Request do describe RestClient::Request do
@ -26,7 +26,7 @@ describe RestClient::Request do
stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200) stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(:url => 'http://some/resource', :method => :post, :payload => {:file => test_file}) RestClient::Request.execute(:url => 'http://some/resource', :method => :post, :payload => {:file => test_file})
test_file.closed?.should be_true test_file.closed?.should be true
end end
end end

View File

@ -1,6 +1,6 @@
require 'spec_helper' require_relative './_lib'
describe RestClient::Request do describe RestClient::Request, :include_helpers do
before do before do
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload') @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
@ -50,7 +50,7 @@ describe RestClient::Request do
end end
it "processes a successful result" do it "processes a successful result" do
res = double("result") res = response_double
res.stub(:code).and_return("200") res.stub(:code).and_return("200")
res.stub(:body).and_return('body') res.stub(:body).and_return('body')
res.stub(:[]).with('content-encoding').and_return(nil) 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 it "doesn't classify successful requests as failed" do
203.upto(207) do |code| 203.upto(207) do |code|
res = double("result") res = response_double
res.stub(:code).and_return(code.to_s) res.stub(:code).and_return(code.to_s)
res.stub(:body).and_return("") res.stub(:body).and_return("")
res.stub(:[]).with('content-encoding').and_return(nil) res.stub(:[]).with('content-encoding').and_return(nil)
@ -381,24 +381,24 @@ describe RestClient::Request do
describe "exception" do describe "exception" do
it "raises Unauthorized when the response is 401" do it "raises Unauthorized when the response is 401" do
res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' ) res = response_double(:code => '401', :[] => ['content-encoding' => ''], :body => '' )
lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized) lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
end end
it "raises ResourceNotFound when the response is 404" do it "raises ResourceNotFound when the response is 404" do
res = double('response', :code => '404', :[] => ['content-encoding' => ''], :body => '' ) res = response_double(:code => '404', :[] => ['content-encoding' => ''], :body => '' )
lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound) lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
end end
it "raises RequestFailed otherwise" do it "raises RequestFailed otherwise" do
res = double('response', :code => '500', :[] => ['content-encoding' => ''], :body => '' ) res = response_double(:code => '500', :[] => ['content-encoding' => ''], :body => '' )
lambda { @request.process_result(res) }.should raise_error(RestClient::InternalServerError) lambda { @request.process_result(res) }.should raise_error(RestClient::InternalServerError)
end end
end end
describe "block usage" do describe "block usage" do
it "returns what asked to" do it "returns what asked to" do
res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' ) res = response_double(:code => '401', :[] => ['content-encoding' => ''], :body => '' )
@request.process_result(res){|response, request| "foo"}.should eq "foo" @request.process_result(res){|response, request| "foo"}.should eq "foo"
end end
end end
@ -406,7 +406,7 @@ describe RestClient::Request do
describe "proxy" do describe "proxy" do
it "creates a proxy class if a proxy url is given" do it "creates a proxy class if a proxy url is given" do
RestClient.stub(:proxy).and_return("http://example.com/") RestClient.stub(:proxy).and_return("http://example.com/")
@request.net_http_class.proxy_class?.should be_true @request.net_http_class.proxy_class?.should be true
end end
it "creates a proxy class with the correct address if a IPv6 proxy url is given" do it "creates a proxy class with the correct address if a IPv6 proxy url is given" do
@ -415,7 +415,7 @@ describe RestClient::Request do
end end
it "creates a non-proxy class if a proxy url is not given" do it "creates a non-proxy class if a proxy url is not given" do
@request.net_http_class.proxy_class?.should be_false @request.net_http_class.proxy_class?.should be_falsey
end end
end end

View File

@ -1,4 +1,4 @@
require 'spec_helper' require_relative '_lib'
describe RestClient::Resource do describe RestClient::Resource do
before do before do

View File

@ -1,6 +1,6 @@
require 'spec_helper' require_relative '_lib'
describe RestClient::Response do describe RestClient::Response, :include_helpers do
before do before do
@net_http_res = double('net http response', :to_hash => {"Status" => ["200 OK"]}, :code => 200) @net_http_res = double('net http response', :to_hash => {"Status" => ["200 OK"]}, :code => 200)
@request = double('http request', :user => nil, :password => nil) @request = double('http request', :user => nil, :password => nil)
@ -55,7 +55,7 @@ describe RestClient::Response do
describe "exceptions processing" do describe "exceptions processing" do
it "should return itself for normal codes" do it "should return itself for normal codes" do
(200..206).each do |code| (200..206).each do |code|
net_http_res = double('net http response', :code => '200') net_http_res = response_double(:code => '200')
response = RestClient::Response.create('abc', net_http_res, {}) response = RestClient::Response.create('abc', net_http_res, {})
response.return! @request response.return! @request
end end
@ -64,7 +64,7 @@ describe RestClient::Response do
it "should throw an exception for other codes" do it "should throw an exception for other codes" do
RestClient::Exceptions::EXCEPTIONS_MAP.each_key do |code| RestClient::Exceptions::EXCEPTIONS_MAP.each_key do |code|
unless (200..207).include? code unless (200..207).include? code
net_http_res = double('net http response', :code => code.to_i) net_http_res = response_double(:code => code.to_i)
response = RestClient::Response.create('abc', net_http_res, {}) response = RestClient::Response.create('abc', net_http_res, {})
lambda { response.return!}.should raise_error lambda { response.return!}.should raise_error
end end
@ -94,25 +94,25 @@ describe RestClient::Response do
end end
it "doesn't follow a 301 when the request is a post" do it "doesn't follow a 301 when the request is a post" do
net_http_res = double('net http response', :code => 301) net_http_res = response_double(:code => 301)
response = RestClient::Response.create('abc', net_http_res, {:method => :post}) response = RestClient::Response.create('abc', net_http_res, {:method => :post})
lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently) lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
end end
it "doesn't follow a 302 when the request is a post" do it "doesn't follow a 302 when the request is a post" do
net_http_res = double('net http response', :code => 302) net_http_res = response_double(:code => 302)
response = RestClient::Response.create('abc', net_http_res, {:method => :post}) response = RestClient::Response.create('abc', net_http_res, {:method => :post})
lambda { response.return!(@request)}.should raise_error(RestClient::Found) lambda { response.return!(@request)}.should raise_error(RestClient::Found)
end end
it "doesn't follow a 307 when the request is a post" do it "doesn't follow a 307 when the request is a post" do
net_http_res = double('net http response', :code => 307) net_http_res = response_double(:code => 307)
response = RestClient::Response.create('abc', net_http_res, {:method => :post}) response = RestClient::Response.create('abc', net_http_res, {:method => :post})
lambda { response.return!(@request)}.should raise_error(RestClient::TemporaryRedirect) lambda { response.return!(@request)}.should raise_error(RestClient::TemporaryRedirect)
end end
it "doesn't follow a redirection when the request is a put" do it "doesn't follow a redirection when the request is a put" do
net_http_res = double('net http response', :code => 301) net_http_res = response_double(:code => 301)
response = RestClient::Response.create('abc', net_http_res, {:method => :put}) response = RestClient::Response.create('abc', net_http_res, {:method => :put})
lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently) lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
end end

View File

@ -1,4 +1,4 @@
require 'spec_helper' require_relative '_lib'
describe RestClient do describe RestClient do
describe "API" do describe "API" do

View File

@ -1,4 +1,4 @@
require 'spec_helper' require_relative '_lib'
describe RestClient::Utils do describe RestClient::Utils do
describe '.get_encoding_from_headers' do describe '.get_encoding_from_headers' do

View File

@ -1,4 +1,4 @@
require 'spec_helper' require_relative '../_lib'
describe 'RestClient::Windows::RootCerts', describe 'RestClient::Windows::RootCerts',
:if => RestClient::Platform.windows? do :if => RestClient::Platform.windows? do