diff --git a/spec/integration/httpbin_spec.rb b/spec/integration/httpbin_spec.rb index 2ef6faf..d77c4bf 100644 --- a/spec/integration/httpbin_spec.rb +++ b/spec/integration/httpbin_spec.rb @@ -41,22 +41,22 @@ describe RestClient::Request do describe '.execute' do it 'sends a user agent' do data = execute_httpbin_json('user-agent', method: :get) - data['user-agent'].should match(/rest-client/) + expect(data['user-agent']).to match(/rest-client/) end it 'receives cookies on 302' do expect { execute_httpbin('cookies/set?foo=bar', method: :get, max_redirects: 0) }.to raise_error(RestClient::Found) { |ex| - ex.http_code.should eq 302 - ex.response.cookies['foo'].should eq 'bar' + expect(ex.http_code).to eq 302 + expect(ex.response.cookies['foo']).to eq 'bar' } end it 'passes along cookies through 302' do data = execute_httpbin_json('cookies/set?foo=bar', method: :get) - data.should have_key('cookies') - data['cookies']['foo'].should eq 'bar' + expect(data).to have_key('cookies') + expect(data['cookies']['foo']).to eq 'bar' end it 'handles quote wrapped cookies' do @@ -64,8 +64,8 @@ describe RestClient::Request do execute_httpbin('cookies/set?foo=' + CGI.escape('"bar:baz"'), method: :get, max_redirects: 0) }.to raise_error(RestClient::Found) { |ex| - ex.http_code.should eq 302 - ex.response.cookies['foo'].should eq '"bar:baz"' + expect(ex.http_code).to eq 302 + expect(ex.response.cookies['foo']).to eq '"bar:baz"' } end end diff --git a/spec/integration/integration_spec.rb b/spec/integration/integration_spec.rb index 4fed8ea..21948aa 100644 --- a/spec/integration/integration_spec.rb +++ b/spec/integration/integration_spec.rb @@ -8,15 +8,15 @@ describe RestClient do body = 'abc' stub_request(:get, "www.example.com").to_return(:body => body, :status => 200) response = RestClient.get "www.example.com" - response.code.should eq 200 - response.body.should eq body + expect(response.code).to eq 200 + expect(response.body).to eq body end 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' } ) response = RestClient.get "www.example.com" - response.code.should eq 200 - response.body.should eq "i'm gziped\n" + expect(response.code).to eq 200 + expect(response.body).to eq "i'm gziped\n" end it "a 404" do @@ -26,10 +26,10 @@ describe RestClient do RestClient.get "www.example.com" raise rescue RestClient::ResourceNotFound => e - e.http_code.should eq 404 - e.response.code.should eq 404 - e.response.body.should eq body - e.http_body.should eq body + expect(e.http_code).to eq 404 + expect(e.response.code).to eq 404 + expect(e.response.body).to eq body + expect(e.http_body).to eq body end end @@ -41,8 +41,8 @@ describe RestClient do 'Content-Type' => 'text/plain; charset=UTF-8' }) response = RestClient.get "www.example.com" - response.encoding.should eq Encoding::UTF_8 - response.valid_encoding?.should eq true + expect(response.encoding).to eq Encoding::UTF_8 + expect(response.valid_encoding?).to eq true end it 'handles windows-1252' do @@ -52,9 +52,9 @@ describe RestClient do 'Content-Type' => 'text/plain; charset=windows-1252' }) response = RestClient.get "www.example.com" - response.encoding.should eq Encoding::WINDOWS_1252 - response.encode('utf-8').should eq "ÿ" - response.valid_encoding?.should eq true + expect(response.encoding).to eq Encoding::WINDOWS_1252 + expect(response.encode('utf-8')).to eq "ÿ" + expect(response.valid_encoding?).to eq true end it 'handles binary' do @@ -64,28 +64,28 @@ describe RestClient do 'Content-Type' => 'application/octet-stream; charset=binary' }) response = RestClient.get "www.example.com" - response.encoding.should eq Encoding::BINARY - lambda { + expect(response.encoding).to eq Encoding::BINARY + expect { response.encode('utf-8') - }.should raise_error(Encoding::UndefinedConversionError) - response.valid_encoding?.should eq true + }.to raise_error(Encoding::UndefinedConversionError) + expect(response.valid_encoding?).to eq true end it 'handles euc-jp' do body = "\xA4\xA2\xA4\xA4\xA4\xA6\xA4\xA8\xA4\xAA". force_encoding(Encoding::BINARY) 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( :body => body, :status => 200, :headers => { 'Content-Type' => 'text/plain; charset=EUC-JP' }) response = RestClient.get 'www.example.com' - response.encoding.should eq Encoding::EUC_JP - response.valid_encoding?.should eq true - response.length.should eq 5 - response.encode('utf-8').should eq body_utf8 + expect(response.encoding).to eq Encoding::EUC_JP + expect(response.valid_encoding?).to eq true + expect(response.length).to eq 5 + expect(response.encode('utf-8')).to eq body_utf8 end it 'defaults to Encoding.default_external' do @@ -95,7 +95,7 @@ describe RestClient do }) response = RestClient.get 'www.example.com' - response.encoding.should eq Encoding.default_external + expect(response.encoding).to eq Encoding.default_external end it 'handles invalid encoding' do @@ -105,7 +105,7 @@ describe RestClient do }) response = RestClient.get 'www.example.com' - response.encoding.should eq Encoding.default_external + expect(response.encoding).to eq Encoding.default_external end it 'leaves images as binary' do @@ -117,7 +117,7 @@ describe RestClient do }) response = RestClient.get 'www.example.com' - response.encoding.should eq Encoding::BINARY + expect(response.encoding).to eq Encoding::BINARY end end end diff --git a/spec/integration/request_spec.rb b/spec/integration/request_spec.rb index 7e305ee..99bfefa 100644 --- a/spec/integration/request_spec.rb +++ b/spec/integration/request_spec.rb @@ -75,7 +75,7 @@ describe RestClient::Request do }, ) expect {request.execute }.to_not raise_error - ran_callback.should eq(true) + expect(ran_callback).to eq(true) end it "fails verification when the callback returns false", diff --git a/spec/unit/abstract_response_spec.rb b/spec/unit/abstract_response_spec.rb index 55c4a14..0407165 100644 --- a/spec/unit/abstract_response_spec.rb +++ b/spec/unit/abstract_response_spec.rb @@ -22,124 +22,124 @@ describe RestClient::AbstractResponse, :include_helpers do end it "fetches the numeric response code" do - @net_http_res.should_receive(:code).and_return('200') - @response.code.should eq 200 + expect(@net_http_res).to receive(:code).and_return('200') + expect(@response.code).to eq 200 end it "has a nice description" do - @net_http_res.should_receive(:to_hash).and_return({'Content-Type' => ['application/pdf']}) - @net_http_res.should_receive(:code).and_return('200') - @response.description.should eq "200 OK | application/pdf bytes\n" + expect(@net_http_res).to receive(:to_hash).and_return({'Content-Type' => ['application/pdf']}) + expect(@net_http_res).to receive(:code).and_return('200') + expect(@response.description).to eq "200 OK | application/pdf bytes\n" end describe '.beautify_headers' do it "beautifies the headers by turning the keys to symbols" do h = RestClient::AbstractResponse.beautify_headers('content-type' => [ 'x' ]) - h.keys.first.should eq :content_type + expect(h.keys.first).to eq :content_type end 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.values.first.should eq 'text/html' + expect(h.values.first).to eq 'text/html' end it 'joins multiple header values by comma' do - RestClient::AbstractResponse.beautify_headers( + expect(RestClient::AbstractResponse.beautify_headers( {'My-Header' => ['one', 'two']} - ).should eq({:my_header => 'one, two'}) + )).to eq({:my_header => 'one, two'}) end it 'leaves set-cookie headers as array' do - RestClient::AbstractResponse.beautify_headers( + expect(RestClient::AbstractResponse.beautify_headers( {'Set-Cookie' => ['cookie1=foo', 'cookie2=bar']} - ).should eq({:set_cookie => ['cookie1=foo', 'cookie2=bar']}) + )).to eq({:set_cookie => ['cookie1=foo', 'cookie2=bar']}) end end it "fetches the headers" do - @net_http_res.should_receive(:to_hash).and_return('content-type' => [ 'text/html' ]) - @response.headers.should eq({ :content_type => 'text/html' }) + expect(@net_http_res).to receive(:to_hash).and_return('content-type' => [ 'text/html' ]) + expect(@response.headers).to eq({ :content_type => 'text/html' }) end it "extracts cookies from response headers" do - @net_http_res.should_receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/']) - @response.cookies.should eq({ 'session_id' => '1' }) + expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/']) + expect(@response.cookies).to eq({ 'session_id' => '1' }) end it "extract strange cookies" do - @net_http_res.should_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=/']}) - @response.cookies.should eq({ 'session_id' => 'ZJ/HQVH6YE+rVkTpn0zvTQ==' }) + expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/']) + expect(@response.headers).to eq({:set_cookie => ['session_id=ZJ/HQVH6YE+rVkTpn0zvTQ==; path=/']}) + expect(@response.cookies).to eq({ 'session_id' => 'ZJ/HQVH6YE+rVkTpn0zvTQ==' }) end it "doesn't escape cookies" do - @net_http_res.should_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(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca; path=/']) + expect(@response.cookies).to eq({ 'session_id' => 'BAh7BzoNYXBwX25hbWUiEGFwcGxpY2F0aW9uOgpsb2dpbiIKYWRtaW4%3D%0A--08114ba654f17c04d20dcc5228ec672508f738ca' }) end describe '.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=/']) - @response.cookie_jar.should be_a HTTP::CookieJar + expect(@net_http_res).to receive(:to_hash).and_return('set-cookie' => ['session_id=1; path=/']) + expect(@response.cookie_jar).to be_a HTTP::CookieJar cookie = @response.cookie_jar.cookies.first - cookie.domain.should eq 'example.com' - cookie.name.should eq 'session_id' - cookie.value.should eq '1' - cookie.path.should eq '/' + expect(cookie.domain).to eq 'example.com' + expect(cookie.name).to eq 'session_id' + expect(cookie.value).to eq '1' + expect(cookie.path).to eq '/' end it 'handles cookies when URI scheme is implicit' do 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'), method: 'get', cookie_jar: HTTP::CookieJar.new) 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.domain.should eq 'example.com' - cookie.name.should eq 'session_id' - cookie.value.should eq '1' - cookie.path.should eq '/' + expect(cookie.domain).to eq 'example.com' + expect(cookie.name).to eq 'session_id' + expect(cookie.value).to eq '1' + expect(cookie.path).to eq '/' end end 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 describe "#return!" do it "should return the response itself on 200-codes" do - @net_http_res.should_receive(:code).and_return('200') - @response.return!.should be_equal(@response) + expect(@net_http_res).to receive(:code).and_return('200') + expect(@response.return!).to be_equal(@response) end it "should raise RequestFailed on unknown codes" do - @net_http_res.should_receive(:code).and_return('1000') - lambda { @response.return! }.should raise_error RestClient::RequestFailed + expect(@net_http_res).to receive(:code).and_return('1000') + expect { @response.return! }.to raise_error RestClient::RequestFailed end it "should raise an error on a redirection after non-GET/HEAD requests" do - @net_http_res.should_receive(:code).and_return('301') - @request.should_receive(:method).and_return('put') - @response.should_not_receive(:follow_redirection) - lambda { @response.return! }.should raise_error RestClient::RequestFailed + expect(@net_http_res).to receive(:code).and_return('301') + expect(@request).to receive(:method).and_return('put') + expect(@response).not_to receive(:follow_redirection) + expect { @response.return! }.to raise_error RestClient::RequestFailed end it "should follow 302 redirect" do - @net_http_res.should_receive(:code).and_return('302') - @response.should_receive(:check_max_redirects).and_return('fake-check') - @response.should_receive(:follow_redirection).and_return('fake-redirection') - @response.return!.should eq 'fake-redirection' + expect(@net_http_res).to receive(:code).and_return('302') + expect(@response).to receive(:check_max_redirects).and_return('fake-check') + expect(@response).to receive(:follow_redirection).and_return('fake-redirection') + expect(@response.return!).to eq 'fake-redirection' end it "should gracefully handle 302 redirect with no location header" do @net_http_res = response_double(code: 302, location: nil) @request = request_double() @response = MyAbstractResponse.new(@net_http_res, @request) - @response.should_receive(:check_max_redirects).and_return('fake-check') - lambda { @response.return! }.should raise_error RestClient::Found + expect(@response).to receive(:check_max_redirects).and_return('fake-check') + expect { @response.return! }.to raise_error RestClient::Found end end end diff --git a/spec/unit/exceptions_spec.rb b/spec/unit/exceptions_spec.rb index 95c3fe5..8675584 100644 --- a/spec/unit/exceptions_spec.rb +++ b/spec/unit/exceptions_spec.rb @@ -3,30 +3,30 @@ require_relative '_lib' 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 e = RestClient::Exception.new - e.message.should eq "RestClient::Exception" + expect(e.message).to eq "RestClient::Exception" end it "returns the 'message' that was set" do e = RestClient::Exception.new message = "An explicitly set message" e.message = message - e.message.should eq message + expect(e.message).to eq message end 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 it "contains exceptions in RestClient" do - RestClient::Unauthorized.new.should be_a_kind_of(RestClient::Exception) - RestClient::ServerBrokeConnection.new.should be_a_kind_of(RestClient::Exception) + expect(RestClient::Unauthorized.new).to be_a_kind_of(RestClient::Exception) + expect(RestClient::ServerBrokeConnection.new).to be_a_kind_of(RestClient::Exception) end end describe RestClient::ServerBrokeConnection do it "should have a default message of 'Server broke connection'" do e = RestClient::ServerBrokeConnection.new - e.message.should eq 'Server broke connection' + expect(e.message).to eq 'Server broke connection' end end @@ -40,21 +40,21 @@ describe RestClient::RequestFailed do begin raise RestClient::RequestFailed, response rescue RestClient::RequestFailed => e - e.response.should eq response + expect(e.response).to eq response end end 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 it "http_body convenience method for fetching the body (decoding when necessary)" do - RestClient::RequestFailed.new(@response).http_code.should eq 502 - RestClient::RequestFailed.new(@response).message.should eq 'HTTP status code 502' + expect(RestClient::RequestFailed.new(@response).http_code).to eq 502 + expect(RestClient::RequestFailed.new(@response).message).to eq 'HTTP status code 502' end 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 @@ -64,7 +64,7 @@ describe RestClient::ResourceNotFound do begin raise RestClient::ResourceNotFound, response rescue RestClient::ResourceNotFound => e - e.response.should eq response + expect(e.response).to eq response end end @@ -75,34 +75,34 @@ describe RestClient::ResourceNotFound do RestClient.get "www.example.com" raise rescue RestClient::ResourceNotFound => e - e.response.body.should eq body + expect(e.response.body).to eq body end end end describe "backwards compatibility" do it 'aliases RestClient::NotFound as ResourceNotFound' do - RestClient::ResourceNotFound.should eq RestClient::NotFound + expect(RestClient::ResourceNotFound).to eq RestClient::NotFound end it 'aliases old names for HTTP 413, 414, 416' do - RestClient::RequestEntityTooLarge.should eq RestClient::PayloadTooLarge - RestClient::RequestURITooLong.should eq RestClient::URITooLong - RestClient::RequestedRangeNotSatisfiable.should eq RestClient::RangeNotSatisfiable + expect(RestClient::RequestEntityTooLarge).to eq RestClient::PayloadTooLarge + expect(RestClient::RequestURITooLong).to eq RestClient::URITooLong + expect(RestClient::RequestedRangeNotSatisfiable).to eq RestClient::RangeNotSatisfiable end it 'subclasses NotFound from RequestFailed, ExceptionWithResponse' do - RestClient::NotFound.should be < RestClient::RequestFailed - RestClient::NotFound.should be < RestClient::ExceptionWithResponse + expect(RestClient::NotFound).to be < RestClient::RequestFailed + expect(RestClient::NotFound).to be < RestClient::ExceptionWithResponse end it 'subclasses timeout from RestClient::RequestTimeout, RequestFailed, EWR' do - RestClient::Exceptions::OpenTimeout.should be < RestClient::Exceptions::Timeout - RestClient::Exceptions::ReadTimeout.should be < RestClient::Exceptions::Timeout + expect(RestClient::Exceptions::OpenTimeout).to be < RestClient::Exceptions::Timeout + expect(RestClient::Exceptions::ReadTimeout).to be < RestClient::Exceptions::Timeout - RestClient::Exceptions::Timeout.should be < RestClient::RequestTimeout - RestClient::Exceptions::Timeout.should be < RestClient::RequestFailed - RestClient::Exceptions::Timeout.should be < RestClient::ExceptionWithResponse + expect(RestClient::Exceptions::Timeout).to be < RestClient::RequestTimeout + expect(RestClient::Exceptions::Timeout).to be < RestClient::RequestFailed + expect(RestClient::Exceptions::Timeout).to be < RestClient::ExceptionWithResponse end end diff --git a/spec/unit/params_array_spec.rb b/spec/unit/params_array_spec.rb index f37a4fe..926f088 100644 --- a/spec/unit/params_array_spec.rb +++ b/spec/unit/params_array_spec.rb @@ -12,11 +12,11 @@ describe RestClient::ParamsArray do [{foo: 123}, [:foo, 456], {bar: 789}, {empty: nil}], [{foo: 123}, [:foo, 456], {bar: 789}, [:empty]], ].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 - RestClient::ParamsArray.new([]).to_a.should eq [] - RestClient::ParamsArray.new([]).empty?.should eq true + expect(RestClient::ParamsArray.new([]).to_a).to eq [] + expect(RestClient::ParamsArray.new([]).empty?).to eq true end it 'rejects various invalid input' do diff --git a/spec/unit/payload_spec.rb b/spec/unit/payload_spec.rb index e789d37..d34634a 100644 --- a/spec/unit/payload_spec.rb +++ b/spec/unit/payload_spec.rb @@ -6,63 +6,63 @@ describe RestClient::Payload do context "Base Payload" do it "should reset stream after to_s" do payload = RestClient::Payload::Base.new('foobar') - payload.to_s.should eq 'foobar' - payload.to_s.should eq 'foobar' + expect(payload.to_s).to eq 'foobar' + expect(payload.to_s).to eq 'foobar' end end context "A regular Payload" do it "should use standard enctype as default content-type" do - RestClient::Payload::UrlEncoded.new({}).headers['Content-Type']. - should eq 'application/x-www-form-urlencoded' + expect(RestClient::Payload::UrlEncoded.new({}).headers['Content-Type']). + to eq 'application/x-www-form-urlencoded' end it "should form properly encoded params" do - RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s. - should eq "foo=bar" - ["foo=bar&baz=qux", "baz=qux&foo=bar"].should include( + expect(RestClient::Payload::UrlEncoded.new({:foo => 'bar'}).to_s). + to eq "foo=bar" + expect(["foo=bar&baz=qux", "baz=qux&foo=bar"]).to include( RestClient::Payload::UrlEncoded.new({:foo => 'bar', :baz => 'qux'}).to_s) end it "should escape parameters" do - RestClient::Payload::UrlEncoded.new({'foo + bar' => 'baz'}).to_s. - should eq "foo+%2B+bar=baz" + expect(RestClient::Payload::UrlEncoded.new({'foo + bar' => 'baz'}).to_s). + to eq "foo+%2B+bar=baz" end it "should properly handle hashes as parameter" do - RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s. - should eq "foo[bar]=baz" - RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s. - should eq "foo[bar][baz]=qux" + expect(RestClient::Payload::UrlEncoded.new({:foo => {:bar => 'baz'}}).to_s). + to eq "foo[bar]=baz" + expect(RestClient::Payload::UrlEncoded.new({:foo => {:bar => {:baz => 'qux'}}}).to_s). + to eq "foo[bar][baz]=qux" end it "should handle many attributes inside a hash" do 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 it "should handle attributes inside an array inside an hash" do 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 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.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 it "should form properly use symbols as parameters" do - RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s. - should eq "foo=bar" - RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s. - should eq "foo[bar]=baz" + expect(RestClient::Payload::UrlEncoded.new({:foo => :bar}).to_s). + to eq "foo=bar" + expect(RestClient::Payload::UrlEncoded.new({:foo => {:bar => :baz}}).to_s). + to eq "foo[bar]=baz" end it "should properly handle arrays as repeated parameters" do - RestClient::Payload::UrlEncoded.new({:foo => ['bar']}).to_s. - should eq "foo[]=bar" - RestClient::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s. - should eq "foo[]=bar&foo[]=baz" + expect(RestClient::Payload::UrlEncoded.new({:foo => ['bar']}).to_s). + to eq "foo[]=bar" + expect(RestClient::Payload::UrlEncoded.new({:foo => ['bar', 'baz']}).to_s). + to eq "foo[]=bar&foo[]=baz" end it 'should not close if stream already closed' do @@ -75,8 +75,8 @@ describe RestClient::Payload do context "A multipart Payload" do it "should use standard enctype as default content-type" do m = RestClient::Payload::Multipart.new({}) - m.stub(:boundary).and_return(123) - m.headers['Content-Type'].should eq 'multipart/form-data; boundary=123' + allow(m).to receive(:boundary).and_return(123) + expect(m.headers['Content-Type']).to eq 'multipart/form-data; boundary=123' end 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 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 Content-Disposition: form-data; name="bar"\r \r @@ -101,7 +101,7 @@ bar\r it "should not escape parameters names" do m = RestClient::Payload::Multipart.new([["bar ", "baz"]]) - m.to_s.should eq <<-EOS + expect(m.to_s).to eq <<-EOS --#{m.boundary}\r Content-Disposition: form-data; name="bar "\r \r @@ -113,7 +113,7 @@ baz\r it "should form properly separated multipart data" do f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") m = RestClient::Payload::Multipart.new({:foo => f}) - m.to_s.should eq <<-EOS + expect(m.to_s).to eq <<-EOS --#{m.boundary}\r Content-Disposition: form-data; name="foo"; filename="master_shake.jpg"\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 f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") m = RestClient::Payload::Multipart.new({nil => f}) - m.to_s.should eq <<-EOS + expect(m.to_s).to eq <<-EOS --#{m.boundary}\r Content-Disposition: form-data; filename="master_shake.jpg"\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 original_filename; 'foo.txt'; end" m = RestClient::Payload::Multipart.new({:foo => f}) - m.to_s.should eq <<-EOS + expect(m.to_s).to eq <<-EOS --#{m.boundary}\r Content-Disposition: form-data; name="foo"; filename="foo.txt"\r Content-Type: text/plain\r @@ -153,7 +153,7 @@ Content-Type: text/plain\r it "should handle hash in hash parameters" do m = RestClient::Payload::Multipart.new({:bar => {:baz => "foo"}}) - m.to_s.should eq <<-EOS + expect(m.to_s).to eq <<-EOS --#{m.boundary}\r Content-Disposition: form-data; name="bar[baz]"\r \r @@ -165,7 +165,7 @@ foo\r f.instance_eval "def content_type; 'text/plain'; end" f.instance_eval "def original_filename; 'foo.txt'; end" m = RestClient::Payload::Multipart.new({:foo => {:bar => f}}) - m.to_s.should eq <<-EOS + expect(m.to_s).to eq <<-EOS --#{m.boundary}\r Content-Disposition: form-data; name="foo[bar]"; filename="foo.txt"\r Content-Type: text/plain\r @@ -176,10 +176,10 @@ Content-Type: text/plain\r end 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') m = RestClient::Payload::Multipart.new({:foo => f}) - m.boundary.should eq('-' * 4 + 'RubyFormBoundary' + 'TGs89AttwBxna6TV') + expect(m.boundary).to eq('-' * 4 + 'RubyFormBoundary' + 'TGs89AttwBxna6TV') end end @@ -188,23 +188,23 @@ Content-Type: text/plain\r it "should properly determine the size of file payloads" do f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") payload = RestClient::Payload.generate(f) - payload.size.should eq 76_988 - payload.length.should eq 76_988 + expect(payload.size).to eq 76_988 + expect(payload.length).to eq 76_988 end it "should properly determine the size of other kinds of streaming payloads" do s = StringIO.new 'foo' payload = RestClient::Payload.generate(s) - payload.size.should eq 3 - payload.length.should eq 3 + expect(payload.size).to eq 3 + expect(payload.length).to eq 3 begin f = Tempfile.new "rest-client" f.write 'foo bar' payload = RestClient::Payload.generate(f) - payload.size.should eq 7 - payload.length.should eq 7 + expect(payload.size).to eq 7 + expect(payload.length).to eq 7 ensure f.close end @@ -213,51 +213,51 @@ Content-Type: text/plain\r context "Payload generation" 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 it "should recognize multipart params" do 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 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 it "should handle deeply nested multipart" do f = File.new(File.dirname(__FILE__) + "/master_shake.jpg") 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 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 it "should recognize nested multipart payloads in hashes" do 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 it "should recognize nested multipart payloads in arrays" do 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 it "should recognize file payloads that can be streamed" do 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 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 # 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 - 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 diff --git a/spec/unit/raw_response_spec.rb b/spec/unit/raw_response_spec.rb index 0c395c9..13f859d 100644 --- a/spec/unit/raw_response_spec.rb +++ b/spec/unit/raw_response_spec.rb @@ -9,10 +9,10 @@ describe RestClient::RawResponse do end 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 it "exposes a Tempfile" do - @response.file.should eq @tf + expect(@response.file).to eq @tf end end diff --git a/spec/unit/request2_spec.rb b/spec/unit/request2_spec.rb index 80f85f0..71e5d6e 100644 --- a/spec/unit/request2_spec.rb +++ b/spec/unit/request2_spec.rb @@ -5,20 +5,20 @@ describe RestClient::Request do context '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) - 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) - 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 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) - 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 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) - RestClient::Request.execute(url: 'http://some/resource', method: :get, headers: { + expect(RestClient::Request.execute(url: 'http://some/resource', method: :get, headers: { params: { a: { foo: [1,2], @@ -27,7 +27,7 @@ describe RestClient::Request do b: 'foo bar', math: '2 + 2 == 4', } - }).body.should eq 'foo' + }).body).to eq 'foo' end end @@ -39,7 +39,7 @@ describe RestClient::Request do 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) 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 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) 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 diff --git a/spec/unit/request_spec.rb b/spec/unit/request_spec.rb index f643e05..f428f76 100644 --- a/spec/unit/request_spec.rb +++ b/spec/unit/request_spec.rb @@ -5,123 +5,123 @@ describe RestClient::Request, :include_helpers do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload') @uri = double("uri") - @uri.stub(:request_uri).and_return('/resource') - @uri.stub(:hostname).and_return('some') - @uri.stub(:port).and_return(80) + allow(@uri).to receive(:request_uri).and_return('/resource') + allow(@uri).to receive(:hostname).and_return('some') + allow(@uri).to receive(:port).and_return(80) @net = double("net::http base") @http = double("net::http connection") - Net::HTTP.stub(:new).and_return(@net) + allow(Net::HTTP).to receive(:new).and_return(@net) - @net.stub(:start).and_yield(@http) - @net.stub(:use_ssl=) - @net.stub(:verify_mode=) - @net.stub(:verify_callback=) + allow(@net).to receive(:start).and_yield(@http) + allow(@net).to receive(:use_ssl=) + allow(@net).to receive(:verify_mode=) + allow(@net).to receive(:verify_callback=) allow(@net).to receive(:ciphers=) allow(@net).to receive(:cert_store=) RestClient.log = nil end it "accept */* mimetype" do - @request.default_headers[:accept].should eq '*/*' + expect(@request.default_headers[:accept]).to eq '*/*' end describe "compression" do it "decodes an uncompressed result body by passing it straight through" do - RestClient::Request.decode(nil, 'xyz').should eq 'xyz' + expect(RestClient::Request.decode(nil, 'xyz')).to eq 'xyz' end it "doesn't fail for nil bodies" do - RestClient::Request.decode('gzip', nil).should be_nil + expect(RestClient::Request.decode('gzip', nil)).to be_nil end it "decodes a gzip body" do - RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should eq "i'm gziped\n" + expect(RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000")).to eq "i'm gziped\n" end it "ingores gzip for empty bodies" do - RestClient::Request.decode('gzip', '').should be_empty + expect(RestClient::Request.decode('gzip', '')).to be_empty end it "decodes a deflated body" do - RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should eq "some deflated text" + expect(RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363")).to eq "some deflated text" end end it "processes a successful result" do res = response_double - res.stub(:code).and_return("200") - res.stub(:body).and_return('body') - res.stub(:[]).with('content-encoding').and_return(nil) - @request.send(:process_result, res).body.should eq 'body' - @request.send(:process_result, res).to_s.should eq 'body' + allow(res).to receive(:code).and_return("200") + allow(res).to receive(:body).and_return('body') + allow(res).to receive(:[]).with('content-encoding').and_return(nil) + expect(@request.send(:process_result, res).body).to eq 'body' + expect(@request.send(:process_result, res).to_s).to eq 'body' end it "doesn't classify successful requests as failed" do 203.upto(207) do |code| res = response_double - res.stub(:code).and_return(code.to_s) - res.stub(:body).and_return("") - res.stub(:[]).with('content-encoding').and_return(nil) - @request.send(:process_result, res).should be_empty + allow(res).to receive(:code).and_return(code.to_s) + allow(res).to receive(:body).and_return("") + allow(res).to receive(:[]).with('content-encoding').and_return(nil) + expect(@request.send(:process_result, res)).to be_empty end end describe '.normalize_url' do it "adds http:// to the front of resources specified in the syntax example.com/resource" do - @request.normalize_url('example.com/resource').should eq 'http://example.com/resource' + expect(@request.normalize_url('example.com/resource')).to eq 'http://example.com/resource' end it 'adds http:// to resources containing a colon' do - @request.normalize_url('example.com:1234').should eq 'http://example.com:1234' + expect(@request.normalize_url('example.com:1234')).to eq 'http://example.com:1234' end it 'does not add http:// to the front of https resources' do - @request.normalize_url('https://example.com/resource').should eq 'https://example.com/resource' + expect(@request.normalize_url('https://example.com/resource')).to eq 'https://example.com/resource' end it 'does not add http:// to the front of capital HTTP resources' do - @request.normalize_url('HTTP://example.com/resource').should eq 'HTTP://example.com/resource' + expect(@request.normalize_url('HTTP://example.com/resource')).to eq 'HTTP://example.com/resource' end it 'does not add http:// to the front of capital HTTPS resources' do - @request.normalize_url('HTTPS://example.com/resource').should eq 'HTTPS://example.com/resource' + expect(@request.normalize_url('HTTPS://example.com/resource')).to eq 'HTTPS://example.com/resource' end it 'raises with invalid URI' do - lambda { + expect { RestClient::Request.new(method: :get, url: 'http://a@b:c') - }.should raise_error(URI::InvalidURIError) - lambda { + }.to raise_error(URI::InvalidURIError) + expect { RestClient::Request.new(method: :get, url: 'http://::') - }.should raise_error(URI::InvalidURIError) + }.to raise_error(URI::InvalidURIError) end end describe "user - password" do it "extracts the username and password when parsing http://user:password@example.com/" do - URI.stub(:parse).and_return(double('uri', user: 'joe', password: 'pass1', hostname: 'example.com')) + allow(URI).to receive(:parse).and_return(double('uri', user: 'joe', password: 'pass1', hostname: 'example.com')) @request.send(:parse_url_with_auth!, 'http://joe:pass1@example.com/resource') - @request.user.should eq 'joe' - @request.password.should eq 'pass1' + expect(@request.user).to eq 'joe' + expect(@request.password).to eq 'pass1' end it "extracts with escaping the username and password when parsing http://user:password@example.com/" do - URI.stub(:parse).and_return(double('uri', user: 'joe%20', password: 'pass1', hostname: 'example.com')) + allow(URI).to receive(:parse).and_return(double('uri', user: 'joe%20', password: 'pass1', hostname: 'example.com')) @request.send(:parse_url_with_auth!, 'http://joe%20:pass1@example.com/resource') - @request.user.should eq 'joe ' - @request.password.should eq 'pass1' + expect(@request.user).to eq 'joe ' + expect(@request.password).to eq 'pass1' end it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do - URI.stub(:parse).and_return(double('uri', user: nil, password: nil, hostname: 'example.com')) + allow(URI).to receive(:parse).and_return(double('uri', user: nil, password: nil, hostname: 'example.com')) @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2') @request.send(:parse_url_with_auth!, 'http://example.com/resource') - @request.user.should eq 'beth' - @request.password.should eq 'pass2' + expect(@request.user).to eq 'beth' + expect(@request.password).to eq 'pass2' end end @@ -148,22 +148,22 @@ describe RestClient::Request, :include_helpers do end request = RestClient::Request.new(method: :get, url: 'example.com', **opts) - request.should_receive(:default_headers).and_return({'Foo' => 'bar'}) - request.make_headers({}).should eq({'Foo' => 'bar', 'Cookie' => 'session_id=1; user_id=someone'}) - request.make_cookie_header.should eq 'session_id=1; user_id=someone' - request.cookies.should eq({'session_id' => '1', 'user_id' => 'someone'}) - request.cookie_jar.cookies.length.should eq 2 - request.cookie_jar.object_id.should_not eq jar.object_id # make sure we dup it + expect(request).to receive(:default_headers).and_return({'Foo' => 'bar'}) + expect(request.make_headers({})).to eq({'Foo' => 'bar', 'Cookie' => 'session_id=1; user_id=someone'}) + expect(request.make_cookie_header).to eq 'session_id=1; user_id=someone' + expect(request.cookies).to eq({'session_id' => '1', 'user_id' => 'someone'}) + expect(request.cookie_jar.cookies.length).to eq 2 + expect(request.cookie_jar.object_id).not_to eq jar.object_id # make sure we dup it end end # test with no cookies request = RestClient::Request.new(method: :get, url: 'example.com') - request.should_receive(:default_headers).and_return({'Foo' => 'bar'}) - request.make_headers({}).should eq({'Foo' => 'bar'}) - request.make_cookie_header.should be_nil - request.cookies.should eq({}) - request.cookie_jar.cookies.length.should eq 0 + expect(request).to receive(:default_headers).and_return({'Foo' => 'bar'}) + expect(request.make_headers({})).to eq({'Foo' => 'bar'}) + expect(request.make_cookie_header).to be_nil + expect(request.cookies).to eq({}) + expect(request.cookie_jar.cookies.length).to eq 0 end it 'strips out cookies set for a different domain name' do @@ -172,31 +172,31 @@ describe RestClient::Request, :include_helpers do jar << HTTP::Cookie.new('user_id', 'someone', domain: 'other.example.com', path: '/') request = RestClient::Request.new(method: :get, url: 'www.example.com', cookies: jar) - request.should_receive(:default_headers).and_return({'Foo' => 'bar'}) - request.make_headers({}).should eq({'Foo' => 'bar'}) - request.make_cookie_header.should eq nil - request.cookies.should eq({}) - request.cookie_jar.cookies.length.should eq 2 + expect(request).to receive(:default_headers).and_return({'Foo' => 'bar'}) + expect(request.make_headers({})).to eq({'Foo' => 'bar'}) + expect(request.make_cookie_header).to eq nil + expect(request.cookies).to eq({}) + expect(request.cookie_jar.cookies.length).to eq 2 end it 'assumes default domain and path for cookies set by hash' do request = RestClient::Request.new(method: :get, url: 'www.example.com', cookies: {'session_id' => '1'}) - request.cookie_jar.cookies.length.should eq 1 + expect(request.cookie_jar.cookies.length).to eq 1 cookie = request.cookie_jar.cookies.first - cookie.should be_a(HTTP::Cookie) - cookie.domain.should eq('www.example.com') - cookie.for_domain?.should be_truthy - cookie.path.should eq('/') + expect(cookie).to be_a(HTTP::Cookie) + expect(cookie.domain).to eq('www.example.com') + expect(cookie.for_domain?).to be_truthy + expect(cookie.path).to eq('/') end it 'rejects or warns with contradictory cookie options' do # same opt in two different places - lambda { + expect { RestClient::Request.new(method: :get, url: 'example.com', cookies: {bar: '456'}, headers: {cookies: {foo: '123'}}) - }.should raise_error(ArgumentError, /Cannot pass :cookies in Request.*headers/) + }.to raise_error(ArgumentError, /Cannot pass :cookies in Request.*headers/) # :cookies opt and Cookie header [ @@ -205,9 +205,9 @@ describe RestClient::Request, :include_helpers do {headers: {cookies: {foo: '123'}, cookie: 'foo'}}, {headers: {cookies: {foo: '123'}, 'Cookie' => 'foo'}}, ].each do |opts| - fake_stderr { + expect(fake_stderr { RestClient::Request.new(method: :get, url: 'example.com', **opts) - }.should match(/warning: overriding "Cookie" header with :cookies option/) + }).to match(/warning: overriding "Cookie" header with :cookies option/) end end @@ -215,8 +215,8 @@ describe RestClient::Request, :include_helpers do cookie = 'Foo%20:Bar%0A~' @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:test => cookie}) - @request.should_receive(:default_headers).and_return({'Foo' => 'bar'}) - @request.make_headers({}).should eq({ + expect(@request).to receive(:default_headers).and_return({'Foo' => 'bar'}) + expect(@request.make_headers({})).to eq({ 'Foo' => 'bar', 'Cookie' => "test=#{cookie}" }) @@ -227,17 +227,17 @@ describe RestClient::Request, :include_helpers do # the RFC 6265 (4.1.1) prohibited characters such as control characters. ['foo=bar', 'foo;bar', "foo\nbar"].each do |cookie_name| - lambda { + expect { RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {cookie_name => 'value'}) - }.should raise_error(ArgumentError, /\AInvalid cookie name/i) + }.to raise_error(ArgumentError, /\AInvalid cookie name/i) end cookie_name = '' - lambda { + expect { RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {cookie_name => 'value'}) - }.should raise_error(ArgumentError, /cookie name cannot be empty/i) + }.to raise_error(ArgumentError, /cookie name cannot be empty/i) end it "rejects cookie values containing invalid characters" do @@ -245,118 +245,118 @@ describe RestClient::Request, :include_helpers do # the RFC 6265 (4.1.1) prohibited characters such as control characters. ["foo\tbar", "foo\nbar"].each do |cookie_value| - lambda { + expect { RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {'test' => cookie_value}) - }.should raise_error(ArgumentError, /\AInvalid cookie value/i) + }.to raise_error(ArgumentError, /\AInvalid cookie value/i) end end it "uses netrc credentials" do - URI.stub(:parse).and_return(double('uri', :user => nil, :password => nil, :hostname => 'example.com')) - Netrc.stub(:read).and_return('example.com' => ['a', 'b']) + allow(URI).to receive(:parse).and_return(double('uri', :user => nil, :password => nil, :hostname => 'example.com')) + allow(Netrc).to receive(:read).and_return('example.com' => ['a', 'b']) @request.send(:parse_url_with_auth!, 'http://example.com/resource') - @request.user.should eq 'a' - @request.password.should eq 'b' + expect(@request.user).to eq 'a' + expect(@request.password).to eq 'b' end it "uses credentials in the url in preference to netrc" do - URI.stub(:parse).and_return(double('uri', :user => 'joe%20', :password => 'pass1', :hostname => 'example.com')) - Netrc.stub(:read).and_return('example.com' => ['a', 'b']) + allow(URI).to receive(:parse).and_return(double('uri', :user => 'joe%20', :password => 'pass1', :hostname => 'example.com')) + allow(Netrc).to receive(:read).and_return('example.com' => ['a', 'b']) @request.send(:parse_url_with_auth!, 'http://joe%20:pass1@example.com/resource') - @request.user.should eq 'joe ' - @request.password.should eq 'pass1' + expect(@request.user).to eq 'joe ' + expect(@request.password).to eq 'pass1' end it "determines the Net::HTTP class to instantiate by the method name" do - @request.net_http_request_class(:put).should eq Net::HTTP::Put + expect(@request.net_http_request_class(:put)).to eq Net::HTTP::Put end describe "user headers" do it "merges user headers with the default headers" do - @request.should_receive(:default_headers).and_return({ :accept => '*/*', :accept_encoding => 'gzip, deflate' }) + expect(@request).to receive(:default_headers).and_return({ :accept => '*/*', :accept_encoding => 'gzip, deflate' }) headers = @request.make_headers("Accept" => "application/json", :accept_encoding => 'gzip') - headers.should have_key "Accept-Encoding" - headers["Accept-Encoding"].should eq "gzip" - headers.should have_key "Accept" - headers["Accept"].should eq "application/json" + expect(headers).to have_key "Accept-Encoding" + expect(headers["Accept-Encoding"]).to eq "gzip" + expect(headers).to have_key "Accept" + expect(headers["Accept"]).to eq "application/json" end it "prefers the user header when the same header exists in the defaults" do - @request.should_receive(:default_headers).and_return({ '1' => '2' }) + expect(@request).to receive(:default_headers).and_return({ '1' => '2' }) headers = @request.make_headers('1' => '3') - headers.should have_key('1') - headers['1'].should eq '3' + expect(headers).to have_key('1') + expect(headers['1']).to eq '3' end it "converts user headers to string before calling CGI::unescape which fails on non string values" do - @request.should_receive(:default_headers).and_return({ '1' => '2' }) + expect(@request).to receive(:default_headers).and_return({ '1' => '2' }) headers = @request.make_headers('1' => 3) - headers.should have_key('1') - headers['1'].should eq '3' + expect(headers).to have_key('1') + expect(headers['1']).to eq '3' end end describe "header symbols" do it "converts header symbols from :content_type to 'Content-Type'" do - @request.should_receive(:default_headers).and_return({}) + expect(@request).to receive(:default_headers).and_return({}) headers = @request.make_headers(:content_type => 'abc') - headers.should have_key('Content-Type') - headers['Content-Type'].should eq 'abc' + expect(headers).to have_key('Content-Type') + expect(headers['Content-Type']).to eq 'abc' end it "converts content-type from extension to real content-type" do - @request.should_receive(:default_headers).and_return({}) + expect(@request).to receive(:default_headers).and_return({}) headers = @request.make_headers(:content_type => 'json') - headers.should have_key('Content-Type') - headers['Content-Type'].should eq 'application/json' + expect(headers).to have_key('Content-Type') + expect(headers['Content-Type']).to eq 'application/json' end it "converts accept from extension(s) to real content-type(s)" do - @request.should_receive(:default_headers).and_return({}) + expect(@request).to receive(:default_headers).and_return({}) headers = @request.make_headers(:accept => 'json, mp3') - headers.should have_key('Accept') - headers['Accept'].should eq 'application/json, audio/mpeg' + expect(headers).to have_key('Accept') + expect(headers['Accept']).to eq 'application/json, audio/mpeg' - @request.should_receive(:default_headers).and_return({}) + expect(@request).to receive(:default_headers).and_return({}) headers = @request.make_headers(:accept => :json) - headers.should have_key('Accept') - headers['Accept'].should eq 'application/json' + expect(headers).to have_key('Accept') + expect(headers['Accept']).to eq 'application/json' end it "only convert symbols in header" do - @request.should_receive(:default_headers).and_return({}) + expect(@request).to receive(:default_headers).and_return({}) headers = @request.make_headers({:foo_bar => 'value', "bar_bar" => 'value'}) - headers['Foo-Bar'].should eq 'value' - headers['bar_bar'].should eq 'value' + expect(headers['Foo-Bar']).to eq 'value' + expect(headers['bar_bar']).to eq 'value' end it "converts header values to strings" do - @request.make_headers('A' => 1)['A'].should eq '1' + expect(@request.make_headers('A' => 1)['A']).to eq '1' end end it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do klass = double("net:http class") - @request.should_receive(:net_http_request_class).with('put').and_return(klass) - klass.should_receive(:new).and_return('result') - @request.should_receive(:transmit).with(@request.uri, 'result', kind_of(RestClient::Payload::Base)) + expect(@request).to receive(:net_http_request_class).with('put').and_return(klass) + expect(klass).to receive(:new).and_return('result') + expect(@request).to receive(:transmit).with(@request.uri, 'result', kind_of(RestClient::Payload::Base)) @request.execute end it "IPv6: executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do @request = RestClient::Request.new(:method => :put, :url => 'http://[::1]/some/resource', :payload => 'payload') klass = double("net:http class") - @request.should_receive(:net_http_request_class).with('put').and_return(klass) + expect(@request).to receive(:net_http_request_class).with('put').and_return(klass) if RUBY_VERSION >= "2.0.0" - klass.should_receive(:new).with(kind_of(URI), kind_of(Hash)).and_return('result') + expect(klass).to receive(:new).with(kind_of(URI), kind_of(Hash)).and_return('result') else - klass.should_receive(:new).with(kind_of(String), kind_of(Hash)).and_return('result') + expect(klass).to receive(:new).with(kind_of(String), kind_of(Hash)).and_return('result') end - @request.should_receive(:transmit) + expect(@request).to receive(:transmit) @request.execute end @@ -364,8 +364,8 @@ describe RestClient::Request, :include_helpers do # part of the private API it "transmits the request with Net::HTTP" do - @http.should_receive(:request).with('req', 'payload') - @request.should_receive(:process_result) + expect(@http).to receive(:request).with('req', 'payload') + expect(@request).to receive(:process_result) @request.send(:transmit, @uri, 'req', 'payload') end @@ -374,187 +374,187 @@ describe RestClient::Request, :include_helpers do # that Request#initialize or Request#execute uses the payload. describe "payload" do it "sends nil payloads" do - @http.should_receive(:request).with('req', nil) - @request.should_receive(:process_result) - @request.stub(:response_log) + expect(@http).to receive(:request).with('req', nil) + expect(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', nil) end it "passes non-hash payloads straight through" do - RestClient::Payload.generate("x").to_s.should eq "x" + expect(RestClient::Payload.generate("x").to_s).to eq "x" end it "converts a hash payload to urlencoded data" do - RestClient::Payload.generate(:a => 'b c+d').to_s.should eq "a=b+c%2Bd" + expect(RestClient::Payload.generate(:a => 'b c+d').to_s).to eq "a=b+c%2Bd" end it "accepts nested hashes in payload" do payload = RestClient::Payload.generate(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }}).to_s - payload.should include('user[name]=joe') - payload.should include('user[location][country]=USA') - payload.should include('user[location][state]=CA') + expect(payload).to include('user[name]=joe') + expect(payload).to include('user[location][country]=USA') + expect(payload).to include('user[location][state]=CA') end end it "set urlencoded content_type header on hash payloads" do req = RestClient::Request.new(method: :post, url: 'http://some/resource', payload: {a: 1}) - req.processed_headers.fetch('Content-Type').should eq 'application/x-www-form-urlencoded' + expect(req.processed_headers.fetch('Content-Type')).to eq 'application/x-www-form-urlencoded' end describe "credentials" do it "sets up the credentials prior to the request" do - @http.stub(:request) + allow(@http).to receive(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) - @request.stub(:user).and_return('joe') - @request.stub(:password).and_return('mypass') - @request.should_receive(:setup_credentials).with('req') + allow(@request).to receive(:user).and_return('joe') + allow(@request).to receive(:password).and_return('mypass') + expect(@request).to receive(:setup_credentials).with('req') @request.send(:transmit, @uri, 'req', nil) end it "does not attempt to send any credentials if user is nil" do - @request.stub(:user).and_return(nil) + allow(@request).to receive(:user).and_return(nil) req = double("request") - req.should_not_receive(:basic_auth) + expect(req).not_to receive(:basic_auth) @request.send(:setup_credentials, req) end it "setup credentials when there's a user" do - @request.stub(:user).and_return('joe') - @request.stub(:password).and_return('mypass') + allow(@request).to receive(:user).and_return('joe') + allow(@request).to receive(:password).and_return('mypass') req = double("request") - req.should_receive(:basic_auth).with('joe', 'mypass') + expect(req).to receive(:basic_auth).with('joe', 'mypass') @request.send(:setup_credentials, req) end it "does not attempt to send credentials if Authorization header is set" do @request.headers['Authorization'] = 'Token abc123' - @request.stub(:user).and_return('joe') - @request.stub(:password).and_return('mypass') + allow(@request).to receive(:user).and_return('joe') + allow(@request).to receive(:password).and_return('mypass') req = double("request") - req.should_not_receive(:basic_auth) + expect(req).not_to receive(:basic_auth) @request.send(:setup_credentials, req) end end it "catches EOFError and shows the more informative ServerBrokeConnection" do - @http.stub(:request).and_raise(EOFError) - lambda { @request.send(:transmit, @uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection) + allow(@http).to receive(:request).and_raise(EOFError) + expect { @request.send(:transmit, @uri, 'req', nil) }.to raise_error(RestClient::ServerBrokeConnection) end it "catches OpenSSL::SSL::SSLError and raise it back without more informative message" do - @http.stub(:request).and_raise(OpenSSL::SSL::SSLError) - lambda { @request.send(:transmit, @uri, 'req', nil) }.should raise_error(OpenSSL::SSL::SSLError) + allow(@http).to receive(:request).and_raise(OpenSSL::SSL::SSLError) + expect { @request.send(:transmit, @uri, 'req', nil) }.to raise_error(OpenSSL::SSL::SSLError) end it "catches Timeout::Error and raise the more informative ReadTimeout" do - @http.stub(:request).and_raise(Timeout::Error) - lambda { @request.send(:transmit, @uri, 'req', nil) }.should raise_error(RestClient::Exceptions::ReadTimeout) + allow(@http).to receive(:request).and_raise(Timeout::Error) + expect { @request.send(:transmit, @uri, 'req', nil) }.to raise_error(RestClient::Exceptions::ReadTimeout) end it "catches Errno::ETIMEDOUT and raise the more informative ReadTimeout" do - @http.stub(:request).and_raise(Errno::ETIMEDOUT) - lambda { @request.send(:transmit, @uri, 'req', nil) }.should raise_error(RestClient::Exceptions::ReadTimeout) + allow(@http).to receive(:request).and_raise(Errno::ETIMEDOUT) + expect { @request.send(:transmit, @uri, 'req', nil) }.to raise_error(RestClient::Exceptions::ReadTimeout) end it "catches Net::ReadTimeout and raises RestClient's ReadTimeout", :if => defined?(Net::ReadTimeout) do - @http.stub(:request).and_raise(Net::ReadTimeout) - lambda { @request.send(:transmit, @uri, 'req', nil) }.should raise_error(RestClient::Exceptions::ReadTimeout) + allow(@http).to receive(:request).and_raise(Net::ReadTimeout) + expect { @request.send(:transmit, @uri, 'req', nil) }.to raise_error(RestClient::Exceptions::ReadTimeout) end it "catches Net::OpenTimeout and raises RestClient's OpenTimeout", :if => defined?(Net::OpenTimeout) do - @http.stub(:request).and_raise(Net::OpenTimeout) - lambda { @request.send(:transmit, @uri, 'req', nil) }.should raise_error(RestClient::Exceptions::OpenTimeout) + allow(@http).to receive(:request).and_raise(Net::OpenTimeout) + expect { @request.send(:transmit, @uri, 'req', nil) }.to raise_error(RestClient::Exceptions::OpenTimeout) end it "uses correct error message for ReadTimeout", :if => defined?(Net::ReadTimeout) do - @http.stub(:request).and_raise(Net::ReadTimeout) - lambda { @request.send(:transmit, @uri, 'req', nil) }.should raise_error(RestClient::Exceptions::ReadTimeout, 'Timed out reading data from server') + allow(@http).to receive(:request).and_raise(Net::ReadTimeout) + expect { @request.send(:transmit, @uri, 'req', nil) }.to raise_error(RestClient::Exceptions::ReadTimeout, 'Timed out reading data from server') end it "uses correct error message for OpenTimeout", :if => defined?(Net::OpenTimeout) do - @http.stub(:request).and_raise(Net::OpenTimeout) - lambda { @request.send(:transmit, @uri, 'req', nil) }.should raise_error(RestClient::Exceptions::OpenTimeout, 'Timed out connecting to server') + allow(@http).to receive(:request).and_raise(Net::OpenTimeout) + expect { @request.send(:transmit, @uri, 'req', nil) }.to raise_error(RestClient::Exceptions::OpenTimeout, 'Timed out connecting to server') end it "class method execute wraps constructor" do req = double("rest request") - RestClient::Request.should_receive(:new).with(1 => 2).and_return(req) - req.should_receive(:execute) + expect(RestClient::Request).to receive(:new).with(1 => 2).and_return(req) + expect(req).to receive(:execute) RestClient::Request.execute(1 => 2) end describe "exception" do it "raises Unauthorized when the response is 401" do res = response_double(:code => '401', :[] => ['content-encoding' => ''], :body => '' ) - lambda { @request.send(:process_result, res) }.should raise_error(RestClient::Unauthorized) + expect { @request.send(:process_result, res) }.to raise_error(RestClient::Unauthorized) end it "raises ResourceNotFound when the response is 404" do res = response_double(:code => '404', :[] => ['content-encoding' => ''], :body => '' ) - lambda { @request.send(:process_result, res) }.should raise_error(RestClient::ResourceNotFound) + expect { @request.send(:process_result, res) }.to raise_error(RestClient::ResourceNotFound) end it "raises RequestFailed otherwise" do res = response_double(:code => '500', :[] => ['content-encoding' => ''], :body => '' ) - lambda { @request.send(:process_result, res) }.should raise_error(RestClient::InternalServerError) + expect { @request.send(:process_result, res) }.to raise_error(RestClient::InternalServerError) end end describe "block usage" do it "returns what asked to" do res = response_double(:code => '401', :[] => ['content-encoding' => ''], :body => '' ) - @request.send(:process_result, res){|response, request| "foo"}.should eq "foo" + expect(@request.send(:process_result, res){|response, request| "foo"}).to eq "foo" end end describe "proxy" do before do # unstub Net::HTTP creation since we need to test it - Net::HTTP.unstub(:new) + allow(Net::HTTP).to receive(:new).and_call_original @proxy_req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload') end it "creates a proxy class if a proxy url is given" do - RestClient.stub(:proxy).and_return("http://example.com/") - RestClient.stub(:proxy_set?).and_return(true) - @proxy_req.net_http_object('host', 80).proxy?.should be true + allow(RestClient).to receive(:proxy).and_return("http://example.com/") + allow(RestClient).to receive(:proxy_set?).and_return(true) + expect(@proxy_req.net_http_object('host', 80).proxy?).to be true end it "creates a proxy class with the correct address if a IPv6 proxy url is given" do - RestClient.stub(:proxy).and_return("http://[::1]/") - RestClient.stub(:proxy_set?).and_return(true) - @proxy_req.net_http_object('host', 80).proxy?.should be true - @proxy_req.net_http_object('host', 80).proxy_address.should == '::1' + allow(RestClient).to receive(:proxy).and_return("http://[::1]/") + allow(RestClient).to receive(:proxy_set?).and_return(true) + expect(@proxy_req.net_http_object('host', 80).proxy?).to be true + expect(@proxy_req.net_http_object('host', 80).proxy_address).to eq('::1') end it "creates a non-proxy class if a proxy url is not given" do - @proxy_req.net_http_object('host', 80).proxy?.should be_falsey + expect(@proxy_req.net_http_object('host', 80).proxy?).to be_falsey end it "disables proxy on a per-request basis" do - RestClient.stub(:proxy).and_return('http://example.com') - RestClient.stub(:proxy_set?).and_return(true) - @proxy_req.net_http_object('host', 80).proxy?.should be true + allow(RestClient).to receive(:proxy).and_return('http://example.com') + allow(RestClient).to receive(:proxy_set?).and_return(true) + expect(@proxy_req.net_http_object('host', 80).proxy?).to be true disabled_req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => nil) - disabled_req.net_http_object('host', 80).proxy?.should be_falsey + expect(disabled_req.net_http_object('host', 80).proxy?).to be_falsey end it "sets proxy on a per-request basis" do - @proxy_req.net_http_object('some', 80).proxy?.should be_falsey + expect(@proxy_req.net_http_object('some', 80).proxy?).to be_falsey req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => 'http://example.com') - req.net_http_object('host', 80).proxy?.should be true + expect(req.net_http_object('host', 80).proxy?).to be true end it "overrides proxy from environment", if: RUBY_VERSION >= '2.0' do @@ -564,19 +564,19 @@ describe RestClient::Request, :include_helpers do allow(ENV).to receive(:[]).with("NETRC").and_return(nil) req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload') obj = req.net_http_object('host', 80) - obj.proxy?.should be true - obj.proxy_address.should eq '127.0.0.1' + expect(obj.proxy?).to be true + expect(obj.proxy_address).to eq '127.0.0.1' # test original method .proxy? req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => nil) obj = req.net_http_object('host', 80) - obj.proxy?.should be_falsey + expect(obj.proxy?).to be_falsey # stub RestClient.proxy_set? to peek into implementation - RestClient.stub(:proxy_set?).and_return(true) + allow(RestClient).to receive(:proxy_set?).and_return(true) req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload') obj = req.net_http_object('host', 80) - obj.proxy?.should be_falsey + expect(obj.proxy?).to be_falsey # test stubbed Net::HTTP.new req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => nil) @@ -585,15 +585,15 @@ describe RestClient::Request, :include_helpers do end it "overrides global proxy with per-request proxy" do - RestClient.stub(:proxy).and_return('http://example.com') - RestClient.stub(:proxy_set?).and_return(true) + allow(RestClient).to receive(:proxy).and_return('http://example.com') + allow(RestClient).to receive(:proxy_set?).and_return(true) obj = @proxy_req.net_http_object('host', 80) - obj.proxy?.should be true - obj.proxy_address.should eq 'example.com' + expect(obj.proxy?).to be true + expect(obj.proxy_address).to eq 'example.com' req = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :proxy => 'http://127.0.0.1/') - req.net_http_object('host', 80).proxy?.should be true - req.net_http_object('host', 80).proxy_address.should == '127.0.0.1' + expect(req.net_http_object('host', 80).proxy?).to be true + expect(req.net_http_object('host', 80).proxy_address).to eq('127.0.0.1') end end @@ -602,121 +602,121 @@ describe RestClient::Request, :include_helpers do it "logs a get request" do log = RestClient.log = [] RestClient::Request.new(:method => :get, :url => 'http://url', :headers => {:user_agent => 'rest-client'}).log_request - log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n} + expect(log[0]).to eq %Q{RestClient.get "http://url", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n} end it "logs a post request with a small payload" do log = RestClient.log = [] RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo', :headers => {:user_agent => 'rest-client'}).log_request - log[0].should eq %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3", "User-Agent"=>"rest-client"\n} + expect(log[0]).to eq %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3", "User-Agent"=>"rest-client"\n} end it "logs a post request with a large payload" do log = RestClient.log = [] RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000), :headers => {:user_agent => 'rest-client'}).log_request - log[0].should eq %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"1000", "User-Agent"=>"rest-client"\n} + expect(log[0]).to eq %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"1000", "User-Agent"=>"rest-client"\n} end it "logs input headers as a hash" do log = RestClient.log = [] RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain', :user_agent => 'rest-client' }).log_request - log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n} + expect(log[0]).to eq %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n} end it "logs a response including the status code, content type, and result body size in bytes" do log = RestClient.log = [] res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd') - res.stub(:[]).with('Content-type').and_return('text/html') + allow(res).to receive(:[]).with('Content-type').and_return('text/html') @request.log_response res - log[0].should eq "# => 200 OK | text/html 4 bytes\n" + expect(log[0]).to eq "# => 200 OK | text/html 4 bytes\n" end it "logs a response with a nil Content-type" do log = RestClient.log = [] res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd') - res.stub(:[]).with('Content-type').and_return(nil) + allow(res).to receive(:[]).with('Content-type').and_return(nil) @request.log_response res - log[0].should eq "# => 200 OK | 4 bytes\n" + expect(log[0]).to eq "# => 200 OK | 4 bytes\n" end it "logs a response with a nil body" do log = RestClient.log = [] res = double('result', :code => '200', :class => Net::HTTPOK, :body => nil) - res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8') + allow(res).to receive(:[]).with('Content-type').and_return('text/html; charset=utf-8') @request.log_response res - log[0].should eq "# => 200 OK | text/html 0 bytes\n" + expect(log[0]).to eq "# => 200 OK | text/html 0 bytes\n" end it 'does not log request password' do log = RestClient.log = [] RestClient::Request.new(:method => :get, :url => 'http://user:password@url', :headers => {:user_agent => 'rest-client'}).log_request - log[0].should eq %Q{RestClient.get "http://user:REDACTED@url", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n} + expect(log[0]).to eq %Q{RestClient.get "http://user:REDACTED@url", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n} end end it "strips the charset from the response content type" do log = RestClient.log = [] res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd') - res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8') + allow(res).to receive(:[]).with('Content-type').and_return('text/html; charset=utf-8') @request.log_response res - log[0].should eq "# => 200 OK | text/html 4 bytes\n" + expect(log[0]).to eq "# => 200 OK | text/html 4 bytes\n" end describe "timeout" do it "does not set timeouts if not specified" do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload') - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) - @net.should_not_receive(:read_timeout=) - @net.should_not_receive(:open_timeout=) + expect(@net).not_to receive(:read_timeout=) + expect(@net).not_to receive(:open_timeout=) @request.send(:transmit, @uri, 'req', nil) end it 'sets read_timeout' do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :read_timeout => 123) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) - @net.should_receive(:read_timeout=).with(123) + expect(@net).to receive(:read_timeout=).with(123) @request.send(:transmit, @uri, 'req', nil) end it "sets open_timeout" do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) - @net.should_receive(:open_timeout=).with(123) + expect(@net).to receive(:open_timeout=).with(123) @request.send(:transmit, @uri, 'req', nil) end it 'sets both timeouts with :timeout' do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) - @net.should_receive(:open_timeout=).with(123) - @net.should_receive(:read_timeout=).with(123) + expect(@net).to receive(:open_timeout=).with(123) + expect(@net).to receive(:read_timeout=).with(123) @request.send(:transmit, @uri, 'req', nil) end it 'supersedes :timeout with open/read_timeout' do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123, :open_timeout => 34, :read_timeout => 56) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) - @net.should_receive(:open_timeout=).with(34) - @net.should_receive(:read_timeout=).with(56) + expect(@net).to receive(:open_timeout=).with(34) + expect(@net).to receive(:read_timeout=).with(56) @request.send(:transmit, @uri, 'req', nil) end @@ -724,40 +724,40 @@ describe RestClient::Request, :include_helpers do it "disable timeout by setting it to nil" do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :read_timeout => nil, :open_timeout => nil) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) - @net.should_receive(:read_timeout=).with(nil) - @net.should_receive(:open_timeout=).with(nil) + expect(@net).to receive(:read_timeout=).with(nil) + expect(@net).to receive(:open_timeout=).with(nil) @request.send(:transmit, @uri, 'req', nil) end it 'deprecated: warns when disabling timeout by setting it to -1' do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :read_timeout => -1) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) - @net.should_receive(:read_timeout=).with(nil) + expect(@net).to receive(:read_timeout=).with(nil) - fake_stderr { + expect(fake_stderr { @request.send(:transmit, @uri, 'req', nil) - }.should match(/^Deprecated: .*timeout.* nil instead of -1$/) + }).to match(/^Deprecated: .*timeout.* nil instead of -1$/) end it "deprecated: disable timeout by setting it to -1" do @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :read_timeout => -1, :open_timeout => -1) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) - @request.should_receive(:warn) - @net.should_receive(:read_timeout=).with(nil) + expect(@request).to receive(:warn) + expect(@net).to receive(:read_timeout=).with(nil) - @request.should_receive(:warn) - @net.should_receive(:open_timeout=).with(nil) + expect(@request).to receive(:warn) + expect(@net).to receive(:open_timeout=).with(nil) @request.send(:transmit, @uri, 'req', nil) end @@ -765,56 +765,56 @@ describe RestClient::Request, :include_helpers do describe "ssl" do it "uses SSL when the URI refers to a https address" do - @uri.stub(:is_a?).with(URI::HTTPS).and_return(true) - @net.should_receive(:use_ssl=).with(true) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@uri).to receive(:is_a?).with(URI::HTTPS).and_return(true) + expect(@net).to receive(:use_ssl=).with(true) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end it "should default to verifying ssl certificates" do - @request.verify_ssl.should eq OpenSSL::SSL::VERIFY_PEER + expect(@request.verify_ssl).to eq OpenSSL::SSL::VERIFY_PEER end it "should have expected values for VERIFY_PEER and VERIFY_NONE" do - OpenSSL::SSL::VERIFY_NONE.should eq(0) - OpenSSL::SSL::VERIFY_PEER.should eq(1) + expect(OpenSSL::SSL::VERIFY_NONE).to eq(0) + expect(OpenSSL::SSL::VERIFY_PEER).to eq(1) end it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do @request = RestClient::Request.new(:method => :put, :verify_ssl => false, :url => 'http://some/resource', :payload => 'payload') - @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true) - @net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).not_to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end it "should set net.verify_mode to OpenSSL::SSL::VERIFY_PEER if verify_ssl is true" do @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true) - @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end it "should set net.verify_mode to OpenSSL::SSL::VERIFY_PEER if verify_ssl is not given" do @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload') - @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -824,15 +824,15 @@ describe RestClient::Request, :include_helpers do :url => 'https://some/resource', :payload => 'payload', :verify_ssl => mode ) - @net.should_receive(:verify_mode=).with(mode) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:verify_mode=).with(mode) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end it "should default to not having an ssl_client_cert" do - @request.ssl_client_cert.should be(nil) + expect(@request.ssl_client_cert).to be(nil) end it "should set the ssl_version if provided" do @@ -842,10 +842,10 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_version => "TLSv1" ) - @net.should_receive(:ssl_version=).with("TLSv1") - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:ssl_version=).with("TLSv1") + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -855,10 +855,10 @@ describe RestClient::Request, :include_helpers do :url => 'https://some/resource', :payload => 'payload' ) - @net.should_not_receive(:ssl_version=).with("TLSv1") - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).not_to receive(:ssl_version=).with("TLSv1") + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -870,10 +870,10 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_ciphers => ciphers ) - @net.should_receive(:ciphers=).with(ciphers) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:ciphers=).with(ciphers) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -884,10 +884,10 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_ciphers => nil, ) - @net.should_not_receive(:ciphers=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).not_to receive(:ciphers=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -908,11 +908,11 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', ) - @net.should_receive(:ciphers=).with(RestClient::Request::DefaultCiphers) + expect(@net).to receive(:ciphers=).with(RestClient::Request::DefaultCiphers) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -933,11 +933,11 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', ) - @net.should_not_receive(:ciphers=) + expect(@net).not_to receive(:ciphers=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -948,10 +948,10 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_client_cert => "whatsupdoc!" ) - @net.should_receive(:cert=).with("whatsupdoc!") - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:cert=).with("whatsupdoc!") + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -961,15 +961,15 @@ describe RestClient::Request, :include_helpers do :url => 'https://some/resource', :payload => 'payload' ) - @net.should_not_receive(:cert=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).not_to receive(:cert=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end it "should default to not having an ssl_client_key" do - @request.ssl_client_key.should be(nil) + expect(@request.ssl_client_key).to be(nil) end it "should set the ssl_client_key if provided" do @@ -979,10 +979,10 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_client_key => "whatsupdoc!" ) - @net.should_receive(:key=).with("whatsupdoc!") - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:key=).with("whatsupdoc!") + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -992,15 +992,15 @@ describe RestClient::Request, :include_helpers do :url => 'https://some/resource', :payload => 'payload' ) - @net.should_not_receive(:key=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).not_to receive(:key=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end it "should default to not having an ssl_ca_file" do - @request.ssl_ca_file.should be(nil) + expect(@request.ssl_ca_file).to be(nil) end it "should set the ssl_ca_file if provided" do @@ -1010,11 +1010,11 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_ca_file => "Certificate Authority File" ) - @net.should_receive(:ca_file=).with("Certificate Authority File") - @net.should_not_receive(:cert_store=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:ca_file=).with("Certificate Authority File") + expect(@net).not_to receive(:cert_store=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -1024,15 +1024,15 @@ describe RestClient::Request, :include_helpers do :url => 'https://some/resource', :payload => 'payload' ) - @net.should_not_receive(:ca_file=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).not_to receive(:ca_file=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end it "should default to not having an ssl_ca_path" do - @request.ssl_ca_path.should be(nil) + expect(@request.ssl_ca_path).to be(nil) end it "should set the ssl_ca_path if provided" do @@ -1042,11 +1042,11 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_ca_path => "Certificate Authority Path" ) - @net.should_receive(:ca_path=).with("Certificate Authority Path") - @net.should_not_receive(:cert_store=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:ca_path=).with("Certificate Authority Path") + expect(@net).not_to receive(:cert_store=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -1056,10 +1056,10 @@ describe RestClient::Request, :include_helpers do :url => 'https://some/resource', :payload => 'payload' ) - @net.should_not_receive(:ca_path=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).not_to receive(:ca_path=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -1073,12 +1073,12 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_cert_store => store ) - @net.should_receive(:cert_store=).with(store) - @net.should_not_receive(:ca_path=) - @net.should_not_receive(:ca_file=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:cert_store=).with(store) + expect(@net).not_to receive(:ca_path=) + expect(@net).not_to receive(:ca_file=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -1088,12 +1088,12 @@ describe RestClient::Request, :include_helpers do :url => 'https://some/resource', :payload => 'payload' ) - @net.should_receive(:cert_store=) - @net.should_not_receive(:ca_path=) - @net.should_not_receive(:ca_file=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).to receive(:cert_store=) + expect(@net).not_to receive(:ca_path=) + expect(@net).not_to receive(:ca_file=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -1104,10 +1104,10 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_cert_store => nil, ) - @net.should_not_receive(:cert_store=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).not_to receive(:cert_store=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -1117,10 +1117,10 @@ describe RestClient::Request, :include_helpers do :url => 'https://some/resource', :payload => 'payload', ) - @net.should_not_receive(:verify_callback=) - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + expect(@net).not_to receive(:verify_callback=) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -1132,7 +1132,7 @@ describe RestClient::Request, :include_helpers do :payload => 'payload', :ssl_verify_callback => callback, ) - @net.should_receive(:verify_callback=).with(callback) + expect(@net).to receive(:verify_callback=).with(callback) # we'll read cert_store on jruby # https://github.com/jruby/jruby/issues/597 @@ -1140,9 +1140,9 @@ describe RestClient::Request, :include_helpers do allow(@net).to receive(:cert_store) end - @http.stub(:request) - @request.stub(:process_result) - @request.stub(:response_log) + allow(@http).to receive(:request) + allow(@request).to receive(:process_result) + allow(@request).to receive(:response_log) @request.send(:transmit, @uri, 'req', 'payload') end @@ -1156,11 +1156,11 @@ describe RestClient::Request, :include_helpers do :payload => 'payload' ) net_http_res = Net::HTTPNoContent.new("", "204", "No Content") - net_http_res.stub(:read_body).and_return(nil) - @http.should_receive(:request).and_return(@request.send(:fetch_body, net_http_res)) + allow(net_http_res).to receive(:read_body).and_return(nil) + expect(@http).to receive(:request).and_return(@request.send(:fetch_body, net_http_res)) response = @request.send(:transmit, @uri, 'req', 'payload') - response.should_not be_nil - response.code.should eq 204 + expect(response).not_to be_nil + expect(response.code).to eq 204 end describe "raw response" do @@ -1168,13 +1168,13 @@ describe RestClient::Request, :include_helpers do @request = RestClient::Request.new(:method => "get", :url => "example.com", :raw_response => true) tempfile = double("tempfile") - tempfile.should_receive(:binmode) - tempfile.stub(:open) - tempfile.stub(:close) - Tempfile.should_receive(:new).with("rest-client.").and_return(tempfile) + expect(tempfile).to receive(:binmode) + allow(tempfile).to receive(:open) + allow(tempfile).to receive(:close) + expect(Tempfile).to receive(:new).with("rest-client.").and_return(tempfile) net_http_res = Net::HTTPOK.new(nil, "200", "body") - net_http_res.stub(:read_body).and_return("body") + allow(net_http_res).to receive(:read_body).and_return("body") @request.send(:fetch_body, net_http_res) end end @@ -1183,8 +1183,8 @@ describe RestClient::Request, :include_helpers do it 'should accept string payloads' do payload = 'Foo' @request = RestClient::Request.new(method: :get, url: 'example.com', :payload => payload) - @request.should_receive(:process_result) - @http.should_receive(:request).with('req', payload) + expect(@request).to receive(:process_result) + expect(@http).to receive(:request).with('req', payload) @request.send(:transmit, @uri, 'req', payload) end @@ -1192,67 +1192,67 @@ describe RestClient::Request, :include_helpers do payload = StringIO.new('streamed') @request = RestClient::Request.new(method: :get, url: 'example.com', :payload => payload) - @request.should_receive(:process_result) + expect(@request).to receive(:process_result) @get = double('net::http::get') - @get.should_receive(:body_stream=).with(instance_of(RestClient::Payload::Streamed)) + expect(@get).to receive(:body_stream=).with(instance_of(RestClient::Payload::Streamed)) - @request.net_http_request_class(:GET).stub(:new).and_return(@get) - @http.should_receive(:request).with(@get, nil) + allow(@request.net_http_request_class(:GET)).to receive(:new).and_return(@get) + expect(@http).to receive(:request).with(@get, nil) @request.execute end end describe 'constructor' do it 'should reject valid URIs with no hostname' do - URI.parse('http:///').hostname.should be_nil + expect(URI.parse('http:///').hostname).to be_nil - lambda { + expect { RestClient::Request.new(method: :get, url: 'http:///') - }.should raise_error(URI::InvalidURIError, /\Abad URI/) + }.to raise_error(URI::InvalidURIError, /\Abad URI/) end it 'should reject invalid URIs' do - lambda { + expect { RestClient::Request.new(method: :get, url: 'http://::') - }.should raise_error(URI::InvalidURIError) + }.to raise_error(URI::InvalidURIError) end end describe 'process_url_params' do it 'should handle basic URL params' do - @request.process_url_params('https://example.com/foo', params: {key1: 123, key2: 'abc'}). - should eq 'https://example.com/foo?key1=123&key2=abc' + expect(@request.process_url_params('https://example.com/foo', params: {key1: 123, key2: 'abc'})). + to eq 'https://example.com/foo?key1=123&key2=abc' - @request.process_url_params('https://example.com/foo', params: {'key1' => 123}). - should eq 'https://example.com/foo?key1=123' + expect(@request.process_url_params('https://example.com/foo', params: {'key1' => 123})). + to eq 'https://example.com/foo?key1=123' - @request.process_url_params('https://example.com/path', - params: {foo: 'one two', bar: 'three + four == seven'}). - should eq 'https://example.com/path?foo=one+two&bar=three+%2B+four+%3D%3D+seven' + expect(@request.process_url_params('https://example.com/path', + params: {foo: 'one two', bar: 'three + four == seven'})). + to eq 'https://example.com/path?foo=one+two&bar=three+%2B+four+%3D%3D+seven' end it 'should combine with & when URL params already exist' do - @request.process_url_params('https://example.com/path?foo=1', params: {bar: 2}). - should eq 'https://example.com/path?foo=1&bar=2' + expect(@request.process_url_params('https://example.com/path?foo=1', params: {bar: 2})). + to eq 'https://example.com/path?foo=1&bar=2' end it 'should handle complex nested URL params per Rack / Rails conventions' do - @request.process_url_params('https://example.com/', params: { + expect(@request.process_url_params('https://example.com/', params: { foo: [1,2,3], null: nil, false: false, math: '2+2=4', nested: {'key + escaped' => 'value + escaped', other: [], arr: [1,2]}, - }).should eq 'https://example.com/?foo[]=1&foo[]=2&foo[]=3&null&false=false&math=2%2B2%3D4' \ + })).to eq 'https://example.com/?foo[]=1&foo[]=2&foo[]=3&null&false=false&math=2%2B2%3D4' \ '&nested[key+%2B+escaped]=value+%2B+escaped&nested[other]' \ '&nested[arr][]=1&nested[arr][]=2' end it 'should handle ParamsArray objects' do - @request.process_url_params('https://example.com/', + expect(@request.process_url_params('https://example.com/', params: RestClient::ParamsArray.new([[:foo, 1], [:foo, 2]]) - ).should eq 'https://example.com/?foo=1&foo=2' + )).to eq 'https://example.com/?foo=1&foo=2' end end end diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 056a8fb..493eb2f 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -7,37 +7,37 @@ describe RestClient::Resource do context "Resource delegation" 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 end 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 end 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' end 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' end 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' end 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 end 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' end end @@ -48,41 +48,41 @@ describe RestClient::Resource do it "is backwards compatible with previous constructor" do @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass') - @resource.user.should eq 'user' - @resource.password.should eq 'pass' + expect(@resource.user).to eq 'user' + expect(@resource.password).to eq 'pass' end 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 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 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 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 it "offers subresources via []" do 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 it "transports options to subresources" do parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password') - parent['posts'].user.should eq 'user' - parent['posts'].password.should eq 'password' + expect(parent['posts'].user).to eq 'user' + expect(parent['posts'].password).to eq 'password' end it "passes a given block to subresources" do block = proc {|r| r} parent = RestClient::Resource.new('http://example.com', &block) - parent['posts'].block.should eq block + expect(parent['posts'].block).to eq block end it "the block should be overrideable" do @@ -90,8 +90,8 @@ describe RestClient::Resource do block2 = proc {|r| } parent = RestClient::Resource.new('http://example.com', &block1) # parent['posts', &block2].block.should eq block2 # ruby 1.9 syntax - parent.send(:[], 'posts', &block2).block.should eq block2 - parent.send(:[], 'posts', &block2).block.should_not eq block1 + expect(parent.send(:[], 'posts', &block2).block).to eq block2 + expect(parent.send(:[], 'posts', &block2).block).not_to eq block1 end it "the block should be overrideable in ruby 1.9 syntax" do @@ -99,31 +99,31 @@ describe RestClient::Resource do block2 = ->(r) {} parent = RestClient::Resource.new('http://example.com', &block1) - parent['posts', &block2].block.should eq block2 - parent['posts', &block2].block.should_not eq block1 + expect(parent['posts', &block2].block).to eq block2 + expect(parent['posts', &block2].block).not_to eq block1 end 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 describe 'block' do it 'can use block when creating the resource' do stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404) resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' } - resource.get.should eq 'foo' + expect(resource.get).to eq 'foo' end it 'can use block when executing the resource' do stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404) 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 it 'execution block override resource block' do stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404) 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 diff --git a/spec/unit/response_spec.rb b/spec/unit/response_spec.rb index aed41e9..7e5a039 100644 --- a/spec/unit/response_spec.rb +++ b/spec/unit/response_spec.rb @@ -9,21 +9,21 @@ describe RestClient::Response, :include_helpers do end it "behaves like string" do - @response.to_s.should eq 'abc' - @response.to_str.should eq 'abc' + expect(@response.to_s).to eq 'abc' + expect(@response.to_str).to eq 'abc' - @response.should_receive(:warn) - @response.to_i.should eq 0 + expect(@response).to receive(:warn) + expect(@response.to_i).to eq 0 end 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 describe 'header processing' do it "test headers and raw headers" do - @response.raw_headers["Status"][0].should eq "200 OK" - @response.headers[:status].should eq "200 OK" + expect(@response.raw_headers["Status"][0]).to eq "200 OK" + expect(@response.headers[:status]).to eq "200 OK" end 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') @response = RestClient::Response.create('abc', @net_http_res, @request) - @response.raw_headers['My-Header'].should eq ['foo', 'bar'] - @response.headers[:my_header].should eq 'foo, bar' + expect(@response.raw_headers['My-Header']).to eq ['foo', 'bar'] + expect(@response.headers[:my_header]).to eq 'foo, bar' 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]}) response = RestClient::Response.create('abc', net_http_res, @request) - response.headers[:set_cookie].should eq [header_val] - response.cookies.should eq({ "main_page" => "main_page_no_rewrite" }) + expect(response.headers[:set_cookie]).to eq [header_val] + expect(response.cookies).to eq({ "main_page" => "main_page_no_rewrite" }) end 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"]}) 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"] - response.cookies.should eq({ + 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"] + expect(response.cookies).to eq({ "main_page" => "main_page_no_rewrite", "remember_me" => "", "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 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.cookies.should eq({ + expect(response.cookies).to eq({ "main_page" => "main_page_no_rewrite", "remember_me" => "", "user" => "somebody" @@ -83,7 +83,7 @@ describe RestClient::Response, :include_helpers do unless (200..207).include? code net_http_res = response_double(:code => code.to_i) resp = RestClient::Response.create('abc', net_http_res, @request) - lambda { resp.return! }.should raise_error + expect { resp.return! }.to raise_error end end end @@ -95,29 +95,29 @@ describe RestClient::Response, :include_helpers 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://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 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://new/resource').to_return(:body => 'Foo') r = RestClient::Request.execute(url: 'http://some/resource', method: :get) - r.body.should eq 'Foo' - r.history.length.should eq 1 - r.history.fetch(0).should be_a(RestClient::Response) - r.history.fetch(0).code.should be 301 + expect(r.body).to eq 'Foo' + expect(r.history.length).to eq 1 + expect(r.history.fetch(0)).to be_a(RestClient::Response) + expect(r.history.fetch(0).code).to be 301 end 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://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 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/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 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( :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 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, request_double(method: 'post')) - lambda { + expect { response.return! - }.should raise_error(RestClient::MovedPermanently) + }.to raise_error(RestClient::MovedPermanently) end it "doesn't follow a 302 when the request is a post" do net_http_res = response_double(:code => 302) response = RestClient::Response.create('abc', net_http_res, request_double(method: 'post')) - lambda { + expect { response.return! - }.should raise_error(RestClient::Found) + }.to raise_error(RestClient::Found) end it "doesn't follow a 307 when the request is a post" do net_http_res = response_double(:code => 307) response = RestClient::Response.create('abc', net_http_res, request_double(method: 'post')) - response.should_not_receive(:follow_redirection) - lambda { + expect(response).not_to receive(:follow_redirection) + expect { response.return! - }.should raise_error(RestClient::TemporaryRedirect) + }.to raise_error(RestClient::TemporaryRedirect) end it "doesn't follow a redirection when the request is a put" do net_http_res = response_double(:code => 301) response = RestClient::Response.create('abc', net_http_res, request_double(method: 'put')) - lambda { + expect { response.return! - }.should raise_error(RestClient::MovedPermanently) + }.to raise_error(RestClient::MovedPermanently) end 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(: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 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://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 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/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 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/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 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://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 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-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'}) - lambda { + expect { RestClient::Request.execute(url: 'http://some/redirect-1', method: :get) - }.should raise_error(RestClient::MovedPermanently) { |ex| - ex.response.history.each {|r| r.should be_a(RestClient::Response) } - ex.response.history.length.should eq 10 + }.to raise_error(RestClient::MovedPermanently) { |ex| + ex.response.history.each {|r| expect(r).to be_a(RestClient::Response) } + 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 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-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) - }.should raise_error(RestClient::MovedPermanently) { |ex| - ex.response.history.length.should eq 5 + }.to raise_error(RestClient::MovedPermanently) { |ex| + 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 it "allows for manual following of redirects" do @@ -232,8 +232,8 @@ describe RestClient::Response, :include_helpers do raise 'notreached' end - resp.code.should eq 200 - resp.body.should eq 'Qux' + expect(resp.code).to eq 200 + expect(resp.body).to eq 'Qux' end end diff --git a/spec/unit/restclient_spec.rb b/spec/unit/restclient_spec.rb index 5e82439..cb4dfe0 100644 --- a/spec/unit/restclient_spec.rb +++ b/spec/unit/restclient_spec.rb @@ -3,37 +3,37 @@ require_relative '_lib' describe RestClient do describe "API" 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') end 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') end 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') end 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') end 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') end 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') end 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') end end @@ -45,27 +45,27 @@ describe RestClient do it "uses << if the log is not a string" do log = RestClient.log = [] - log.should_receive(:<<).with('xyz') + expect(log).to receive(:<<).with('xyz') RestClient.log << 'xyz' end it "displays the log to stdout" do RestClient.log = 'stdout' - STDOUT.should_receive(:puts).with('xyz') + expect(STDOUT).to receive(:puts).with('xyz') RestClient.log << 'xyz' end it "displays the log to stderr" do RestClient.log = 'stderr' - STDERR.should_receive(:puts).with('xyz') + expect(STDERR).to receive(:puts).with('xyz') RestClient.log << 'xyz' end it "append the log to the requested filename" do RestClient.log = '/tmp/restclient.log' f = double('file handle') - File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f) - f.should_receive(:puts).with('xyz') + expect(File).to receive(:open).with('/tmp/restclient.log', 'a').and_yield(f) + expect(f).to receive(:puts).with('xyz') RestClient.log << 'xyz' end end @@ -73,7 +73,7 @@ describe RestClient do describe 'version' do it 'has a version ~> 2.0.0.alpha' do 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 diff --git a/spec/unit/utils_spec.rb b/spec/unit/utils_spec.rb index 687bd2c..9c90ec0 100644 --- a/spec/unit/utils_spec.rb +++ b/spec/unit/utils_spec.rb @@ -4,67 +4,67 @@ describe RestClient::Utils do describe '.get_encoding_from_headers' do it 'assumes no encoding by default for text' do headers = {:content_type => 'text/plain'} - RestClient::Utils.get_encoding_from_headers(headers). - should eq nil + expect(RestClient::Utils.get_encoding_from_headers(headers)). + to eq nil end it 'returns nil on failures' do - RestClient::Utils.get_encoding_from_headers( - {:content_type => 'blah'}).should eq nil - RestClient::Utils.get_encoding_from_headers( - {}).should eq nil - RestClient::Utils.get_encoding_from_headers( - {:content_type => 'foo; bar=baz'}).should eq nil + expect(RestClient::Utils.get_encoding_from_headers( + {:content_type => 'blah'})).to eq nil + expect(RestClient::Utils.get_encoding_from_headers( + {})).to eq nil + expect(RestClient::Utils.get_encoding_from_headers( + {:content_type => 'foo; bar=baz'})).to eq nil end it 'handles various charsets' do - RestClient::Utils.get_encoding_from_headers( - {:content_type => 'text/plain; charset=UTF-8'}).should eq 'UTF-8' - RestClient::Utils.get_encoding_from_headers( - {:content_type => 'application/json; charset=ISO-8859-1'}). - should eq 'ISO-8859-1' - RestClient::Utils.get_encoding_from_headers( - {:content_type => 'text/html; charset=windows-1251'}). - should eq 'windows-1251' + expect(RestClient::Utils.get_encoding_from_headers( + {:content_type => 'text/plain; charset=UTF-8'})).to eq 'UTF-8' + expect(RestClient::Utils.get_encoding_from_headers( + {:content_type => 'application/json; charset=ISO-8859-1'})). + to eq 'ISO-8859-1' + expect(RestClient::Utils.get_encoding_from_headers( + {:content_type => 'text/html; charset=windows-1251'})). + to eq 'windows-1251' - RestClient::Utils.get_encoding_from_headers( - {:content_type => 'text/html; charset="UTF-16"'}). - should eq 'UTF-16' + expect(RestClient::Utils.get_encoding_from_headers( + {:content_type => 'text/html; charset="UTF-16"'})). + to eq 'UTF-16' end end describe '.cgi_parse_header' do it 'parses headers' do - RestClient::Utils.cgi_parse_header('text/plain'). - should eq ['text/plain', {}] + expect(RestClient::Utils.cgi_parse_header('text/plain')). + to eq ['text/plain', {}] - RestClient::Utils.cgi_parse_header('text/vnd.just.made.this.up ; '). - should eq ['text/vnd.just.made.this.up', {}] + expect(RestClient::Utils.cgi_parse_header('text/vnd.just.made.this.up ; ')). + to eq ['text/vnd.just.made.this.up', {}] - RestClient::Utils.cgi_parse_header('text/plain;charset=us-ascii'). - should eq ['text/plain', {'charset' => 'us-ascii'}] + expect(RestClient::Utils.cgi_parse_header('text/plain;charset=us-ascii')). + to eq ['text/plain', {'charset' => 'us-ascii'}] - RestClient::Utils.cgi_parse_header('text/plain ; charset="us-ascii"'). - should eq ['text/plain', {'charset' => 'us-ascii'}] + expect(RestClient::Utils.cgi_parse_header('text/plain ; charset="us-ascii"')). + to eq ['text/plain', {'charset' => 'us-ascii'}] - RestClient::Utils.cgi_parse_header( - 'text/plain ; charset="us-ascii"; another=opt'). - should eq ['text/plain', {'charset' => 'us-ascii', 'another' => 'opt'}] + expect(RestClient::Utils.cgi_parse_header( + 'text/plain ; charset="us-ascii"; another=opt')). + to eq ['text/plain', {'charset' => 'us-ascii', 'another' => 'opt'}] - RestClient::Utils.cgi_parse_header( - 'attachment; filename="silly.txt"'). - should eq ['attachment', {'filename' => 'silly.txt'}] + expect(RestClient::Utils.cgi_parse_header( + 'attachment; filename="silly.txt"')). + to eq ['attachment', {'filename' => 'silly.txt'}] - RestClient::Utils.cgi_parse_header( - 'attachment; filename="strange;name"'). - should eq ['attachment', {'filename' => 'strange;name'}] + expect(RestClient::Utils.cgi_parse_header( + 'attachment; filename="strange;name"')). + to eq ['attachment', {'filename' => 'strange;name'}] - RestClient::Utils.cgi_parse_header( - 'attachment; filename="strange;name";size=123;').should eq \ + expect(RestClient::Utils.cgi_parse_header( + 'attachment; filename="strange;name";size=123;')).to eq \ ['attachment', {'filename' => 'strange;name', 'size' => '123'}] - RestClient::Utils.cgi_parse_header( - 'form-data; name="files"; filename="fo\\"o;bar"').should eq \ + expect(RestClient::Utils.cgi_parse_header( + 'form-data; name="files"; filename="fo\\"o;bar"')).to eq \ ['form-data', {'name' => 'files', 'filename' => 'fo"o;bar'}] end end @@ -78,7 +78,7 @@ describe RestClient::Utils do {escaped: '1+2=3'} => 'escaped=1%2B2%3D3', {'escaped + key' => 'foo'} => 'escaped+%2B+key=foo', }.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 @@ -89,7 +89,7 @@ describe RestClient::Utils do {foo: ['one two', 3]} => 'foo[]=one+two&foo[]=3', {'a+b' => [1,2,3]} => 'a%2Bb[]=1&a%2Bb[]=2&a%2Bb[]=3', }.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 @@ -98,7 +98,7 @@ describe RestClient::Utils do {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', }.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 @@ -107,7 +107,7 @@ describe RestClient::Utils do {string: '', empty: nil, list: [], hash: {}, falsey: false } => 'string=&empty&list&hash&falsey=false', }.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 @@ -115,7 +115,7 @@ describe RestClient::Utils do { {foo: {string: '', empty: nil}} => 'foo[string]=&foo[empty]', }.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 @@ -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', }.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 @@ -131,7 +131,7 @@ describe RestClient::Utils do { RestClient::ParamsArray.new([[:foo, 1], [:foo, 2], [:foo, 3]]) => 'foo=1&foo=2&foo=3', }.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 @@ -140,7 +140,7 @@ describe RestClient::Utils do {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', }.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 diff --git a/spec/unit/windows/root_certs_spec.rb b/spec/unit/windows/root_certs_spec.rb index 53c1dfb..6229333 100644 --- a/spec/unit/windows/root_certs_spec.rb +++ b/spec/unit/windows/root_certs_spec.rb @@ -5,7 +5,7 @@ describe 'RestClient::Windows::RootCerts', let(:x509_store) { RestClient::Windows::RootCerts.instance.to_a } 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 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 x509_store.each do |cert| - cert.should be_a(OpenSSL::X509::Certificate) + expect(cert).to be_a(OpenSSL::X509::Certificate) end end end