alinta-rest-client 2.2.0-x64-mingw32
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 +9 -0
- data/.mailmap +10 -0
- data/.rspec +2 -0
- data/.rubocop +2 -0
- data/.rubocop-disables.yml +393 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +49 -0
- data/AUTHORS +106 -0
- data/Gemfile +11 -0
- data/LICENSE +21 -0
- data/README.md +896 -0
- data/Rakefile +140 -0
- data/bin/restclient +92 -0
- data/history.md +357 -0
- data/lib/rest-client.rb +2 -0
- data/lib/rest_client.rb +2 -0
- data/lib/restclient.rb +183 -0
- data/lib/restclient/abstract_response.rb +252 -0
- data/lib/restclient/exceptions.rb +244 -0
- data/lib/restclient/params_array.rb +72 -0
- data/lib/restclient/payload.rb +234 -0
- data/lib/restclient/platform.rb +49 -0
- data/lib/restclient/raw_response.rb +49 -0
- data/lib/restclient/request.rb +875 -0
- data/lib/restclient/resource.rb +178 -0
- data/lib/restclient/response.rb +90 -0
- data/lib/restclient/utils.rb +274 -0
- data/lib/restclient/version.rb +8 -0
- data/lib/restclient/windows.rb +8 -0
- data/lib/restclient/windows/root_certs.rb +105 -0
- data/rest-client.gemspec +32 -0
- data/rest-client.windows.gemspec +19 -0
- data/spec/ISS.jpg +0 -0
- data/spec/helpers.rb +54 -0
- data/spec/integration/_lib.rb +1 -0
- data/spec/integration/capath_digicert/244b5494.0 +19 -0
- data/spec/integration/capath_digicert/81b9768f.0 +19 -0
- data/spec/integration/capath_digicert/README +8 -0
- data/spec/integration/capath_digicert/digicert.crt +19 -0
- data/spec/integration/capath_verisign/415660c1.0 +14 -0
- data/spec/integration/capath_verisign/7651b327.0 +14 -0
- data/spec/integration/capath_verisign/README +8 -0
- data/spec/integration/capath_verisign/verisign.crt +14 -0
- data/spec/integration/certs/digicert.crt +19 -0
- data/spec/integration/certs/verisign.crt +14 -0
- data/spec/integration/httpbin_spec.rb +128 -0
- data/spec/integration/integration_spec.rb +118 -0
- data/spec/integration/request_spec.rb +127 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/unit/_lib.rb +1 -0
- data/spec/unit/abstract_response_spec.rb +145 -0
- data/spec/unit/exceptions_spec.rb +108 -0
- data/spec/unit/params_array_spec.rb +36 -0
- data/spec/unit/payload_spec.rb +295 -0
- data/spec/unit/raw_response_spec.rb +22 -0
- data/spec/unit/request2_spec.rb +54 -0
- data/spec/unit/request_spec.rb +1238 -0
- data/spec/unit/resource_spec.rb +134 -0
- data/spec/unit/response_spec.rb +252 -0
- data/spec/unit/restclient_spec.rb +80 -0
- data/spec/unit/utils_spec.rb +147 -0
- data/spec/unit/windows/root_certs_spec.rb +22 -0
- metadata +318 -0
@@ -0,0 +1,134 @@
|
|
1
|
+
require_relative '_lib'
|
2
|
+
|
3
|
+
describe RestClient::Resource do
|
4
|
+
before do
|
5
|
+
@resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => {'X-Something' => '1'})
|
6
|
+
end
|
7
|
+
|
8
|
+
context "Resource delegation" do
|
9
|
+
it "GET" do
|
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
|
+
@resource.get
|
12
|
+
end
|
13
|
+
|
14
|
+
it "HEAD" do
|
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
|
+
@resource.head
|
17
|
+
end
|
18
|
+
|
19
|
+
it "POST" do
|
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
|
+
@resource.post 'abc', :content_type => 'image/jpg'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "PUT" do
|
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
|
+
@resource.put 'abc', :content_type => 'image/jpg'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "PATCH" do
|
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
|
+
@resource.patch 'abc', :content_type => 'image/jpg'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "DELETE" do
|
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
|
+
@resource.delete
|
37
|
+
end
|
38
|
+
|
39
|
+
it "overrides resource headers" do
|
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
|
+
@resource.get 'X-Something' => '2'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "can instantiate with no user/password" do
|
46
|
+
@resource = RestClient::Resource.new('http://some/resource')
|
47
|
+
end
|
48
|
+
|
49
|
+
it "is backwards compatible with previous constructor" do
|
50
|
+
@resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
|
51
|
+
expect(@resource.user).to eq 'user'
|
52
|
+
expect(@resource.password).to eq 'pass'
|
53
|
+
end
|
54
|
+
|
55
|
+
it "concatenates urls, inserting a slash when it needs one" do
|
56
|
+
expect(@resource.concat_urls('http://example.com', 'resource')).to eq 'http://example.com/resource'
|
57
|
+
end
|
58
|
+
|
59
|
+
it "concatenates urls, using no slash if the first url ends with a slash" do
|
60
|
+
expect(@resource.concat_urls('http://example.com/', 'resource')).to eq 'http://example.com/resource'
|
61
|
+
end
|
62
|
+
|
63
|
+
it "concatenates urls, using no slash if the second url starts with a slash" do
|
64
|
+
expect(@resource.concat_urls('http://example.com', '/resource')).to eq 'http://example.com/resource'
|
65
|
+
end
|
66
|
+
|
67
|
+
it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
|
68
|
+
expect(@resource.concat_urls(:posts, 1)).to eq 'posts/1'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "offers subresources via []" do
|
72
|
+
parent = RestClient::Resource.new('http://example.com')
|
73
|
+
expect(parent['posts'].url).to eq 'http://example.com/posts'
|
74
|
+
end
|
75
|
+
|
76
|
+
it "transports options to subresources" do
|
77
|
+
parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
|
78
|
+
expect(parent['posts'].user).to eq 'user'
|
79
|
+
expect(parent['posts'].password).to eq 'password'
|
80
|
+
end
|
81
|
+
|
82
|
+
it "passes a given block to subresources" do
|
83
|
+
block = proc {|r| r}
|
84
|
+
parent = RestClient::Resource.new('http://example.com', &block)
|
85
|
+
expect(parent['posts'].block).to eq block
|
86
|
+
end
|
87
|
+
|
88
|
+
it "the block should be overrideable" do
|
89
|
+
block1 = proc {|r| r}
|
90
|
+
block2 = proc {|r| }
|
91
|
+
parent = RestClient::Resource.new('http://example.com', &block1)
|
92
|
+
# parent['posts', &block2].block.should eq block2 # ruby 1.9 syntax
|
93
|
+
expect(parent.send(:[], 'posts', &block2).block).to eq block2
|
94
|
+
expect(parent.send(:[], 'posts', &block2).block).not_to eq block1
|
95
|
+
end
|
96
|
+
|
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
|
102
|
+
block1 = proc {|r| r}
|
103
|
+
block2 = ->(r) {}
|
104
|
+
|
105
|
+
parent = RestClient::Resource.new('http://example.com', &block1)
|
106
|
+
expect(parent['posts', &block2].block).to eq block2
|
107
|
+
expect(parent['posts', &block2].block).not_to eq block1
|
108
|
+
end
|
109
|
+
|
110
|
+
it "prints its url with to_s" do
|
111
|
+
expect(RestClient::Resource.new('x').to_s).to eq 'x'
|
112
|
+
end
|
113
|
+
|
114
|
+
describe 'block' do
|
115
|
+
it 'can use block when creating the resource' do
|
116
|
+
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
|
117
|
+
resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
|
118
|
+
expect(resource.get).to eq 'foo'
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'can use block when executing the resource' do
|
122
|
+
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
|
123
|
+
resource = RestClient::Resource.new('www.example.com')
|
124
|
+
expect(resource.get { |response, request| 'foo' }).to eq 'foo'
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'execution block override resource block' do
|
128
|
+
stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
|
129
|
+
resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
|
130
|
+
expect(resource.get { |response, request| 'bar' }).to eq 'bar'
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,252 @@
|
|
1
|
+
require_relative '_lib'
|
2
|
+
|
3
|
+
describe RestClient::Response, :include_helpers do
|
4
|
+
before do
|
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)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "behaves like string" do
|
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
|
17
|
+
end
|
18
|
+
|
19
|
+
it "accepts nil strings and sets it to empty for the case of HEAD" do
|
20
|
+
# TODO
|
21
|
+
expect(RestClient::Response.create(nil, @net_http_res, @request).to_s).to eq ""
|
22
|
+
end
|
23
|
+
|
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
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "cookie processing" do
|
42
|
+
it "should correctly deal with one Set-Cookie header with one cookie inside" do
|
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" })
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should correctly deal with multiple cookies [multiple Set-Cookie headers]" do
|
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({
|
56
|
+
"main_page" => "main_page_no_rewrite",
|
57
|
+
"remember_me" => "",
|
58
|
+
"user" => "somebody"
|
59
|
+
})
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should correctly deal with multiple cookies [one Set-Cookie header with multiple cookies]" do
|
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({
|
66
|
+
"main_page" => "main_page_no_rewrite",
|
67
|
+
"remember_me" => "",
|
68
|
+
"user" => "somebody"
|
69
|
+
})
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "exceptions processing" do
|
74
|
+
it "should return itself for normal codes" do
|
75
|
+
(200..206).each do |code|
|
76
|
+
net_http_res = res_double(:code => '200')
|
77
|
+
resp = RestClient::Response.create('abc', net_http_res, @request)
|
78
|
+
resp.return!
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should throw an exception for other codes" do
|
83
|
+
RestClient::Exceptions::EXCEPTIONS_MAP.each_pair do |code, exc|
|
84
|
+
unless (200..207).include? code
|
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)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "redirection" do
|
96
|
+
|
97
|
+
it "follows a redirection when the request is a get" do
|
98
|
+
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
99
|
+
stub_request(:get, 'http://new/resource').to_return(:body => '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
|
111
|
+
end
|
112
|
+
|
113
|
+
it "follows a redirection and keep the parameters" do
|
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'
|
117
|
+
end
|
118
|
+
|
119
|
+
it "follows a redirection and keep the cookies" do
|
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'
|
132
|
+
end
|
133
|
+
|
134
|
+
it "doesn't follow a 301 when the request is a post" do
|
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)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "doesn't follow a 302 when the request is a post" do
|
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)
|
150
|
+
end
|
151
|
+
|
152
|
+
it "doesn't follow a 307 when the request is a post" do
|
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)
|
160
|
+
end
|
161
|
+
|
162
|
+
it "doesn't follow a redirection when the request is a put" do
|
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)
|
168
|
+
end
|
169
|
+
|
170
|
+
it "follows a redirection when the request is a post and result is a 303" do
|
171
|
+
stub_request(:put, 'http://some/resource').to_return(:body => '', :status => 303, :headers => {'Location' => 'http://new/resource'})
|
172
|
+
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
|
173
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :put).body).to eq 'Foo'
|
174
|
+
end
|
175
|
+
|
176
|
+
it "follows a redirection when the request is a head" do
|
177
|
+
stub_request(:head, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
178
|
+
stub_request(:head, 'http://new/resource').to_return(:body => 'Foo')
|
179
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :head).body).to eq 'Foo'
|
180
|
+
end
|
181
|
+
|
182
|
+
it "handles redirects with relative paths" do
|
183
|
+
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index'})
|
184
|
+
stub_request(:get, 'http://some/index').to_return(:body => 'Foo')
|
185
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
186
|
+
end
|
187
|
+
|
188
|
+
it "handles redirects with relative path and query string" do
|
189
|
+
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'index?q=1'})
|
190
|
+
stub_request(:get, 'http://some/index?q=1').to_return(:body => 'Foo')
|
191
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
192
|
+
end
|
193
|
+
|
194
|
+
it "follow a redirection when the request is a get and the response is in the 30x range" do
|
195
|
+
stub_request(:get, 'http://some/resource').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://new/resource'})
|
196
|
+
stub_request(:get, 'http://new/resource').to_return(:body => 'Foo')
|
197
|
+
expect(RestClient::Request.execute(:url => 'http://some/resource', :method => :get).body).to eq 'Foo'
|
198
|
+
end
|
199
|
+
|
200
|
+
it "follows no more than 10 redirections before raising error" do
|
201
|
+
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
202
|
+
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
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)
|
210
|
+
end
|
211
|
+
|
212
|
+
it "follows no more than max_redirects redirections, if specified" do
|
213
|
+
stub_request(:get, 'http://some/redirect-1').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
214
|
+
stub_request(:get, 'http://some/redirect-2').to_return(:body => '', :status => 301, :headers => {'Location' => 'http://some/redirect-2'})
|
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'
|
237
|
+
end
|
238
|
+
end
|
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(:<<)
|
248
|
+
|
249
|
+
request.execute
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require_relative '_lib'
|
2
|
+
|
3
|
+
describe RestClient do
|
4
|
+
describe "API" do
|
5
|
+
it "GET" do
|
6
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {})
|
7
|
+
RestClient.get('http://some/resource')
|
8
|
+
end
|
9
|
+
|
10
|
+
it "POST" do
|
11
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'payload', :headers => {})
|
12
|
+
RestClient.post('http://some/resource', 'payload')
|
13
|
+
end
|
14
|
+
|
15
|
+
it "PUT" do
|
16
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'payload', :headers => {})
|
17
|
+
RestClient.put('http://some/resource', 'payload')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "PATCH" do
|
21
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'payload', :headers => {})
|
22
|
+
RestClient.patch('http://some/resource', 'payload')
|
23
|
+
end
|
24
|
+
|
25
|
+
it "DELETE" do
|
26
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {})
|
27
|
+
RestClient.delete('http://some/resource')
|
28
|
+
end
|
29
|
+
|
30
|
+
it "HEAD" do
|
31
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {})
|
32
|
+
RestClient.head('http://some/resource')
|
33
|
+
end
|
34
|
+
|
35
|
+
it "OPTIONS" do
|
36
|
+
expect(RestClient::Request).to receive(:execute).with(:method => :options, :url => 'http://some/resource', :headers => {})
|
37
|
+
RestClient.options('http://some/resource')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "logging" do
|
42
|
+
after do
|
43
|
+
RestClient.log = nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it "uses << if the log is not a string" do
|
47
|
+
log = RestClient.log = []
|
48
|
+
expect(log).to receive(:<<).with('xyz')
|
49
|
+
RestClient.log << 'xyz'
|
50
|
+
end
|
51
|
+
|
52
|
+
it "displays the log to stdout" do
|
53
|
+
RestClient.log = 'stdout'
|
54
|
+
expect(STDOUT).to receive(:puts).with('xyz')
|
55
|
+
RestClient.log << 'xyz'
|
56
|
+
end
|
57
|
+
|
58
|
+
it "displays the log to stderr" do
|
59
|
+
RestClient.log = 'stderr'
|
60
|
+
expect(STDERR).to receive(:puts).with('xyz')
|
61
|
+
RestClient.log << 'xyz'
|
62
|
+
end
|
63
|
+
|
64
|
+
it "append the log to the requested filename" do
|
65
|
+
RestClient.log = '/tmp/restclient.log'
|
66
|
+
f = double('file handle')
|
67
|
+
expect(File).to receive(:open).with('/tmp/restclient.log', 'a').and_yield(f)
|
68
|
+
expect(f).to receive(:puts).with('xyz')
|
69
|
+
RestClient.log << 'xyz'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'version' 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
|
76
|
+
ver = Gem::Version.new(RestClient.version)
|
77
|
+
expect(Gem::Requirement.new('> 2.0.0.alpha', '< 3.0')).to be_satisfied_by(ver)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|