forked from Shopify/shopify-api-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_client_test.rb
230 lines (176 loc) · 8.68 KB
/
http_client_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# typed: false
# frozen_string_literal: true
require_relative "../test_helper.rb"
module ShopifyAPITest
module Clients
class HttpClientTest < Test::Unit::TestCase
def setup
super
@shop = "test-shop.myshopify.com"
@token = SecureRandom.alphanumeric(10)
@base_path = "/base_path"
@session = ShopifyAPI::Auth::Session.new(shop: @shop, access_token: @token)
@client = ShopifyAPI::Clients::HttpClient.new(session: @session, base_path: @base_path)
@request = ShopifyAPI::Clients::HttpRequest.new(
http_method: :post,
path: "some-path",
body: { foo: "bar" },
body_type: "application/json",
query: { id: 1234 },
extra_headers: { extra: "header" },
)
@expected_headers = {
"X-Shopify-Access-Token": @token,
}.merge(@request.extra_headers)
.merge(TestHelpers::Constants::DEFAULT_CLIENT_HEADERS)
@success_body = { "success" => true }
@response_headers = { "content-type" => "application/json", "x-request-id" => "123" }
end
def test_get
simple_http_test(:get)
end
def test_delete
simple_http_test(:delete)
end
def test_put
simple_http_test(:put)
end
def test_post
simple_http_test(:post)
end
def test_request_with_empty_base_path
@base_path = ""
@client = ShopifyAPI::Clients::HttpClient.new(session: @session, base_path: @base_path)
simple_http_test(:get)
end
def test_request_using_active_session
ShopifyAPI::Context.activate_session(@session)
@client = ShopifyAPI::Clients::HttpClient.new(base_path: @base_path)
simple_http_test(:get)
ShopifyAPI::Context.deactivate_session
end
def test_request_with_empty_response_body
@success_body = {}
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: "", headers: @response_headers)
verify_http_request
end
def test_request_with_no_access_token
@session = ShopifyAPI::Auth::Session.new(shop: @shop)
@client = ShopifyAPI::Clients::HttpClient.new(session: @session, base_path: @base_path)
@expected_headers.delete(:"X-Shopify-Access-Token")
apply_simple_http_stub
verify_http_request
end
def test_request_with_user_agent_prefix
@expected_headers[:"User-Agent"] = "some_prefix | " + @expected_headers[:"User-Agent"]
modify_context(user_agent_prefix: "some_prefix")
@client = ShopifyAPI::Clients::HttpClient.new(session: @session, base_path: @base_path)
apply_simple_http_stub
verify_http_request
end
def test_request_with_no_optional_parameters
@request = ShopifyAPI::Clients::HttpRequest.new(http_method: :get, path: @request.path)
@expected_headers.delete(:extra)
@expected_headers.delete(:"Content-Type")
stub_request(:get, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(headers: @expected_headers)
.to_return(body: @success_body.to_json, headers: @response_headers)
verify_http_request
end
def test_request_with_invalid_request
@request.http_method = :bad
assert_raises(ShopifyAPI::Errors::InvalidHttpRequestError) { @client.request(@request) }
end
def test_non_retriable_error_code
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: { errors: "Something very not good" }.to_json, headers: @response_headers, status: 400)
assert_raises(ShopifyAPI::Errors::HttpResponseError) { @client.request(@request) }
end
def test_retriable_error_code_no_retries
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: { errors: "Something very not good" }.to_json, headers: @response_headers, status: 500)
assert_raises(ShopifyAPI::Errors::HttpResponseError) { @client.request(@request) }
end
def test_retry_throttle_error
@request.tries = 2
@client.expects(:sleep).with(2).times(1)
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: { errors: "Something very not good" }.to_json,
headers: @response_headers.merge("Retry-After" => "2.0"), status: 429).times(1)
.then.to_return(body: @success_body.to_json, headers: @response_headers)
verify_http_request
end
def test_retry_internal_error
@request.tries = 2
@client.expects(:sleep).with(1).times(1)
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: { errors: "Something very not good" }.to_json, headers: @response_headers, status: 500)
.times(1)
.then.to_return(body: @success_body.to_json, headers: @response_headers)
verify_http_request
end
def test_retries_exceeded
@request.tries = 3
@client.expects(:sleep).with(1).times(2)
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: { errors: "Something very not good" }.to_json, headers: @response_headers, status: 500)
error = assert_raises(ShopifyAPI::Errors::MaxHttpRetriesExceededError) { @client.request(@request) }
assert_instance_of(ShopifyAPI::Clients::HttpResponse, error.response)
end
def test_throttle_error_no_retry_after_header
@request.tries = 2
@client.expects(:sleep).with(1).times(1)
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: { errors: "Something very not good" }.to_json, headers: @response_headers, status: 429)
.times(1)
.then.to_return(body: @success_body.to_json, headers: @response_headers)
verify_http_request
end
def test_throttle_error_with_retry_after_header_in_error_object
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: { errors: "Call limit exceeded" }.to_json,
headers: @response_headers.merge("Retry-After" => "2.0"), status: 429)
error = assert_raises(ShopifyAPI::Errors::HttpResponseError) { @client.request(@request) }
assert_equal(429, error.code)
assert_equal("2.0", error.response.headers["retry-after"][0])
end
def test_warns_on_deprecation_header
reader, writer = IO.pipe
modify_context(logger: Logger.new(writer), api_version: "unstable")
deprecate_reason = "https://help.shopify.com/tutorials#foobar-endpoint-is-removed"
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: @success_body.to_json,
headers: @response_headers.merge("X-Shopify-API-Deprecated-Reason" => deprecate_reason))
@client.request(@request)
assert_match(/#{@request.path}.*#{deprecate_reason}/, reader.gets)
end
private
def simple_http_test(http_method)
@request.http_method = http_method
apply_simple_http_stub
verify_http_request
end
def apply_simple_http_stub
stub_request(@request.http_method, "https://#{@shop}#{@base_path}/#{@request.path}")
.with(body: @request.body.to_json, query: @request.query, headers: @expected_headers)
.to_return(body: @success_body.to_json, headers: @response_headers)
end
def verify_http_request
response = @client.request(@request)
assert(response.ok?)
assert_equal(@success_body, response.body)
assert_equal(@response_headers.to_h { |k, v| [k, [v]] }, response.headers)
end
end
end
end