1
|
require "spec_helper"
|
1
|
2
|
require 'ostruct'
|
1
|
3
|
require 'oauth'
|
1
|
4
|
|
|
5
|
describe "ApiOperator" do
|
1
|
6
|
|
|
7
|
before do
|
1
|
8
|
response_digestor = stub(Sevendigital::ApiResponseDigestor)
|
22
|
9
|
response_digestor.stub!(:from_http_response). and_return(fake_digested_response)
|
22
|
10
|
|
|
11
|
stub_api_client(test_configuration, response_digestor)
|
22
|
12
|
|
|
13
|
Net::HTTP.stub!(:get_response).and_return(fake_api_response)
|
22
|
14
|
|
|
15
|
@api_operator = Sevendigital::ApiOperator.new(@client)
|
22
|
16
|
|
|
17
|
@stub_api_request = stub_api_request()
|
22
|
18
|
|
|
19
|
end
|
|
20
|
|
|
21
|
it "should create http request uri based on api method and client configuration" do
|
1
|
22
|
|
|
23
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
24
|
|
|
25
|
uri = @api_operator.create_request_uri(api_request)
|
1
|
26
|
|
|
27
|
uri.kind_of?(URI).should == true
|
1
|
28
|
|
|
29
|
uri.to_s.should =~ /http:\/\/base.api.url\/version\/api\/method/
|
1
|
30
|
uri.to_s.should =~ /[\?\&]param1=value/
|
1
|
31
|
uri.to_s.should =~ /[\?\&]paramTwo=2/
|
1
|
32
|
|
|
33
|
end
|
|
34
|
|
|
35
|
it "should URL encode parameters accourding to OAuth spec" do
|
1
|
36
|
|
|
37
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "a+b@c.d", :paramTwo => "<a b>"})
|
1
|
38
|
|
|
39
|
uri = @api_operator.create_request_uri(api_request)
|
1
|
40
|
|
|
41
|
uri.to_s.should =~ /[\?\&]param1=a%2Bb%40c.d/
|
1
|
42
|
uri.to_s.should =~ /[\?\&]paramTwo=%3Ca%20b%3E/
|
1
|
43
|
|
|
44
|
end
|
|
45
|
|
|
46
|
it "should create http GET request" do
|
1
|
47
|
|
|
48
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
49
|
|
|
50
|
client, request = @api_operator.create_http_request(api_request)
|
1
|
51
|
|
|
52
|
request.class.should == Net::HTTP::Get
|
1
|
53
|
|
|
54
|
end
|
|
55
|
|
|
56
|
it "should create http POST request" do
|
1
|
57
|
|
|
58
|
api_request = Sevendigital::ApiRequest.new(:POST, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
59
|
|
|
60
|
client, request = @api_operator.create_http_request(api_request)
|
1
|
61
|
|
|
62
|
request.class.should == Net::HTTP::Post
|
1
|
63
|
|
|
64
|
end
|
|
65
|
|
|
66
|
it "should create http DELETE request" do
|
1
|
67
|
|
|
68
|
api_request = Sevendigital::ApiRequest.new(:DELETE, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
69
|
|
|
70
|
client, request = @api_operator.create_http_request(api_request)
|
1
|
71
|
|
|
72
|
request.class.should == Net::HTTP::Delete
|
1
|
73
|
|
|
74
|
end
|
|
75
|
|
|
76
|
it "should create http PUT request" do
|
1
|
77
|
|
|
78
|
api_request = Sevendigital::ApiRequest.new(:PUT, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
79
|
|
|
80
|
client, request = @api_operator.create_http_request(api_request)
|
1
|
81
|
|
|
82
|
request.class.should == Net::HTTP::Put
|
1
|
83
|
|
|
84
|
end
|
|
85
|
|
|
86
|
it "should have empty body if no form parameters provided" do
|
1
|
87
|
|
|
88
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
89
|
|
|
90
|
client, request = @api_operator.create_http_request(api_request)
|
1
|
91
|
|
|
92
|
request.instance_variable_get("@body").empty?.should == true
|
1
|
93
|
|
|
94
|
end
|
|
95
|
|
|
96
|
it "should set up any form parameters in body" do
|
1
|
97
|
|
|
98
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
99
|
api_request.form_parameters[:shop_id] = "1234"
|
1
|
100
|
api_request.form_parameters[:email] = "test@example.com"
|
1
|
101
|
api_request.form_parameters[:ignore] = nil
|
1
|
102
|
|
|
103
|
client, request = @api_operator.create_http_request(api_request)
|
1
|
104
|
|
|
105
|
request.instance_variable_get("@body").should == "shopId=1234&email=test%40example.com"
|
1
|
106
|
|
|
107
|
end
|
|
108
|
|
|
109
|
it "should create HTTPS request uri based on api method that requires secure connection and client configuration" do
|
1
|
110
|
|
|
111
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
112
|
api_request.require_secure_connection
|
1
|
113
|
|
|
114
|
uri = @api_operator.create_request_uri(api_request)
|
1
|
115
|
|
|
116
|
uri.kind_of?(URI).should == true
|
1
|
117
|
|
|
118
|
uri.to_s.should =~ /https:\/\/base.api.url\/version\/api\/method/
|
1
|
119
|
uri.to_s.should =~ /[\?\&]param1=value/
|
1
|
120
|
uri.to_s.should =~ /[\?\&]paramTwo=2/
|
1
|
121
|
|
|
122
|
end
|
|
123
|
|
|
124
|
it "should create http request uri based on api method for non standard api service" do
|
1
|
125
|
|
|
126
|
@client.should_receive(:api_host_and_version).with(:media).and_return(["media-base.api.url","media-version"])
|
1
|
127
|
|
|
128
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
129
|
api_request.api_service = :media
|
1
|
130
|
uri = @api_operator.create_request_uri(api_request)
|
1
|
131
|
|
|
132
|
uri.kind_of?(URI).should == true
|
1
|
133
|
|
|
134
|
uri.to_s.should =~ /http:\/\/media-base.api.url\/media-version\/api\/method/
|
1
|
135
|
uri.to_s.should =~ /[\?\&]param1=value/
|
1
|
136
|
uri.to_s.should =~ /[\?\&]paramTwo=2/
|
1
|
137
|
|
|
138
|
end
|
|
139
|
|
|
140
|
it "should make HTTP request and get http response" do
|
1
|
141
|
|
|
142
|
http_response = fake_api_response
|
1
|
143
|
|
|
144
|
@stub_http_request = stub(Net::HTTP::Get)
|
1
|
145
|
@stub_http_client = stub(Net::HTTP)
|
1
|
146
|
|
|
147
|
@api_operator.should_receive(:create_http_request).with(@stub_api_request).and_return([@stub_http_client, @stub_http_request])
|
1
|
148
|
|
|
149
|
@stub_http_client.should_receive(:request).with(@stub_http_request).and_return(http_response)
|
1
|
150
|
|
|
151
|
response = @api_operator.make_http_request(@stub_api_request)
|
1
|
152
|
|
|
153
|
response.should == http_response
|
1
|
154
|
|
|
155
|
end
|
|
156
|
|
|
157
|
it "should digest the HTTP response and get it out" do
|
1
|
158
|
|
|
159
|
http_response = fake_api_response
|
1
|
160
|
digested_response = fake_digested_response
|
1
|
161
|
|
|
162
|
@client.api_response_digestor.should_receive(:from_http_response).with(http_response).and_return(digested_response)
|
1
|
163
|
|
|
164
|
response = @api_operator.digest_http_response(http_response)
|
1
|
165
|
|
|
166
|
response.should == digested_response
|
1
|
167
|
|
|
168
|
end
|
|
169
|
|
|
170
|
it "should call API by making an http request and digesting the response" do
|
1
|
171
|
|
|
172
|
http_response = fake_api_response
|
1
|
173
|
digested_response = fake_digested_response
|
1
|
174
|
|
|
175
|
@api_operator.should_receive(:make_http_request).and_return(http_response)
|
1
|
176
|
|
|
177
|
@client.api_response_digestor.should_receive(:from_http_response).with(http_response).and_return(digested_response)
|
1
|
178
|
|
|
179
|
response = @api_operator.call_api(@stub_api_request)
|
1
|
180
|
|
|
181
|
response.should == digested_response
|
1
|
182
|
|
|
183
|
end
|
|
184
|
|
|
185
|
it "should throw an exception if response is not ok" do
|
1
|
186
|
|
|
187
|
failed_response = fake_digested_response(false)
|
1
|
188
|
failed_response.stub!(:error_code).and_return(4000)
|
1
|
189
|
failed_response.stub!(:error_message).and_return("error")
|
1
|
190
|
@api_operator.should_receive(:make_http_request).and_return(fake_api_response)
|
1
|
191
|
@client.api_response_digestor.stub!(:from_http_response).and_return(failed_response)
|
1
|
192
|
|
|
193
|
running { @api_operator.call_api(@stub_api_request) }.should raise_error(Sevendigital::SevendigitalError) { |error|
|
2
|
194
|
error.error_code.should == 4000
|
1
|
195
|
error.error_message.should == "error"
|
1
|
196
|
}
|
|
197
|
|
|
198
|
end
|
|
199
|
|
|
200
|
it "should create a 2-legged signed HTTP request" do
|
1
|
201
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
202
|
api_request.require_signature
|
1
|
203
|
http_client, http_request = @api_operator.create_http_request(api_request)
|
1
|
204
|
http_client.use_ssl?.should == false
|
1
|
205
|
http_request.path.should =~ /api\/method/
|
1
|
206
|
http_request.path.should =~ /param1=value/
|
1
|
207
|
http_request["Authorization"].should =~ /OAuth oauth_consumer_key="oauth_consumer_key"/
|
1
|
208
|
http_request["Authorization"].should =~ / oauth_signature=/
|
1
|
209
|
# http_request["Authorization"].should !=~ / oauth_token="token"/
|
|
210
|
end
|
|
211
|
|
|
212
|
it "should create a 3-legged signed HTTP request" do
|
1
|
213
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
214
|
api_request.require_signature
|
1
|
215
|
api_request.token = OAuth::AccessToken.new(@client.oauth_consumer, "token", "secret")
|
1
|
216
|
http_client, http_request = @api_operator.create_http_request(api_request)
|
1
|
217
|
http_client.use_ssl?.should == false
|
1
|
218
|
http_request.path.should =~ /api\/method/
|
1
|
219
|
http_request.path.should =~ /param1=value/
|
1
|
220
|
http_request["Authorization"].should =~ /OAuth oauth_consumer_key="oauth_consumer_key"/
|
1
|
221
|
http_request["Authorization"].should =~ / oauth_signature=/
|
1
|
222
|
http_request["Authorization"].should =~ / oauth_token="token"/
|
1
|
223
|
end
|
|
224
|
|
|
225
|
it "should create a 3-legged signed HTTPS request" do
|
1
|
226
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
227
|
api_request.require_signature
|
1
|
228
|
api_request.require_secure_connection
|
1
|
229
|
api_request.token = OAuth::AccessToken.new(@client.oauth_consumer, "token", "secret")
|
1
|
230
|
http_client, http_request = @api_operator.create_http_request(api_request)
|
1
|
231
|
http_client.inspect.should =~ /base\.api\.url:443/
|
1
|
232
|
http_client.use_ssl?.should == true
|
1
|
233
|
http_client.verify_mode.should == OpenSSL::SSL::VERIFY_NONE
|
1
|
234
|
http_request.path.should =~ /api\/method/
|
1
|
235
|
http_request.path.should =~ /param1=value/
|
1
|
236
|
http_request["Authorization"].should =~ /OAuth oauth_consumer_key="oauth_consumer_key"/
|
1
|
237
|
http_request["Authorization"].should =~ / oauth_signature=/
|
1
|
238
|
http_request["Authorization"].should =~ / oauth_token="token"/
|
1
|
239
|
end
|
|
240
|
|
|
241
|
it "should create a standard HTTP request" do
|
1
|
242
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
243
|
http_client, http_request = @api_operator.create_http_request(api_request)
|
1
|
244
|
http_client.inspect.should =~ /base\.api\.url:80/
|
1
|
245
|
http_client.use_ssl?.should == false
|
1
|
246
|
http_request.path.should =~ /api\/method/
|
1
|
247
|
http_request.path.should =~ /param1=value/
|
1
|
248
|
http_request.path.should =~ /oauth_consumer_key=oauth_consumer_key/
|
1
|
249
|
end
|
|
250
|
|
|
251
|
it "should add custom user agent info to created HTTP request" do
|
1
|
252
|
|
|
253
|
@client.stub!(:user_agent_info).and_return("7digital Gem")
|
1
|
254
|
|
|
255
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
256
|
http_client, http_request = @api_operator.create_http_request(api_request)
|
1
|
257
|
http_request["User-agent"].should =~ /7digital Gem/
|
1
|
258
|
end
|
|
259
|
|
|
260
|
it "get_request_uri should return oauth sign URI if api request requires signature" do
|
1
|
261
|
|
|
262
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
263
|
api_request.token = OAuth::AccessToken.new(nil, "token", "token_secret")
|
1
|
264
|
api_request.require_signature
|
1
|
265
|
api_request.require_secure_connection
|
1
|
266
|
signed_uri = @api_operator.get_request_uri(api_request)
|
1
|
267
|
|
|
268
|
signed_uri.should =~ /oauth_signature=/
|
1
|
269
|
signed_uri.should =~ /oauth_consumer_key=oauth_consumer_key&/
|
1
|
270
|
signed_uri.should =~ /oauth_token=token&/
|
1
|
271
|
signed_uri.should =~ /https:\/\/base.api.url\/version\/api\/method/
|
1
|
272
|
end
|
|
273
|
|
|
274
|
it "get_request_uri should return simple request URI if api request does not require signature" do
|
1
|
275
|
|
|
276
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
277
|
signed_uri = @api_operator.get_request_uri(api_request)
|
1
|
278
|
|
|
279
|
signed_uri.match(/oauth_signature=/).should == nil
|
1
|
280
|
signed_uri.should =~ /oauth_consumer_key=oauth_consumer_key/
|
1
|
281
|
signed_uri.should =~ /http:\/\/base.api.url\/version\/api\/method/
|
1
|
282
|
end
|
|
283
|
|
|
284
|
it "get_request_uri should return secure request URI if api request does not require signature" do
|
1
|
285
|
|
|
286
|
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
1
|
287
|
api_request.require_secure_connection
|
1
|
288
|
signed_uri = @api_operator.get_request_uri(api_request)
|
1
|
289
|
|
|
290
|
signed_uri.match(/oauth_signature=/).should == nil
|
1
|
291
|
signed_uri.should =~ /oauth_consumer_key=oauth_consumer_key/
|
1
|
292
|
signed_uri.should =~ /https:\/\/base.api.url\/version\/api\/method/
|
1
|
293
|
end
|
|
294
|
|
|
295
|
def test_configuration
|
1
|
296
|
configuration = OpenStruct.new
|
22
|
297
|
configuration.oauth_consumer_key = "oauth_consumer_key"
|
22
|
298
|
return configuration
|
22
|
299
|
end
|
|
300
|
|
|
301
|
def fake_api_response(code = 200, body = "response_body")
|
1
|
302
|
return Net::HTTP.new("1.1", code, body)
|
26
|
303
|
end
|
|
304
|
|
|
305
|
def fake_digested_response(is_ok = true)
|
1
|
306
|
proxy = stub(Peachy::Proxy)#.new('<response status="ok"><content>test</content></response>')
|
25
|
307
|
proxy.stub!(:ok?).and_return(is_ok)
|
25
|
308
|
proxy
|
25
|
309
|
end
|
|
310
|
|
|
311
|
def stub_api_client(configuration, response_digestor)
|
1
|
312
|
@client = stub(Sevendigital::Client)
|
22
|
313
|
@client.stub!(:configuration).and_return(configuration)
|
22
|
314
|
@client.stub!(:oauth_consumer).and_return(OAuth::Consumer.new( configuration.oauth_consumer_key, configuration.oauth_consumer_secret))
|
22
|
315
|
@client.stub!(:api_response_digestor).and_return(response_digestor)
|
22
|
316
|
@client.stub!(:default_parameters).and_return({:country => 'sk'})
|
22
|
317
|
@client.stub!(:user_agent_info).and_return("7digital")
|
22
|
318
|
@client.stub!(:verbose?).and_return(false)
|
22
|
319
|
@client.stub!(:very_verbose?).and_return(false)
|
22
|
320
|
@client.stub!(:api_host_and_version).and_return(["base.api.url","version"])
|
22
|
321
|
|
|
322
|
end
|
|
323
|
|
|
324
|
def stub_api_request
|
1
|
325
|
api_request = stub(Sevendigital::ApiRequest)
|
22
|
326
|
|
|
327
|
api_request.stub!(:parameters).and_return({})
|
22
|
328
|
api_request.stub!(:api_service).and_return(nil)
|
22
|
329
|
api_request.stub!(:api_method).and_return("m")
|
22
|
330
|
api_request.stub!(:requires_signature?).and_return(false)
|
22
|
331
|
api_request.stub!(:requires_secure_connection?).and_return(false)
|
22
|
332
|
api_request.stub!(:ensure_country_is_set)
|
22
|
333
|
return api_request
|
22
|
334
|
end
|
|
335
|
|
|
336
|
end
|
|