rest-client 1.8.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -0
  3. data/.mailmap +10 -0
  4. data/.rspec +2 -1
  5. data/.rubocop +2 -0
  6. data/.rubocop-disables.yml +386 -0
  7. data/.rubocop.yml +8 -0
  8. data/.travis.yml +56 -8
  9. data/AUTHORS +26 -1
  10. data/README.md +901 -0
  11. data/Rakefile +27 -3
  12. data/bin/restclient +3 -5
  13. data/history.md +181 -0
  14. data/lib/restclient/abstract_response.rb +172 -55
  15. data/lib/restclient/exceptions.rb +96 -55
  16. data/lib/restclient/params_array.rb +72 -0
  17. data/lib/restclient/payload.rb +70 -74
  18. data/lib/restclient/platform.rb +19 -0
  19. data/lib/restclient/raw_response.rb +21 -7
  20. data/lib/restclient/request.rb +540 -281
  21. data/lib/restclient/resource.rb +19 -9
  22. data/lib/restclient/response.rb +75 -6
  23. data/lib/restclient/utils.rb +274 -0
  24. data/lib/restclient/version.rb +2 -1
  25. data/lib/restclient.rb +21 -3
  26. data/rest-client.gemspec +12 -10
  27. data/spec/ISS.jpg +0 -0
  28. data/spec/helpers.rb +54 -0
  29. data/spec/integration/_lib.rb +1 -0
  30. data/spec/integration/capath_digicert/3513523f.0 +22 -0
  31. data/spec/integration/capath_digicert/399e7759.0 +22 -0
  32. data/spec/integration/capath_digicert/digicert.crt +20 -17
  33. data/spec/integration/certs/digicert.crt +20 -17
  34. data/spec/integration/httpbin_spec.rb +128 -0
  35. data/spec/integration/integration_spec.rb +97 -14
  36. data/spec/integration/request_spec.rb +25 -2
  37. data/spec/spec_helper.rb +28 -1
  38. data/spec/unit/_lib.rb +1 -0
  39. data/spec/unit/abstract_response_spec.rb +95 -38
  40. data/spec/unit/exceptions_spec.rb +41 -28
  41. data/spec/unit/params_array_spec.rb +36 -0
  42. data/spec/unit/payload_spec.rb +118 -68
  43. data/spec/unit/raw_response_spec.rb +10 -6
  44. data/spec/unit/request2_spec.rb +34 -12
  45. data/spec/unit/request_spec.rb +745 -424
  46. data/spec/unit/resource_spec.rb +31 -27
  47. data/spec/unit/response_spec.rb +134 -57
  48. data/spec/unit/restclient_spec.rb +16 -15
  49. data/spec/unit/utils_spec.rb +147 -0
  50. data/spec/unit/windows/root_certs_spec.rb +3 -3
  51. metadata +79 -29
  52. data/README.rdoc +0 -324
  53. data/spec/integration/capath_digicert/244b5494.0 +0 -19
  54. data/spec/integration/capath_digicert/81b9768f.0 +0 -19
  55. data/spec/unit/master_shake.jpg +0 -0
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require_relative '_lib'
2
2
 
3
3
  describe RestClient::Resource do
4
4
  before do
@@ -7,37 +7,37 @@ describe RestClient::Resource do
7
7
 
8
8
  context "Resource delegation" do
9
9
  it "GET" do
10
- RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
10
+ expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
11
11
  @resource.get
12
12
  end
13
13
 
14
14
  it "HEAD" do
15
- RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
15
+ expect(RestClient::Request).to receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
16
16
  @resource.head
17
17
  end
18
18
 
19
19
  it "POST" do
20
- 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')
20
+ 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', :log => nil)
21
21
  @resource.post 'abc', :content_type => 'image/jpg'
22
22
  end
23
23
 
24
24
  it "PUT" do
25
- 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')
25
+ 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', :log => nil)
26
26
  @resource.put 'abc', :content_type => 'image/jpg'
27
27
  end
28
28
 
29
29
  it "PATCH" do
30
- 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')
30
+ 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', :log => nil)
31
31
  @resource.patch 'abc', :content_type => 'image/jpg'
32
32
  end
33
33
 
34
34
  it "DELETE" do
35
- RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
35
+ expect(RestClient::Request).to receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass', :log => nil)
36
36
  @resource.delete
37
37
  end
38
38
 
39
39
  it "overrides resource headers" do
40
- RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
40
+ expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass', :log => nil)
41
41
  @resource.get 'X-Something' => '2'
42
42
  end
43
43
  end
@@ -48,41 +48,41 @@ describe RestClient::Resource do
48
48
 
49
49
  it "is backwards compatible with previous constructor" do
50
50
  @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
51
- @resource.user.should eq 'user'
52
- @resource.password.should eq 'pass'
51
+ expect(@resource.user).to eq 'user'
52
+ expect(@resource.password).to eq 'pass'
53
53
  end
54
54
 
55
55
  it "concatenates urls, inserting a slash when it needs one" do
56
- @resource.concat_urls('http://example.com', 'resource').should eq 'http://example.com/resource'
56
+ expect(@resource.concat_urls('http://example.com', 'resource')).to eq 'http://example.com/resource'
57
57
  end
58
58
 
59
59
  it "concatenates urls, using no slash if the first url ends with a slash" do
60
- @resource.concat_urls('http://example.com/', 'resource').should eq 'http://example.com/resource'
60
+ expect(@resource.concat_urls('http://example.com/', 'resource')).to eq 'http://example.com/resource'
61
61
  end
62
62
 
63
63
  it "concatenates urls, using no slash if the second url starts with a slash" do
64
- @resource.concat_urls('http://example.com', '/resource').should eq 'http://example.com/resource'
64
+ expect(@resource.concat_urls('http://example.com', '/resource')).to eq 'http://example.com/resource'
65
65
  end
66
66
 
67
67
  it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
68
- @resource.concat_urls(:posts, 1).should eq 'posts/1'
68
+ expect(@resource.concat_urls(:posts, 1)).to eq 'posts/1'
69
69
  end
70
70
 
71
71
  it "offers subresources via []" do
72
72
  parent = RestClient::Resource.new('http://example.com')
73
- parent['posts'].url.should eq 'http://example.com/posts'
73
+ expect(parent['posts'].url).to eq 'http://example.com/posts'
74
74
  end
75
75
 
76
76
  it "transports options to subresources" do
77
77
  parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
78
- parent['posts'].user.should eq 'user'
79
- parent['posts'].password.should eq 'password'
78
+ expect(parent['posts'].user).to eq 'user'
79
+ expect(parent['posts'].password).to eq 'password'
80
80
  end
81
81
 
82
82
  it "passes a given block to subresources" do
83
83
  block = proc {|r| r}
84
84
  parent = RestClient::Resource.new('http://example.com', &block)
85
- parent['posts'].block.should eq block
85
+ expect(parent['posts'].block).to eq block
86
86
  end
87
87
 
88
88
  it "the block should be overrideable" do
@@ -90,40 +90,44 @@ describe RestClient::Resource do
90
90
  block2 = proc {|r| }
91
91
  parent = RestClient::Resource.new('http://example.com', &block1)
92
92
  # parent['posts', &block2].block.should eq block2 # ruby 1.9 syntax
93
- parent.send(:[], 'posts', &block2).block.should eq block2
94
- parent.send(:[], 'posts', &block2).block.should_not eq block1
93
+ expect(parent.send(:[], 'posts', &block2).block).to eq block2
94
+ expect(parent.send(:[], 'posts', &block2).block).not_to eq block1
95
95
  end
96
96
 
97
- it "the block should be overrideable in ruby 1.9 syntax" do
97
+ # Test fails on jruby 9.1.[0-5].* due to
98
+ # https://github.com/jruby/jruby/issues/4217
99
+ it "the block should be overrideable in ruby 1.9 syntax",
100
+ :unless => (RUBY_ENGINE == 'jruby' && JRUBY_VERSION =~ /\A9\.1\.[0-5]\./) \
101
+ do
98
102
  block1 = proc {|r| r}
99
103
  block2 = ->(r) {}
100
104
 
101
105
  parent = RestClient::Resource.new('http://example.com', &block1)
102
- parent['posts', &block2].block.should eq block2
103
- parent['posts', &block2].block.should_not eq block1
106
+ expect(parent['posts', &block2].block).to eq block2
107
+ expect(parent['posts', &block2].block).not_to eq block1
104
108
  end
105
109
 
106
110
  it "prints its url with to_s" do
107
- RestClient::Resource.new('x').to_s.should eq 'x'
111
+ expect(RestClient::Resource.new('x').to_s).to eq 'x'
108
112
  end
109
113
 
110
114
  describe 'block' do
111
115
  it 'can use block when creating the resource' do
112
116
  stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
113
117
  resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
114
- resource.get.should eq 'foo'
118
+ expect(resource.get).to eq 'foo'
115
119
  end
116
120
 
117
121
  it 'can use block when executing the resource' do
118
122
  stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
119
123
  resource = RestClient::Resource.new('www.example.com')
120
- resource.get { |response, request| 'foo' }.should eq 'foo'
124
+ expect(resource.get { |response, request| 'foo' }).to eq 'foo'
121
125
  end
122
126
 
123
127
  it 'execution block override resource block' do
124
128
  stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
125
129
  resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
126
- resource.get { |response, request| 'bar' }.should eq 'bar'
130
+ expect(resource.get { |response, request| 'bar' }).to eq 'bar'
127
131
  end
128
132
 
129
133
  end
@@ -1,26 +1,41 @@
1
- require 'spec_helper'
1
+ require_relative '_lib'
2
2
 
3
- describe RestClient::Response do
3
+ describe RestClient::Response, :include_helpers do
4
4
  before do
5
- @net_http_res = double('net http response', :to_hash => {"Status" => ["200 OK"]}, :code => 200)
5
+ @net_http_res = res_double(to_hash: {'Status' => ['200 OK']}, code: '200', body: 'abc')
6
6
  @example_url = 'http://example.com'
7
- @request = double('http request', :user => nil, :password => nil, :url => @example_url)
8
- @response = RestClient::Response.create('abc', @net_http_res, {}, @request)
7
+ @request = request_double(url: @example_url, method: 'get')
8
+ @response = response_from_res_double(@net_http_res, @request, duration: 1)
9
9
  end
10
10
 
11
11
  it "behaves like string" do
12
- @response.to_s.should eq 'abc'
13
- @response.to_str.should eq 'abc'
14
- @response.to_i.should eq 200
12
+ expect(@response.to_s).to eq 'abc'
13
+ expect(@response.to_str).to eq 'abc'
14
+
15
+ expect(@response).to receive(:warn)
16
+ expect(@response.to_i).to eq 0
15
17
  end
16
18
 
17
19
  it "accepts nil strings and sets it to empty for the case of HEAD" do
18
- RestClient::Response.create(nil, @net_http_res, {}, @request).to_s.should eq ""
20
+ # TODO
21
+ expect(RestClient::Response.create(nil, @net_http_res, @request).to_s).to eq ""
19
22
  end
20
23
 
21
- it "test headers and raw headers" do
22
- @response.raw_headers["Status"][0].should eq "200 OK"
23
- @response.headers[:status].should eq "200 OK"
24
+ describe 'header processing' do
25
+ it "test headers and raw headers" do
26
+ expect(@response.raw_headers["Status"][0]).to eq "200 OK"
27
+ expect(@response.headers[:status]).to eq "200 OK"
28
+ end
29
+
30
+ it 'handles multiple headers by joining with comma' do
31
+ net_http_res = res_double(to_hash: {'My-Header' => ['foo', 'bar']}, code: '200', body: nil)
32
+ example_url = 'http://example.com'
33
+ request = request_double(url: example_url, method: 'get')
34
+ response = response_from_res_double(net_http_res, request)
35
+
36
+ expect(response.raw_headers['My-Header']).to eq ['foo', 'bar']
37
+ expect(response.headers[:my_header]).to eq 'foo, bar'
38
+ end
24
39
  end
25
40
 
26
41
  describe "cookie processing" do
@@ -28,16 +43,16 @@ describe RestClient::Response do
28
43
  header_val = "main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT".freeze
29
44
 
30
45
  net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => [header_val]})
31
- response = RestClient::Response.create('abc', net_http_res, {}, @request)
32
- response.headers[:set_cookie].should eq [header_val]
33
- response.cookies.should eq({ "main_page" => "main_page_no_rewrite" })
46
+ response = RestClient::Response.create('abc', net_http_res, @request)
47
+ expect(response.headers[:set_cookie]).to eq [header_val]
48
+ expect(response.cookies).to eq({ "main_page" => "main_page_no_rewrite" })
34
49
  end
35
50
 
36
51
  it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
37
52
  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"]})
38
- response = RestClient::Response.create('abc', net_http_res, {}, @request)
39
- 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"]
40
- response.cookies.should eq({
53
+ response = RestClient::Response.create('abc', net_http_res, @request)
54
+ 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"]
55
+ expect(response.cookies).to eq({
41
56
  "main_page" => "main_page_no_rewrite",
42
57
  "remember_me" => "",
43
58
  "user" => "somebody"
@@ -46,8 +61,8 @@ describe RestClient::Response do
46
61
 
47
62
  it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
48
63
  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"]})
49
- response = RestClient::Response.create('abc', net_http_res, {}, @request)
50
- response.cookies.should eq({
64
+ response = RestClient::Response.create('abc', net_http_res, @request)
65
+ expect(response.cookies).to eq({
51
66
  "main_page" => "main_page_no_rewrite",
52
67
  "remember_me" => "",
53
68
  "user" => "somebody"
@@ -58,18 +73,19 @@ describe RestClient::Response do
58
73
  describe "exceptions processing" do
59
74
  it "should return itself for normal codes" do
60
75
  (200..206).each do |code|
61
- net_http_res = double('net http response', :code => '200')
62
- response = RestClient::Response.create('abc', net_http_res, {}, @request)
63
- response.return! @request
76
+ net_http_res = res_double(:code => '200')
77
+ resp = RestClient::Response.create('abc', net_http_res, @request)
78
+ resp.return!
64
79
  end
65
80
  end
66
81
 
67
82
  it "should throw an exception for other codes" do
68
- RestClient::Exceptions::EXCEPTIONS_MAP.each_key do |code|
83
+ RestClient::Exceptions::EXCEPTIONS_MAP.each_pair do |code, exc|
69
84
  unless (200..207).include? code
70
- net_http_res = double('net http response', :code => code.to_i)
71
- response = RestClient::Response.create('abc', net_http_res, {}, @request)
72
- lambda { response.return!}.should raise_error
85
+ net_http_res = res_double(:code => code.to_i)
86
+ resp = RestClient::Response.create('abc', net_http_res, @request)
87
+ allow(@request).to receive(:max_redirects).and_return(5)
88
+ expect { resp.return! }.to raise_error(exc)
73
89
  end
74
90
  end
75
91
  end
@@ -81,95 +97,156 @@ describe RestClient::Response do
81
97
  it "follows a redirection when the request is a get" do
82
98
  stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
83
99
  stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
84
- RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
100
+ expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
101
+ end
102
+
103
+ it "keeps redirection history" do
104
+ stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
105
+ stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
106
+ r = RestClient::Request.execute(url: 'http://some/resource', method: :get)
107
+ expect(r.body).to eq 'Foo'
108
+ expect(r.history.length).to eq 1
109
+ expect(r.history.fetch(0)).to be_a(RestClient::Response)
110
+ expect(r.history.fetch(0).code).to be 301
85
111
  end
86
112
 
87
113
  it "follows a redirection and keep the parameters" do
88
- stub_request(:get, 'http://foo:bar@some/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
89
- stub_request(:get, 'http://foo:bar@new/resource').with(:headers => {'Accept' => 'application/json'}).to_return(:body => 'Foo')
90
- RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body.should eq 'Foo'
114
+ 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'})
115
+ stub_request(:get, 'http://new/resource').with(:headers => {'Accept' => 'application/json'}, :basic_auth => ['foo', 'bar']).to_return(:body => 'Foo')
116
+ expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body).to eq 'Foo'
91
117
  end
92
118
 
93
119
  it "follows a redirection and keep the cookies" do
94
120
  stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://some/new_resource', })
95
121
  stub_request(:get, 'http://some/new_resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
96
- RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Qux'
122
+ expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Qux'
97
123
  end
98
124
 
99
- it 'does not keep cookies across domains' do
100
- stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://new/resource', })
101
- stub_request(:get, 'http://new/resource').with(:headers => {'Cookie' => ''}).to_return(:body => 'Qux')
102
- RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Qux'
125
+ it 'respects cookie domains on redirect' do
126
+ stub_request(:get, 'http://some.example.com/').to_return(:body => '', :status => 301,
127
+ :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://new.example.com/', })
128
+ stub_request(:get, 'http://new.example.com/').with(
129
+ :headers => {'Cookie' => 'passedthrough=1'}).to_return(:body => 'Qux')
130
+
131
+ 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'
103
132
  end
104
133
 
105
134
  it "doesn't follow a 301 when the request is a post" do
106
- net_http_res = double('net http response', :code => 301)
107
- response = RestClient::Response.create('abc', net_http_res, {:method => :post}, @request)
108
- lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
135
+ net_http_res = res_double(:code => 301)
136
+ response = response_from_res_double(net_http_res, request_double(method: 'post'))
137
+
138
+ expect {
139
+ response.return!
140
+ }.to raise_error(RestClient::MovedPermanently)
109
141
  end
110
142
 
111
143
  it "doesn't follow a 302 when the request is a post" do
112
- net_http_res = double('net http response', :code => 302)
113
- response = RestClient::Response.create('abc', net_http_res, {:method => :post}, @request)
114
- lambda { response.return!(@request)}.should raise_error(RestClient::Found)
144
+ net_http_res = res_double(:code => 302)
145
+ response = response_from_res_double(net_http_res, request_double(method: 'post'))
146
+
147
+ expect {
148
+ response.return!
149
+ }.to raise_error(RestClient::Found)
115
150
  end
116
151
 
117
152
  it "doesn't follow a 307 when the request is a post" do
118
- net_http_res = double('net http response', :code => 307)
119
- response = RestClient::Response.create('abc', net_http_res, {:method => :post}, @request)
120
- lambda { response.return!(@request)}.should raise_error(RestClient::TemporaryRedirect)
153
+ net_http_res = res_double(:code => 307)
154
+ response = response_from_res_double(net_http_res, request_double(method: 'post'))
155
+
156
+ expect(response).not_to receive(:follow_redirection)
157
+ expect {
158
+ response.return!
159
+ }.to raise_error(RestClient::TemporaryRedirect)
121
160
  end
122
161
 
123
162
  it "doesn't follow a redirection when the request is a put" do
124
- net_http_res = double('net http response', :code => 301)
125
- response = RestClient::Response.create('abc', net_http_res, {:method => :put}, @request)
126
- lambda { response.return!(@request)}.should raise_error(RestClient::MovedPermanently)
163
+ net_http_res = res_double(:code => 301)
164
+ response = response_from_res_double(net_http_res, request_double(method: 'put'))
165
+ expect {
166
+ response.return!
167
+ }.to raise_error(RestClient::MovedPermanently)
127
168
  end
128
169
 
129
170
  it "follows a redirection when the request is a post and result is a 303" do
130
171
  stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'})
131
172
  stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
132
- RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body.should eq 'Foo'
173
+ expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body).to eq 'Foo'
133
174
  end
134
175
 
135
176
  it "follows a redirection when the request is a head" do
136
177
  stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
137
178
  stub_request(:head, 'http://new/resource').to_return(:body => 'Foo')
138
- RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body.should eq 'Foo'
179
+ expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body).to eq 'Foo'
139
180
  end
140
181
 
141
182
  it "handles redirects with relative paths" do
142
183
  stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'})
143
184
  stub_request(:get, 'http://some/index').to_return(:body => 'Foo')
144
- RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
185
+ expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
145
186
  end
146
187
 
147
188
  it "handles redirects with relative path and query string" do
148
189
  stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'})
149
190
  stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo')
150
- RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
191
+ expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
151
192
  end
152
193
 
153
194
  it "follow a redirection when the request is a get and the response is in the 30x range" do
154
195
  stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
155
196
  stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
156
- RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.should eq 'Foo'
197
+ expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
157
198
  end
158
199
 
159
200
  it "follows no more than 10 redirections before raising error" do
160
201
  stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
161
202
  stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
162
- lambda { RestClient::Request.execute(:url => 'http://some/redirect-1', :method => :get) }.should raise_error(RestClient::MaxRedirectsReached)
163
- WebMock.should have_requested(:get, 'http://some/redirect-2').times(10)
203
+ expect {
204
+ RestClient::Request.execute(url: 'http://some/redirect-1', method: :get)
205
+ }.to raise_error(RestClient::MovedPermanently) { |ex|
206
+ ex.response.history.each {|r| expect(r).to be_a(RestClient::Response) }
207
+ expect(ex.response.history.length).to eq 10
208
+ }
209
+ expect(WebMock).to have_requested(:get, 'http://some/redirect-2').times(10)
164
210
  end
165
211
 
166
212
  it "follows no more than max_redirects redirections, if specified" do
167
213
  stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
168
214
  stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
169
- lambda { RestClient::Request.execute(:url => 'http://some/redirect-1', :method => :get, :max_redirects => 5) }.should raise_error(RestClient::MaxRedirectsReached)
170
- WebMock.should have_requested(:get, 'http://some/redirect-2').times(5)
215
+ expect {
216
+ RestClient::Request.execute(url: 'http://some/redirect-1', method: :get, max_redirects: 5)
217
+ }.to raise_error(RestClient::MovedPermanently) { |ex|
218
+ expect(ex.response.history.length).to eq 5
219
+ }
220
+ expect(WebMock).to have_requested(:get, 'http://some/redirect-2').times(5)
221
+ end
222
+
223
+ it "allows for manual following of redirects" do
224
+ stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/resource'})
225
+ stub_request(:get, 'http://some/resource').to_return(:body => 'Qux', :status => 200)
226
+
227
+ begin
228
+ RestClient::Request.execute(url: 'http://some/redirect-1', method: :get, max_redirects: 0)
229
+ rescue RestClient::MovedPermanently => err
230
+ resp = err.response.follow_redirection
231
+ else
232
+ raise 'notreached'
233
+ end
234
+
235
+ expect(resp.code).to eq 200
236
+ expect(resp.body).to eq 'Qux'
171
237
  end
172
238
  end
173
239
 
240
+ describe "logging" do
241
+ it "uses the request's logger" do
242
+ stub_request(:get, 'http://some/resource').to_return(body: 'potato', status: 200)
174
243
 
244
+ logger = double('logger', '<<' => true)
245
+ request = RestClient::Request.new(url: 'http://some/resource', method: :get, log: logger)
246
+
247
+ expect(logger).to receive(:<<)
248
+
249
+ request.execute
250
+ end
251
+ end
175
252
  end
@@ -1,39 +1,39 @@
1
- require 'spec_helper'
1
+ require_relative '_lib'
2
2
 
3
3
  describe RestClient do
4
4
  describe "API" do
5
5
  it "GET" do
6
- RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
6
+ expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
7
7
  RestClient.get('http://some/resource')
8
8
  end
9
9
 
10
10
  it "POST" do
11
- RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
11
+ expect(RestClient::Request).to receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
12
12
  RestClient.post('http://some/resource', 'payload')
13
13
  end
14
14
 
15
15
  it "PUT" do
16
- RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
16
+ expect(RestClient::Request).to receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
17
17
  RestClient.put('http://some/resource', 'payload')
18
18
  end
19
19
 
20
20
  it "PATCH" do
21
- RestClient::Request.should_receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'payload', :headers => {})
21
+ expect(RestClient::Request).to receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'payload', :headers => {})
22
22
  RestClient.patch('http://some/resource', 'payload')
23
23
  end
24
24
 
25
25
  it "DELETE" do
26
- RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
26
+ expect(RestClient::Request).to receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
27
27
  RestClient.delete('http://some/resource')
28
28
  end
29
29
 
30
30
  it "HEAD" do
31
- RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {})
31
+ expect(RestClient::Request).to receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {})
32
32
  RestClient.head('http://some/resource')
33
33
  end
34
34
 
35
35
  it "OPTIONS" do
36
- RestClient::Request.should_receive(:execute).with(:method => :options, :url => 'http://some/resource', :headers => {})
36
+ expect(RestClient::Request).to receive(:execute).with(:method => :options, :url => 'http://some/resource', :headers => {})
37
37
  RestClient.options('http://some/resource')
38
38
  end
39
39
  end
@@ -45,35 +45,36 @@ describe RestClient do
45
45
 
46
46
  it "uses << if the log is not a string" do
47
47
  log = RestClient.log = []
48
- log.should_receive(:<<).with('xyz')
48
+ expect(log).to receive(:<<).with('xyz')
49
49
  RestClient.log << 'xyz'
50
50
  end
51
51
 
52
52
  it "displays the log to stdout" do
53
53
  RestClient.log = 'stdout'
54
- STDOUT.should_receive(:puts).with('xyz')
54
+ expect(STDOUT).to receive(:puts).with('xyz')
55
55
  RestClient.log << 'xyz'
56
56
  end
57
57
 
58
58
  it "displays the log to stderr" do
59
59
  RestClient.log = 'stderr'
60
- STDERR.should_receive(:puts).with('xyz')
60
+ expect(STDERR).to receive(:puts).with('xyz')
61
61
  RestClient.log << 'xyz'
62
62
  end
63
63
 
64
64
  it "append the log to the requested filename" do
65
65
  RestClient.log = '/tmp/restclient.log'
66
66
  f = double('file handle')
67
- File.should_receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
68
- f.should_receive(:puts).with('xyz')
67
+ expect(File).to receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
68
+ expect(f).to receive(:puts).with('xyz')
69
69
  RestClient.log << 'xyz'
70
70
  end
71
71
  end
72
72
 
73
73
  describe 'version' do
74
- it 'has a version ~> 1.8.0.alpha' do
74
+ # test that there is a sane version number to avoid accidental 0.0.0 again
75
+ it 'has a version > 2.0.0.alpha, < 3.0' do
75
76
  ver = Gem::Version.new(RestClient.version)
76
- Gem::Requirement.new('~> 1.8.0.alpha').should be_satisfied_by(ver)
77
+ expect(Gem::Requirement.new('> 2.0.0.alpha', '< 3.0')).to be_satisfied_by(ver)
77
78
  end
78
79
  end
79
80
  end