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

Test with an image in the public domain. (#607)

Switch to using NASA photo S130-E-012142 of the ISS. It is in the public
domain because it is a work created solely by NASA.

Fixes: #606
This commit is contained in:
Andy Brody 2017-07-05 01:40:21 -04:00 committed by GitHub
parent 8c3375b299
commit 2e072dcbb2
6 changed files with 32 additions and 29 deletions

View file

@ -886,11 +886,10 @@ See AUTHORS for the full list.
## Legal ## Legal
Released under the MIT License: http://www.opensource.org/licenses/mit-license.php Released under the MIT License: https://opensource.org/licenses/MIT
"Master Shake" photo (http://www.flickr.com/photos/solgrundy/924205581/) by Photo of the International Space Station was produced by NASA and is in the
"SolGrundy"; used under terms of the Creative Commons Attribution-ShareAlike 2.0 public domain.
Generic license (http://creativecommons.org/licenses/by-sa/2.0/)
Code for reading Windows root certificate store derived from work by Puppet; Code for reading Windows root certificate store derived from work by Puppet;
used under terms of the Apache License, Version 2.0. used under terms of the Apache License, Version 2.0.

BIN
spec/ISS.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

View file

@ -47,4 +47,8 @@ module Helpers
cookie_jar: HTTP::CookieJar.new, redirection_history: nil, cookie_jar: HTTP::CookieJar.new, redirection_history: nil,
args: {url: url, method: method}) args: {url: url, method: method})
end end
def test_image_path
File.dirname(__FILE__) + "/ISS.jpg"
end
end end

Binary file not shown.

Before

Width:  |  Height:  |  Size: 75 KiB

View file

@ -2,7 +2,7 @@
require_relative '_lib' require_relative '_lib'
describe RestClient::Payload do describe RestClient::Payload, :include_helpers do
context "Base Payload" do context "Base Payload" do
it "should reset stream after to_s" do it "should reset stream after to_s" do
payload = RestClient::Payload::Base.new('foobar') payload = RestClient::Payload::Base.new('foobar')
@ -80,7 +80,7 @@ describe RestClient::Payload do
end end
it 'should not error on close if stream already closed' do 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'))) m = RestClient::Payload::Multipart.new(:file => File.new(test_image_path))
3.times {m.close} 3.times {m.close}
end end
@ -111,11 +111,11 @@ baz\r
end end
it "should form properly separated multipart data" do it "should form properly separated multipart data" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
m = RestClient::Payload::Multipart.new({:foo => f}) m = RestClient::Payload::Multipart.new({:foo => f})
expect(m.to_s).to eq <<-EOS expect(m.to_s).to eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="foo"; filename="master_shake.jpg"\r Content-Disposition: form-data; name="foo"; filename="ISS.jpg"\r
Content-Type: image/jpeg\r Content-Type: image/jpeg\r
\r \r
#{File.open(f.path, 'rb'){|bin| bin.read}}\r #{File.open(f.path, 'rb'){|bin| bin.read}}\r
@ -124,11 +124,11 @@ Content-Type: image/jpeg\r
end end
it "should ignore the name attribute when it's not set" do it "should ignore the name attribute when it's not set" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
m = RestClient::Payload::Multipart.new({nil => f}) m = RestClient::Payload::Multipart.new({nil => f})
expect(m.to_s).to eq <<-EOS expect(m.to_s).to eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; filename="master_shake.jpg"\r Content-Disposition: form-data; filename="ISS.jpg"\r
Content-Type: image/jpeg\r Content-Type: image/jpeg\r
\r \r
#{File.open(f.path, 'rb'){|bin| bin.read}}\r #{File.open(f.path, 'rb'){|bin| bin.read}}\r
@ -137,9 +137,9 @@ Content-Type: image/jpeg\r
end end
it "should detect optional (original) content type and filename" do it "should detect optional (original) content type and filename" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
f.instance_eval "def content_type; 'text/plain'; end" expect(f).to receive(:content_type).and_return('text/plain')
f.instance_eval "def original_filename; 'foo.txt'; end" expect(f).to receive(:original_filename).and_return('foo.txt')
m = RestClient::Payload::Multipart.new({:foo => f}) m = RestClient::Payload::Multipart.new({:foo => f})
expect(m.to_s).to eq <<-EOS expect(m.to_s).to eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
@ -161,7 +161,7 @@ foo\r
--#{m.boundary}--\r --#{m.boundary}--\r
EOS EOS
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
f.instance_eval "def content_type; 'text/plain'; end" f.instance_eval "def content_type; 'text/plain'; end"
f.instance_eval "def original_filename; 'foo.txt'; end" f.instance_eval "def original_filename; 'foo.txt'; end"
m = RestClient::Payload::Multipart.new({:foo => {:bar => f}}) m = RestClient::Payload::Multipart.new({:foo => {:bar => f}})
@ -177,7 +177,7 @@ Content-Type: text/plain\r
it 'should correctly format hex boundary' do it 'should correctly format hex boundary' do
allow(SecureRandom).to receive(:base64).with(12).and_return('TGs89+ttw/xna6TV') allow(SecureRandom).to receive(:base64).with(12).and_return('TGs89+ttw/xna6TV')
f = File.new(File.dirname(__FILE__) + '/master_shake.jpg') f = File.new(test_image_path)
m = RestClient::Payload::Multipart.new({:foo => f}) m = RestClient::Payload::Multipart.new({:foo => f})
expect(m.boundary).to eq('-' * 4 + 'RubyFormBoundary' + 'TGs89AttwBxna6TV') expect(m.boundary).to eq('-' * 4 + 'RubyFormBoundary' + 'TGs89AttwBxna6TV')
end end
@ -186,10 +186,10 @@ Content-Type: text/plain\r
context "streamed payloads" do context "streamed payloads" do
it "should properly determine the size of file payloads" do it "should properly determine the size of file payloads" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
payload = RestClient::Payload.generate(f) payload = RestClient::Payload.generate(f)
expect(payload.size).to eq 76_988 expect(payload.size).to eq 72_463
expect(payload.length).to eq 76_988 expect(payload.length).to eq 72_463
end end
it "should properly determine the size of other kinds of streaming payloads" do it "should properly determine the size of other kinds of streaming payloads" do
@ -211,7 +211,7 @@ Content-Type: text/plain\r
end end
it "should have a closed? method" do it "should have a closed? method" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
payload = RestClient::Payload.generate(f) payload = RestClient::Payload.generate(f)
expect(payload.closed?).to be_falsey expect(payload.closed?).to be_falsey
payload.close payload.close
@ -225,7 +225,7 @@ Content-Type: text/plain\r
end end
it "should recognize multipart params" do it "should recognize multipart params" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
expect(RestClient::Payload.generate({"foo" => f})).to be_kind_of(RestClient::Payload::Multipart) expect(RestClient::Payload.generate({"foo" => f})).to be_kind_of(RestClient::Payload::Multipart)
end end
@ -234,7 +234,7 @@ Content-Type: text/plain\r
end end
it "should handle deeply nested multipart" do it "should handle deeply nested multipart" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
params = {foo: RestClient::ParamsArray.new({nested: f})} params = {foo: RestClient::ParamsArray.new({nested: f})}
expect(RestClient::Payload.generate(params)).to be_kind_of(RestClient::Payload::Multipart) expect(RestClient::Payload.generate(params)).to be_kind_of(RestClient::Payload::Multipart)
end end
@ -245,17 +245,17 @@ Content-Type: text/plain\r
end end
it "should recognize nested multipart payloads in hashes" do it "should recognize nested multipart payloads in hashes" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
expect(RestClient::Payload.generate({"foo" => {"file" => f}})).to be_kind_of(RestClient::Payload::Multipart) expect(RestClient::Payload.generate({"foo" => {"file" => f}})).to be_kind_of(RestClient::Payload::Multipart)
end end
it "should recognize nested multipart payloads in arrays" do it "should recognize nested multipart payloads in arrays" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
expect(RestClient::Payload.generate({"foo" => [f]})).to be_kind_of(RestClient::Payload::Multipart) expect(RestClient::Payload.generate({"foo" => [f]})).to be_kind_of(RestClient::Payload::Multipart)
end end
it "should recognize file payloads that can be streamed" do it "should recognize file payloads that can be streamed" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
expect(RestClient::Payload.generate(f)).to be_kind_of(RestClient::Payload::Streamed) expect(RestClient::Payload.generate(f)).to be_kind_of(RestClient::Payload::Streamed)
end end
@ -269,7 +269,7 @@ Content-Type: text/plain\r
end end
it "should recognize multipart payload wrapped in ParamsArray" do it "should recognize multipart payload wrapped in ParamsArray" do
f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") f = File.new(test_image_path)
params = RestClient::ParamsArray.new([[:image, f]]) params = RestClient::ParamsArray.new([[:image, f]])
expect(RestClient::Payload.generate(params)).to be_kind_of(RestClient::Payload::Multipart) expect(RestClient::Payload.generate(params)).to be_kind_of(RestClient::Payload::Multipart)
end end
@ -283,8 +283,8 @@ Content-Type: text/plain\r
payloads = [ payloads = [
RestClient::Payload::Base.new('foobar'), RestClient::Payload::Base.new('foobar'),
RestClient::Payload::UrlEncoded.new({:foo => 'bar'}), RestClient::Payload::UrlEncoded.new({:foo => 'bar'}),
RestClient::Payload::Streamed.new(File.new(File.dirname(__FILE__) + "/master_shake.jpg")), RestClient::Payload::Streamed.new(File.new(test_image_path)),
RestClient::Payload::Multipart.new({myfile: File.new(File.dirname(__FILE__) + "/master_shake.jpg")}), RestClient::Payload::Multipart.new({myfile: File.new(test_image_path)}),
] ]
payloads.each do |payload| payloads.each do |payload|

View file

@ -1,6 +1,6 @@
require_relative '_lib' require_relative '_lib'
describe RestClient::Request do describe RestClient::Request, :include_helpers do
context 'params for GET requests' do context 'params for GET requests' do
it "manage params for get requests" do it "manage params for get requests" do
@ -43,7 +43,7 @@ describe RestClient::Request do
end end
it 'closes payload if not nil' do it 'closes payload if not nil' do
test_file = File.new(File.join( File.dirname(File.expand_path(__FILE__)), 'master_shake.jpg')) test_file = File.new(test_image_path)
stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*'}).to_return(:body => 'foo', :status => 200) stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(:url => 'http://some/resource', :method => :post, :payload => {:file => test_file}) RestClient::Request.execute(:url => 'http://some/resource', :method => :post, :payload => {:file => test_file})