2012-11-16 06:57:54 -05:00
|
|
|
# encoding: binary
|
2013-08-06 11:44:47 -04:00
|
|
|
|
2015-03-13 21:00:51 -04:00
|
|
|
require_relative '_lib'
|
2008-07-23 16:16:05 -04:00
|
|
|
|
2008-08-03 19:09:19 -04:00
|
|
|
describe RestClient::Payload do
|
2014-04-01 14:46:20 -04:00
|
|
|
context "Base Payload" do
|
|
|
|
it "should reset stream after to_s" do
|
|
|
|
payload = RestClient::Payload::Base.new('foobar')
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(payload.to_s).to eq 'foobar'
|
|
|
|
expect(payload.to_s).to eq 'foobar'
|
2014-04-01 14:46:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
context "A regular Payload" do
|
|
|
|
it "should use standard enctype as default content-type" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload::UrlEncoded.new({}).headers['Content-Type']).
|
|
|
|
to eq 'application/x-www-form-urlencoded'
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should form properly encoded params" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s).
|
|
|
|
to eq "foo=bar"
|
|
|
|
expect(["foo=bar&baz=qux", "baz=qux&foo=bar"]).to include(
|
2011-08-15 04:17:43 -04:00
|
|
|
RestClient::Payload::UrlEncoded.new({:foo => 'bar', :baz => 'qux'}).to_s)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-05-31 14:53:37 -04:00
|
|
|
it "should escape parameters" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload::UrlEncoded.new({'foo + bar' => 'baz'}).to_s).
|
|
|
|
to eq "foo+%2B+bar=baz"
|
2010-05-31 14:53:37 -04:00
|
|
|
end
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "should properly handle hashes as parameter" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s).
|
|
|
|
to eq "foo[bar]=baz"
|
|
|
|
expect(RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s).
|
|
|
|
to eq "foo[bar][baz]=qux"
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-03-02 14:21:04 -05:00
|
|
|
it "should handle many attributes inside a hash" do
|
|
|
|
parameters = RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz', :baz => 'qux'}}).to_s
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(parameters).to eq 'foo[bar]=baz&foo[baz]=qux'
|
2010-03-02 14:21:04 -05:00
|
|
|
end
|
|
|
|
|
2015-11-12 06:34:06 -05:00
|
|
|
it "should handle attributes inside an array inside an hash" do
|
2010-04-15 13:26:33 -04:00
|
|
|
parameters = RestClient::Payload::UrlEncoded.new({"foo" => [{"bar" => 'baz'}, {"bar" => 'qux'}]}).to_s
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(parameters).to eq 'foo[][bar]=baz&foo[][bar]=qux'
|
2010-04-15 13:26:33 -04:00
|
|
|
end
|
|
|
|
|
2015-11-12 06:34:06 -05:00
|
|
|
it "should handle arrays inside a hash inside a hash" do
|
|
|
|
parameters = RestClient::Payload::UrlEncoded.new({"foo" => {'even' => [0, 2], 'odd' => [1, 3]}}).to_s
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(parameters).to eq 'foo[even][]=0&foo[even][]=2&foo[odd][]=1&foo[odd][]=3'
|
2010-04-15 13:26:33 -04:00
|
|
|
end
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "should form properly use symbols as parameters" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s).
|
|
|
|
to eq "foo=bar"
|
|
|
|
expect(RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s).
|
|
|
|
to eq "foo[bar]=baz"
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-03-04 09:49:53 -05:00
|
|
|
it "should properly handle arrays as repeated parameters" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload::UrlEncoded.new({:foo => ['bar']}).to_s).
|
|
|
|
to eq "foo[]=bar"
|
|
|
|
expect(RestClient::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s).
|
|
|
|
to eq "foo[]=bar&foo[]=baz"
|
2010-01-28 23:08:57 -05:00
|
|
|
end
|
2013-07-31 22:24:18 -04:00
|
|
|
|
2011-07-07 13:04:29 -04:00
|
|
|
it 'should not close if stream already closed' do
|
|
|
|
p = RestClient::Payload::UrlEncoded.new({'foo ' => 'bar'})
|
|
|
|
3.times {p.close}
|
|
|
|
end
|
2010-01-28 23:08:57 -05:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "A multipart Payload" do
|
|
|
|
it "should use standard enctype as default content-type" do
|
|
|
|
m = RestClient::Payload::Multipart.new({})
|
2016-06-05 19:52:16 -04:00
|
|
|
allow(m).to receive(:boundary).and_return(123)
|
|
|
|
expect(m.headers['Content-Type']).to eq 'multipart/form-data; boundary=123'
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
2013-07-31 22:24:18 -04:00
|
|
|
|
2011-07-07 13:04:29 -04:00
|
|
|
it 'should not error on close if stream already closed' do
|
|
|
|
m = RestClient::Payload::Multipart.new(:file => File.new(File.join(File.dirname(File.expand_path(__FILE__)), 'master_shake.jpg')))
|
|
|
|
3.times {m.close}
|
|
|
|
end
|
2009-12-29 12:27:39 -05:00
|
|
|
|
2009-12-29 14:30:38 -05:00
|
|
|
it "should form properly separated multipart data" do
|
2009-12-29 12:27:39 -05:00
|
|
|
m = RestClient::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]])
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(m.to_s).to eq <<-EOS
|
2008-12-10 23:06:10 -05:00
|
|
|
--#{m.boundary}\r
|
2010-01-23 00:19:45 -05:00
|
|
|
Content-Disposition: form-data; name="bar"\r
|
2009-08-12 15:33:28 -04:00
|
|
|
\r
|
|
|
|
baz\r
|
|
|
|
--#{m.boundary}\r
|
2010-01-23 00:19:45 -05:00
|
|
|
Content-Disposition: form-data; name="foo"\r
|
2008-07-23 16:16:05 -04:00
|
|
|
\r
|
|
|
|
bar\r
|
2008-12-10 23:06:10 -05:00
|
|
|
--#{m.boundary}--\r
|
2009-12-29 12:27:39 -05:00
|
|
|
EOS
|
|
|
|
end
|
2008-07-23 16:16:05 -04:00
|
|
|
|
2010-05-31 14:53:37 -04:00
|
|
|
it "should not escape parameters names" do
|
|
|
|
m = RestClient::Payload::Multipart.new([["bar ", "baz"]])
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(m.to_s).to eq <<-EOS
|
2010-05-31 14:53:37 -04:00
|
|
|
--#{m.boundary}\r
|
|
|
|
Content-Disposition: form-data; name="bar "\r
|
|
|
|
\r
|
|
|
|
baz\r
|
|
|
|
--#{m.boundary}--\r
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2009-12-29 14:30:38 -05:00
|
|
|
it "should form properly separated multipart data" do
|
2009-12-29 12:27:39 -05:00
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
|
|
|
m = RestClient::Payload::Multipart.new({:foo => f})
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(m.to_s).to eq <<-EOS
|
2009-12-26 06:04:13 -05:00
|
|
|
--#{m.boundary}\r
|
2010-01-23 00:19:45 -05:00
|
|
|
Content-Disposition: form-data; name="foo"; filename="master_shake.jpg"\r
|
2009-12-26 07:22:31 -05:00
|
|
|
Content-Type: image/jpeg\r
|
2009-12-26 06:04:13 -05:00
|
|
|
\r
|
2012-11-16 06:57:54 -05:00
|
|
|
#{File.open(f.path, 'rb'){|bin| bin.read}}\r
|
2009-12-26 06:04:13 -05:00
|
|
|
--#{m.boundary}--\r
|
2009-12-29 12:27:39 -05:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2010-03-22 13:43:41 -04:00
|
|
|
it "should ignore the name attribute when it's not set" do
|
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
|
|
|
m = RestClient::Payload::Multipart.new({nil => f})
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(m.to_s).to eq <<-EOS
|
2010-03-22 13:43:41 -04:00
|
|
|
--#{m.boundary}\r
|
|
|
|
Content-Disposition: form-data; filename="master_shake.jpg"\r
|
|
|
|
Content-Type: image/jpeg\r
|
|
|
|
\r
|
2012-11-16 06:57:54 -05:00
|
|
|
#{File.open(f.path, 'rb'){|bin| bin.read}}\r
|
2010-03-22 13:43:41 -04:00
|
|
|
--#{m.boundary}--\r
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "should detect optional (original) content type and filename" do
|
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
|
|
|
f.instance_eval "def content_type; 'text/plain'; end"
|
|
|
|
f.instance_eval "def original_filename; 'foo.txt'; end"
|
|
|
|
m = RestClient::Payload::Multipart.new({:foo => f})
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(m.to_s).to eq <<-EOS
|
2008-12-10 23:06:10 -05:00
|
|
|
--#{m.boundary}\r
|
2010-01-23 00:19:45 -05:00
|
|
|
Content-Disposition: form-data; name="foo"; filename="foo.txt"\r
|
2009-12-26 07:22:31 -05:00
|
|
|
Content-Type: text/plain\r
|
2008-07-23 16:16:05 -04:00
|
|
|
\r
|
2012-11-16 06:57:54 -05:00
|
|
|
#{File.open(f.path, 'rb'){|bin| bin.read}}\r
|
2008-12-10 23:06:10 -05:00
|
|
|
--#{m.boundary}--\r
|
2009-12-29 12:27:39 -05:00
|
|
|
EOS
|
|
|
|
end
|
2009-09-18 12:55:31 -04:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "should handle hash in hash parameters" do
|
|
|
|
m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}})
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(m.to_s).to eq <<-EOS
|
2009-12-26 07:22:31 -05:00
|
|
|
--#{m.boundary}\r
|
2010-01-23 00:19:45 -05:00
|
|
|
Content-Disposition: form-data; name="bar[baz]"\r
|
2009-12-26 07:22:31 -05:00
|
|
|
\r
|
|
|
|
foo\r
|
|
|
|
--#{m.boundary}--\r
|
2009-12-29 12:27:39 -05:00
|
|
|
EOS
|
2009-12-26 07:22:31 -05:00
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
|
|
|
f.instance_eval "def content_type; 'text/plain'; end"
|
|
|
|
f.instance_eval "def original_filename; 'foo.txt'; end"
|
|
|
|
m = RestClient::Payload::Multipart.new({:foo => {:bar => f}})
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(m.to_s).to eq <<-EOS
|
2009-09-18 12:55:31 -04:00
|
|
|
--#{m.boundary}\r
|
2010-01-23 00:19:45 -05:00
|
|
|
Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r
|
2009-09-18 12:55:31 -04:00
|
|
|
Content-Type: text/plain\r
|
|
|
|
\r
|
2012-11-16 06:57:54 -05:00
|
|
|
#{File.open(f.path, 'rb'){|bin| bin.read}}\r
|
2009-09-18 12:55:31 -04:00
|
|
|
--#{m.boundary}--\r
|
2009-12-29 12:27:39 -05:00
|
|
|
EOS
|
|
|
|
end
|
|
|
|
|
2015-11-14 21:37:56 -05:00
|
|
|
it 'should correctly format hex boundary' do
|
2016-06-05 19:52:16 -04:00
|
|
|
allow(SecureRandom).to receive(:base64).with(12).and_return('TGs89+ttw/xna6TV')
|
2015-11-14 21:37:56 -05:00
|
|
|
f = File.new(File.dirname(__FILE__) + '/master_shake.jpg')
|
|
|
|
m = RestClient::Payload::Multipart.new({:foo => f})
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(m.boundary).to eq('-' * 4 + 'RubyFormBoundary' + 'TGs89AttwBxna6TV')
|
2015-11-14 21:37:56 -05:00
|
|
|
end
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2010-06-18 19:28:52 -04:00
|
|
|
context "streamed payloads" do
|
|
|
|
it "should properly determine the size of file payloads" do
|
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
|
|
|
payload = RestClient::Payload.generate(f)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(payload.size).to eq 76_988
|
|
|
|
expect(payload.length).to eq 76_988
|
2010-06-18 19:28:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should properly determine the size of other kinds of streaming payloads" do
|
|
|
|
s = StringIO.new 'foo'
|
|
|
|
payload = RestClient::Payload.generate(s)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(payload.size).to eq 3
|
|
|
|
expect(payload.length).to eq 3
|
2010-06-18 19:28:52 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
f = Tempfile.new "rest-client"
|
|
|
|
f.write 'foo bar'
|
|
|
|
|
|
|
|
payload = RestClient::Payload.generate(f)
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(payload.size).to eq 7
|
|
|
|
expect(payload.length).to eq 7
|
2010-06-18 19:28:52 -04:00
|
|
|
ensure
|
|
|
|
f.close
|
|
|
|
end
|
|
|
|
end
|
2017-05-22 01:31:06 -04:00
|
|
|
|
|
|
|
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
|
2010-06-18 19:28:52 -04:00
|
|
|
end
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
context "Payload generation" do
|
|
|
|
it "should recognize standard urlencoded params" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate({"foo" => 'bar'})).to be_kind_of(RestClient::Payload::UrlEncoded)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should recognize multipart params" do
|
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate({"foo" => f})).to be_kind_of(RestClient::Payload::Multipart)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should be multipart if forced" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate({"foo" => "bar", :multipart => true})).to be_kind_of(RestClient::Payload::Multipart)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2015-11-26 22:30:58 -05:00
|
|
|
it "should handle deeply nested multipart" do
|
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
|
|
|
params = {foo: RestClient::ParamsArray.new({nested: f})}
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate(params)).to be_kind_of(RestClient::Payload::Multipart)
|
2015-11-26 22:30:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-12-29 12:27:39 -05:00
|
|
|
it "should return data if no of the above" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate("data")).to be_kind_of(RestClient::Payload::Base)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2011-08-15 04:17:43 -04:00
|
|
|
it "should recognize nested multipart payloads in hashes" do
|
2009-12-29 12:27:39 -05:00
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate({"foo" => {"file" => f}})).to be_kind_of(RestClient::Payload::Multipart)
|
2009-12-29 12:27:39 -05:00
|
|
|
end
|
|
|
|
|
2011-08-15 04:17:43 -04:00
|
|
|
it "should recognize nested multipart payloads in arrays" do
|
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate({"foo" => [f]})).to be_kind_of(RestClient::Payload::Multipart)
|
2011-08-15 04:17:43 -04:00
|
|
|
end
|
|
|
|
|
2010-06-18 19:28:52 -04:00
|
|
|
it "should recognize file payloads that can be streamed" do
|
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate(f)).to be_kind_of(RestClient::Payload::Streamed)
|
2010-06-18 19:28:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should recognize other payloads that can be streamed" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate(StringIO.new('foo'))).to be_kind_of(RestClient::Payload::Streamed)
|
2010-06-18 19:28:52 -04:00
|
|
|
end
|
2012-11-16 06:57:54 -05:00
|
|
|
|
|
|
|
# hashery gem introduces Hash#read convenience method. Existence of #read method used to determine of content is streameable :/
|
|
|
|
it "shouldn't treat hashes as streameable" do
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(RestClient::Payload.generate({"foo" => 'bar'})).to be_kind_of(RestClient::Payload::UrlEncoded)
|
2012-11-16 06:57:54 -05:00
|
|
|
end
|
2016-07-05 04:56:27 -04:00
|
|
|
|
|
|
|
it "should recognize multipart payload wrapped in ParamsArray" do
|
|
|
|
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg")
|
|
|
|
params = RestClient::ParamsArray.new([[:image, f]])
|
|
|
|
expect(RestClient::Payload.generate(params)).to be_kind_of(RestClient::Payload::Multipart)
|
|
|
|
end
|
2017-05-22 01:05:53 -04:00
|
|
|
|
|
|
|
it "should handle non-multipart payload wrapped in ParamsArray" do
|
|
|
|
params = RestClient::ParamsArray.new([[:arg, 'value1'], [:arg, 'value2']])
|
|
|
|
expect(RestClient::Payload.generate(params)).to be_kind_of(RestClient::Payload::UrlEncoded)
|
|
|
|
end
|
2017-05-22 01:31:06 -04:00
|
|
|
|
|
|
|
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
|
2012-11-16 06:57:54 -05:00
|
|
|
end
|
2008-07-23 16:16:05 -04:00
|
|
|
end
|