Skip to content

Commit

Permalink
Merge pull request #767 from jnunemaker/request-marshal-fix
Browse files Browse the repository at this point in the history
Request marshal fix
  • Loading branch information
jnunemaker authored Nov 4, 2022
2 parents 0f705e6 + 34e9606 commit fbeafe0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ gemspec

gem 'rake'
gem 'mongrel', '1.2.0.pre2'
gem 'json'

group :development do
gem 'guard'
Expand All @@ -11,6 +12,7 @@ group :development do
end

group :test do
gem 'rexml'
gem 'rspec', '~> 3.4'
gem 'simplecov', require: false
gem 'aruba'
Expand Down
5 changes: 3 additions & 2 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
rspec_options = {
version: 1,
all_after_pass: false,
all_on_start: false
all_on_start: false,
failed_mode: :keep,
cmd: 'bundle exec rspec',
}

guard 'rspec', rspec_options do
Expand Down
10 changes: 7 additions & 3 deletions lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ class Request #:nodoc:
end

def self._load(data)
http_method, path, options = Marshal.load(data)
new(http_method, path, options)
http_method, path, options, last_response, last_uri, raw_request = Marshal.load(data)
instance = new(http_method, path, options)
instance.last_response = last_response
instance.last_uri = last_uri
instance.instance_variable_set("@raw_request", raw_request)
instance
end

attr_accessor :http_method, :options, :last_response, :redirect, :last_uri
Expand Down Expand Up @@ -184,7 +188,7 @@ def _dump(_level)
opts = options.dup
opts.delete(:logger)
opts.delete(:parser) if opts[:parser] && opts[:parser].is_a?(Proc)
Marshal.dump([http_method, path, opts])
Marshal.dump([http_method, path, opts, last_response, @last_uri, @raw_request])
end

private
Expand Down
18 changes: 18 additions & 0 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1511,10 +1511,28 @@

describe "marshalling" do
it "properly marshals the request object" do
MarshalFakeResponse = Struct.new(:body) do
def marshal_dump
{
"body" => body,
}
end

def marshal_load(hash)
self.body = hash["body"]
end
end
@request.last_uri = URI("http://api.foo.com/v1")
@request.last_response = MarshalFakeResponse.new("body")
@request.instance_variable_set("@raw_request", @request.http_method.new(URI("http://api.foo.com/v1")))
marshalled = Marshal.load(Marshal.dump(@request))

expect(marshalled.http_method).to eq @request.http_method
expect(marshalled.path).to eq @request.path
expect(marshalled.options).to eq @request.options
expect(marshalled.last_response.body).to eq "body"
expect(marshalled.last_uri).to eq URI("http://api.foo.com/v1")
expect(marshalled.instance_variable_get("@raw_request").path).to eq "/v1"
end
end
end

0 comments on commit fbeafe0

Please sign in to comment.