rest-client-wrapper 3.2.1 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +47 -47
- data/lib/rest_client_wrapper/authenticators/basic.rb +3 -3
- data/lib/rest_client_wrapper/authenticators/custom.rb +3 -3
- data/lib/rest_client_wrapper/authenticators/oauth.rb +6 -6
- data/lib/rest_client_wrapper/http.rb +1 -1
- data/lib/rest_client_wrapper/paginators/echo.rb +4 -4
- data/lib/rest_client_wrapper/paginators/echo_paginator.rb +5 -5
- data/lib/rest_client_wrapper/paginators/header_link.rb +6 -6
- data/lib/rest_client_wrapper/paginators/header_link_paginator.rb +6 -6
- data/lib/rest_client_wrapper/request.rb +10 -10
- data/lib/rest_client_wrapper/rest_client.rb +14 -14
- data/lib/rest_client_wrapper/version.rb +1 -1
- data/rest-client-wrapper.gemspec +6 -11
- metadata +20 -109
- data/spec/authenticators/oauth_spec.rb +0 -103
- data/spec/factories/request.rb +0 -48
- data/spec/factories/response.rb +0 -47
- data/spec/request_spec.rb +0 -185
- data/spec/rest_client_spec.rb +0 -249
- data/spec/spec_helper.rb +0 -75
- data/spec/support/factory_bot.rb +0 -28
- data/spec/support/stub_helper.rb +0 -20
data/spec/rest_client_spec.rb
DELETED
@@ -1,249 +0,0 @@
|
|
1
|
-
# Copyright (C) 2019 The University of Adelaide
|
2
|
-
#
|
3
|
-
# This file is part of Rest-Client-Wrapper.
|
4
|
-
#
|
5
|
-
# Rest-Client-Wrapper is free software: you can redistribute it and/or modify
|
6
|
-
# it under the terms of the GNU General Public License as published by
|
7
|
-
# the Free Software Foundation, either version 3 of the License, or
|
8
|
-
# (at your option) any later version.
|
9
|
-
#
|
10
|
-
# Rest-Client-Wrapper is distributed in the hope that it will be useful,
|
11
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
-
# GNU General Public License for more details.
|
14
|
-
#
|
15
|
-
# You should have received a copy of the GNU General Public License
|
16
|
-
# along with Rest-Client-Wrapper. If not, see <http://www.gnu.org/licenses/>.
|
17
|
-
#
|
18
|
-
|
19
|
-
require "spec_helper"
|
20
|
-
|
21
|
-
# RestClientWrapper
|
22
|
-
module RestClientWrapper # rubocop:disable Metrics/ModuleLength
|
23
|
-
|
24
|
-
describe RestClient do
|
25
|
-
|
26
|
-
before(:context) do
|
27
|
-
@api_url = URI.parse("http://fake_api_site.com")
|
28
|
-
@rest_client = RestClient.new({ host: @api_url.to_s })
|
29
|
-
|
30
|
-
@client_id = "client_id"
|
31
|
-
@client_secret = "client_secret"
|
32
|
-
@api_token_uri = URI.parse("http://fake_oauth_token.com/token_url_path")
|
33
|
-
|
34
|
-
@oauth_client = RestClient.new({ host: @api_url.to_s })
|
35
|
-
@oauth_client.authenticator = Authenticator::Oauth.new(
|
36
|
-
{
|
37
|
-
site: "#{ @api_token_uri.scheme }://#{ @api_token_uri.host }",
|
38
|
-
token_url_path: @api_token_uri.path,
|
39
|
-
client_id: @client_id,
|
40
|
-
client_secret: @client_secret
|
41
|
-
}
|
42
|
-
)
|
43
|
-
end
|
44
|
-
|
45
|
-
describe "#execute" do
|
46
|
-
|
47
|
-
before(:example) do
|
48
|
-
@course_id = "fake_course_id"
|
49
|
-
@uri = "/api/v1/courses/#{ @course_id }"
|
50
|
-
@rest_client.authenticator = Authenticator::Basic.new({ username: "username", password: "password" })
|
51
|
-
request = FactoryBot.build(:request, { body: {}, headers: FactoryBot.build(:request_headers, { host: @api_url.host }).to_h }).to_h
|
52
|
-
response = FactoryBot.build(:response).to_h
|
53
|
-
@api_request = stub_request(:get, "#{ @api_url }#{ @uri }").with(request).to_return(response)
|
54
|
-
end
|
55
|
-
|
56
|
-
context "when the request is valid" do
|
57
|
-
|
58
|
-
it "will return a valid response" do
|
59
|
-
request = Request.new({ http_method: :get, uri: "/api/v1/courses/%<course_id>s" })
|
60
|
-
request.segment_params = { course_id: @course_id }
|
61
|
-
@rest_client.execute({ request: request })
|
62
|
-
expect(@api_request).to have_been_requested
|
63
|
-
expect(@api_request).to have_been_requested.times(1)
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
|
-
context "when put request with query params and payload" do
|
69
|
-
|
70
|
-
before(:example) do
|
71
|
-
@request = Request.new({ http_method: :put, uri: "/api/v1/courses/%<course_id>s" })
|
72
|
-
@request.segment_params = { course_id: @course_id }
|
73
|
-
@request.payload = { id: "value" }
|
74
|
-
@request.query_params = { param: "value" }
|
75
|
-
|
76
|
-
@mock_request = FactoryBot.build(:request, { body: @request.payload, headers: FactoryBot.build(:request_headers, { host: @api_url.host }).to_h }).to_h
|
77
|
-
@response = FactoryBot.build(:response).to_h
|
78
|
-
end
|
79
|
-
|
80
|
-
it "has query string params in URL and payload as json in body" do
|
81
|
-
@api_request = stub_request(:put, "#{ @api_url }#{ @uri }?#{ @request.query_params.to_query }").with(@mock_request).to_return(@response)
|
82
|
-
@rest_client.execute({ request: @request })
|
83
|
-
expect(@api_request).to have_been_requested
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
context "when access token is no longer valid/expired" do
|
89
|
-
|
90
|
-
before(:example) do
|
91
|
-
@uri = "/api/v1/courses/course_id"
|
92
|
-
@requests = []
|
93
|
-
request = FactoryBot.build(:request, { body: {}, headers: FactoryBot.build(:request_headers, { host: @api_url.host }).to_h }).to_h
|
94
|
-
# Respond with unauthorized error to force reauth
|
95
|
-
response = FactoryBot.build(:response, { status: Rack::Utils::SYMBOL_TO_STATUS_CODE[:unauthorized] }).to_h
|
96
|
-
|
97
|
-
@requests << stub_request(:get, "#{ @api_url }#{ @uri }").with(request).to_return(response)
|
98
|
-
|
99
|
-
oauth_request = FactoryBot.build(:oauth_token_request, { headers: FactoryBot.build(:request_headers, { host: @api_token_uri.host }).to_h }).to_h
|
100
|
-
oauth_response = FactoryBot.build(:auth_token_response).to_h
|
101
|
-
|
102
|
-
@requests << stub_request(:post, @api_token_uri.to_s).with(oauth_request).to_return(oauth_response)
|
103
|
-
|
104
|
-
response = FactoryBot.build(:response, { status: Rack::Utils::SYMBOL_TO_STATUS_CODE[:ok] }).to_h
|
105
|
-
@requests << stub_request(:get, "#{ @api_url }#{ @uri }").with(request).to_return(response)
|
106
|
-
end
|
107
|
-
|
108
|
-
it "will get a new access_token" do
|
109
|
-
request = Request.new({ http_method: :get, uri: @uri })
|
110
|
-
request.segment_params = { course_id: @course_id }
|
111
|
-
@oauth_client.execute({ request: request })
|
112
|
-
@requests.each do |req|
|
113
|
-
expect(req).to have_been_requested.times(1)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
context "when there's a response with a status code not in the retry list" do
|
120
|
-
|
121
|
-
before(:example) do
|
122
|
-
WebMock.reset_executed_requests!
|
123
|
-
request = FactoryBot.build(:request, { body: {}, headers: FactoryBot.build(:request_headers, { host: @api_url.host }).to_h }).to_h
|
124
|
-
response = FactoryBot.build(:response, { status: Rack::Utils::SYMBOL_TO_STATUS_CODE[:internal_server_error] }).to_h
|
125
|
-
@api_request = stub_request(:get, "#{ @api_url }#{ @uri }").with(request).to_return(response)
|
126
|
-
end
|
127
|
-
|
128
|
-
it "will raise an exception" do
|
129
|
-
request = Request.new({ http_method: :get, uri: "/api/v1/courses/%<course_id>s" })
|
130
|
-
request.segment_params = { course_id: @course_id }
|
131
|
-
expect { @rest_client.execute({ request: request }) }.to raise_exception(RestClientError)
|
132
|
-
expect(@api_request).to have_been_requested.times(1)
|
133
|
-
end
|
134
|
-
|
135
|
-
end
|
136
|
-
|
137
|
-
context "when there's a response status code is 429 (too many requests)" do
|
138
|
-
|
139
|
-
before(:example) do
|
140
|
-
WebMock.reset_executed_requests!
|
141
|
-
request = FactoryBot.build(:request, { body: {}, headers: FactoryBot.build(:request_headers, { host: @api_url.host }).to_h }).to_h
|
142
|
-
response = FactoryBot.build(:response, { status: Rack::Utils::SYMBOL_TO_STATUS_CODE[:too_many_requests] }).to_h
|
143
|
-
@api_request = stub_request(:get, "#{ @api_url }#{ @uri }").with(request).to_return(response)
|
144
|
-
end
|
145
|
-
|
146
|
-
it "will retry three times than raise an exception" do
|
147
|
-
request = Request.new({ http_method: :get, uri: "/api/v1/courses/%<course_id>s" })
|
148
|
-
request.segment_params = { course_id: @course_id }
|
149
|
-
expect { @rest_client.execute({ request: request }) }.to raise_exception(RestClientError)
|
150
|
-
expect(@api_request).to have_been_requested.times(4) # initial request + 3 retries
|
151
|
-
end
|
152
|
-
|
153
|
-
end
|
154
|
-
|
155
|
-
end
|
156
|
-
|
157
|
-
describe "#make_request" do
|
158
|
-
|
159
|
-
before(:example) do
|
160
|
-
WebMock.reset_executed_requests!
|
161
|
-
@course_id = "fake_course_id"
|
162
|
-
@uri = "/api/v1/courses/#{ @course_id }"
|
163
|
-
@rest_client.authenticator = Authenticator::Basic.new({ username: "username", password: "password" })
|
164
|
-
request = FactoryBot.build(:request, { body: {}, headers: FactoryBot.build(:request_headers, { host: @api_url.host }).to_h }).to_h
|
165
|
-
response = FactoryBot.build(:response).to_h
|
166
|
-
@api_request = stub_request(:get, "#{ @api_url }#{ @uri }").with(request).to_return(response)
|
167
|
-
end
|
168
|
-
|
169
|
-
context "when the request is valid" do
|
170
|
-
|
171
|
-
it "will return a valid response" do
|
172
|
-
@rest_client.make_request({ http_method: :get, uri: @uri })
|
173
|
-
expect(@api_request).to have_been_requested
|
174
|
-
expect(@api_request).to have_been_requested.times(1)
|
175
|
-
end
|
176
|
-
|
177
|
-
end
|
178
|
-
|
179
|
-
end
|
180
|
-
|
181
|
-
describe "#get_all_pages" do
|
182
|
-
|
183
|
-
before(:example) do
|
184
|
-
WebMock.reset_executed_requests!
|
185
|
-
@uri = "/api/v1/courses/enrollments"
|
186
|
-
@per_page = 500
|
187
|
-
@requests = []
|
188
|
-
|
189
|
-
@rest_client.authenticator = Authenticator::Basic.new({ username: "username", password: "password" })
|
190
|
-
|
191
|
-
# Stubing requests and responses for pagination calls
|
192
|
-
1.upto(4) do |page_number|
|
193
|
-
response = FactoryBot.build(:paginated_response).to_h
|
194
|
-
# Add "next" link header for all responses except the last one. (i.e. so that we're only processing 4 pages)
|
195
|
-
response[:headers][:link] = "<#{ @api_url }#{ @uri }?page=#{ page_number + 1 }&per_page=#{ @per_page }>; rel=\"next\"" if page_number < 4
|
196
|
-
request = FactoryBot.build(:request, { body: { per_page: @per_page, page: page_number }, headers: FactoryBot.build(:request_headers, { host: @api_url.host }).to_h }).to_h
|
197
|
-
api_request = stub_request(:get, "#{ @api_url }#{ @uri }").with(request).to_return(response)
|
198
|
-
@requests << api_request
|
199
|
-
end
|
200
|
-
|
201
|
-
end
|
202
|
-
|
203
|
-
context "when a pagination request is made" do
|
204
|
-
|
205
|
-
it "will recursively make pagination requests until there's no page left" do
|
206
|
-
skip "TODO: move to Paginator"
|
207
|
-
@rest_client.get_all_pages({ http_method: :get, uri: @uri, payload: { per_page: @per_page } })
|
208
|
-
@requests.each do |request|
|
209
|
-
expect(request).to have_been_requested.times(1)
|
210
|
-
end
|
211
|
-
|
212
|
-
end
|
213
|
-
|
214
|
-
end
|
215
|
-
|
216
|
-
end
|
217
|
-
|
218
|
-
describe "#_validate_request" do
|
219
|
-
|
220
|
-
before(:example) do
|
221
|
-
@request = Request.new({ http_method: :get, uri: "/public/api/v1/users/%<user_id>s" })
|
222
|
-
end
|
223
|
-
|
224
|
-
context "when URI is missing a segments parameter" do
|
225
|
-
|
226
|
-
it "will raise ArgumentError" do
|
227
|
-
expect { @rest_client.send(:_validate_request, @request) }.to raise_error(ArgumentError)
|
228
|
-
end
|
229
|
-
|
230
|
-
end
|
231
|
-
|
232
|
-
context "when all segment parameters are present for URI" do
|
233
|
-
|
234
|
-
before(:example) do
|
235
|
-
@request = Request.new({ http_method: :get, uri: "/public/api/v1/courses/%<course_id>s/user/%<user_id>s" })
|
236
|
-
@request.segment_params = { user_id: "user_id", course_id: "course_id" }
|
237
|
-
end
|
238
|
-
|
239
|
-
it "return not raise an error" do
|
240
|
-
expect { @rest_client.send(:_validate_request, @request) }.not_to raise_error
|
241
|
-
end
|
242
|
-
|
243
|
-
end
|
244
|
-
|
245
|
-
end
|
246
|
-
|
247
|
-
end
|
248
|
-
|
249
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
# Copyright (C) 2019 The University of Adelaide
|
2
|
-
#
|
3
|
-
# This file is part of Rest-Client-Wrapper.
|
4
|
-
#
|
5
|
-
# Rest-Client-Wrapper is free software: you can redistribute it and/or modify
|
6
|
-
# it under the terms of the GNU General Public License as published by
|
7
|
-
# the Free Software Foundation, either version 3 of the License, or
|
8
|
-
# (at your option) any later version.
|
9
|
-
#
|
10
|
-
# Rest-Client-Wrapper is distributed in the hope that it will be useful,
|
11
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
-
# GNU General Public License for more details.
|
14
|
-
#
|
15
|
-
# You should have received a copy of the GNU General Public License
|
16
|
-
# along with Rest-Client-Wrapper. If not, see <http://www.gnu.org/licenses/>.
|
17
|
-
#
|
18
|
-
|
19
|
-
require "pry"
|
20
|
-
require "rest_client_wrapper/authenticators/basic"
|
21
|
-
require "rest_client_wrapper/authenticators/custom"
|
22
|
-
require "rest_client_wrapper/authenticators/oauth"
|
23
|
-
require "rest_client_wrapper/rest_client"
|
24
|
-
require "rest_client_wrapper/request"
|
25
|
-
require "rest_client_wrapper/response"
|
26
|
-
require "support/factory_bot"
|
27
|
-
require "webmock/rspec"
|
28
|
-
require "faker"
|
29
|
-
|
30
|
-
# This file was generated by the `rspec --init` command. Conventionally, all
|
31
|
-
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
32
|
-
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
33
|
-
# this file to always be loaded, without a need to explicitly require it in any
|
34
|
-
# files.
|
35
|
-
#
|
36
|
-
# Given that it is always loaded, you are encouraged to keep this file as
|
37
|
-
# light-weight as possible. Requiring heavyweight dependencies from this file
|
38
|
-
# will add to the boot time of your test suite on EVERY test run, even for an
|
39
|
-
# individual file that may not need all of that loaded. Instead, consider making
|
40
|
-
# a separate helper file that requires the additional dependencies and performs
|
41
|
-
# the additional setup, and require it from the spec files that actually need
|
42
|
-
# it.
|
43
|
-
#
|
44
|
-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
45
|
-
RSpec.configure do |config|
|
46
|
-
# rspec-expectations config goes here. You can use an alternate
|
47
|
-
# assertion/expectation library such as wrong or the stdlib/minitest
|
48
|
-
# assertions if you prefer.
|
49
|
-
config.expect_with :rspec do |expectations|
|
50
|
-
# This option will default to `true` in RSpec 4. It makes the `description`
|
51
|
-
# and `failure_message` of custom matchers include text for helper methods
|
52
|
-
# defined using `chain`, e.g.:
|
53
|
-
# be_bigger_than(2).and_smaller_than(4).description
|
54
|
-
# # => "be bigger than 2 and smaller than 4"
|
55
|
-
# ...rather than:
|
56
|
-
# # => "be bigger than 2"
|
57
|
-
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
58
|
-
end
|
59
|
-
|
60
|
-
# rspec-mocks config goes here. You can use an alternate test double
|
61
|
-
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
62
|
-
config.mock_with :rspec do |mocks|
|
63
|
-
# Prevents you from mocking or stubbing a method that does not exist on
|
64
|
-
# a real object. This is generally recommended, and will default to
|
65
|
-
# `true` in RSpec 4.
|
66
|
-
mocks.verify_partial_doubles = true
|
67
|
-
end
|
68
|
-
|
69
|
-
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
70
|
-
# have no way to turn it off -- the option exists only for backwards
|
71
|
-
# compatibility in RSpec 3). It causes shared context metadata to be
|
72
|
-
# inherited by the metadata hash of host groups and examples, rather than
|
73
|
-
# triggering implicit auto-inclusion in groups with matching metadata.
|
74
|
-
config.shared_context_metadata_behavior = :apply_to_host_groups
|
75
|
-
end
|
data/spec/support/factory_bot.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# Copyright (C) 2019 The University of Adelaide
|
2
|
-
#
|
3
|
-
# This file is part of Rest-Client-Wrapper.
|
4
|
-
#
|
5
|
-
# Rest-Client-Wrapper is free software: you can redistribute it and/or modify
|
6
|
-
# it under the terms of the GNU General Public License as published by
|
7
|
-
# the Free Software Foundation, either version 3 of the License, or
|
8
|
-
# (at your option) any later version.
|
9
|
-
#
|
10
|
-
# Rest-Client-Wrapper is distributed in the hope that it will be useful,
|
11
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
-
# GNU General Public License for more details.
|
14
|
-
#
|
15
|
-
# You should have received a copy of the GNU General Public License
|
16
|
-
# along with Rest-Client-Wrapper. If not, see <http://www.gnu.org/licenses/>.
|
17
|
-
#
|
18
|
-
|
19
|
-
require "factory_bot"
|
20
|
-
require_relative "stub_helper"
|
21
|
-
|
22
|
-
RSpec.configure do |config|
|
23
|
-
config.include FactoryBot::Syntax::Methods
|
24
|
-
config.before(:suite) do
|
25
|
-
FactoryBot.find_definitions
|
26
|
-
config.include_context "stub_helper"
|
27
|
-
end
|
28
|
-
end
|
data/spec/support/stub_helper.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# Copyright (C) 2019 The University of Adelaide
|
2
|
-
#
|
3
|
-
# This file is part of Rest-Client-Wrapper.
|
4
|
-
#
|
5
|
-
# Rest-Client-Wrapper is free software: you can redistribute it and/or modify
|
6
|
-
# it under the terms of the GNU General Public License as published by
|
7
|
-
# the Free Software Foundation, either version 3 of the License, or
|
8
|
-
# (at your option) any later version.
|
9
|
-
#
|
10
|
-
# Rest-Client-Wrapper is distributed in the hope that it will be useful,
|
11
|
-
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
-
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
-
# GNU General Public License for more details.
|
14
|
-
#
|
15
|
-
# You should have received a copy of the GNU General Public License
|
16
|
-
# along with Rest-Client-Wrapper. If not, see <http://www.gnu.org/licenses/>.
|
17
|
-
#
|
18
|
-
|
19
|
-
RSpec.shared_context "stub_helper", { shared_context: :metadata } do
|
20
|
-
end
|