1
0
Fork 0
mirror of https://github.com/rest-client/rest-client.git synced 2022-11-09 13:49:40 -05:00

Fix up Payload generation to be slightly nicer.

- If Payload.generate() receives a Payload::Base or subclass, then
  return it as is rather than pointlessly wrapping it in a
  Payload::Streamed.
- Add a `.closed?` method to Payload::Base that wraps the underlying
  stream's `.closed?` method.
This commit is contained in:
Andy Brody 2017-05-22 01:31:06 -04:00
parent 950e25191d
commit 36c80d8b32
2 changed files with 34 additions and 2 deletions

View file

@ -14,7 +14,10 @@ module RestClient
extend self
def generate(params)
if params.is_a?(String)
if params.is_a?(RestClient::Payload::Base)
# pass through Payload objects unchanged
params
elsif params.is_a?(String)
Base.new(params)
elsif params.is_a?(Hash)
if params.delete(:multipart) == true || has_file?(params)
@ -87,12 +90,20 @@ module RestClient
@stream.close unless @stream.closed?
end
def closed?
@stream.closed?
end
def to_s_inspect
to_s.inspect
end
def short_inspect
(size > 500 ? "#{size} byte(s) length" : to_s_inspect)
if size && size > 500
"#{size} byte(s) length"
else
to_s_inspect
end
end
end

View file

@ -209,6 +209,14 @@ Content-Type: text/plain\r
f.close
end
end
it "should have a closed? method" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
payload = RestClient::Payload.generate(f)
expect(payload.closed?).to be_falsey
payload.close
expect(payload.closed?).to be_truthy
end
end
context "Payload generation" do
@ -270,5 +278,18 @@ Content-Type: text/plain\r
params = RestClient::ParamsArray.new([[:arg, 'value1'], [:arg, 'value2']])
expect(RestClient::Payload.generate(params)).to be_kind_of(RestClient::Payload::UrlEncoded)
end
it "should pass through Payload::Base and subclasses unchanged" do
payloads = [
RestClient::Payload::Base.new('foobar'),
RestClient::Payload::UrlEncoded.new({:foo => 'bar'}),
RestClient::Payload::Streamed.new(File.new(File.dirname(__FILE__) + "/master_shake.jpg")),
RestClient::Payload::Multipart.new({myfile: File.new(File.dirname(__FILE__) + "/master_shake.jpg")}),
]
payloads.each do |payload|
expect(RestClient::Payload.generate(payload)).to equal(payload)
end
end
end
end