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

Convert specs to RSpec 2.99.2 syntax with Transpec

This conversion is done by Transpec 3.2.2 with the following command:
    transpec

* 317 conversions
    from: obj.should
      to: expect(obj).to

* 160 conversions
    from: obj.stub(:message)
      to: allow(obj).to receive(:message)

* 100 conversions
    from: obj.should_receive(:message)
      to: expect(obj).to receive(:message)

* 30 conversions
    from: lambda { }.should
      to: expect { }.to

* 22 conversions
    from: obj.should_not_receive(:message)
      to: expect(obj).not_to receive(:message)

* 4 conversions
    from: obj.should_not
      to: expect(obj).not_to

* 2 conversions
    from: == expected
      to: eq(expected)

* 1 conversion
    from: expect(collection).to have_at_least(n).items
      to: expect(collection.size).to be >= n

* 1 conversion
    from: obj.unstub(:message)
      to: allow(obj).to receive(:message).and_call_original

For more details: https://github.com/yujinakayama/transpec#supported-conversions
This commit is contained in:
Andy Brody 2016-06-05 19:52:16 -04:00
parent 718c5ffb4e
commit 4971d1a6e1
15 changed files with 694 additions and 694 deletions

View file

@ -41,22 +41,22 @@ describe RestClient::Request do
describe '.execute' do describe '.execute' do
it 'sends a user agent' do it 'sends a user agent' do
data = execute_httpbin_json('user-agent', method: :get) data = execute_httpbin_json('user-agent', method: :get)
data['user-agent'].should match(/rest-client/) expect(data['user-agent']).to match(/rest-client/)
end end
it 'receives cookies on 302' do it 'receives cookies on 302' do
expect { expect {
execute_httpbin('cookies/set?foo=bar', method: :get, max_redirects: 0) execute_httpbin('cookies/set?foo=bar', method: :get, max_redirects: 0)
}.to raise_error(RestClient::Found) { |ex| }.to raise_error(RestClient::Found) { |ex|
ex.http_code.should eq 302 expect(ex.http_code).to eq 302
ex.response.cookies['foo'].should eq 'bar' expect(ex.response.cookies['foo']).to eq 'bar'
} }
end end
it 'passes along cookies through 302' do it 'passes along cookies through 302' do
data = execute_httpbin_json('cookies/set?foo=bar', method: :get) data = execute_httpbin_json('cookies/set?foo=bar', method: :get)
data.should have_key('cookies') expect(data).to have_key('cookies')
data['cookies']['foo'].should eq 'bar' expect(data['cookies']['foo']).to eq 'bar'
end end
it 'handles quote wrapped cookies' do it 'handles quote wrapped cookies' do
@ -64,8 +64,8 @@ describe RestClient::Request do
execute_httpbin('cookies/set?foo=' + CGI.escape('"bar:baz"'), execute_httpbin('cookies/set?foo=' + CGI.escape('"bar:baz"'),
method: :get, max_redirects: 0) method: :get, max_redirects: 0)
}.to raise_error(RestClient::Found) { |ex| }.to raise_error(RestClient::Found) { |ex|
ex.http_code.should eq 302 expect(ex.http_code).to eq 302
ex.response.cookies['foo'].should eq '"bar:baz"' expect(ex.response.cookies['foo']).to eq '"bar:baz"'
} }
end end
end end

View file

@ -8,15 +8,15 @@ describe RestClient do
body = 'abc' body = 'abc'
stub_request(:get, "www.example.com").to_return(:body => body, :status => 200) stub_request(:get, "www.example.com").to_return(:body => body, :status => 200)
response = RestClient.get "www.example.com" response = RestClient.get "www.example.com"
response.code.should eq 200 expect(response.code).to eq 200
response.body.should eq body expect(response.body).to eq body
end end
it "a simple request with gzipped content" do it "a simple request with gzipped content" do
stub_request(:get, "www.example.com").with(:headers => { 'Accept-Encoding' => 'gzip, deflate' }).to_return(:body => "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000", :status => 200, :headers => { 'Content-Encoding' => 'gzip' } ) stub_request(:get, "www.example.com").with(:headers => { 'Accept-Encoding' => 'gzip, deflate' }).to_return(:body => "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000", :status => 200, :headers => { 'Content-Encoding' => 'gzip' } )
response = RestClient.get "www.example.com" response = RestClient.get "www.example.com"
response.code.should eq 200 expect(response.code).to eq 200
response.body.should eq "i'm gziped\n" expect(response.body).to eq "i'm gziped\n"
end end
it "a 404" do it "a 404" do
@ -26,10 +26,10 @@ describe RestClient do
RestClient.get "www.example.com" RestClient.get "www.example.com"
raise raise
rescue RestClient::ResourceNotFound => e rescue RestClient::ResourceNotFound => e
e.http_code.should eq 404 expect(e.http_code).to eq 404
e.response.code.should eq 404 expect(e.response.code).to eq 404
e.response.body.should eq body expect(e.response.body).to eq body
e.http_body.should eq body expect(e.http_body).to eq body
end end
end end
@ -41,8 +41,8 @@ describe RestClient do
'Content-Type' => 'text/plain; charset=UTF-8' 'Content-Type' => 'text/plain; charset=UTF-8'
}) })
response = RestClient.get "www.example.com" response = RestClient.get "www.example.com"
response.encoding.should eq Encoding::UTF_8 expect(response.encoding).to eq Encoding::UTF_8
response.valid_encoding?.should eq true expect(response.valid_encoding?).to eq true
end end
it 'handles windows-1252' do it 'handles windows-1252' do
@ -52,9 +52,9 @@ describe RestClient do
'Content-Type' => 'text/plain; charset=windows-1252' 'Content-Type' => 'text/plain; charset=windows-1252'
}) })
response = RestClient.get "www.example.com" response = RestClient.get "www.example.com"
response.encoding.should eq Encoding::WINDOWS_1252 expect(response.encoding).to eq Encoding::WINDOWS_1252
response.encode('utf-8').should eq "ÿ" expect(response.encode('utf-8')).to eq "ÿ"
response.valid_encoding?.should eq true expect(response.valid_encoding?).to eq true
end end
it 'handles binary' do it 'handles binary' do
@ -64,28 +64,28 @@ describe RestClient do
'Content-Type' => 'application/octet-stream; charset=binary' 'Content-Type' => 'application/octet-stream; charset=binary'
}) })
response = RestClient.get "www.example.com" response = RestClient.get "www.example.com"
response.encoding.should eq Encoding::BINARY expect(response.encoding).to eq Encoding::BINARY
lambda { expect {
response.encode('utf-8') response.encode('utf-8')
}.should raise_error(Encoding::UndefinedConversionError) }.to raise_error(Encoding::UndefinedConversionError)
response.valid_encoding?.should eq true expect(response.valid_encoding?).to eq true
end end
it 'handles euc-jp' do it 'handles euc-jp' do
body = "\xA4\xA2\xA4\xA4\xA4\xA6\xA4\xA8\xA4\xAA". body = "\xA4\xA2\xA4\xA4\xA4\xA6\xA4\xA8\xA4\xAA".
force_encoding(Encoding::BINARY) force_encoding(Encoding::BINARY)
body_utf8 = 'あいうえお' body_utf8 = 'あいうえお'
body_utf8.encoding.should eq Encoding::UTF_8 expect(body_utf8.encoding).to eq Encoding::UTF_8
stub_request(:get, 'www.example.com').to_return( stub_request(:get, 'www.example.com').to_return(
:body => body, :status => 200, :headers => { :body => body, :status => 200, :headers => {
'Content-Type' => 'text/plain; charset=EUC-JP' 'Content-Type' => 'text/plain; charset=EUC-JP'
}) })
response = RestClient.get 'www.example.com' response = RestClient.get 'www.example.com'
response.encoding.should eq Encoding::EUC_JP expect(response.encoding).to eq Encoding::EUC_JP
response.valid_encoding?.should eq true expect(response.valid_encoding?).to eq true
response.length.should eq 5 expect(response.length).to eq 5
response.encode('utf-8').should eq body_utf8 expect(response.encode('utf-8')).to eq body_utf8
end end
it 'defaults to Encoding.default_external' do it 'defaults to Encoding.default_external' do
@ -95,7 +95,7 @@ describe RestClient do
}) })
response = RestClient.get 'www.example.com' response = RestClient.get 'www.example.com'
response.encoding.should eq Encoding.default_external expect(response.encoding).to eq Encoding.default_external
end end
it 'handles invalid encoding' do it 'handles invalid encoding' do
@ -105,7 +105,7 @@ describe RestClient do
}) })
response = RestClient.get 'www.example.com' response = RestClient.get 'www.example.com'
response.encoding.should eq Encoding.default_external expect(response.encoding).to eq Encoding.default_external
end end
it 'leaves images as binary' do it 'leaves images as binary' do
@ -117,7 +117,7 @@ describe RestClient do
}) })
response = RestClient.get 'www.example.com' response = RestClient.get 'www.example.com'
response.encoding.should eq Encoding::BINARY expect(response.encoding).to eq Encoding::BINARY
end end
end end
end end

View file

@ -75,7 +75,7 @@ describe RestClient::Request do
}, },
) )
expect {request.execute }.to_not raise_error expect {request.execute }.to_not raise_error
ran_callback.should eq(true) expect(ran_callback).to eq(true)
end end
it "fails verification when the callback returns false", it "fails verification when the callback returns false",

View file

@ -22,124 +22,124 @@ describe RestClient::AbstractResponse, :include_helpers do
end end
it "fetches the numeric response code" do it "fetches the numeric response code" do
@net_http_res.should_receive(:code).and_return('200') expect(@net_http_res).to receive(:code).and_return('200')
@response.code.should eq 200 expect(@response.code).to eq 200
end end
it "has a nice description" do it "has a nice description" do
@net_http_res.should_receive(:to_hash).and_return({'Content-Type' => ['application/pdf']}) expect(@net_http_res).to receive(:to_hash).and_return({'Content-Type' => ['application/pdf']})
@net_http_res.should_receive(:code).and_return('200') expect(@net_http_res).to receive(:code).and_return('200')
@response.description.should eq "200 OK | application/pdf bytes\n" expect(@response.description).to eq "200 OK | application/pdf bytes\n"
end end
describe '.beautify_headers' do describe '.beautify_headers' do
it "beautifies the headers by turning the keys to symbols" do it "beautifies the headers by turning the keys to symbols" do
h = RestClient::AbstractResponse.beautify_headers('content-type' => [ 'x' ]) h = RestClient::AbstractResponse.beautify_headers('content-type' => [ 'x' ])
h.keys.first.should eq :content_type expect(h.keys.first).to eq :content_type
end end
it "beautifies the headers by turning the values to strings instead of one-element arrays" do it "beautifies the headers by turning the values to strings instead of one-element arrays" do
h = RestClient::AbstractResponse.beautify_headers('x' => [ 'text/html' ] ) h = RestClient::AbstractResponse.beautify_headers('x' => [ 'text/html' ] )
h.values.first.should eq 'text/html' expect(h.values.first).to eq 'text/html'
end end
it 'joins multiple header values by comma' do it 'joins multiple header values by comma' do
RestClient::AbstractResponse.beautify_headers( expect(RestClient::AbstractResponse.beautify_headers(
{'My-Header' => ['one', 'two']} {'My-Header' => ['one', 'two']}
).should eq({:my_header => 'one, two'}) )).to eq({:my_header => 'one, two'})
end end
it 'leaves set-cookie headers as array' do it 'leaves set-cookie headers as array' do
RestClient::AbstractResponse.beautify_headers( expect(RestClient::AbstractResponse.beautify_headers(
{'Set-Cookie' => ['cookie1=foo', 'cookie2=bar']} {'Set-Cookie' => ['cookie1=foo', 'cookie2=bar']}
).should eq({:set_cookie => ['cookie1=foo', 'cookie2=bar']}) )).to eq({:set_cookie => ['cookie1=foo', 'cookie2=bar']})
end end
end end
it "fetches the headers" do it "fetches the headers" do
@net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ]) expect(@net_http_res).to receive(:to_hash).and_return('content-type' => [ 'text/html' ])
@response.headers.should eq({ :content_type => 'text/html' }) expect(@response.headers).to eq({ :content_type => 'text/html' })
end end
it "extracts cookies from response headers" do it "extracts cookies from response headers" do
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/']) expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
@response.cookies.should eq({ 'session_id' => '1' }) expect(@response.cookies).to eq({ 'session_id' => '1' })
end end
it "extract strange cookies" do it "extract strange cookies" do
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/']) expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/'])
@response.headers.should eq({:set_cookie => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/']}) expect(@response.headers).to eq({:set_cookie => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/']})
@response.cookies.should eq({ 'session_id' => 'ZJ/HQVH6YE+rVkTpn0zvTQ==' }) expect(@response.cookies).to eq({ 'session_id' => 'ZJ/HQVH6YE+rVkTpn0zvTQ==' })
end end
it "doesn't escape cookies" do it "doesn't escape cookies" do
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca; path=/']) expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca; path=/'])
@response.cookies.should eq({ 'session_id' => 'BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca' }) expect(@response.cookies).to eq({ 'session_id' => 'BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca' })
end end
describe '.cookie_jar' do describe '.cookie_jar' do
it 'extracts cookies into cookie jar' do it 'extracts cookies into cookie jar' do
@net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/']) expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
@response.cookie_jar.should be_a HTTP::CookieJar expect(@response.cookie_jar).to be_a HTTP::CookieJar
cookie = @response.cookie_jar.cookies.first cookie = @response.cookie_jar.cookies.first
cookie.domain.should eq 'example.com' expect(cookie.domain).to eq 'example.com'
cookie.name.should eq 'session_id' expect(cookie.name).to eq 'session_id'
cookie.value.should eq '1' expect(cookie.value).to eq '1'
cookie.path.should eq '/' expect(cookie.path).to eq '/'
end end
it 'handles cookies when URI scheme is implicit' do it 'handles cookies when URI scheme is implicit' do
net_http_res = double('net http response') net_http_res = double('net http response')
net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/']) expect(net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/'])
request = double(url: 'example.com', uri: URI.parse('http://example.com'), request = double(url: 'example.com', uri: URI.parse('http://example.com'),
method: 'get', cookie_jar: HTTP::CookieJar.new) method: 'get', cookie_jar: HTTP::CookieJar.new)
response = MyAbstractResponse.new(net_http_res, request) response = MyAbstractResponse.new(net_http_res, request)
response.cookie_jar.should be_a HTTP::CookieJar expect(response.cookie_jar).to be_a HTTP::CookieJar
cookie = response.cookie_jar.cookies.first cookie = response.cookie_jar.cookies.first
cookie.domain.should eq 'example.com' expect(cookie.domain).to eq 'example.com'
cookie.name.should eq 'session_id' expect(cookie.name).to eq 'session_id'
cookie.value.should eq '1' expect(cookie.value).to eq '1'
cookie.path.should eq '/' expect(cookie.path).to eq '/'
end end
end end
it "can access the net http result directly" do it "can access the net http result directly" do
@response.net_http_res.should eq @net_http_res expect(@response.net_http_res).to eq @net_http_res
end end
describe "#return!" do describe "#return!" do
it "should return the response itself on 200-codes" do it "should return the response itself on 200-codes" do
@net_http_res.should_receive(:code).and_return('200') expect(@net_http_res).to receive(:code).and_return('200')
@response.return!.should be_equal(@response) expect(@response.return!).to be_equal(@response)
end end
it "should raise RequestFailed on unknown codes" do it "should raise RequestFailed on unknown codes" do
@net_http_res.should_receive(:code).and_return('1000') expect(@net_http_res).to receive(:code).and_return('1000')
lambda { @response.return! }.should raise_error RestClient::RequestFailed expect { @response.return! }.to raise_error RestClient::RequestFailed
end end
it "should raise an error on a redirection after non-GET/HEAD requests" do it "should raise an error on a redirection after non-GET/HEAD requests" do
@net_http_res.should_receive(:code).and_return('301') expect(@net_http_res).to receive(:code).and_return('301')
@request.should_receive(:method).and_return('put') expect(@request).to receive(:method).and_return('put')
@response.should_not_receive(:follow_redirection) expect(@response).not_to receive(:follow_redirection)
lambda { @response.return! }.should raise_error RestClient::RequestFailed expect { @response.return! }.to raise_error RestClient::RequestFailed
end end
it "should follow 302 redirect" do it "should follow 302 redirect" do
@net_http_res.should_receive(:code).and_return('302') expect(@net_http_res).to receive(:code).and_return('302')
@response.should_receive(:check_max_redirects).and_return('fake-check') expect(@response).to receive(:check_max_redirects).and_return('fake-check')
@response.should_receive(:follow_redirection).and_return('fake-redirection') expect(@response).to receive(:follow_redirection).and_return('fake-redirection')
@response.return!.should eq 'fake-redirection' expect(@response.return!).to eq 'fake-redirection'
end end
it "should gracefully handle 302 redirect with no location header" do it "should gracefully handle 302 redirect with no location header" do
@net_http_res = response_double(code: 302, location: nil) @net_http_res = response_double(code: 302, location: nil)
@request = request_double() @request = request_double()
@response = MyAbstractResponse.new(@net_http_res, @request) @response = MyAbstractResponse.new(@net_http_res, @request)
@response.should_receive(:check_max_redirects).and_return('fake-check') expect(@response).to receive(:check_max_redirects).and_return('fake-check')
lambda { @response.return! }.should raise_error RestClient::Found expect { @response.return! }.to raise_error RestClient::Found
end end
end end
end end

View file

@ -3,30 +3,30 @@ require_relative '_lib'
describe RestClient::Exception do describe RestClient::Exception do
it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do it "returns a 'message' equal to the class name if the message is not set, because 'message' should not be nil" do
e = RestClient::Exception.new e = RestClient::Exception.new
e.message.should eq "RestClient::Exception" expect(e.message).to eq "RestClient::Exception"
end end
it "returns the 'message' that was set" do it "returns the 'message' that was set" do
e = RestClient::Exception.new e = RestClient::Exception.new
message = "An explicitly set message" message = "An explicitly set message"
e.message = message e.message = message
e.message.should eq message expect(e.message).to eq message
end end
it "sets the exception message to ErrorMessage" do it "sets the exception message to ErrorMessage" do
RestClient::ResourceNotFound.new.message.should eq 'Not Found' expect(RestClient::ResourceNotFound.new.message).to eq 'Not Found'
end end
it "contains exceptions in RestClient" do it "contains exceptions in RestClient" do
RestClient::Unauthorized.new.should be_a_kind_of(RestClient::Exception) expect(RestClient::Unauthorized.new).to be_a_kind_of(RestClient::Exception)
RestClient::ServerBrokeConnection.new.should be_a_kind_of(RestClient::Exception) expect(RestClient::ServerBrokeConnection.new).to be_a_kind_of(RestClient::Exception)
end end
end end
describe RestClient::ServerBrokeConnection do describe RestClient::ServerBrokeConnection do
it "should have a default message of 'Server broke connection'" do it "should have a default message of 'Server broke connection'" do
e = RestClient::ServerBrokeConnection.new e = RestClient::ServerBrokeConnection.new
e.message.should eq 'Server broke connection' expect(e.message).to eq 'Server broke connection'
end end
end end
@ -40,21 +40,21 @@ describe RestClient::RequestFailed do
begin begin
raise RestClient::RequestFailed, response raise RestClient::RequestFailed, response
rescue RestClient::RequestFailed => e rescue RestClient::RequestFailed => e
e.response.should eq response expect(e.response).to eq response
end end
end end
it "http_code convenience method for fetching the code as an integer" do it "http_code convenience method for fetching the code as an integer" do
RestClient::RequestFailed.new(@response).http_code.should eq 502 expect(RestClient::RequestFailed.new(@response).http_code).to eq 502
end end
it "http_body convenience method for fetching the body (decoding when necessary)" do it "http_body convenience method for fetching the body (decoding when necessary)" do
RestClient::RequestFailed.new(@response).http_code.should eq 502 expect(RestClient::RequestFailed.new(@response).http_code).to eq 502
RestClient::RequestFailed.new(@response).message.should eq 'HTTP status code 502' expect(RestClient::RequestFailed.new(@response).message).to eq 'HTTP status code 502'
end end
it "shows the status code in the message" do it "shows the status code in the message" do
RestClient::RequestFailed.new(@response).to_s.should match(/502/) expect(RestClient::RequestFailed.new(@response).to_s).to match(/502/)
end end
end end
@ -64,7 +64,7 @@ describe RestClient::ResourceNotFound do
begin begin
raise RestClient::ResourceNotFound, response raise RestClient::ResourceNotFound, response
rescue RestClient::ResourceNotFound => e rescue RestClient::ResourceNotFound => e
e.response.should eq response expect(e.response).to eq response
end end
end end
@ -75,34 +75,34 @@ describe RestClient::ResourceNotFound do
RestClient.get "www.example.com" RestClient.get "www.example.com"
raise raise
rescue RestClient::ResourceNotFound => e rescue RestClient::ResourceNotFound => e
e.response.body.should eq body expect(e.response.body).to eq body
end end
end end
end end
describe "backwards compatibility" do describe "backwards compatibility" do
it 'aliases RestClient::NotFound as ResourceNotFound' do it 'aliases RestClient::NotFound as ResourceNotFound' do
RestClient::ResourceNotFound.should eq RestClient::NotFound expect(RestClient::ResourceNotFound).to eq RestClient::NotFound
end end
it 'aliases old names for HTTP 413, 414, 416' do it 'aliases old names for HTTP 413, 414, 416' do
RestClient::RequestEntityTooLarge.should eq RestClient::PayloadTooLarge expect(RestClient::RequestEntityTooLarge).to eq RestClient::PayloadTooLarge
RestClient::RequestURITooLong.should eq RestClient::URITooLong expect(RestClient::RequestURITooLong).to eq RestClient::URITooLong
RestClient::RequestedRangeNotSatisfiable.should eq RestClient::RangeNotSatisfiable expect(RestClient::RequestedRangeNotSatisfiable).to eq RestClient::RangeNotSatisfiable
end end
it 'subclasses NotFound from RequestFailed, ExceptionWithResponse' do it 'subclasses NotFound from RequestFailed, ExceptionWithResponse' do
RestClient::NotFound.should be < RestClient::RequestFailed expect(RestClient::NotFound).to be < RestClient::RequestFailed
RestClient::NotFound.should be < RestClient::ExceptionWithResponse expect(RestClient::NotFound).to be < RestClient::ExceptionWithResponse
end end
it 'subclasses timeout from RestClient::RequestTimeout, RequestFailed, EWR' do it 'subclasses timeout from RestClient::RequestTimeout, RequestFailed, EWR' do
RestClient::Exceptions::OpenTimeout.should be < RestClient::Exceptions::Timeout expect(RestClient::Exceptions::OpenTimeout).to be < RestClient::Exceptions::Timeout
RestClient::Exceptions::ReadTimeout.should be < RestClient::Exceptions::Timeout expect(RestClient::Exceptions::ReadTimeout).to be < RestClient::Exceptions::Timeout
RestClient::Exceptions::Timeout.should be < RestClient::RequestTimeout expect(RestClient::Exceptions::Timeout).to be < RestClient::RequestTimeout
RestClient::Exceptions::Timeout.should be < RestClient::RequestFailed expect(RestClient::Exceptions::Timeout).to be < RestClient::RequestFailed
RestClient::Exceptions::Timeout.should be < RestClient::ExceptionWithResponse expect(RestClient::Exceptions::Timeout).to be < RestClient::ExceptionWithResponse
end end
end end

View file

@ -12,11 +12,11 @@ describe RestClient::ParamsArray do
[{foo: 123}, [:foo, 456], {bar: 789}, {empty: nil}], [{foo: 123}, [:foo, 456], {bar: 789}, {empty: nil}],
[{foo: 123}, [:foo, 456], {bar: 789}, [:empty]], [{foo: 123}, [:foo, 456], {bar: 789}, [:empty]],
].each do |input| ].each do |input|
RestClient::ParamsArray.new(input).to_a.should eq as_array expect(RestClient::ParamsArray.new(input).to_a).to eq as_array
end end
RestClient::ParamsArray.new([]).to_a.should eq [] expect(RestClient::ParamsArray.new([]).to_a).to eq []
RestClient::ParamsArray.new([]).empty?.should eq true expect(RestClient::ParamsArray.new([]).empty?).to eq true
end end
it 'rejects various invalid input' do it 'rejects various invalid input' do

View file

@ -6,63 +6,63 @@ describe RestClient::Payload 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')
payload.to_s.should eq 'foobar' expect(payload.to_s).to eq 'foobar'
payload.to_s.should eq 'foobar' expect(payload.to_s).to eq 'foobar'
end end
end end
context "A regular Payload" do context "A regular Payload" do
it "should use standard enctype as default content-type" do it "should use standard enctype as default content-type" do
RestClient::Payload::UrlEncoded.new({}).headers['Content-Type']. expect(RestClient::Payload::UrlEncoded.new({}).headers['Content-Type']).
should eq 'application/x-www-form-urlencoded' to eq 'application/x-www-form-urlencoded'
end end
it "should form properly encoded params" do it "should form properly encoded params" do
RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s. expect(RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s).
should eq "foo=bar" to eq "foo=bar"
["foo=bar&baz=qux", "baz=qux&foo=bar"].should include( expect(["foo=bar&baz=qux", "baz=qux&foo=bar"]).to include(
RestClient::Payload::UrlEncoded.new({:foo => 'bar', :baz => 'qux'}).to_s) RestClient::Payload::UrlEncoded.new({:foo => 'bar', :baz => 'qux'}).to_s)
end end
it "should escape parameters" do it "should escape parameters" do
RestClient::Payload::UrlEncoded.new({'foo + bar' => 'baz'}).to_s. expect(RestClient::Payload::UrlEncoded.new({'foo + bar' => 'baz'}).to_s).
should eq "foo+%2B+bar=baz" to eq "foo+%2B+bar=baz"
end end
it "should properly handle hashes as parameter" do it "should properly handle hashes as parameter" do
RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s. expect(RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s).
should eq "foo[bar]=baz" to eq "foo[bar]=baz"
RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s. expect(RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s).
should eq "foo[bar][baz]=qux" to eq "foo[bar][baz]=qux"
end end
it "should handle many attributes inside a hash" do it "should handle many attributes inside a hash" do
parameters = RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz', :baz => 'qux'}}).to_s parameters = RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz', :baz => 'qux'}}).to_s
parameters.should eq 'foo[bar]=baz&foo[baz]=qux' expect(parameters).to eq 'foo[bar]=baz&foo[baz]=qux'
end end
it "should handle attributes inside an array inside an hash" do it "should handle attributes inside an array inside an hash" do
parameters = RestClient::Payload::UrlEncoded.new({"foo" => [{"bar" => 'baz'}, {"bar" => 'qux'}]}).to_s parameters = RestClient::Payload::UrlEncoded.new({"foo" => [{"bar" => 'baz'}, {"bar" => 'qux'}]}).to_s
parameters.should eq 'foo[][bar]=baz&foo[][bar]=qux' expect(parameters).to eq 'foo[][bar]=baz&foo[][bar]=qux'
end end
it "should handle arrays inside a hash inside a hash" do 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 parameters = RestClient::Payload::UrlEncoded.new({"foo" => {'even' => [0, 2], 'odd' => [1, 3]}}).to_s
parameters.should eq 'foo[even][]=0&foo[even][]=2&foo[odd][]=1&foo[odd][]=3' expect(parameters).to eq 'foo[even][]=0&foo[even][]=2&foo[odd][]=1&foo[odd][]=3'
end end
it "should form properly use symbols as parameters" do it "should form properly use symbols as parameters" do
RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s. expect(RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s).
should eq "foo=bar" to eq "foo=bar"
RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s. expect(RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s).
should eq "foo[bar]=baz" to eq "foo[bar]=baz"
end end
it "should properly handle arrays as repeated parameters" do it "should properly handle arrays as repeated parameters" do
RestClient::Payload::UrlEncoded.new({:foo => ['bar']}).to_s. expect(RestClient::Payload::UrlEncoded.new({:foo => ['bar']}).to_s).
should eq "foo[]=bar" to eq "foo[]=bar"
RestClient::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s. expect(RestClient::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s).
should eq "foo[]=bar&foo[]=baz" to eq "foo[]=bar&foo[]=baz"
end end
it 'should not close if stream already closed' do it 'should not close if stream already closed' do
@ -75,8 +75,8 @@ describe RestClient::Payload do
context "A multipart Payload" do context "A multipart Payload" do
it "should use standard enctype as default content-type" do it "should use standard enctype as default content-type" do
m = RestClient::Payload::Multipart.new({}) m = RestClient::Payload::Multipart.new({})
m.stub(:boundary).and_return(123) allow(m).to receive(:boundary).and_return(123)
m.headers['Content-Type'].should eq 'multipart/form-data; boundary=123' expect(m.headers['Content-Type']).to eq 'multipart/form-data; boundary=123'
end end
it 'should not error on close if stream already closed' do it 'should not error on close if stream already closed' do
@ -86,7 +86,7 @@ describe RestClient::Payload do
it "should form properly separated multipart data" do it "should form properly separated multipart data" do
m = RestClient::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]]) m = RestClient::Payload::Multipart.new([[:bar, "baz"], [:foo, "bar"]])
m.to_s.should eq <<-EOS expect(m.to_s).to eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="bar"\r Content-Disposition: form-data; name="bar"\r
\r \r
@ -101,7 +101,7 @@ bar\r
it "should not escape parameters names" do it "should not escape parameters names" do
m = RestClient::Payload::Multipart.new([["bar ", "baz"]]) m = RestClient::Payload::Multipart.new([["bar ", "baz"]])
m.to_s.should eq <<-EOS expect(m.to_s).to eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="bar "\r Content-Disposition: form-data; name="bar "\r
\r \r
@ -113,7 +113,7 @@ baz\r
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(File.dirname(__FILE__) + "/master_shake.jpg")
m = RestClient::Payload::Multipart.new({:foo => f}) m = RestClient::Payload::Multipart.new({:foo => f})
m.to_s.should 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="master_shake.jpg"\r
Content-Type: image/jpeg\r Content-Type: image/jpeg\r
@ -126,7 +126,7 @@ Content-Type: image/jpeg\r
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(File.dirname(__FILE__) + "/master_shake.jpg")
m = RestClient::Payload::Multipart.new({nil => f}) m = RestClient::Payload::Multipart.new({nil => f})
m.to_s.should 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="master_shake.jpg"\r
Content-Type: image/jpeg\r Content-Type: image/jpeg\r
@ -141,7 +141,7 @@ Content-Type: image/jpeg\r
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 => f}) m = RestClient::Payload::Multipart.new({:foo => f})
m.to_s.should eq <<-EOS expect(m.to_s).to eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="foo"; filename="foo.txt"\r Content-Disposition: form-data; name="foo"; filename="foo.txt"\r
Content-Type: text/plain\r Content-Type: text/plain\r
@ -153,7 +153,7 @@ Content-Type: text/plain\r
it "should handle hash in hash parameters" do it "should handle hash in hash parameters" do
m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}}) m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}})
m.to_s.should eq <<-EOS expect(m.to_s).to eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="bar[baz]"\r Content-Disposition: form-data; name="bar[baz]"\r
\r \r
@ -165,7 +165,7 @@ foo\r
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}})
m.to_s.should eq <<-EOS expect(m.to_s).to eq <<-EOS
--#{m.boundary}\r --#{m.boundary}\r
Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r
Content-Type: text/plain\r Content-Type: text/plain\r
@ -176,10 +176,10 @@ Content-Type: text/plain\r
end end
it 'should correctly format hex boundary' do it 'should correctly format hex boundary' do
SecureRandom.stub(: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(File.dirname(__FILE__) + '/master_shake.jpg')
m = RestClient::Payload::Multipart.new({:foo => f}) m = RestClient::Payload::Multipart.new({:foo => f})
m.boundary.should eq('-' * 4 + 'RubyFormBoundary' + 'TGs89AttwBxna6TV') expect(m.boundary).to eq('-' * 4 + 'RubyFormBoundary' + 'TGs89AttwBxna6TV')
end end
end end
@ -188,23 +188,23 @@ Content-Type: text/plain\r
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(File.dirname(__FILE__) + "/master_shake.jpg")
payload = RestClient::Payload.generate(f) payload = RestClient::Payload.generate(f)
payload.size.should eq 76_988 expect(payload.size).to eq 76_988
payload.length.should eq 76_988 expect(payload.length).to eq 76_988
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
s = StringIO.new 'foo' s = StringIO.new 'foo'
payload = RestClient::Payload.generate(s) payload = RestClient::Payload.generate(s)
payload.size.should eq 3 expect(payload.size).to eq 3
payload.length.should eq 3 expect(payload.length).to eq 3
begin begin
f = Tempfile.new "rest-client" f = Tempfile.new "rest-client"
f.write 'foo bar' f.write 'foo bar'
payload = RestClient::Payload.generate(f) payload = RestClient::Payload.generate(f)
payload.size.should eq 7 expect(payload.size).to eq 7
payload.length.should eq 7 expect(payload.length).to eq 7
ensure ensure
f.close f.close
end end
@ -213,51 +213,51 @@ Content-Type: text/plain\r
context "Payload generation" do context "Payload generation" do
it "should recognize standard urlencoded params" do it "should recognize standard urlencoded params" do
RestClient::Payload.generate({"foo" => 'bar'}).should be_kind_of(RestClient::Payload::UrlEncoded) expect(RestClient::Payload.generate({"foo" => 'bar'})).to be_kind_of(RestClient::Payload::UrlEncoded)
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(File.dirname(__FILE__) + "/master_shake.jpg")
RestClient::Payload.generate({"foo" => f}).should be_kind_of(RestClient::Payload::Multipart) expect(RestClient::Payload.generate({"foo" => f})).to be_kind_of(RestClient::Payload::Multipart)
end end
it "should be multipart if forced" do it "should be multipart if forced" do
RestClient::Payload.generate({"foo" => "bar", :multipart => true}).should be_kind_of(RestClient::Payload::Multipart) expect(RestClient::Payload.generate({"foo" => "bar", :multipart => true})).to be_kind_of(RestClient::Payload::Multipart)
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(File.dirname(__FILE__) + "/master_shake.jpg")
params = {foo: RestClient::ParamsArray.new({nested: f})} params = {foo: RestClient::ParamsArray.new({nested: f})}
RestClient::Payload.generate(params).should be_kind_of(RestClient::Payload::Multipart) expect(RestClient::Payload.generate(params)).to be_kind_of(RestClient::Payload::Multipart)
end end
it "should return data if no of the above" do it "should return data if no of the above" do
RestClient::Payload.generate("data").should be_kind_of(RestClient::Payload::Base) expect(RestClient::Payload.generate("data")).to be_kind_of(RestClient::Payload::Base)
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(File.dirname(__FILE__) + "/master_shake.jpg")
RestClient::Payload.generate({"foo" => {"file" => f}}).should 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(File.dirname(__FILE__) + "/master_shake.jpg")
RestClient::Payload.generate({"foo" => [f]}).should 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(File.dirname(__FILE__) + "/master_shake.jpg")
RestClient::Payload.generate(f).should be_kind_of(RestClient::Payload::Streamed) expect(RestClient::Payload.generate(f)).to be_kind_of(RestClient::Payload::Streamed)
end end
it "should recognize other payloads that can be streamed" do it "should recognize other payloads that can be streamed" do
RestClient::Payload.generate(StringIO.new('foo')).should be_kind_of(RestClient::Payload::Streamed) expect(RestClient::Payload.generate(StringIO.new('foo'))).to be_kind_of(RestClient::Payload::Streamed)
end end
# hashery gem introduces Hash#read convenience method. Existence of #read method used to determine of content is streameable :/ # 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 it "shouldn't treat hashes as streameable" do
RestClient::Payload.generate({"foo" => 'bar'}).should be_kind_of(RestClient::Payload::UrlEncoded) expect(RestClient::Payload.generate({"foo" => 'bar'})).to be_kind_of(RestClient::Payload::UrlEncoded)
end end
end end
end end

View file

@ -9,10 +9,10 @@ describe RestClient::RawResponse do
end end
it "behaves like string" do it "behaves like string" do
@response.to_s.should eq 'the answer is 42' expect(@response.to_s).to eq 'the answer is 42'
end end
it "exposes a Tempfile" do it "exposes a Tempfile" do
@response.file.should eq @tf expect(@response.file).to eq @tf
end end
end end

View file

@ -5,20 +5,20 @@ describe RestClient::Request 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
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200) stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body.should eq 'foo' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}).body).to eq 'foo'
stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200) stub_request(:get, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body.should eq 'foo' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => :a}).body).to eq 'foo'
end end
it 'adds GET params when params are present in URL' do it 'adds GET params when params are present in URL' do
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200) stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(:url => 'http://some/resource?a=b', :method => :get, :headers => {:foo => :bar, :params => {:c => 'd'}}).body.should eq 'foo' expect(RestClient::Request.execute(:url => 'http://some/resource?a=b', :method => :get, :headers => {:foo => :bar, :params => {:c => 'd'}}).body).to eq 'foo'
end end
it 'encodes nested GET params' do it 'encodes nested GET params' do
stub_request(:get, 'http://some/resource?a[foo][]=1&a[foo][]=2&a[bar]&b=foo+bar&math=2+%2B+2+%3D%3D+4').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200) stub_request(:get, 'http://some/resource?a[foo][]=1&a[foo][]=2&a[bar]&b=foo+bar&math=2+%2B+2+%3D%3D+4').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(url: 'http://some/resource', method: :get, headers: { expect(RestClient::Request.execute(url: 'http://some/resource', method: :get, headers: {
params: { params: {
a: { a: {
foo: [1,2], foo: [1,2],
@ -27,7 +27,7 @@ describe RestClient::Request do
b: 'foo bar', b: 'foo bar',
math: '2 + 2 == 4', math: '2 + 2 == 4',
} }
}).body.should eq 'foo' }).body).to eq 'foo'
end end
end end
@ -39,7 +39,7 @@ describe RestClient::Request do
end end
stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200) stub_request(:get, 'http://some/resource?a=b&c=d').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate', 'Foo'=>'bar'}).to_return(:body => 'foo', :status => 200)
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block) RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :headers => {:foo => :bar, :params => {:a => :b, 'c' => 'd'}}, :block_response => block)
response_value.should eq "foo" expect(response_value).to eq "foo"
end end
it 'closes payload if not nil' do it 'closes payload if not nil' do
@ -48,7 +48,7 @@ describe RestClient::Request do
stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate'}).to_return(:body => 'foo', :status => 200) stub_request(:post, 'http://some/resource').with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip, deflate'}).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})
test_file.closed?.should be true expect(test_file.closed?).to be true
end end
end end

File diff suppressed because it is too large Load diff

View file

@ -7,37 +7,37 @@ describe RestClient::Resource do
context "Resource delegation" do context "Resource delegation" do
it "GET" do it "GET" do
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass') expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
@resource.get @resource.get
end end
it "HEAD" do it "HEAD" do
RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass') expect(RestClient::Request).to receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
@resource.head @resource.head
end end
it "POST" do it "POST" do
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass') expect(RestClient::Request).to receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
@resource.post 'abc', :content_type => 'image/jpg' @resource.post 'abc', :content_type => 'image/jpg'
end end
it "PUT" do it "PUT" do
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass') expect(RestClient::Request).to receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
@resource.put 'abc', :content_type => 'image/jpg' @resource.put 'abc', :content_type => 'image/jpg'
end end
it "PATCH" do it "PATCH" do
RestClient::Request.should_receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass') expect(RestClient::Request).to receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
@resource.patch 'abc', :content_type => 'image/jpg' @resource.patch 'abc', :content_type => 'image/jpg'
end end
it "DELETE" do it "DELETE" do
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass') expect(RestClient::Request).to receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
@resource.delete @resource.delete
end end
it "overrides resource headers" do it "overrides resource headers" do
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass') expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
@resource.get 'X-Something' => '2' @resource.get 'X-Something' => '2'
end end
end end
@ -48,41 +48,41 @@ describe RestClient::Resource do
it "is backwards compatible with previous constructor" do it "is backwards compatible with previous constructor" do
@resource = RestClient::Resource.new('http://some/resource', 'user', 'pass') @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
@resource.user.should eq 'user' expect(@resource.user).to eq 'user'
@resource.password.should eq 'pass' expect(@resource.password).to eq 'pass'
end end
it "concatenates urls, inserting a slash when it needs one" do it "concatenates urls, inserting a slash when it needs one" do
@resource.concat_urls('http://example.com', 'resource').should eq 'http://example.com/resource' expect(@resource.concat_urls('http://example.com', 'resource')).to eq 'http://example.com/resource'
end end
it "concatenates urls, using no slash if the first url ends with a slash" do it "concatenates urls, using no slash if the first url ends with a slash" do
@resource.concat_urls('http://example.com/', 'resource').should eq 'http://example.com/resource' expect(@resource.concat_urls('http://example.com/', 'resource')).to eq 'http://example.com/resource'
end end
it "concatenates urls, using no slash if the second url starts with a slash" do it "concatenates urls, using no slash if the second url starts with a slash" do
@resource.concat_urls('http://example.com', '/resource').should eq 'http://example.com/resource' expect(@resource.concat_urls('http://example.com', '/resource')).to eq 'http://example.com/resource'
end end
it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
@resource.concat_urls(:posts, 1).should eq 'posts/1' expect(@resource.concat_urls(:posts, 1)).to eq 'posts/1'
end end
it "offers subresources via []" do it "offers subresources via []" do
parent = RestClient::Resource.new('http://example.com') parent = RestClient::Resource.new('http://example.com')
parent['posts'].url.should eq 'http://example.com/posts' expect(parent['posts'].url).to eq 'http://example.com/posts'
end end
it "transports options to subresources" do it "transports options to subresources" do
parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password') parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
parent['posts'].user.should eq 'user' expect(parent['posts'].user).to eq 'user'
parent['posts'].password.should eq 'password' expect(parent['posts'].password).to eq 'password'
end end
it "passes a given block to subresources" do it "passes a given block to subresources" do
block = proc {|r| r} block = proc {|r| r}
parent = RestClient::Resource.new('http://example.com', &block) parent = RestClient::Resource.new('http://example.com', &block)
parent['posts'].block.should eq block expect(parent['posts'].block).to eq block
end end
it "the block should be overrideable" do it "the block should be overrideable" do
@ -90,8 +90,8 @@ describe RestClient::Resource do
block2 = proc {|r| } block2 = proc {|r| }
parent = RestClient::Resource.new('http://example.com', &block1) parent = RestClient::Resource.new('http://example.com', &block1)
# parent['posts', &block2].block.should eq block2 # ruby 1.9 syntax # parent['posts', &block2].block.should eq block2 # ruby 1.9 syntax
parent.send(:[], 'posts', &block2).block.should eq block2 expect(parent.send(:[], 'posts', &block2).block).to eq block2
parent.send(:[], 'posts', &block2).block.should_not eq block1 expect(parent.send(:[], 'posts', &block2).block).not_to eq block1
end end
it "the block should be overrideable in ruby 1.9 syntax" do it "the block should be overrideable in ruby 1.9 syntax" do
@ -99,31 +99,31 @@ describe RestClient::Resource do
block2 = ->(r) {} block2 = ->(r) {}
parent = RestClient::Resource.new('http://example.com', &block1) parent = RestClient::Resource.new('http://example.com', &block1)
parent['posts', &block2].block.should eq block2 expect(parent['posts', &block2].block).to eq block2
parent['posts', &block2].block.should_not eq block1 expect(parent['posts', &block2].block).not_to eq block1
end end
it "prints its url with to_s" do it "prints its url with to_s" do
RestClient::Resource.new('x').to_s.should eq 'x' expect(RestClient::Resource.new('x').to_s).to eq 'x'
end end
describe 'block' do describe 'block' do
it 'can use block when creating the resource' do it 'can use block when creating the resource' do
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404) stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' } resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
resource.get.should eq 'foo' expect(resource.get).to eq 'foo'
end end
it 'can use block when executing the resource' do it 'can use block when executing the resource' do
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404) stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
resource = RestClient::Resource.new('www.example.com') resource = RestClient::Resource.new('www.example.com')
resource.get { |response, request| 'foo' }.should eq 'foo' expect(resource.get { |response, request| 'foo' }).to eq 'foo'
end end
it 'execution block override resource block' do it 'execution block override resource block' do
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404) stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' } resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
resource.get { |response, request| 'bar' }.should eq 'bar' expect(resource.get { |response, request| 'bar' }).to eq 'bar'
end end
end end

View file

@ -9,21 +9,21 @@ describe RestClient::Response, :include_helpers do
end end
it "behaves like string" do it "behaves like string" do
@response.to_s.should eq 'abc' expect(@response.to_s).to eq 'abc'
@response.to_str.should eq 'abc' expect(@response.to_str).to eq 'abc'
@response.should_receive(:warn) expect(@response).to receive(:warn)
@response.to_i.should eq 0 expect(@response.to_i).to eq 0
end end
it "accepts nil strings and sets it to empty for the case of HEAD" do it "accepts nil strings and sets it to empty for the case of HEAD" do
RestClient::Response.create(nil, @net_http_res, @request).to_s.should eq "" expect(RestClient::Response.create(nil, @net_http_res, @request).to_s).to eq ""
end end
describe 'header processing' do describe 'header processing' do
it "test headers and raw headers" do it "test headers and raw headers" do
@response.raw_headers["Status"][0].should eq "200 OK" expect(@response.raw_headers["Status"][0]).to eq "200 OK"
@response.headers[:status].should eq "200 OK" expect(@response.headers[:status]).to eq "200 OK"
end end
it 'handles multiple headers by joining with comma' do it 'handles multiple headers by joining with comma' do
@ -32,8 +32,8 @@ describe RestClient::Response, :include_helpers do
@request = request_double(url: @example_url, method: 'get') @request = request_double(url: @example_url, method: 'get')
@response = RestClient::Response.create('abc', @net_http_res, @request) @response = RestClient::Response.create('abc', @net_http_res, @request)
@response.raw_headers['My-Header'].should eq ['foo', 'bar'] expect(@response.raw_headers['My-Header']).to eq ['foo', 'bar']
@response.headers[:my_header].should eq 'foo, bar' expect(@response.headers[:my_header]).to eq 'foo, bar'
end end
end end
@ -43,15 +43,15 @@ describe RestClient::Response, :include_helpers do
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => [header_val]}) net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => [header_val]})
response = RestClient::Response.create('abc', net_http_res, @request) response = RestClient::Response.create('abc', net_http_res, @request)
response.headers[:set_cookie].should eq [header_val] expect(response.headers[:set_cookie]).to eq [header_val]
response.cookies.should eq({ "main_page" => "main_page_no_rewrite" }) expect(response.cookies).to eq({ "main_page" => "main_page_no_rewrite" })
end end
it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT", "remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT", "user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]}) net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT", "remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT", "user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]})
response = RestClient::Response.create('abc', net_http_res, @request) response = RestClient::Response.create('abc', net_http_res, @request)
response.headers[:set_cookie].should eq ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT", "remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT", "user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"] expect(response.headers[:set_cookie]).to eq ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT", "remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT", "user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]
response.cookies.should eq({ expect(response.cookies).to eq({
"main_page" => "main_page_no_rewrite", "main_page" => "main_page_no_rewrite",
"remember_me" => "", "remember_me" => "",
"user" => "somebody" "user" => "somebody"
@ -61,7 +61,7 @@ describe RestClient::Response, :include_helpers do
it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT, remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT, user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]}) net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT, remember_me=; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT, user=somebody; path=/; expires=Sat, 10-Jan-2037 00:00:00 GMT"]})
response = RestClient::Response.create('abc', net_http_res, @request) response = RestClient::Response.create('abc', net_http_res, @request)
response.cookies.should eq({ expect(response.cookies).to eq({
"main_page" => "main_page_no_rewrite", "main_page" => "main_page_no_rewrite",
"remember_me" => "", "remember_me" => "",
"user" => "somebody" "user" => "somebody"
@ -83,7 +83,7 @@ describe RestClient::Response, :include_helpers do
unless (200..207).include? code unless (200..207).include? code
net_http_res = response_double(:code => code.to_i) net_http_res = response_double(:code => code.to_i)
resp = RestClient::Response.create('abc', net_http_res, @request) resp = RestClient::Response.create('abc', net_http_res, @request)
lambda { resp.return! }.should raise_error expect { resp.return! }.to raise_error
end end
end end
end end
@ -95,29 +95,29 @@ describe RestClient::Response, :include_helpers do
it "follows a redirection when the request is a get" do it "follows a redirection when the request is a get" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'}) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo') stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
end end
it "keeps redirection history" do it "keeps redirection history" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'}) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo') stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
r = RestClient::Request.execute(url: 'http://some/resource', method: :get) r = RestClient::Request.execute(url: 'http://some/resource', method: :get)
r.body.should eq 'Foo' expect(r.body).to eq 'Foo'
r.history.length.should eq 1 expect(r.history.length).to eq 1
r.history.fetch(0).should be_a(RestClient::Response) expect(r.history.fetch(0)).to be_a(RestClient::Response)
r.history.fetch(0).code.should be 301 expect(r.history.fetch(0).code).to be 301
end end
it "follows a redirection and keep the parameters" do it "follows a redirection and keep the parameters" do
stub_request(:get, 'http://some/resource').with(:headers => {'Accept' => 'application/json'}, :basic_auth => ['foo', 'bar']).to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'}) stub_request(:get, 'http://some/resource').with(:headers => {'Accept' => 'application/json'}, :basic_auth => ['foo', 'bar']).to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
stub_request(:get, 'http://new/resource').with(:headers => {'Accept' => 'application/json'}, :basic_auth => ['foo', 'bar']).to_return(:body => 'Foo') stub_request(:get, 'http://new/resource').with(:headers => {'Accept' => 'application/json'}, :basic_auth => ['foo', 'bar']).to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body.should eq 'Foo' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body).to eq 'Foo'
end end
it "follows a redirection and keep the cookies" do it "follows a redirection and keep the cookies" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://some/new_resource', }) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://some/new_resource', })
stub_request(:get, 'http://some/new_resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux') stub_request(:get, 'http://some/new_resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Qux' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Qux'
end end
it 'respects cookie domains on redirect' do it 'respects cookie domains on redirect' do
@ -126,7 +126,7 @@ describe RestClient::Response, :include_helpers do
stub_request(:get, 'http://new.example.com/').with( stub_request(:get, 'http://new.example.com/').with(
:headers => {'Cookie' => 'passedthrough=1'}).to_return(:body => 'Qux') :headers => {'Cookie' => 'passedthrough=1'}).to_return(:body => 'Qux')
RestClient::Request.execute(:url => 'http://some.example.com/', :method => :get, cookies: [HTTP::Cookie.new('passedthrough', '1', domain: 'new.example.com', path: '/')]).body.should eq 'Qux' expect(RestClient::Request.execute(:url => 'http://some.example.com/', :method => :get, cookies: [HTTP::Cookie.new('passedthrough', '1', domain: 'new.example.com', path: '/')]).body).to eq 'Qux'
end end
it "doesn't follow a 301 when the request is a post" do it "doesn't follow a 301 when the request is a post" do
@ -134,90 +134,90 @@ describe RestClient::Response, :include_helpers do
response = RestClient::Response.create('abc', net_http_res, response = RestClient::Response.create('abc', net_http_res,
request_double(method: 'post')) request_double(method: 'post'))
lambda { expect {
response.return! response.return!
}.should raise_error(RestClient::MovedPermanently) }.to raise_error(RestClient::MovedPermanently)
end end
it "doesn't follow a 302 when the request is a post" do it "doesn't follow a 302 when the request is a post" do
net_http_res = response_double(:code => 302) net_http_res = response_double(:code => 302)
response = RestClient::Response.create('abc', net_http_res, response = RestClient::Response.create('abc', net_http_res,
request_double(method: 'post')) request_double(method: 'post'))
lambda { expect {
response.return! response.return!
}.should raise_error(RestClient::Found) }.to raise_error(RestClient::Found)
end end
it "doesn't follow a 307 when the request is a post" do it "doesn't follow a 307 when the request is a post" do
net_http_res = response_double(:code => 307) net_http_res = response_double(:code => 307)
response = RestClient::Response.create('abc', net_http_res, response = RestClient::Response.create('abc', net_http_res,
request_double(method: 'post')) request_double(method: 'post'))
response.should_not_receive(:follow_redirection) expect(response).not_to receive(:follow_redirection)
lambda { expect {
response.return! response.return!
}.should raise_error(RestClient::TemporaryRedirect) }.to raise_error(RestClient::TemporaryRedirect)
end end
it "doesn't follow a redirection when the request is a put" do it "doesn't follow a redirection when the request is a put" do
net_http_res = response_double(:code => 301) net_http_res = response_double(:code => 301)
response = RestClient::Response.create('abc', net_http_res, response = RestClient::Response.create('abc', net_http_res,
request_double(method: 'put')) request_double(method: 'put'))
lambda { expect {
response.return! response.return!
}.should raise_error(RestClient::MovedPermanently) }.to raise_error(RestClient::MovedPermanently)
end end
it "follows a redirection when the request is a post and result is a 303" do it "follows a redirection when the request is a post and result is a 303" do
stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'}) stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'})
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo') stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body.should eq 'Foo' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body).to eq 'Foo'
end end
it "follows a redirection when the request is a head" do it "follows a redirection when the request is a head" do
stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'}) stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
stub_request(:head, 'http://new/resource').to_return(:body => 'Foo') stub_request(:head, 'http://new/resource').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body.should eq 'Foo' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body).to eq 'Foo'
end end
it "handles redirects with relative paths" do it "handles redirects with relative paths" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'}) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'})
stub_request(:get, 'http://some/index').to_return(:body => 'Foo') stub_request(:get, 'http://some/index').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
end end
it "handles redirects with relative path and query string" do it "handles redirects with relative path and query string" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'}) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'})
stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo') stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
end end
it "follow a redirection when the request is a get and the response is in the 30x range" do it "follow a redirection when the request is a get and the response is in the 30x range" do
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'}) stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo') stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo' expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
end end
it "follows no more than 10 redirections before raising error" do it "follows no more than 10 redirections before raising error" do
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'}) stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'}) stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
lambda { expect {
RestClient::Request.execute(url: 'http://some/redirect-1', method: :get) RestClient::Request.execute(url: 'http://some/redirect-1', method: :get)
}.should raise_error(RestClient::MovedPermanently) { |ex| }.to raise_error(RestClient::MovedPermanently) { |ex|
ex.response.history.each {|r| r.should be_a(RestClient::Response) } ex.response.history.each {|r| expect(r).to be_a(RestClient::Response) }
ex.response.history.length.should eq 10 expect(ex.response.history.length).to eq 10
} }
WebMock.should have_requested(:get, 'http://some/redirect-2').times(10) expect(WebMock).to have_requested(:get, 'http://some/redirect-2').times(10)
end end
it "follows no more than max_redirects redirections, if specified" do it "follows no more than max_redirects redirections, if specified" do
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'}) stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'}) stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
lambda { expect {
RestClient::Request.execute(url: 'http://some/redirect-1', method: :get, max_redirects: 5) RestClient::Request.execute(url: 'http://some/redirect-1', method: :get, max_redirects: 5)
}.should raise_error(RestClient::MovedPermanently) { |ex| }.to raise_error(RestClient::MovedPermanently) { |ex|
ex.response.history.length.should eq 5 expect(ex.response.history.length).to eq 5
} }
WebMock.should have_requested(:get, 'http://some/redirect-2').times(5) expect(WebMock).to have_requested(:get, 'http://some/redirect-2').times(5)
end end
it "allows for manual following of redirects" do it "allows for manual following of redirects" do
@ -232,8 +232,8 @@ describe RestClient::Response, :include_helpers do
raise 'notreached' raise 'notreached'
end end
resp.code.should eq 200 expect(resp.code).to eq 200
resp.body.should eq 'Qux' expect(resp.body).to eq 'Qux'
end end
end end

View file

@ -3,37 +3,37 @@ require_relative '_lib'
describe RestClient do describe RestClient do
describe "API" do describe "API" do
it "GET" do it "GET" do
RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {}) expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
RestClient.get('http://some/resource') RestClient.get('http://some/resource')
end end
it "POST" do it "POST" do
RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {}) expect(RestClient::Request).to receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
RestClient.post('http://some/resource', 'payload') RestClient.post('http://some/resource', 'payload')
end end
it "PUT" do it "PUT" do
RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {}) expect(RestClient::Request).to receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
RestClient.put('http://some/resource', 'payload') RestClient.put('http://some/resource', 'payload')
end end
it "PATCH" do it "PATCH" do
RestClient::Request.should_receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'payload', :headers => {}) expect(RestClient::Request).to receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'payload', :headers => {})
RestClient.patch('http://some/resource', 'payload') RestClient.patch('http://some/resource', 'payload')
end end
it "DELETE" do it "DELETE" do
RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {}) expect(RestClient::Request).to receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
RestClient.delete('http://some/resource') RestClient.delete('http://some/resource')
end end
it "HEAD" do it "HEAD" do
RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {}) expect(RestClient::Request).to receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {})
RestClient.head('http://some/resource') RestClient.head('http://some/resource')
end end
it "OPTIONS" do it "OPTIONS" do
RestClient::Request.should_receive(:execute).with(:method => :options, :url => 'http://some/resource', :headers => {}) expect(RestClient::Request).to receive(:execute).with(:method => :options, :url => 'http://some/resource', :headers => {})
RestClient.options('http://some/resource') RestClient.options('http://some/resource')
end end
end end
@ -45,27 +45,27 @@ describe RestClient do
it "uses << if the log is not a string" do it "uses << if the log is not a string" do
log = RestClient.log = [] log = RestClient.log = []
log.should_receive(:<<).with('xyz') expect(log).to receive(:<<).with('xyz')
RestClient.log << 'xyz' RestClient.log << 'xyz'
end end
it "displays the log to stdout" do it "displays the log to stdout" do
RestClient.log = 'stdout' RestClient.log = 'stdout'
STDOUT.should_receive(:puts).with('xyz') expect(STDOUT).to receive(:puts).with('xyz')
RestClient.log << 'xyz' RestClient.log << 'xyz'
end end
it "displays the log to stderr" do it "displays the log to stderr" do
RestClient.log = 'stderr' RestClient.log = 'stderr'
STDERR.should_receive(:puts).with('xyz') expect(STDERR).to receive(:puts).with('xyz')
RestClient.log << 'xyz' RestClient.log << 'xyz'
end end
it "append the log to the requested filename" do it "append the log to the requested filename" do
RestClient.log = '/tmp/restclient.log' RestClient.log = '/tmp/restclient.log'
f = double('file handle') f = double('file handle')
File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f) expect(File).to receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
f.should_receive(:puts).with('xyz') expect(f).to receive(:puts).with('xyz')
RestClient.log << 'xyz' RestClient.log << 'xyz'
end end
end end
@ -73,7 +73,7 @@ describe RestClient do
describe 'version' do describe 'version' do
it 'has a version ~> 2.0.0.alpha' do it 'has a version ~> 2.0.0.alpha' do
ver = Gem::Version.new(RestClient.version) ver = Gem::Version.new(RestClient.version)
Gem::Requirement.new('~> 2.0.0.alpha').should be_satisfied_by(ver) expect(Gem::Requirement.new('~> 2.0.0.alpha')).to be_satisfied_by(ver)
end end
end end
end end

View file

@ -4,67 +4,67 @@ describe RestClient::Utils do
describe '.get_encoding_from_headers' do describe '.get_encoding_from_headers' do
it 'assumes no encoding by default for text' do it 'assumes no encoding by default for text' do
headers = {:content_type => 'text/plain'} headers = {:content_type => 'text/plain'}
RestClient::Utils.get_encoding_from_headers(headers). expect(RestClient::Utils.get_encoding_from_headers(headers)).
should eq nil to eq nil
end end
it 'returns nil on failures' do it 'returns nil on failures' do
RestClient::Utils.get_encoding_from_headers( expect(RestClient::Utils.get_encoding_from_headers(
{:content_type => 'blah'}).should eq nil {:content_type => 'blah'})).to eq nil
RestClient::Utils.get_encoding_from_headers( expect(RestClient::Utils.get_encoding_from_headers(
{}).should eq nil {})).to eq nil
RestClient::Utils.get_encoding_from_headers( expect(RestClient::Utils.get_encoding_from_headers(
{:content_type => 'foo; bar=baz'}).should eq nil {:content_type => 'foo; bar=baz'})).to eq nil
end end
it 'handles various charsets' do it 'handles various charsets' do
RestClient::Utils.get_encoding_from_headers( expect(RestClient::Utils.get_encoding_from_headers(
{:content_type => 'text/plain; charset=UTF-8'}).should eq 'UTF-8' {:content_type => 'text/plain; charset=UTF-8'})).to eq 'UTF-8'
RestClient::Utils.get_encoding_from_headers( expect(RestClient::Utils.get_encoding_from_headers(
{:content_type => 'application/json; charset=ISO-8859-1'}). {:content_type => 'application/json; charset=ISO-8859-1'})).
should eq 'ISO-8859-1' to eq 'ISO-8859-1'
RestClient::Utils.get_encoding_from_headers( expect(RestClient::Utils.get_encoding_from_headers(
{:content_type => 'text/html; charset=windows-1251'}). {:content_type => 'text/html; charset=windows-1251'})).
should eq 'windows-1251' to eq 'windows-1251'
RestClient::Utils.get_encoding_from_headers( expect(RestClient::Utils.get_encoding_from_headers(
{:content_type => 'text/html; charset="UTF-16"'}). {:content_type => 'text/html; charset="UTF-16"'})).
should eq 'UTF-16' to eq 'UTF-16'
end end
end end
describe '.cgi_parse_header' do describe '.cgi_parse_header' do
it 'parses headers' do it 'parses headers' do
RestClient::Utils.cgi_parse_header('text/plain'). expect(RestClient::Utils.cgi_parse_header('text/plain')).
should eq ['text/plain', {}] to eq ['text/plain', {}]
RestClient::Utils.cgi_parse_header('text/vnd.just.made.this.up ; '). expect(RestClient::Utils.cgi_parse_header('text/vnd.just.made.this.up ; ')).
should eq ['text/vnd.just.made.this.up', {}] to eq ['text/vnd.just.made.this.up', {}]
RestClient::Utils.cgi_parse_header('text/plain;charset=us-ascii'). expect(RestClient::Utils.cgi_parse_header('text/plain;charset=us-ascii')).
should eq ['text/plain', {'charset' => 'us-ascii'}] to eq ['text/plain', {'charset' => 'us-ascii'}]
RestClient::Utils.cgi_parse_header('text/plain ; charset="us-ascii"'). expect(RestClient::Utils.cgi_parse_header('text/plain ; charset="us-ascii"')).
should eq ['text/plain', {'charset' => 'us-ascii'}] to eq ['text/plain', {'charset' => 'us-ascii'}]
RestClient::Utils.cgi_parse_header( expect(RestClient::Utils.cgi_parse_header(
'text/plain ; charset="us-ascii"; another=opt'). 'text/plain ; charset="us-ascii"; another=opt')).
should eq ['text/plain', {'charset' => 'us-ascii', 'another' => 'opt'}] to eq ['text/plain', {'charset' => 'us-ascii', 'another' => 'opt'}]
RestClient::Utils.cgi_parse_header( expect(RestClient::Utils.cgi_parse_header(
'attachment; filename="silly.txt"'). 'attachment; filename="silly.txt"')).
should eq ['attachment', {'filename' => 'silly.txt'}] to eq ['attachment', {'filename' => 'silly.txt'}]
RestClient::Utils.cgi_parse_header( expect(RestClient::Utils.cgi_parse_header(
'attachment; filename="strange;name"'). 'attachment; filename="strange;name"')).
should eq ['attachment', {'filename' => 'strange;name'}] to eq ['attachment', {'filename' => 'strange;name'}]
RestClient::Utils.cgi_parse_header( expect(RestClient::Utils.cgi_parse_header(
'attachment; filename="strange;name";size=123;').should eq \ 'attachment; filename="strange;name";size=123;')).to eq \
['attachment', {'filename' => 'strange;name', 'size' => '123'}] ['attachment', {'filename' => 'strange;name', 'size' => '123'}]
RestClient::Utils.cgi_parse_header( expect(RestClient::Utils.cgi_parse_header(
'form-data; name="files"; filename="fo\\"o;bar"').should eq \ 'form-data; name="files"; filename="fo\\"o;bar"')).to eq \
['form-data', {'name' => 'files', 'filename' => 'fo"o;bar'}] ['form-data', {'name' => 'files', 'filename' => 'fo"o;bar'}]
end end
end end
@ -78,7 +78,7 @@ describe RestClient::Utils do
{escaped: '1+2=3'} => 'escaped=1%2B2%3D3', {escaped: '1+2=3'} => 'escaped=1%2B2%3D3',
{'escaped + key' => 'foo'} => 'escaped+%2B+key=foo', {'escaped + key' => 'foo'} => 'escaped+%2B+key=foo',
}.each_pair do |input, expected| }.each_pair do |input, expected|
RestClient::Utils.encode_query_string(input).should eq expected expect(RestClient::Utils.encode_query_string(input)).to eq expected
end end
end end
@ -89,7 +89,7 @@ describe RestClient::Utils do
{foo: ['one two', 3]} => 'foo[]=one+two&foo[]=3', {foo: ['one two', 3]} => 'foo[]=one+two&foo[]=3',
{'a+b' => [1,2,3]} => 'a%2Bb[]=1&a%2Bb[]=2&a%2Bb[]=3', {'a+b' => [1,2,3]} => 'a%2Bb[]=1&a%2Bb[]=2&a%2Bb[]=3',
}.each_pair do |input, expected| }.each_pair do |input, expected|
RestClient::Utils.encode_query_string(input).should eq expected expect(RestClient::Utils.encode_query_string(input)).to eq expected
end end
end end
@ -98,7 +98,7 @@ describe RestClient::Utils do
{outer: {foo: 123, bar: 456}} => 'outer[foo]=123&outer[bar]=456', {outer: {foo: 123, bar: 456}} => 'outer[foo]=123&outer[bar]=456',
{outer: {foo: [1, 2, 3], bar: 'baz'}} => 'outer[foo][]=1&outer[foo][]=2&outer[foo][]=3&outer[bar]=baz', {outer: {foo: [1, 2, 3], bar: 'baz'}} => 'outer[foo][]=1&outer[foo][]=2&outer[foo][]=3&outer[bar]=baz',
}.each_pair do |input, expected| }.each_pair do |input, expected|
RestClient::Utils.encode_query_string(input).should eq expected expect(RestClient::Utils.encode_query_string(input)).to eq expected
end end
end end
@ -107,7 +107,7 @@ describe RestClient::Utils do
{string: '', empty: nil, list: [], hash: {}, falsey: false } => {string: '', empty: nil, list: [], hash: {}, falsey: false } =>
'string=&empty&list&hash&falsey=false', 'string=&empty&list&hash&falsey=false',
}.each_pair do |input, expected| }.each_pair do |input, expected|
RestClient::Utils.encode_query_string(input).should eq expected expect(RestClient::Utils.encode_query_string(input)).to eq expected
end end
end end
@ -115,7 +115,7 @@ describe RestClient::Utils do
{ {
{foo: {string: '', empty: nil}} => 'foo[string]=&foo[empty]', {foo: {string: '', empty: nil}} => 'foo[string]=&foo[empty]',
}.each_pair do |input, expected| }.each_pair do |input, expected|
RestClient::Utils.encode_query_string(input).should eq expected expect(RestClient::Utils.encode_query_string(input)).to eq expected
end end
end end
@ -123,7 +123,7 @@ describe RestClient::Utils do
{ {
{coords: [{x: 1, y: 0}, {x: 2}, {x: 3}]} => 'coords[][x]=1&coords[][y]=0&coords[][x]=2&coords[][x]=3', {coords: [{x: 1, y: 0}, {x: 2}, {x: 3}]} => 'coords[][x]=1&coords[][y]=0&coords[][x]=2&coords[][x]=3',
}.each_pair do |input, expected| }.each_pair do |input, expected|
RestClient::Utils.encode_query_string(input).should eq expected expect(RestClient::Utils.encode_query_string(input)).to eq expected
end end
end end
@ -131,7 +131,7 @@ describe RestClient::Utils do
{ {
RestClient::ParamsArray.new([[:foo, 1], [:foo, 2], [:foo, 3]]) => 'foo=1&foo=2&foo=3', RestClient::ParamsArray.new([[:foo, 1], [:foo, 2], [:foo, 3]]) => 'foo=1&foo=2&foo=3',
}.each_pair do |input, expected| }.each_pair do |input, expected|
RestClient::Utils.encode_query_string(input).should eq expected expect(RestClient::Utils.encode_query_string(input)).to eq expected
end end
end end
@ -140,7 +140,7 @@ describe RestClient::Utils do
{foo: RestClient::ParamsArray.new([[:a, 1], [:a, 2]])} => 'foo[a]=1&foo[a]=2', {foo: RestClient::ParamsArray.new([[:a, 1], [:a, 2]])} => 'foo[a]=1&foo[a]=2',
RestClient::ParamsArray.new([[:foo, {a: 1}], [:foo, {a: 2}]]) => 'foo[a]=1&foo[a]=2', RestClient::ParamsArray.new([[:foo, {a: 1}], [:foo, {a: 2}]]) => 'foo[a]=1&foo[a]=2',
}.each_pair do |input, expected| }.each_pair do |input, expected|
RestClient::Utils.encode_query_string(input).should eq expected expect(RestClient::Utils.encode_query_string(input)).to eq expected
end end
end end
end end

View file

@ -5,7 +5,7 @@ describe 'RestClient::Windows::RootCerts',
let(:x509_store) { RestClient::Windows::RootCerts.instance.to_a } let(:x509_store) { RestClient::Windows::RootCerts.instance.to_a }
it 'should return at least one X509 certificate' do it 'should return at least one X509 certificate' do
expect(x509_store.to_a).to have_at_least(1).items expect(x509_store.to_a.size).to be >= 1
end end
it 'should return an X509 certificate with a subject' do it 'should return an X509 certificate with a subject' do
@ -16,7 +16,7 @@ describe 'RestClient::Windows::RootCerts',
it 'should return X509 certificate objects' do it 'should return X509 certificate objects' do
x509_store.each do |cert| x509_store.each do |cert|
cert.should be_a(OpenSSL::X509::Certificate) expect(cert).to be_a(OpenSSL::X509::Certificate)
end end
end end
end end