Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Response object has access to the last request

  • Loading branch information...
commit f2cc75f870e2aa8781987e8baae8dd889d5f4980 1 parent 9beebeb
@sandro sandro authored
View
2  lib/httparty/request.rb
@@ -201,7 +201,7 @@ def handle_response
last_response
end
else
- Response.new(last_response, parse_response(last_response.body))
+ Response.new(self, last_response, parse_response(last_response.body))
end
end
View
5 lib/httparty/response.rb
@@ -33,9 +33,10 @@ def self.underscore(string)
string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase
end
- attr_reader :response, :parsed_response, :body, :headers
+ attr_reader :request, :response, :parsed_response, :body, :headers
- def initialize(response, parsed_response)
+ def initialize(request, response, parsed_response)
+ @request = request
@response = response
@body = response.body
@parsed_response = parsed_response
View
32 spec/httparty/response_spec.rb
@@ -4,13 +4,13 @@
before do
@last_modified = Date.new(2010, 1, 15).to_s
@content_length = '1024'
- @response_object = {'foo' => 'bar'}
+ @request_object = HTTParty::Request.new Net::HTTP::Get, '/'
@response_object = Net::HTTPOK.new('1.1', 200, 'OK')
@response_object.stub(:body => "{foo:'bar'}")
@response_object['last-modified'] = @last_modified
@response_object['content-length'] = @content_length
@parsed_response = {"foo" => "bar"}
- @response = HTTParty::Response.new(@response_object, @parsed_response)
+ @response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
end
describe ".underscore" do
@@ -46,17 +46,17 @@
end
it "returns response headers" do
- response = HTTParty::Response.new(@response_object, @parsed_response)
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
response.headers.should == {'last-modified' => [@last_modified], 'content-length' => [@content_length]}
end
it "should send missing methods to delegate" do
- response = HTTParty::Response.new(@response_object, {'foo' => 'bar'})
+ response = HTTParty::Response.new(@request_object, @response_object, {'foo' => 'bar'})
response['foo'].should == 'bar'
end
it "should be able to iterate if it is array" do
- response = HTTParty::Response.new(@response_object, [{'foo' => 'bar'}, {'foo' => 'baz'}])
+ response = HTTParty::Response.new(@request_object, @response_object, [{'foo' => 'bar'}, {'foo' => 'baz'}])
response.size.should == 2
expect {
response.each { |item| }
@@ -64,20 +64,20 @@
end
it "allows headers to be accessed by mixed-case names in hash notation" do
- response = HTTParty::Response.new(@response_object, @parsed_response)
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
response.headers['Content-LENGTH'].should == @content_length
end
it "returns a comma-delimited value when multiple values exist" do
@response_object.add_field 'set-cookie', 'csrf_id=12345; path=/'
@response_object.add_field 'set-cookie', '_github_ses=A123CdE; path=/'
- response = HTTParty::Response.new(@response_object, @parsed_response)
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
response.headers['set-cookie'].should == "csrf_id=12345; path=/, _github_ses=A123CdE; path=/"
end
# Backwards-compatibility - previously, #headers returned a Hash
it "responds to hash methods" do
- response = HTTParty::Response.new(@response_object, @parsed_response)
+ response = HTTParty::Response.new(@request_object, @response_object, @parsed_response)
hash_methods = {}.methods - response.headers.methods
hash_methods.each do |method_name|
response.headers.respond_to?(method_name).should be_true
@@ -85,12 +85,12 @@
end
xit "should allow hashes to be accessed with dot notation" do
- response = HTTParty::Response.new({'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK')
+ response = HTTParty::Response.new(@request_object, {'foo' => 'bar'}, "{foo:'bar'}", 200, 'OK')
response.foo.should == 'bar'
end
xit "should allow nested hashes to be accessed with dot notation" do
- response = HTTParty::Response.new({'foo' => {'bar' => 'baz'}}, "{foo: {bar:'baz'}}", 200, 'OK')
+ response = HTTParty::Response.new(@request_object, {'foo' => {'bar' => 'baz'}}, "{foo: {bar:'baz'}}", 200, 'OK')
response.foo.should == {'bar' => 'baz'}
response.foo.bar.should == 'baz'
end
@@ -105,31 +105,31 @@ def response_mock(klass)
context "major codes" do
it "is information" do
net_response = response_mock(Net::HTTPInformation)
- response = HTTParty::Response.new(net_response, '')
+ response = HTTParty::Response.new(@request_object, net_response, '')
response.information?.should be_true
end
it "is success" do
net_response = response_mock(Net::HTTPSuccess)
- response = HTTParty::Response.new(net_response, '')
+ response = HTTParty::Response.new(@request_object, net_response, '')
response.success?.should be_true
end
it "is redirection" do
net_response = response_mock(Net::HTTPRedirection)
- response = HTTParty::Response.new(net_response, '')
+ response = HTTParty::Response.new(@request_object, net_response, '')
response.redirection?.should be_true
end
it "is client error" do
net_response = response_mock(Net::HTTPClientError)
- response = HTTParty::Response.new(net_response, '')
+ response = HTTParty::Response.new(@request_object, net_response, '')
response.client_error?.should be_true
end
it "is server error" do
net_response = response_mock(Net::HTTPServerError)
- response = HTTParty::Response.new(net_response, '')
+ response = HTTParty::Response.new(@request_object, net_response, '')
response.server_error?.should be_true
end
end
@@ -179,7 +179,7 @@ def response_mock(klass)
}.each do |method, klass|
it "responds to #{method}" do
net_response = response_mock(klass)
- response = HTTParty::Response.new(net_response, '')
+ response = HTTParty::Response.new(@request_object, net_response, '')
response.__send__(method).should be_true
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.