mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
Fix up tests for encoding changes.
Add a `.to_hash => {}` for the Net HTTP response mocks.
This commit is contained in:
parent
6d7818f517
commit
5ce0c6ebde
16 changed files with 56 additions and 31 deletions
3
.rspec
3
.rspec
|
@ -1 +1,2 @@
|
|||
--colour --format progress --order random
|
||||
--color
|
||||
--format progress
|
||||
|
|
5
spec/helpers.rb
Normal file
5
spec/helpers.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
module Helpers
|
||||
def response_double(opts={})
|
||||
double('response', {:to_hash => {}}.merge(opts))
|
||||
end
|
||||
end
|
|
@ -66,7 +66,7 @@ describe RestClient do
|
|||
response.encoding.should eq Encoding::BINARY
|
||||
lambda {
|
||||
response.encode('utf-8')
|
||||
}.should_raise(Encoding::UndefinedConversionError)
|
||||
}.should raise_error(Encoding::UndefinedConversionError)
|
||||
response.valid_encoding?.should eq true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,2 +1,20 @@
|
|||
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
1
spec/unit/_lib.rb
Normal file
|
@ -0,0 +1 @@
|
|||
require_relative '../spec_helper'
|
|
@ -1,4 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require_relative '_lib'
|
||||
|
||||
describe RestClient::AbstractResponse do
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require_relative '_lib'
|
||||
|
||||
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
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# encoding: binary
|
||||
|
||||
require 'spec_helper'
|
||||
require_relative '_lib'
|
||||
|
||||
describe RestClient::Payload do
|
||||
context "A regular Payload" do
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require_relative '_lib'
|
||||
|
||||
describe RestClient::RawResponse do
|
||||
before do
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require_relative '_lib'
|
||||
|
||||
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)
|
||||
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
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
require_relative './_lib'
|
||||
|
||||
describe RestClient::Request do
|
||||
describe RestClient::Request, :include_helpers do
|
||||
before do
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
|
||||
|
||||
|
@ -50,7 +50,7 @@ describe RestClient::Request do
|
|||
end
|
||||
|
||||
it "processes a successful result" do
|
||||
res = double("result")
|
||||
res = response_double
|
||||
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 = double("result")
|
||||
res = response_double
|
||||
res.stub(:code).and_return(code.to_s)
|
||||
res.stub(:body).and_return("")
|
||||
res.stub(:[]).with('content-encoding').and_return(nil)
|
||||
|
@ -381,24 +381,24 @@ describe RestClient::Request do
|
|||
|
||||
describe "exception" 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)
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
||||
describe "block usage" 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"
|
||||
end
|
||||
end
|
||||
|
@ -406,7 +406,7 @@ describe RestClient::Request do
|
|||
describe "proxy" do
|
||||
it "creates a proxy class if a proxy url is given" do
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require_relative '_lib'
|
||||
|
||||
describe RestClient::Resource do
|
||||
before do
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
require 'spec_helper'
|
||||
require_relative '_lib'
|
||||
|
||||
describe RestClient::Response do
|
||||
describe RestClient::Response, :include_helpers do
|
||||
before do
|
||||
@net_http_res = double('net http response', :to_hash => {"Status" => ["200 OK"]}, :code => 200)
|
||||
@request = double('http request', :user => nil, :password => nil)
|
||||
|
@ -55,7 +55,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 = double('net http response', :code => '200')
|
||||
net_http_res = response_double(:code => '200')
|
||||
response = RestClient::Response.create('abc', net_http_res, {})
|
||||
response.return! @request
|
||||
end
|
||||
|
@ -64,7 +64,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 = 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, {})
|
||||
lambda { response.return!}.should raise_error
|
||||
end
|
||||
|
@ -94,25 +94,25 @@ describe RestClient::Response do
|
|||
end
|
||||
|
||||
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})
|
||||
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 = double('net http response', :code => 302)
|
||||
net_http_res = response_double(: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 = double('net http response', :code => 307)
|
||||
net_http_res = response_double(: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 = double('net http response', :code => 301)
|
||||
net_http_res = response_double(:code => 301)
|
||||
response = RestClient::Response.create('abc', net_http_res, {:method => :put})
|
||||
lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
|
||||
end
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require_relative '_lib'
|
||||
|
||||
describe RestClient do
|
||||
describe "API" do
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require_relative '_lib'
|
||||
|
||||
describe RestClient::Utils do
|
||||
describe '.get_encoding_from_headers' do
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require_relative '../_lib'
|
||||
|
||||
describe 'RestClient::Windows::RootCerts',
|
||||
:if => RestClient::Platform.windows? do
|
||||
|
|
Loading…
Reference in a new issue