mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
whitespace fixes
This commit is contained in:
parent
c6a2265f93
commit
075c6746f2
8 changed files with 198 additions and 196 deletions
|
@ -1,43 +1,43 @@
|
|||
module RestClient
|
||||
module Mixin
|
||||
module Response
|
||||
attr_reader :net_http_res
|
||||
|
||||
# HTTP status code, always 200 since RestClient throws exceptions for
|
||||
# other codes.
|
||||
def code
|
||||
@code ||= @net_http_res.code.to_i
|
||||
end
|
||||
module Mixin
|
||||
module Response
|
||||
attr_reader :net_http_res
|
||||
|
||||
# A hash of the headers, beautified with symbols and underscores.
|
||||
# e.g. "Content-type" will become :content_type.
|
||||
def headers
|
||||
@headers ||= self.class.beautify_headers(@net_http_res.to_hash)
|
||||
end
|
||||
# HTTP status code, always 200 since RestClient throws exceptions for
|
||||
# other codes.
|
||||
def code
|
||||
@code ||= @net_http_res.code.to_i
|
||||
end
|
||||
|
||||
# Hash of cookies extracted from response headers
|
||||
def cookies
|
||||
@cookies ||= (self.headers[:set_cookie] || "").split('; ').inject({}) do |out, raw_c|
|
||||
key, val = raw_c.split('=')
|
||||
unless %w(expires domain path secure).member?(key)
|
||||
out[key] = val
|
||||
end
|
||||
out
|
||||
end
|
||||
end
|
||||
|
||||
def self.included(receiver)
|
||||
receiver.extend(RestClient::Mixin::Response::ClassMethods)
|
||||
end
|
||||
# A hash of the headers, beautified with symbols and underscores.
|
||||
# e.g. "Content-type" will become :content_type.
|
||||
def headers
|
||||
@headers ||= self.class.beautify_headers(@net_http_res.to_hash)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def beautify_headers(headers)
|
||||
headers.inject({}) do |out, (key, value)|
|
||||
out[key.gsub(/-/, '_').to_sym] = value.first
|
||||
out
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
# Hash of cookies extracted from response headers
|
||||
def cookies
|
||||
@cookies ||= (self.headers[:set_cookie] || "").split('; ').inject({}) do |out, raw_c|
|
||||
key, val = raw_c.split('=')
|
||||
unless %w(expires domain path secure).member?(key)
|
||||
out[key] = val
|
||||
end
|
||||
out
|
||||
end
|
||||
end
|
||||
|
||||
def self.included(receiver)
|
||||
receiver.extend(RestClient::Mixin::Response::ClassMethods)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def beautify_headers(headers)
|
||||
headers.inject({}) do |out, (key, value)|
|
||||
out[key.gsub(/-/, '_').to_sym] = value.first
|
||||
out
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,8 +12,8 @@ module RestClient
|
|||
# a Tempfile object at res.file, which contains the path to the raw
|
||||
# downloaded request body.
|
||||
class RawResponse
|
||||
include RestClient::Mixin::Response
|
||||
|
||||
include RestClient::Mixin::Response
|
||||
|
||||
attr_reader :file
|
||||
|
||||
def initialize(tempfile, net_http_res)
|
||||
|
@ -21,10 +21,10 @@ module RestClient
|
|||
@file = tempfile
|
||||
end
|
||||
|
||||
def to_s
|
||||
@file.open
|
||||
@file.read
|
||||
end
|
||||
def to_s
|
||||
@file.open
|
||||
@file.read
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,10 +6,12 @@ module RestClient
|
|||
# main API. For example:
|
||||
#
|
||||
# RestClient::Request.execute(:method => :head, :url => 'http://example.com')
|
||||
#
|
||||
#
|
||||
class Request
|
||||
attr_reader :method, :url, :payload, :headers, :cookies, :user, :password, :timeout, :open_timeout,
|
||||
:raw_response, :verify_ssl, :ssl_client_cert, :ssl_client_key
|
||||
attr_reader :method, :url, :payload, :headers,
|
||||
:cookies, :user, :password, :timeout, :open_timeout,
|
||||
:verify_ssl, :ssl_client_cert, :ssl_client_key,
|
||||
:raw_response
|
||||
|
||||
def self.execute(args)
|
||||
new(args).execute
|
||||
|
@ -19,7 +21,7 @@ module RestClient
|
|||
@method = args[:method] or raise ArgumentError, "must pass :method"
|
||||
@url = args[:url] or raise ArgumentError, "must pass :url"
|
||||
@headers = args[:headers] || {}
|
||||
@cookies = @headers.delete(:cookies) || args[:cookies] || {}
|
||||
@cookies = @headers.delete(:cookies) || args[:cookies] || {}
|
||||
@payload = process_payload(args[:payload])
|
||||
@user = args[:user]
|
||||
@password = args[:password]
|
||||
|
@ -45,13 +47,13 @@ module RestClient
|
|||
end
|
||||
|
||||
def make_headers(user_headers)
|
||||
unless @cookies.empty?
|
||||
user_headers[:cookie] = @cookies.map {|key, val| "#{key.to_s}=#{val}" }.join('; ')
|
||||
end
|
||||
unless @cookies.empty?
|
||||
user_headers[:cookie] = @cookies.map {|key, val| "#{key.to_s}=#{val}" }.join('; ')
|
||||
end
|
||||
|
||||
default_headers.merge(user_headers).inject({}) do |final, (key, value)|
|
||||
final[key.to_s.gsub(/_/, '-').capitalize] = value.to_s
|
||||
final
|
||||
final
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -118,7 +120,7 @@ module RestClient
|
|||
if result.kind_of?(String) or @method == :head
|
||||
Response.new(result, res)
|
||||
elsif @raw_response
|
||||
RawResponse.new(@tf, res)
|
||||
RawResponse.new(@tf, res)
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
@ -132,39 +134,39 @@ module RestClient
|
|||
def setup_credentials(req)
|
||||
req.basic_auth(user, password) if user
|
||||
end
|
||||
|
||||
|
||||
def fetch_body(http_response)
|
||||
if @raw_response
|
||||
# Taken from Chef, which as in turn...
|
||||
# Stolen from http://www.ruby-forum.com/topic/166423
|
||||
# Kudos to _why!
|
||||
@tf = Tempfile.new("rest-client")
|
||||
size, total = 0, http_response.header['Content-Length'].to_i
|
||||
http_response.read_body do |chunk|
|
||||
@tf.write(chunk)
|
||||
size += chunk.size
|
||||
if size == 0
|
||||
display_log("#{@method} #{@url} done (0 length file)")
|
||||
elsif total == 0
|
||||
display_log("#{@method} #{@url} (zero content length)")
|
||||
else
|
||||
display_log("#{@method} #{@url} %d%% done (%d of %d)" % [(size * 100) / total, size, total])
|
||||
end
|
||||
end
|
||||
@tf.close
|
||||
@tf
|
||||
else
|
||||
http_response.read_body
|
||||
end
|
||||
http_response
|
||||
end
|
||||
if @raw_response
|
||||
# Taken from Chef, which as in turn...
|
||||
# Stolen from http://www.ruby-forum.com/topic/166423
|
||||
# Kudos to _why!
|
||||
@tf = Tempfile.new("rest-client")
|
||||
size, total = 0, http_response.header['Content-Length'].to_i
|
||||
http_response.read_body do |chunk|
|
||||
@tf.write(chunk)
|
||||
size += chunk.size
|
||||
if size == 0
|
||||
display_log("#{@method} #{@url} done (0 length file)")
|
||||
elsif total == 0
|
||||
display_log("#{@method} #{@url} (zero content length)")
|
||||
else
|
||||
display_log("#{@method} #{@url} %d%% done (%d of %d)" % [(size * 100) / total, size, total])
|
||||
end
|
||||
end
|
||||
@tf.close
|
||||
@tf
|
||||
else
|
||||
http_response.read_body
|
||||
end
|
||||
http_response
|
||||
end
|
||||
|
||||
def process_result(res)
|
||||
if res.code =~ /\A2\d{2}\z/
|
||||
# We don't decode raw requests
|
||||
unless @raw_response
|
||||
decode res['content-encoding'], res.body if res.body
|
||||
end
|
||||
# We don't decode raw requests
|
||||
unless @raw_response
|
||||
decode res['content-encoding'], res.body if res.body
|
||||
end
|
||||
elsif %w(301 302 303).include? res.code
|
||||
url = res.header['Location']
|
||||
|
||||
|
@ -205,7 +207,7 @@ module RestClient
|
|||
end
|
||||
|
||||
def response_log(res)
|
||||
size = @raw_response ? File.size(@tf.path) : res.body.size
|
||||
size = @raw_response ? File.size(@tf.path) : res.body.size
|
||||
"# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{size} bytes"
|
||||
end
|
||||
|
||||
|
|
|
@ -100,8 +100,8 @@ module RestClient
|
|||
end
|
||||
|
||||
def open_timeout
|
||||
options[:open_timeout]
|
||||
end
|
||||
options[:open_timeout]
|
||||
end
|
||||
|
||||
# Construct a subresource, preserving authentication.
|
||||
#
|
||||
|
|
|
@ -8,7 +8,7 @@ module RestClient
|
|||
# RestClient.get('http://example.com').headers[:content_type]
|
||||
#
|
||||
class Response < String
|
||||
|
||||
|
||||
include RestClient::Mixin::Response
|
||||
|
||||
def initialize(string, net_http_res)
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
require File.dirname(__FILE__) + '/../base'
|
||||
|
||||
class MockResponse
|
||||
include RestClient::Mixin::Response
|
||||
|
||||
def initialize(body, res)
|
||||
@net_http_res = res
|
||||
@body = @body
|
||||
end
|
||||
include RestClient::Mixin::Response
|
||||
|
||||
def initialize(body, res)
|
||||
@net_http_res = res
|
||||
@body = @body
|
||||
end
|
||||
end
|
||||
|
||||
describe RestClient::Mixin::Response do
|
||||
|
@ -35,10 +35,10 @@ describe RestClient::Mixin::Response do
|
|||
@response.headers.should == { :content_type => 'text/html' }
|
||||
end
|
||||
|
||||
it "extracts cookies from response headers" do
|
||||
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
|
||||
@response.cookies.should == { 'session_id' => '1' }
|
||||
end
|
||||
it "extracts cookies from response headers" do
|
||||
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
|
||||
@response.cookies.should == { 'session_id' => '1' }
|
||||
end
|
||||
|
||||
it "can access the net http result directly" do
|
||||
@response.net_http_res.should == @net_http_res
|
||||
|
|
|
@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/base'
|
|||
|
||||
describe RestClient::RawResponse do
|
||||
before do
|
||||
@tf = mock("Tempfile", :read => "the answer is 42", :open => true)
|
||||
@tf = mock("Tempfile", :read => "the answer is 42", :open => true)
|
||||
@net_http_res = mock('net http response')
|
||||
@response = RestClient::RawResponse.new(@tf, @net_http_res)
|
||||
end
|
||||
|
@ -11,7 +11,7 @@ describe RestClient::RawResponse do
|
|||
@response.to_s.should == 'the answer is 42'
|
||||
end
|
||||
|
||||
it "exposes a Tempfile" do
|
||||
@response.file.should == @tf
|
||||
end
|
||||
it "exposes a Tempfile" do
|
||||
@response.file.should == @tf
|
||||
end
|
||||
end
|
||||
|
|
|
@ -28,7 +28,7 @@ describe RestClient::Request do
|
|||
it "decodes a gzip body" do
|
||||
@request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n"
|
||||
end
|
||||
|
||||
|
||||
it "ingores gzip for empty bodies" do
|
||||
@request.decode('gzip', '').should be_empty
|
||||
end
|
||||
|
@ -37,23 +37,23 @@ describe RestClient::Request do
|
|||
@request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
|
||||
end
|
||||
|
||||
it "processes a successful result" do
|
||||
res = mock("result")
|
||||
res.stub!(:code).and_return("200")
|
||||
res.stub!(:body).and_return('body')
|
||||
res.stub!(:[]).with('content-encoding').and_return(nil)
|
||||
@request.process_result(res).should == 'body'
|
||||
end
|
||||
it "processes a successful result" do
|
||||
res = mock("result")
|
||||
res.stub!(:code).and_return("200")
|
||||
res.stub!(:body).and_return('body')
|
||||
res.stub!(:[]).with('content-encoding').and_return(nil)
|
||||
@request.process_result(res).should == 'body'
|
||||
end
|
||||
|
||||
it "doesn't classify successful requests as failed" do
|
||||
203.upto(206) do |code|
|
||||
res = mock("result")
|
||||
res.stub!(:code).and_return(code.to_s)
|
||||
res.stub!(:body).and_return("")
|
||||
res.stub!(:[]).with('content-encoding').and_return(nil)
|
||||
@request.process_result(res).should be_empty
|
||||
end
|
||||
end
|
||||
it "doesn't classify successful requests as failed" do
|
||||
203.upto(206) do |code|
|
||||
res = mock("result")
|
||||
res.stub!(:code).and_return(code.to_s)
|
||||
res.stub!(:body).and_return("")
|
||||
res.stub!(:[]).with('content-encoding').and_return(nil)
|
||||
@request.process_result(res).should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it "parses a url into a URI object" do
|
||||
URI.should_receive(:parse).with('http://example.com/resource')
|
||||
|
@ -80,12 +80,12 @@ describe RestClient::Request do
|
|||
@request.password.should == 'pass2'
|
||||
end
|
||||
|
||||
it "correctly formats cookies provided to the constructor" do
|
||||
URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
|
||||
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1' })
|
||||
@request.should_receive(:default_headers).and_return({'foo' => 'bar'})
|
||||
headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1'}
|
||||
end
|
||||
it "correctly formats cookies provided to the constructor" do
|
||||
URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
|
||||
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1' })
|
||||
@request.should_receive(:default_headers).and_return({'foo' => 'bar'})
|
||||
headers = @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1'}
|
||||
end
|
||||
|
||||
it "determines the Net::HTTP class to instantiate by the method name" do
|
||||
@request.net_http_request_class(:put).should == Net::HTTP::Put
|
||||
|
@ -243,7 +243,7 @@ describe RestClient::Request do
|
|||
end
|
||||
|
||||
it "creates a non-proxy class if a proxy url is not given" do
|
||||
@request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
|
||||
@request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
|
||||
end
|
||||
|
||||
it "logs a get request" do
|
||||
|
@ -303,109 +303,109 @@ describe RestClient::Request do
|
|||
f.should_receive(:puts).with('xyz')
|
||||
@request.display_log('xyz')
|
||||
end
|
||||
|
||||
|
||||
it "set read_timeout" do
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
|
||||
|
||||
@net.should_receive(:read_timeout=).with(123)
|
||||
|
||||
|
||||
@request.transmit(@uri, 'req', nil)
|
||||
end
|
||||
|
||||
|
||||
it "set open_timeout" do
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
|
||||
|
||||
@net.should_receive(:open_timeout=).with(123)
|
||||
|
||||
|
||||
@request.transmit(@uri, 'req', nil)
|
||||
end
|
||||
|
||||
|
||||
it "should default to not verifying ssl certificates" do
|
||||
@request.verify_ssl.should == false
|
||||
@request.verify_ssl.should == false
|
||||
end
|
||||
|
||||
|
||||
it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
|
||||
@net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
@net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
end
|
||||
|
||||
|
||||
it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
|
||||
@net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
|
||||
@net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
end
|
||||
|
||||
|
||||
it "should default to not having an ssl_client_cert" do
|
||||
@request.ssl_client_cert.should be(nil)
|
||||
@request.ssl_client_cert.should be(nil)
|
||||
end
|
||||
|
||||
|
||||
it "should set the ssl_client_cert if provided" do
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_client_cert => "whatsupdoc!"
|
||||
)
|
||||
@net.should_receive(:cert=).with("whatsupdoc!")
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_client_cert => "whatsupdoc!"
|
||||
)
|
||||
@net.should_receive(:cert=).with("whatsupdoc!")
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
end
|
||||
|
||||
|
||||
it "should not set the ssl_client_cert if it is not provided" do
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload'
|
||||
)
|
||||
@net.should_not_receive(:cert=).with("whatsupdoc!")
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload'
|
||||
)
|
||||
@net.should_not_receive(:cert=).with("whatsupdoc!")
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
end
|
||||
|
||||
|
||||
it "should default to not having an ssl_client_key" do
|
||||
@request.ssl_client_key.should be(nil)
|
||||
@request.ssl_client_key.should be(nil)
|
||||
end
|
||||
|
||||
|
||||
it "should set the ssl_client_key if provided" do
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_client_key => "whatsupdoc!"
|
||||
)
|
||||
@net.should_receive(:key=).with("whatsupdoc!")
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_client_key => "whatsupdoc!"
|
||||
)
|
||||
@net.should_receive(:key=).with("whatsupdoc!")
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
end
|
||||
|
||||
|
||||
it "should not set the ssl_client_key if it is not provided" do
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload'
|
||||
)
|
||||
@net.should_not_receive(:key=).with("whatsupdoc!")
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload'
|
||||
)
|
||||
@net.should_not_receive(:key=).with("whatsupdoc!")
|
||||
@http.stub!(:request)
|
||||
@request.stub!(:process_result)
|
||||
@request.stub!(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue