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

Merge pull request #2 from witlessbird/fixes

A bunch of fixes, mostly to get rest-client working with recent dependencies and under 1.9.3
Conflicts:
	Rakefile
	rest-client.gemspec
	spec/request_spec.rb

(Cherry-picked from c1866d9f6a)
This commit is contained in:
Matthew Manning 2012-12-07 20:53:47 -08:00 committed by Larry Gilbert
parent dac3f48e26
commit 76963912eb
9 changed files with 36 additions and 21 deletions

View file

@ -19,7 +19,7 @@ end
############################
require 'spec/rake/spectask'
require "rspec/core/rake_task"
desc "Run all specs"
task :spec => ["spec:unit", "spec:integration"]

View file

@ -9,14 +9,14 @@ module RestClient
def generate(params)
if params.is_a?(String)
Base.new(params)
elsif params.respond_to?(:read)
Streamed.new(params)
elsif params
elsif params.is_a?(Hash)
if params.delete(:multipart) == true || has_file?(params)
Multipart.new(params)
else
UrlEncoded.new(params)
end
elsif params.respond_to?(:read)
Streamed.new(params)
else
nil
end

View file

@ -145,13 +145,6 @@ module RestClient
net.verify_mode = OpenSSL::SSL::VERIFY_NONE
elsif @verify_ssl.is_a? Integer
net.verify_mode = @verify_ssl
net.verify_callback = lambda do |preverify_ok, ssl_context|
if (!preverify_ok) || ssl_context.error != 0
err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
raise SSLCertificateNotVerified.new(err_msg)
end
true
end
end
net.cert = @ssl_client_cert if @ssl_client_cert
net.key = @ssl_client_key if @ssl_client_key
@ -182,6 +175,11 @@ module RestClient
raise RestClient::ServerBrokeConnection
rescue Timeout::Error
raise RestClient::RequestTimeout
rescue OpenSSL::SSL::SSLError => error
# UGH. Not sure if this is needed at all. SSLCertificateNotVerified is not being used internally.
# I think it would be better to leave SSLError processing to the client (they'd have to do that anyway...)
raise SSLCertificateNotVerified.new(error.message) if error.message.include?("certificate verify failed")
raise error
end
def setup_credentials(req)

View file

@ -59,6 +59,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency(%q<mime-types>, ["~> 1.16"])
s.add_development_dependency(%q<webmock>, [">= 0.9.1"])
s.add_development_dependency(%q<rspec>, [">= 0"])
s.add_development_dependency(%q<rspec>, [">= 2.0"])
end

View file

@ -1,9 +1,7 @@
def is_ruby_19?
RUBY_VERSION == '1.9.1' or RUBY_VERSION == '1.9.2'
RUBY_VERSION > '1.9'
end
Encoding.default_internal = Encoding.default_external = "ASCII-8BIT" if is_ruby_19?
require 'rubygems'
begin

View file

@ -12,6 +12,16 @@ describe RestClient::Request do
expect { request.execute }.to_not raise_error
end
# I don' think this feature is useful anymore (under 1.9.3 at the very least).
#
# Exceptions in verify_callback are ignored; RestClient has to catch OpenSSL::SSL::SSLError
# and either re-throw it as is, or throw SSLCertificateNotVerified
# based on the contents of the message field of the original exception
#.
# The client has to handle OpenSSL::SSL::SSLError exceptions anyway,
# why make them handle both OpenSSL *AND* RestClient exceptions???
#
# also see https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl.c#L237
it "is unsuccessful with an incorrect ca_file" do
request = RestClient::Request.new(
:method => :get,

View file

@ -1,3 +1,4 @@
# encoding: binary
require File.join(File.dirname(File.expand_path(__FILE__)), 'base')
describe RestClient::Payload do
@ -108,7 +109,7 @@ baz\r
Content-Disposition: form-data; name="foo"; filename="master_shake.jpg"\r
Content-Type: image/jpeg\r
\r
#{IO.read(f.path)}\r
#{File.open(f.path, 'rb'){|bin| bin.read}}\r
--#{m.boundary}--\r
EOS
end
@ -121,7 +122,7 @@ Content-Type: image/jpeg\r
Content-Disposition: form-data; filename="master_shake.jpg"\r
Content-Type: image/jpeg\r
\r
#{IO.read(f.path)}\r
#{File.open(f.path, 'rb'){|bin| bin.read}}\r
--#{m.boundary}--\r
EOS
end
@ -136,7 +137,7 @@ Content-Type: image/jpeg\r
Content-Disposition: form-data; name="foo"; filename="foo.txt"\r
Content-Type: text/plain\r
\r
#{IO.read(f.path)}\r
#{File.open(f.path, 'rb'){|bin| bin.read}}\r
--#{m.boundary}--\r
EOS
end
@ -160,7 +161,7 @@ foo\r
Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r
Content-Type: text/plain\r
\r
#{IO.read(f.path)}\r
#{File.open(f.path, 'rb'){|bin| bin.read}}\r
--#{m.boundary}--\r
EOS
end
@ -230,5 +231,14 @@ Content-Type: text/plain\r
it "should recognize other payloads that can be streamed" do
RestClient::Payload.generate(StringIO.new('foo')).should be_kind_of(RestClient::Payload::Streamed)
end
# hashery gem introduces Hash#read convenience method. Existence of #read method used to determine of content is streameable :/
it "shouldn't treat hashes as streameable" do
RestClient::Payload.generate({"foo" => 'bar'}).should be_kind_of(RestClient::Payload::UrlEncoded)
end
end
class HashMapForTesting < Hash
alias :read :[]
end
end

View file

@ -412,7 +412,6 @@ describe RestClient::Request do
:payload => 'payload',
:verify_ssl => mode )
@net.should_receive(:verify_mode=).with(mode)
@net.should_receive(:verify_callback=)
@http.stub!(:request)
@request.stub!(:process_result)
@request.stub!(:response_log)

View file

@ -91,7 +91,7 @@ describe RestClient::Response do
end
it "follows a redirection and keep the cookies" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => CGI::Cookie.new('Foo', 'Bar'), 'Location' => 'http://new/resource', })
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://new/resource', })
stub_request(:get, 'http://new/resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should == 'Qux'
end