7digital 0.1.2 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.rdoc +143 -143
- data/lib/sevendigital.rb +55 -55
- data/lib/sevendigital/api_operator.rb +127 -127
- data/lib/sevendigital/api_operator_cached.rb +91 -62
- data/lib/sevendigital/api_request.rb +72 -62
- data/lib/sevendigital/client.rb +122 -122
- data/lib/sevendigital/client_configuration.rb +85 -83
- data/lib/sevendigital/client_digestors.rb +122 -122
- data/lib/sevendigital/client_managers.rb +57 -57
- data/lib/sevendigital/digestion_tract/api_response_digestor.rb +48 -48
- data/lib/sevendigital/digestion_tract/artist_digestor.rb +35 -35
- data/lib/sevendigital/digestion_tract/basket_digestor.rb +19 -19
- data/lib/sevendigital/digestion_tract/basket_item_digestor.rb +26 -26
- data/lib/sevendigital/digestion_tract/chart_item_digestor.rb +34 -34
- data/lib/sevendigital/digestion_tract/country_digestor.rb +15 -15
- data/lib/sevendigital/digestion_tract/digestor.rb +122 -122
- data/lib/sevendigital/digestion_tract/download_url_digestor.rb +23 -23
- data/lib/sevendigital/digestion_tract/format_digestor.rb +24 -24
- data/lib/sevendigital/digestion_tract/label_digestor.rb +21 -21
- data/lib/sevendigital/digestion_tract/list_digestor.rb +24 -24
- data/lib/sevendigital/digestion_tract/list_item_digestor.rb +26 -26
- data/lib/sevendigital/digestion_tract/locker_digestor.rb +22 -22
- data/lib/sevendigital/digestion_tract/locker_release_digestor.rb +26 -26
- data/lib/sevendigital/digestion_tract/locker_track_digestor.rb +26 -26
- data/lib/sevendigital/digestion_tract/oauth_access_token_digestor.rb +18 -18
- data/lib/sevendigital/digestion_tract/oauth_request_token_digestor.rb +18 -18
- data/lib/sevendigital/digestion_tract/pager_digestor.rb +28 -28
- data/lib/sevendigital/digestion_tract/price_digestor.rb +28 -28
- data/lib/sevendigital/digestion_tract/release_digestor.rb +45 -45
- data/lib/sevendigital/digestion_tract/tag_digestor.rb +23 -23
- data/lib/sevendigital/digestion_tract/track_digestor.rb +38 -37
- data/lib/sevendigital/digestion_tract/user_card_digestor.rb +24 -24
- data/lib/sevendigital/digestion_tract/user_digestor.rb +21 -21
- data/lib/sevendigital/management/artist_manager.rb +97 -97
- data/lib/sevendigital/management/basket_manager.rb +29 -29
- data/lib/sevendigital/management/country_manager.rb +10 -10
- data/lib/sevendigital/management/list_manager.rb +18 -18
- data/lib/sevendigital/management/manager.rb +12 -12
- data/lib/sevendigital/management/oauth_manager.rb +23 -23
- data/lib/sevendigital/management/release_manager.rb +54 -54
- data/lib/sevendigital/management/tag_manager.rb +10 -10
- data/lib/sevendigital/management/track_manager.rb +29 -27
- data/lib/sevendigital/management/user_card_manager.rb +26 -26
- data/lib/sevendigital/management/user_manager.rb +76 -76
- data/lib/sevendigital/model/api_response.rb +32 -32
- data/lib/sevendigital/model/artist.rb +104 -104
- data/lib/sevendigital/model/basket.rb +15 -15
- data/lib/sevendigital/model/basket_item.rb +14 -14
- data/lib/sevendigital/model/card.rb +11 -11
- data/lib/sevendigital/model/chart_item.rb +9 -9
- data/lib/sevendigital/model/download_url.rb +7 -7
- data/lib/sevendigital/model/format.rb +7 -7
- data/lib/sevendigital/model/label.rb +7 -7
- data/lib/sevendigital/model/list.rb +4 -4
- data/lib/sevendigital/model/list_item.rb +8 -8
- data/lib/sevendigital/model/locker.rb +4 -4
- data/lib/sevendigital/model/locker_release.rb +9 -9
- data/lib/sevendigital/model/locker_track.rb +9 -9
- data/lib/sevendigital/model/price.rb +9 -9
- data/lib/sevendigital/model/release.rb +57 -57
- data/lib/sevendigital/model/sevendigital_error.rb +11 -11
- data/lib/sevendigital/model/sevendigital_object.rb +57 -57
- data/lib/sevendigital/model/tag.rb +7 -7
- data/lib/sevendigital/model/track.rb +87 -83
- data/lib/sevendigital/model/user.rb +74 -74
- data/lib/sevendigital/pager.rb +20 -20
- data/lib/sevendigital/version.rb +5 -5
- data/spec/api_operator_cached_spec.rb +233 -178
- data/spec/api_operator_spec.rb +320 -320
- data/spec/api_request_spec.rb +46 -31
- data/spec/client_configuration_spec.rb +78 -78
- data/spec/client_spec.rb +161 -161
- data/spec/data/config/sevendigital.yml +5 -5
- data/spec/data/configuration_env_override.yml +5 -5
- data/spec/data/configuration_override.yml +1 -1
- data/spec/digestion_tract/api_response_digestor_spec.rb +93 -93
- data/spec/digestion_tract/artist_digestor_spec.rb +81 -81
- data/spec/digestion_tract/basket_digestor_spec.rb +31 -31
- data/spec/digestion_tract/basket_item_digestor_spec.rb +58 -58
- data/spec/digestion_tract/chart_digestor_spec.rb +52 -52
- data/spec/digestion_tract/country_digestor_spec.rb +33 -33
- data/spec/digestion_tract/digestor_spec.rb +33 -33
- data/spec/digestion_tract/download_url_digestor_spec.rb +40 -40
- data/spec/digestion_tract/format_digestor_spec.rb +38 -38
- data/spec/digestion_tract/label_digestor_spec.rb +33 -33
- data/spec/digestion_tract/list_digestor_spec.rb +31 -31
- data/spec/digestion_tract/list_item_digestor_spec.rb +80 -80
- data/spec/digestion_tract/locker_digestor_spec.rb +30 -30
- data/spec/digestion_tract/locker_release_digestor_spec.rb +70 -70
- data/spec/digestion_tract/locker_track_digestor_spec.rb +56 -56
- data/spec/digestion_tract/oauth_access_token_digestor_spec.rb +34 -34
- data/spec/digestion_tract/oauth_request_token_digestor_spec.rb +34 -34
- data/spec/digestion_tract/pager_digestor_spec.rb +50 -50
- data/spec/digestion_tract/price_digestor_spec.rb +73 -73
- data/spec/digestion_tract/release_digestor_spec.rb +115 -115
- data/spec/digestion_tract/tag_digestor_spec.rb +46 -46
- data/spec/digestion_tract/track_digestor_spec.rb +105 -104
- data/spec/digestion_tract/user_card_digestor_spec.rb +58 -58
- data/spec/digestion_tract/user_digestor_spec.rb +42 -42
- data/spec/integration/artist_spec.rb +99 -86
- data/spec/integration/country_spec.rb +18 -18
- data/spec/integration/list_spec.rb +22 -22
- data/spec/integration/purchasing_spec.rb +24 -24
- data/spec/integration/release_spec.rb +34 -34
- data/spec/integration/sevendigital_spec.yml +4 -4
- data/spec/integration/track_spec.rb +38 -23
- data/spec/integration/user_spec.rb +22 -22
- data/spec/management/artist_manager_spec.rb +172 -172
- data/spec/management/basket_manager_spec.rb +77 -77
- data/spec/management/country_manager_spec.rb +34 -34
- data/spec/management/list_manager_spec.rb +26 -26
- data/spec/management/oauth_manager_spec.rb +72 -72
- data/spec/management/release_manager_spec.rb +200 -200
- data/spec/management/tag_manager_spec.rb +34 -34
- data/spec/management/track_manager_spec.rb +111 -107
- data/spec/management/user_card_manager_spec.rb +78 -78
- data/spec/management/user_manager_spec.rb +218 -218
- data/spec/model/api_response_spec.rb +48 -48
- data/spec/model/artist_spec.rb +162 -162
- data/spec/model/basket_spec.rb +62 -62
- data/spec/model/release_spec.rb +133 -133
- data/spec/model/sevendigital_object_spec.rb +73 -73
- data/spec/model/track_spec.rb +146 -144
- data/spec/model/user_spec.rb +181 -181
- data/spec/spec_helper.rb +86 -84
- data/spec/test-xml/methods/artist/browse.xml +21 -21
- data/spec/test-xml/methods/artist/byTag/top.xml +50 -50
- data/spec/test-xml/methods/artist/chart.xml +30 -30
- data/spec/test-xml/methods/artist/details.xml +9 -9
- data/spec/test-xml/methods/artist/releases.xml +544 -544
- data/spec/test-xml/methods/artist/search.xml +18 -18
- data/spec/test-xml/methods/artist/similar.xml +39 -39
- data/spec/test-xml/methods/artist/tags.xml +11 -11
- data/spec/test-xml/methods/artist/toptracks.xml +279 -279
- data/spec/test-xml/methods/basket/additem.xml +23 -23
- data/spec/test-xml/methods/basket/create.xml +10 -10
- data/spec/test-xml/methods/basket/index.xml +23 -23
- data/spec/test-xml/methods/basket/removeitem.xml +10 -10
- data/spec/test-xml/methods/country/resolve.xml +6 -6
- data/spec/test-xml/methods/editorial/list.xml +170 -170
- data/spec/test-xml/methods/oauth/accesstoken.xml +5 -5
- data/spec/test-xml/methods/oauth/requesttoken.xml +5 -5
- data/spec/test-xml/methods/release/bydate.xml +173 -173
- data/spec/test-xml/methods/release/bytag/top.xml +150 -150
- data/spec/test-xml/methods/release/chart.xml +181 -181
- data/spec/test-xml/methods/release/details.xml +48 -48
- data/spec/test-xml/methods/release/recommend.xml +89 -89
- data/spec/test-xml/methods/release/search.xml +194 -194
- data/spec/test-xml/methods/release/tags.xml +27 -27
- data/spec/test-xml/methods/release/tracks.xml +28 -28
- data/spec/test-xml/methods/tag/index.xml +16 -16
- data/spec/test-xml/methods/track/chart.xml +149 -149
- data/spec/test-xml/methods/track/details.xml +30 -30
- data/spec/test-xml/methods/track/search.xml +159 -159
- data/spec/test-xml/methods/user/details.xml +4 -4
- data/spec/test-xml/methods/user/locker.xml +49 -49
- data/spec/test-xml/methods/user/payment/card/add.xml +7 -7
- data/spec/test-xml/methods/user/payment/card/index.xml +7 -7
- data/spec/test-xml/methods/user/purchase/basket.xml +48 -48
- data/spec/test-xml/methods/user/purchase/item.xml +48 -48
- data/spec/test-xml/methods/user/signUp.xml +5 -5
- data/spec/test-xml/objects/artist.xml +7 -7
- data/spec/test-xml/objects/artist_chart_item.xml +7 -7
- data/spec/test-xml/objects/artist_list.xml +22 -22
- data/spec/test-xml/objects/artist_list_empty.xml +4 -4
- data/spec/test-xml/objects/basket.xml +21 -21
- data/spec/test-xml/objects/basket_item.xml +11 -11
- data/spec/test-xml/objects/basket_item_list.xml +25 -25
- data/spec/test-xml/objects/basket_item_list_empty.xml +1 -1
- data/spec/test-xml/objects/download_url.xml +7 -7
- data/spec/test-xml/objects/list.xml +89 -89
- data/spec/test-xml/objects/list_item_list.xml +85 -85
- data/spec/test-xml/objects/list_item_release.xml +13 -13
- data/spec/test-xml/objects/locker.xml +47 -47
- data/spec/test-xml/objects/locker_release.xml +40 -40
- data/spec/test-xml/objects/locker_release_list.xml +87 -87
- data/spec/test-xml/objects/locker_release_list_empty.xml +4 -4
- data/spec/test-xml/objects/locker_release_one_item_list.xml +43 -43
- data/spec/test-xml/objects/locker_track.xml +25 -25
- data/spec/test-xml/objects/locker_track_list.xml +26 -26
- data/spec/test-xml/objects/locker_track_list_empty.xml +2 -2
- data/spec/test-xml/objects/nested_release_list.xml +22 -22
- data/spec/test-xml/objects/oauth_access_token.xml +3 -3
- data/spec/test-xml/objects/oauth_request_token.xml +3 -3
- data/spec/test-xml/objects/price.xml +7 -7
- data/spec/test-xml/objects/release.xml +40 -40
- data/spec/test-xml/objects/release_chart_item.xml +39 -39
- data/spec/test-xml/objects/release_list.xml +18 -18
- data/spec/test-xml/objects/release_list_empty.xml +4 -4
- data/spec/test-xml/objects/response.xml +38 -38
- data/spec/test-xml/objects/tag.xml +4 -4
- data/spec/test-xml/objects/track.xml +34 -33
- data/spec/test-xml/objects/track_chart_item.xml +27 -27
- data/spec/test-xml/objects/user.xml +4 -4
- data/spec/test-xml/objects/user_payment_card.xml +5 -5
- metadata +15 -25
data/spec/api_operator_spec.rb
CHANGED
@@ -1,321 +1,321 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
require 'ostruct'
|
3
|
-
require 'oauth'
|
4
|
-
|
5
|
-
describe "ApiOperator" do
|
6
|
-
|
7
|
-
before do
|
8
|
-
response_digestor = stub(Sevendigital::ApiResponseDigestor)
|
9
|
-
response_digestor.stub!(:from_http_response). and_return(fake_api_response)
|
10
|
-
|
11
|
-
stub_api_client(test_configuration, response_digestor)
|
12
|
-
|
13
|
-
Net::HTTP.stub!(:get_response).and_return(fake_http_response)
|
14
|
-
|
15
|
-
@api_operator = Sevendigital::ApiOperator.new(@client)
|
16
|
-
|
17
|
-
@stub_api_request = stub_api_request()
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should create http request uri based on api method and client configuration" do
|
22
|
-
|
23
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
24
|
-
|
25
|
-
uri = @api_operator.create_request_uri(api_request)
|
26
|
-
|
27
|
-
uri.kind_of?(URI).should == true
|
28
|
-
|
29
|
-
uri.to_s.should =~ /http:\/\/base.api.url\/version\/api\/method/
|
30
|
-
uri.to_s.should =~ /[\?\&]param1=value/
|
31
|
-
uri.to_s.should =~ /[\?\&]paramTwo=2/
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should URL encode parameters accourding to OAuth spec" do
|
36
|
-
|
37
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "a+b@c.d", :paramTwo => "<a b>"})
|
38
|
-
|
39
|
-
uri = @api_operator.create_request_uri(api_request)
|
40
|
-
|
41
|
-
uri.to_s.should =~ /[\?\&]param1=a%2Bb%40c.d/
|
42
|
-
uri.to_s.should =~ /[\?\&]paramTwo=%3Ca%20b%3E/
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should create http GET request" do
|
47
|
-
|
48
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
49
|
-
|
50
|
-
client, request = @api_operator.create_http_request(api_request)
|
51
|
-
|
52
|
-
request.class.should == Net::HTTP::Get
|
53
|
-
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should create http POST request" do
|
57
|
-
|
58
|
-
api_request = Sevendigital::ApiRequest.new(:POST, "api/method", {:param1 => "value", :paramTwo => 2})
|
59
|
-
|
60
|
-
client, request = @api_operator.create_http_request(api_request)
|
61
|
-
|
62
|
-
request.class.should == Net::HTTP::Post
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
it "should create http DELETE request" do
|
67
|
-
|
68
|
-
api_request = Sevendigital::ApiRequest.new(:DELETE, "api/method", {:param1 => "value", :paramTwo => 2})
|
69
|
-
|
70
|
-
client, request = @api_operator.create_http_request(api_request)
|
71
|
-
|
72
|
-
request.class.should == Net::HTTP::Delete
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should create http PUT request" do
|
77
|
-
|
78
|
-
api_request = Sevendigital::ApiRequest.new(:PUT, "api/method", {:param1 => "value", :paramTwo => 2})
|
79
|
-
|
80
|
-
client, request = @api_operator.create_http_request(api_request)
|
81
|
-
|
82
|
-
request.class.should == Net::HTTP::Put
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
it "should have empty body if no form parameters provided" do
|
87
|
-
|
88
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
89
|
-
|
90
|
-
client, request = @api_operator.create_http_request(api_request)
|
91
|
-
|
92
|
-
request.instance_variable_get("@body").empty?.should == true
|
93
|
-
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should set up any form parameters in body" do
|
97
|
-
|
98
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
99
|
-
api_request.form_parameters[:shop_id] = "1234"
|
100
|
-
api_request.form_parameters[:email] = "test+email@example.com"
|
101
|
-
api_request.form_parameters[:ignore] = nil
|
102
|
-
|
103
|
-
client, request = @api_operator.create_http_request(api_request)
|
104
|
-
|
105
|
-
#request.instance_variable_get("@body").should == "email=test%40example.com&shopId=1234"
|
106
|
-
request.instance_variable_get("@body").should =~ /email=test%2Bemail%40example.com/
|
107
|
-
request.instance_variable_get("@body").should =~ /shopId=1234/
|
108
|
-
end
|
109
|
-
|
110
|
-
it "should create HTTPS request uri based on api method that requires secure connection and client configuration" do
|
111
|
-
|
112
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
113
|
-
api_request.require_secure_connection
|
114
|
-
|
115
|
-
uri = @api_operator.create_request_uri(api_request)
|
116
|
-
|
117
|
-
uri.kind_of?(URI).should == true
|
118
|
-
|
119
|
-
uri.to_s.should =~ /https:\/\/base.api.url\/version\/api\/method/
|
120
|
-
uri.to_s.should =~ /[\?\&]param1=value/
|
121
|
-
uri.to_s.should =~ /[\?\&]paramTwo=2/
|
122
|
-
|
123
|
-
end
|
124
|
-
|
125
|
-
it "should create http request uri based on api method for non standard api service" do
|
126
|
-
|
127
|
-
@client.should_receive(:api_host_and_version).with(:media).and_return(["media-base.api.url","media-version"])
|
128
|
-
|
129
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
130
|
-
api_request.api_service = :media
|
131
|
-
uri = @api_operator.create_request_uri(api_request)
|
132
|
-
|
133
|
-
uri.kind_of?(URI).should == true
|
134
|
-
|
135
|
-
uri.to_s.should =~ /http:\/\/media-base.api.url\/media-version\/api\/method/
|
136
|
-
uri.to_s.should =~ /[\?\&]param1=value/
|
137
|
-
uri.to_s.should =~ /[\?\&]paramTwo=2/
|
138
|
-
|
139
|
-
end
|
140
|
-
|
141
|
-
it "should make HTTP request and get http response" do
|
142
|
-
|
143
|
-
http_response = fake_http_response
|
144
|
-
|
145
|
-
@stub_http_request = stub(Net::HTTP::Get)
|
146
|
-
@stub_http_client = stub(Net::HTTP)
|
147
|
-
|
148
|
-
@api_operator.should_receive(:create_http_request).with(@stub_api_request).and_return([@stub_http_client, @stub_http_request])
|
149
|
-
|
150
|
-
@stub_http_client.should_receive(:request).with(@stub_http_request).and_return(http_response)
|
151
|
-
|
152
|
-
response = @api_operator.make_http_request(@stub_api_request)
|
153
|
-
|
154
|
-
response.should == http_response
|
155
|
-
|
156
|
-
end
|
157
|
-
|
158
|
-
it "should digest the HTTP response and get it out" do
|
159
|
-
|
160
|
-
http_response = fake_http_response
|
161
|
-
digested_response = fake_api_response
|
162
|
-
|
163
|
-
@client.api_response_digestor.should_receive(:from_http_response).with(http_response).and_return(digested_response)
|
164
|
-
|
165
|
-
response = @api_operator.digest_http_response(http_response)
|
166
|
-
|
167
|
-
response.should == digested_response
|
168
|
-
|
169
|
-
end
|
170
|
-
|
171
|
-
it "should call API by making an http request and digesting the response" do
|
172
|
-
|
173
|
-
http_response = fake_http_response
|
174
|
-
digested_response = fake_api_response
|
175
|
-
|
176
|
-
@api_operator.should_receive(:make_http_request).and_return(http_response)
|
177
|
-
|
178
|
-
@client.api_response_digestor.should_receive(:from_http_response).with(http_response).and_return(digested_response)
|
179
|
-
|
180
|
-
response = @api_operator.call_api(@stub_api_request)
|
181
|
-
|
182
|
-
response.should == digested_response
|
183
|
-
|
184
|
-
end
|
185
|
-
|
186
|
-
it "should throw an exception if response is not ok" do
|
187
|
-
|
188
|
-
failed_response = fake_api_response(false)
|
189
|
-
failed_response.stub!(:error_code).and_return(4000)
|
190
|
-
failed_response.stub!(:error_message).and_return("error")
|
191
|
-
@api_operator.should_receive(:make_http_request).and_return(fake_http_response)
|
192
|
-
@client.api_response_digestor.stub!(:from_http_response).and_return(failed_response)
|
193
|
-
|
194
|
-
running { @api_operator.call_api(@stub_api_request) }.should raise_error(Sevendigital::SevendigitalError) { |error|
|
195
|
-
error.error_code.should == 4000
|
196
|
-
error.error_message.should == "error"
|
197
|
-
}
|
198
|
-
|
199
|
-
end
|
200
|
-
|
201
|
-
it "should create a 2-legged signed HTTP request" do
|
202
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
203
|
-
api_request.require_signature
|
204
|
-
http_client, http_request = @api_operator.create_http_request(api_request)
|
205
|
-
http_client.use_ssl?.should == false
|
206
|
-
http_request.path.should =~ /api\/method/
|
207
|
-
http_request.path.should =~ /param1=value/
|
208
|
-
http_request["Authorization"].should =~ /OAuth oauth_consumer_key="oauth_consumer_key"/
|
209
|
-
http_request["Authorization"].should =~ / oauth_signature=/
|
210
|
-
# http_request["Authorization"].should !=~ / oauth_token="token"/
|
211
|
-
end
|
212
|
-
|
213
|
-
it "should create a 3-legged signed HTTP request" do
|
214
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
215
|
-
api_request.require_signature
|
216
|
-
api_request.token = OAuth::AccessToken.new(@client.oauth_consumer, "token", "secret")
|
217
|
-
http_client, http_request = @api_operator.create_http_request(api_request)
|
218
|
-
http_client.use_ssl?.should == false
|
219
|
-
http_request.path.should =~ /api\/method/
|
220
|
-
http_request.path.should =~ /param1=value/
|
221
|
-
http_request["Authorization"].should =~ /OAuth oauth_consumer_key="oauth_consumer_key"/
|
222
|
-
http_request["Authorization"].should =~ / oauth_signature=/
|
223
|
-
http_request["Authorization"].should =~ / oauth_token="token"/
|
224
|
-
end
|
225
|
-
|
226
|
-
it "should create a 3-legged signed HTTPS request" do
|
227
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
228
|
-
api_request.require_signature
|
229
|
-
api_request.require_secure_connection
|
230
|
-
api_request.token = OAuth::AccessToken.new(@client.oauth_consumer, "token", "secret")
|
231
|
-
http_client, http_request = @api_operator.create_http_request(api_request)
|
232
|
-
http_client.inspect.should =~ /base\.api\.url:443/
|
233
|
-
http_client.use_ssl?.should == true
|
234
|
-
http_client.verify_mode.should == OpenSSL::SSL::VERIFY_NONE
|
235
|
-
http_request.path.should =~ /api\/method/
|
236
|
-
http_request.path.should =~ /param1=value/
|
237
|
-
http_request["Authorization"].should =~ /OAuth oauth_consumer_key="oauth_consumer_key"/
|
238
|
-
http_request["Authorization"].should =~ / oauth_signature=/
|
239
|
-
http_request["Authorization"].should =~ / oauth_token="token"/
|
240
|
-
end
|
241
|
-
|
242
|
-
it "should create a standard HTTP request" do
|
243
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
244
|
-
http_client, http_request = @api_operator.create_http_request(api_request)
|
245
|
-
http_client.inspect.should =~ /base\.api\.url:80/
|
246
|
-
http_client.use_ssl?.should == false
|
247
|
-
http_request.path.should =~ /api\/method/
|
248
|
-
http_request.path.should =~ /param1=value/
|
249
|
-
http_request.path.should =~ /oauth_consumer_key=oauth_consumer_key/
|
250
|
-
end
|
251
|
-
|
252
|
-
it "should add custom user agent info to created HTTP request" do
|
253
|
-
|
254
|
-
@client.stub!(:user_agent_info).and_return("7digital Gem")
|
255
|
-
|
256
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
257
|
-
http_client, http_request = @api_operator.create_http_request(api_request)
|
258
|
-
http_request["User-agent"].should =~ /7digital Gem/
|
259
|
-
end
|
260
|
-
|
261
|
-
it "get_request_uri should return oauth sign URI if api request requires signature" do
|
262
|
-
|
263
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
264
|
-
api_request.token = OAuth::AccessToken.new(nil, "token", "token_secret")
|
265
|
-
api_request.require_signature
|
266
|
-
api_request.require_secure_connection
|
267
|
-
signed_uri = @api_operator.get_request_uri(api_request)
|
268
|
-
|
269
|
-
signed_uri.should =~ /oauth_signature=/
|
270
|
-
signed_uri.should =~ /oauth_consumer_key=oauth_consumer_key&/
|
271
|
-
signed_uri.should =~ /oauth_token=token&/
|
272
|
-
signed_uri.should =~ /https:\/\/base.api.url\/version\/api\/method/
|
273
|
-
end
|
274
|
-
|
275
|
-
it "get_request_uri should return simple request URI if api request does not require signature" do
|
276
|
-
|
277
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
278
|
-
signed_uri = @api_operator.get_request_uri(api_request)
|
279
|
-
|
280
|
-
signed_uri.match(/oauth_signature=/).should == nil
|
281
|
-
signed_uri.should =~ /oauth_consumer_key=oauth_consumer_key/
|
282
|
-
signed_uri.should =~ /http:\/\/base.api.url\/version\/api\/method/
|
283
|
-
end
|
284
|
-
|
285
|
-
it "get_request_uri should return secure request URI if api request does not require signature" do
|
286
|
-
|
287
|
-
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
288
|
-
api_request.require_secure_connection
|
289
|
-
signed_uri = @api_operator.get_request_uri(api_request)
|
290
|
-
|
291
|
-
signed_uri.match(/oauth_signature=/).should == nil
|
292
|
-
signed_uri.should =~ /oauth_consumer_key=oauth_consumer_key/
|
293
|
-
signed_uri.should =~ /https:\/\/base.api.url\/version\/api\/method/
|
294
|
-
end
|
295
|
-
|
296
|
-
def fake_http_response(code = 200, body = "response_body")
|
297
|
-
http_stub = stub('Net::HTTPResponse')
|
298
|
-
http_stub.stub!(:code => code, :body => body, :header => {})
|
299
|
-
http_stub
|
300
|
-
#Net::HTTP.new("1.1", code, body).tap {|r| puts r.inspect}
|
301
|
-
end
|
302
|
-
|
303
|
-
def fake_api_response(is_ok = true)
|
304
|
-
r = stub(Sevendigital::ApiResponse)
|
305
|
-
r.stub(:ok?).and_return(is_ok)
|
306
|
-
r
|
307
|
-
end
|
308
|
-
|
309
|
-
def stub_api_request
|
310
|
-
api_request = stub(Sevendigital::ApiRequest)
|
311
|
-
|
312
|
-
api_request.stub!(:parameters).and_return({})
|
313
|
-
api_request.stub!(:api_service).and_return(nil)
|
314
|
-
api_request.stub!(:api_method).and_return("m")
|
315
|
-
api_request.stub!(:requires_signature?).and_return(false)
|
316
|
-
api_request.stub!(:requires_secure_connection?).and_return(false)
|
317
|
-
api_request.stub!(:ensure_country_is_set)
|
318
|
-
return api_request
|
319
|
-
end
|
320
|
-
|
1
|
+
require "spec_helper"
|
2
|
+
require 'ostruct'
|
3
|
+
require 'oauth'
|
4
|
+
|
5
|
+
describe "ApiOperator" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
response_digestor = stub(Sevendigital::ApiResponseDigestor)
|
9
|
+
response_digestor.stub!(:from_http_response). and_return(fake_api_response)
|
10
|
+
|
11
|
+
stub_api_client(test_configuration, response_digestor)
|
12
|
+
|
13
|
+
Net::HTTP.stub!(:get_response).and_return(fake_http_response)
|
14
|
+
|
15
|
+
@api_operator = Sevendigital::ApiOperator.new(@client)
|
16
|
+
|
17
|
+
@stub_api_request = stub_api_request()
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should create http request uri based on api method and client configuration" do
|
22
|
+
|
23
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
24
|
+
|
25
|
+
uri = @api_operator.create_request_uri(api_request)
|
26
|
+
|
27
|
+
uri.kind_of?(URI).should == true
|
28
|
+
|
29
|
+
uri.to_s.should =~ /http:\/\/base.api.url\/version\/api\/method/
|
30
|
+
uri.to_s.should =~ /[\?\&]param1=value/
|
31
|
+
uri.to_s.should =~ /[\?\&]paramTwo=2/
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should URL encode parameters accourding to OAuth spec" do
|
36
|
+
|
37
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "a+b@c.d", :paramTwo => "<a b>"})
|
38
|
+
|
39
|
+
uri = @api_operator.create_request_uri(api_request)
|
40
|
+
|
41
|
+
uri.to_s.should =~ /[\?\&]param1=a%2Bb%40c.d/
|
42
|
+
uri.to_s.should =~ /[\?\&]paramTwo=%3Ca%20b%3E/
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should create http GET request" do
|
47
|
+
|
48
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
49
|
+
|
50
|
+
client, request = @api_operator.create_http_request(api_request)
|
51
|
+
|
52
|
+
request.class.should == Net::HTTP::Get
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should create http POST request" do
|
57
|
+
|
58
|
+
api_request = Sevendigital::ApiRequest.new(:POST, "api/method", {:param1 => "value", :paramTwo => 2})
|
59
|
+
|
60
|
+
client, request = @api_operator.create_http_request(api_request)
|
61
|
+
|
62
|
+
request.class.should == Net::HTTP::Post
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should create http DELETE request" do
|
67
|
+
|
68
|
+
api_request = Sevendigital::ApiRequest.new(:DELETE, "api/method", {:param1 => "value", :paramTwo => 2})
|
69
|
+
|
70
|
+
client, request = @api_operator.create_http_request(api_request)
|
71
|
+
|
72
|
+
request.class.should == Net::HTTP::Delete
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should create http PUT request" do
|
77
|
+
|
78
|
+
api_request = Sevendigital::ApiRequest.new(:PUT, "api/method", {:param1 => "value", :paramTwo => 2})
|
79
|
+
|
80
|
+
client, request = @api_operator.create_http_request(api_request)
|
81
|
+
|
82
|
+
request.class.should == Net::HTTP::Put
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should have empty body if no form parameters provided" do
|
87
|
+
|
88
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
89
|
+
|
90
|
+
client, request = @api_operator.create_http_request(api_request)
|
91
|
+
|
92
|
+
request.instance_variable_get("@body").empty?.should == true
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should set up any form parameters in body" do
|
97
|
+
|
98
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
99
|
+
api_request.form_parameters[:shop_id] = "1234"
|
100
|
+
api_request.form_parameters[:email] = "test+email@example.com"
|
101
|
+
api_request.form_parameters[:ignore] = nil
|
102
|
+
|
103
|
+
client, request = @api_operator.create_http_request(api_request)
|
104
|
+
|
105
|
+
#request.instance_variable_get("@body").should == "email=test%40example.com&shopId=1234"
|
106
|
+
request.instance_variable_get("@body").should =~ /email=test%2Bemail%40example.com/
|
107
|
+
request.instance_variable_get("@body").should =~ /shopId=1234/
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should create HTTPS request uri based on api method that requires secure connection and client configuration" do
|
111
|
+
|
112
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
113
|
+
api_request.require_secure_connection
|
114
|
+
|
115
|
+
uri = @api_operator.create_request_uri(api_request)
|
116
|
+
|
117
|
+
uri.kind_of?(URI).should == true
|
118
|
+
|
119
|
+
uri.to_s.should =~ /https:\/\/base.api.url\/version\/api\/method/
|
120
|
+
uri.to_s.should =~ /[\?\&]param1=value/
|
121
|
+
uri.to_s.should =~ /[\?\&]paramTwo=2/
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should create http request uri based on api method for non standard api service" do
|
126
|
+
|
127
|
+
@client.should_receive(:api_host_and_version).with(:media).and_return(["media-base.api.url","media-version"])
|
128
|
+
|
129
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
130
|
+
api_request.api_service = :media
|
131
|
+
uri = @api_operator.create_request_uri(api_request)
|
132
|
+
|
133
|
+
uri.kind_of?(URI).should == true
|
134
|
+
|
135
|
+
uri.to_s.should =~ /http:\/\/media-base.api.url\/media-version\/api\/method/
|
136
|
+
uri.to_s.should =~ /[\?\&]param1=value/
|
137
|
+
uri.to_s.should =~ /[\?\&]paramTwo=2/
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should make HTTP request and get http response" do
|
142
|
+
|
143
|
+
http_response = fake_http_response
|
144
|
+
|
145
|
+
@stub_http_request = stub(Net::HTTP::Get)
|
146
|
+
@stub_http_client = stub(Net::HTTP)
|
147
|
+
|
148
|
+
@api_operator.should_receive(:create_http_request).with(@stub_api_request).and_return([@stub_http_client, @stub_http_request])
|
149
|
+
|
150
|
+
@stub_http_client.should_receive(:request).with(@stub_http_request).and_return(http_response)
|
151
|
+
|
152
|
+
response = @api_operator.make_http_request(@stub_api_request)
|
153
|
+
|
154
|
+
response.should == http_response
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should digest the HTTP response and get it out" do
|
159
|
+
|
160
|
+
http_response = fake_http_response
|
161
|
+
digested_response = fake_api_response
|
162
|
+
|
163
|
+
@client.api_response_digestor.should_receive(:from_http_response).with(http_response).and_return(digested_response)
|
164
|
+
|
165
|
+
response = @api_operator.digest_http_response(http_response)
|
166
|
+
|
167
|
+
response.should == digested_response
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should call API by making an http request and digesting the response" do
|
172
|
+
|
173
|
+
http_response = fake_http_response
|
174
|
+
digested_response = fake_api_response
|
175
|
+
|
176
|
+
@api_operator.should_receive(:make_http_request).and_return(http_response)
|
177
|
+
|
178
|
+
@client.api_response_digestor.should_receive(:from_http_response).with(http_response).and_return(digested_response)
|
179
|
+
|
180
|
+
response = @api_operator.call_api(@stub_api_request)
|
181
|
+
|
182
|
+
response.should == digested_response
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should throw an exception if response is not ok" do
|
187
|
+
|
188
|
+
failed_response = fake_api_response(false)
|
189
|
+
failed_response.stub!(:error_code).and_return(4000)
|
190
|
+
failed_response.stub!(:error_message).and_return("error")
|
191
|
+
@api_operator.should_receive(:make_http_request).and_return(fake_http_response)
|
192
|
+
@client.api_response_digestor.stub!(:from_http_response).and_return(failed_response)
|
193
|
+
|
194
|
+
running { @api_operator.call_api(@stub_api_request) }.should raise_error(Sevendigital::SevendigitalError) { |error|
|
195
|
+
error.error_code.should == 4000
|
196
|
+
error.error_message.should == "error"
|
197
|
+
}
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
it "should create a 2-legged signed HTTP request" do
|
202
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
203
|
+
api_request.require_signature
|
204
|
+
http_client, http_request = @api_operator.create_http_request(api_request)
|
205
|
+
http_client.use_ssl?.should == false
|
206
|
+
http_request.path.should =~ /api\/method/
|
207
|
+
http_request.path.should =~ /param1=value/
|
208
|
+
http_request["Authorization"].should =~ /OAuth oauth_consumer_key="oauth_consumer_key"/
|
209
|
+
http_request["Authorization"].should =~ / oauth_signature=/
|
210
|
+
# http_request["Authorization"].should !=~ / oauth_token="token"/
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should create a 3-legged signed HTTP request" do
|
214
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
215
|
+
api_request.require_signature
|
216
|
+
api_request.token = OAuth::AccessToken.new(@client.oauth_consumer, "token", "secret")
|
217
|
+
http_client, http_request = @api_operator.create_http_request(api_request)
|
218
|
+
http_client.use_ssl?.should == false
|
219
|
+
http_request.path.should =~ /api\/method/
|
220
|
+
http_request.path.should =~ /param1=value/
|
221
|
+
http_request["Authorization"].should =~ /OAuth oauth_consumer_key="oauth_consumer_key"/
|
222
|
+
http_request["Authorization"].should =~ / oauth_signature=/
|
223
|
+
http_request["Authorization"].should =~ / oauth_token="token"/
|
224
|
+
end
|
225
|
+
|
226
|
+
it "should create a 3-legged signed HTTPS request" do
|
227
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
228
|
+
api_request.require_signature
|
229
|
+
api_request.require_secure_connection
|
230
|
+
api_request.token = OAuth::AccessToken.new(@client.oauth_consumer, "token", "secret")
|
231
|
+
http_client, http_request = @api_operator.create_http_request(api_request)
|
232
|
+
http_client.inspect.should =~ /base\.api\.url:443/
|
233
|
+
http_client.use_ssl?.should == true
|
234
|
+
http_client.verify_mode.should == OpenSSL::SSL::VERIFY_NONE
|
235
|
+
http_request.path.should =~ /api\/method/
|
236
|
+
http_request.path.should =~ /param1=value/
|
237
|
+
http_request["Authorization"].should =~ /OAuth oauth_consumer_key="oauth_consumer_key"/
|
238
|
+
http_request["Authorization"].should =~ / oauth_signature=/
|
239
|
+
http_request["Authorization"].should =~ / oauth_token="token"/
|
240
|
+
end
|
241
|
+
|
242
|
+
it "should create a standard HTTP request" do
|
243
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
244
|
+
http_client, http_request = @api_operator.create_http_request(api_request)
|
245
|
+
http_client.inspect.should =~ /base\.api\.url:80/
|
246
|
+
http_client.use_ssl?.should == false
|
247
|
+
http_request.path.should =~ /api\/method/
|
248
|
+
http_request.path.should =~ /param1=value/
|
249
|
+
http_request.path.should =~ /oauth_consumer_key=oauth_consumer_key/
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should add custom user agent info to created HTTP request" do
|
253
|
+
|
254
|
+
@client.stub!(:user_agent_info).and_return("7digital Gem")
|
255
|
+
|
256
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
257
|
+
http_client, http_request = @api_operator.create_http_request(api_request)
|
258
|
+
http_request["User-agent"].should =~ /7digital Gem/
|
259
|
+
end
|
260
|
+
|
261
|
+
it "get_request_uri should return oauth sign URI if api request requires signature" do
|
262
|
+
|
263
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
264
|
+
api_request.token = OAuth::AccessToken.new(nil, "token", "token_secret")
|
265
|
+
api_request.require_signature
|
266
|
+
api_request.require_secure_connection
|
267
|
+
signed_uri = @api_operator.get_request_uri(api_request)
|
268
|
+
|
269
|
+
signed_uri.should =~ /oauth_signature=/
|
270
|
+
signed_uri.should =~ /oauth_consumer_key=oauth_consumer_key&/
|
271
|
+
signed_uri.should =~ /oauth_token=token&/
|
272
|
+
signed_uri.should =~ /https:\/\/base.api.url\/version\/api\/method/
|
273
|
+
end
|
274
|
+
|
275
|
+
it "get_request_uri should return simple request URI if api request does not require signature" do
|
276
|
+
|
277
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
278
|
+
signed_uri = @api_operator.get_request_uri(api_request)
|
279
|
+
|
280
|
+
signed_uri.match(/oauth_signature=/).should == nil
|
281
|
+
signed_uri.should =~ /oauth_consumer_key=oauth_consumer_key/
|
282
|
+
signed_uri.should =~ /http:\/\/base.api.url\/version\/api\/method/
|
283
|
+
end
|
284
|
+
|
285
|
+
it "get_request_uri should return secure request URI if api request does not require signature" do
|
286
|
+
|
287
|
+
api_request = Sevendigital::ApiRequest.new(:GET, "api/method", {:param1 => "value", :paramTwo => 2})
|
288
|
+
api_request.require_secure_connection
|
289
|
+
signed_uri = @api_operator.get_request_uri(api_request)
|
290
|
+
|
291
|
+
signed_uri.match(/oauth_signature=/).should == nil
|
292
|
+
signed_uri.should =~ /oauth_consumer_key=oauth_consumer_key/
|
293
|
+
signed_uri.should =~ /https:\/\/base.api.url\/version\/api\/method/
|
294
|
+
end
|
295
|
+
|
296
|
+
def fake_http_response(code = 200, body = "response_body")
|
297
|
+
http_stub = stub('Net::HTTPResponse')
|
298
|
+
http_stub.stub!(:code => code, :body => body, :header => {})
|
299
|
+
http_stub
|
300
|
+
#Net::HTTP.new("1.1", code, body).tap {|r| puts r.inspect}
|
301
|
+
end
|
302
|
+
|
303
|
+
def fake_api_response(is_ok = true)
|
304
|
+
r = stub(Sevendigital::ApiResponse)
|
305
|
+
r.stub(:ok?).and_return(is_ok)
|
306
|
+
r
|
307
|
+
end
|
308
|
+
|
309
|
+
def stub_api_request
|
310
|
+
api_request = stub(Sevendigital::ApiRequest)
|
311
|
+
|
312
|
+
api_request.stub!(:parameters).and_return({})
|
313
|
+
api_request.stub!(:api_service).and_return(nil)
|
314
|
+
api_request.stub!(:api_method).and_return("m")
|
315
|
+
api_request.stub!(:requires_signature?).and_return(false)
|
316
|
+
api_request.stub!(:requires_secure_connection?).and_return(false)
|
317
|
+
api_request.stub!(:ensure_country_is_set)
|
318
|
+
return api_request
|
319
|
+
end
|
320
|
+
|
321
321
|
end
|