Make Faraday::Request serialisable with Marshal. (#803)

This commit is contained in:
Michiel 2018-05-28 22:42:12 +01:00 committed by Mattia
parent 8ba0797af9
commit f08a985bd1
2 changed files with 35 additions and 1 deletions

View File

@ -69,6 +69,26 @@ module Faraday
headers[key] = value
end
def marshal_dump
{
:method => method,
:body => body,
:headers => headers,
:path => path,
:params => params,
:options => options
}
end
def marshal_load(serialised)
self.method = serialised[:method]
self.body = serialised[:body]
self.headers = serialised[:headers]
self.path = serialised[:path]
self.params = serialised[:params]
self.options = serialised[:options]
end
# ENV Keys
# :method - a symbolized request method (:get, :post)
# :body - the request body that will eventually be converted to a string.

View File

@ -233,7 +233,7 @@ class ResponseTest < Faraday::TestCase
assert_equal :post, @response.env[:method]
end
def test_marshal
def test_marshal_response
@response = Faraday::Response.new
@response.on_complete { }
@response.finish @env.merge(:params => 'moo')
@ -243,6 +243,20 @@ class ResponseTest < Faraday::TestCase
assert_equal %w[body response_headers status], loaded.env.keys.map { |k| k.to_s }.sort
end
def test_marshal_request
@request = Faraday::Request.create(:post) do |request|
request.options = Faraday::RequestOptions.new
request.params = Faraday::Utils::ParamsHash.new({ 'a' => 'c' })
request.headers = { 'b' => 'd' }
request.body = 'hello, world!'
request.url 'http://localhost/foo'
end
loaded = Marshal.load(Marshal.dump(@request))
assert_equal @request, loaded
end
def test_hash
hash = @response.to_hash
assert_kind_of Hash, hash