rest-client 1.7.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.mailmap +10 -0
- data/.rspec +2 -1
- data/.rubocop +2 -0
- data/.rubocop-disables.yml +386 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +56 -8
- data/AUTHORS +26 -1
- data/README.md +901 -0
- data/Rakefile +27 -3
- data/bin/restclient +3 -5
- data/history.md +199 -0
- data/lib/restclient/abstract_response.rb +196 -50
- data/lib/restclient/exceptions.rb +96 -55
- data/lib/restclient/params_array.rb +72 -0
- data/lib/restclient/payload.rb +70 -74
- data/lib/restclient/platform.rb +20 -1
- data/lib/restclient/raw_response.rb +21 -6
- data/lib/restclient/request.rb +572 -284
- data/lib/restclient/resource.rb +19 -9
- data/lib/restclient/response.rb +75 -9
- data/lib/restclient/utils.rb +274 -0
- data/lib/restclient/version.rb +2 -1
- data/lib/restclient.rb +21 -3
- data/rest-client.gemspec +13 -10
- data/spec/ISS.jpg +0 -0
- data/spec/helpers.rb +54 -0
- data/spec/integration/_lib.rb +1 -0
- data/spec/integration/capath_digicert/3513523f.0 +22 -0
- data/spec/integration/capath_digicert/399e7759.0 +22 -0
- data/spec/integration/capath_digicert/digicert.crt +20 -17
- data/spec/integration/certs/digicert.crt +20 -17
- data/spec/integration/httpbin_spec.rb +128 -0
- data/spec/integration/integration_spec.rb +97 -14
- data/spec/integration/request_spec.rb +29 -6
- data/spec/spec_helper.rb +28 -1
- data/spec/unit/_lib.rb +1 -0
- data/spec/unit/abstract_response_spec.rb +95 -35
- data/spec/unit/exceptions_spec.rb +41 -28
- data/spec/unit/params_array_spec.rb +36 -0
- data/spec/unit/payload_spec.rb +118 -68
- data/spec/unit/raw_response_spec.rb +10 -5
- data/spec/unit/request2_spec.rb +34 -12
- data/spec/unit/request_spec.rb +751 -418
- data/spec/unit/resource_spec.rb +31 -27
- data/spec/unit/response_spec.rb +144 -58
- data/spec/unit/restclient_spec.rb +16 -15
- data/spec/unit/utils_spec.rb +147 -0
- data/spec/unit/windows/root_certs_spec.rb +3 -3
- metadata +121 -70
- data/README.rdoc +0 -324
- data/spec/integration/capath_digicert/244b5494.0 +0 -19
- data/spec/integration/capath_digicert/81b9768f.0 +0 -19
- data/spec/unit/master_shake.jpg +0 -0
data/spec/unit/resource_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
52
|
-
@resource.password.
|
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').
|
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').
|
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').
|
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).
|
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.
|
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.
|
79
|
-
parent['posts'].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.
|
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.
|
94
|
-
parent.send(:[], 'posts', &block2).block.
|
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
|
-
|
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.
|
103
|
-
parent['posts', &block2].block.
|
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.
|
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.
|
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' }.
|
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' }.
|
130
|
+
expect(resource.get { |response, request| 'bar' }).to eq 'bar'
|
127
131
|
end
|
128
132
|
|
129
133
|
end
|
data/spec/unit/response_spec.rb
CHANGED
@@ -1,40 +1,58 @@
|
|
1
|
-
|
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 =
|
6
|
-
@
|
7
|
-
@
|
5
|
+
@net_http_res = res_double(to_hash: {'Status' => ['200 OK']}, code: '200', body: 'abc')
|
6
|
+
@example_url = 'http://example.com'
|
7
|
+
@request = request_double(url: @example_url, method: 'get')
|
8
|
+
@response = response_from_res_double(@net_http_res, @request, duration: 1)
|
8
9
|
end
|
9
10
|
|
10
11
|
it "behaves like string" do
|
11
|
-
@response.to_s.
|
12
|
-
@response.to_str.
|
13
|
-
|
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
|
14
17
|
end
|
15
18
|
|
16
19
|
it "accepts nil strings and sets it to empty for the case of HEAD" do
|
17
|
-
|
20
|
+
# TODO
|
21
|
+
expect(RestClient::Response.create(nil, @net_http_res, @request).to_s).to eq ""
|
18
22
|
end
|
19
23
|
|
20
|
-
|
21
|
-
|
22
|
-
|
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
|
23
39
|
end
|
24
40
|
|
25
41
|
describe "cookie processing" do
|
26
42
|
it "should correctly deal with one Set-Cookie header with one cookie inside" do
|
27
|
-
|
28
|
-
|
29
|
-
response
|
30
|
-
response.
|
43
|
+
header_val = "main_page=main_page_no_rewrite; path=/; expires=Sat, 10-Jan-2037 15:03:14 GMT".freeze
|
44
|
+
|
45
|
+
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => [header_val]})
|
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" })
|
31
49
|
end
|
32
50
|
|
33
51
|
it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
|
34
|
-
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=
|
35
|
-
response = RestClient::Response.create('abc', net_http_res,
|
36
|
-
response.headers[:set_cookie].
|
37
|
-
response.cookies.
|
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"]})
|
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({
|
38
56
|
"main_page" => "main_page_no_rewrite",
|
39
57
|
"remember_me" => "",
|
40
58
|
"user" => "somebody"
|
@@ -42,9 +60,9 @@ describe RestClient::Response do
|
|
42
60
|
end
|
43
61
|
|
44
62
|
it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
|
45
|
-
net_http_res = double('net http response', :to_hash => {"etag" => ["\"e1ac1a2df945942ef4cac8116366baad\""], "set-cookie" => ["main_page=main_page_no_rewrite; path=/; expires=
|
46
|
-
response = RestClient::Response.create('abc', net_http_res,
|
47
|
-
response.cookies.
|
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"]})
|
64
|
+
response = RestClient::Response.create('abc', net_http_res, @request)
|
65
|
+
expect(response.cookies).to eq({
|
48
66
|
"main_page" => "main_page_no_rewrite",
|
49
67
|
"remember_me" => "",
|
50
68
|
"user" => "somebody"
|
@@ -55,18 +73,19 @@ describe RestClient::Response do
|
|
55
73
|
describe "exceptions processing" do
|
56
74
|
it "should return itself for normal codes" do
|
57
75
|
(200..206).each do |code|
|
58
|
-
net_http_res =
|
59
|
-
|
60
|
-
|
76
|
+
net_http_res = res_double(:code => '200')
|
77
|
+
resp = RestClient::Response.create('abc', net_http_res, @request)
|
78
|
+
resp.return!
|
61
79
|
end
|
62
80
|
end
|
63
81
|
|
64
82
|
it "should throw an exception for other codes" do
|
65
|
-
RestClient::Exceptions::EXCEPTIONS_MAP.
|
83
|
+
RestClient::Exceptions::EXCEPTIONS_MAP.each_pair do |code, exc|
|
66
84
|
unless (200..207).include? code
|
67
|
-
net_http_res =
|
68
|
-
|
69
|
-
|
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)
|
70
89
|
end
|
71
90
|
end
|
72
91
|
end
|
@@ -78,89 +97,156 @@ describe RestClient::Response do
|
|
78
97
|
it "follows a redirection when the request is a get" do
|
79
98
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
80
99
|
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
|
81
|
-
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.
|
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
|
82
111
|
end
|
83
112
|
|
84
113
|
it "follows a redirection and keep the parameters" do
|
85
|
-
stub_request(:get, 'http://
|
86
|
-
stub_request(:get, 'http://
|
87
|
-
RestClient::Request.execute(:url => 'http://some/resource', :method => :get, :user => 'foo', :password => 'bar', :headers => {:accept => :json}).body.
|
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'
|
88
117
|
end
|
89
118
|
|
90
119
|
it "follows a redirection and keep the cookies" do
|
91
|
-
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://
|
92
|
-
stub_request(:get, 'http://
|
93
|
-
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.
|
120
|
+
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Set-Cookie' => 'Foo=Bar', 'Location' => 'http://some/new_resource', })
|
121
|
+
stub_request(:get, 'http://some/new_resource').with(:headers => {'Cookie' => 'Foo=Bar'}).to_return(:body => 'Qux')
|
122
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Qux'
|
123
|
+
end
|
124
|
+
|
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'
|
94
132
|
end
|
95
133
|
|
96
134
|
it "doesn't follow a 301 when the request is a post" do
|
97
|
-
net_http_res =
|
98
|
-
response =
|
99
|
-
|
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)
|
100
141
|
end
|
101
142
|
|
102
143
|
it "doesn't follow a 302 when the request is a post" do
|
103
|
-
net_http_res =
|
104
|
-
response =
|
105
|
-
|
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)
|
106
150
|
end
|
107
151
|
|
108
152
|
it "doesn't follow a 307 when the request is a post" do
|
109
|
-
net_http_res =
|
110
|
-
response =
|
111
|
-
|
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)
|
112
160
|
end
|
113
161
|
|
114
162
|
it "doesn't follow a redirection when the request is a put" do
|
115
|
-
net_http_res =
|
116
|
-
response =
|
117
|
-
|
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)
|
118
168
|
end
|
119
169
|
|
120
170
|
it "follows a redirection when the request is a post and result is a 303" do
|
121
171
|
stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'})
|
122
172
|
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
|
123
|
-
RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body.
|
173
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body).to eq 'Foo'
|
124
174
|
end
|
125
175
|
|
126
176
|
it "follows a redirection when the request is a head" do
|
127
177
|
stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
128
178
|
stub_request(:head, 'http://new/resource').to_return(:body => 'Foo')
|
129
|
-
RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body.
|
179
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body).to eq 'Foo'
|
130
180
|
end
|
131
181
|
|
132
182
|
it "handles redirects with relative paths" do
|
133
183
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'})
|
134
184
|
stub_request(:get, 'http://some/index').to_return(:body => 'Foo')
|
135
|
-
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.
|
185
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
136
186
|
end
|
137
187
|
|
138
188
|
it "handles redirects with relative path and query string" do
|
139
189
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'})
|
140
190
|
stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo')
|
141
|
-
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.
|
191
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
142
192
|
end
|
143
193
|
|
144
194
|
it "follow a redirection when the request is a get and the response is in the 30x range" do
|
145
195
|
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
146
196
|
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
|
147
|
-
RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body.
|
197
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
148
198
|
end
|
149
199
|
|
150
200
|
it "follows no more than 10 redirections before raising error" do
|
151
201
|
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
152
202
|
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
153
|
-
|
154
|
-
|
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)
|
155
210
|
end
|
156
211
|
|
157
212
|
it "follows no more than max_redirects redirections, if specified" do
|
158
213
|
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
159
214
|
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
160
|
-
|
161
|
-
|
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'
|
162
237
|
end
|
163
238
|
end
|
164
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)
|
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(:<<)
|
165
248
|
|
249
|
+
request.execute
|
250
|
+
end
|
251
|
+
end
|
166
252
|
end
|
@@ -1,39 +1,39 @@
|
|
1
|
-
|
1
|
+
require_relative '_lib'
|
2
2
|
|
3
3
|
describe RestClient do
|
4
4
|
describe "API" do
|
5
5
|
it "GET" do
|
6
|
-
RestClient::Request.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
68
|
-
f.
|
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
|
-
|
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('
|
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
|