mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Response object has access to the last request
This commit is contained in:
parent
9beebebf70
commit
f2cc75f870
3 changed files with 20 additions and 19 deletions
|
@ -201,7 +201,7 @@ module HTTParty
|
|||
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
|
||||
|
||||
|
|
|
@ -33,9 +33,10 @@ module HTTParty
|
|||
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
|
||||
|
|
|
@ -4,13 +4,13 @@ describe HTTParty::Response do
|
|||
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 @@ describe HTTParty::Response do
|
|||
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 @@ describe HTTParty::Response do
|
|||
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 @@ describe HTTParty::Response do
|
|||
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 @@ describe HTTParty::Response do
|
|||
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 @@ describe HTTParty::Response do
|
|||
}.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
|
||||
|
|
Loading…
Reference in a new issue