forked from dbalatero/monster_mash
/
base_spec.rb
272 lines (234 loc) · 7 KB
/
base_spec.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
require File.dirname(__FILE__) + '/../spec_helper'
class MockApi < MonsterMash::Base
def self.test_class_method
"testvalue"
end
end
class CustomMockError < StandardError; end
class A < MonsterMash::Base
defaults do
cache_timeout 9999
timeout 500
end
end
class B < A
end
class C < A
defaults do
cache_timeout 100
end
end
describe MonsterMash::Base do
before(:all) do
@hydra = Typhoeus::Hydra.new
end
describe "inheriting defaults from superclasses" do
it "should propagate defaults to B" do
B.defaults.should == A.defaults
B.defaults.should have(1).thing
end
it "should allow override of defaults by C" do
C.defaults.should_not == A.defaults
C.defaults.should have(2).things
end
end
describe "#self.defaults" do
it "should default to empty array" do
MockApi.defaults.should == []
end
it "should save a defaults proc in the class" do
defaults_block = lambda { nil }
MockApi.defaults(&defaults_block)
MockApi.defaults.should == [defaults_block]
MonsterMash::Base.defaults.should be_empty
end
end
describe "#get" do
it "should proxy to build_method" do
MockApi.should_receive(:build_method).
with(:get, :method_name)
MockApi.get(:method_name) { nil }
end
end
describe "#post" do
it "should proxy to build_method" do
MockApi.should_receive(:build_method).
with(:post, :method_name)
MockApi.post(:method_name) { nil }
end
end
describe "#put" do
it "should proxy to build_method" do
MockApi.should_receive(:build_method).
with(:put, :method_name)
MockApi.put(:method_name) { nil }
end
end
describe "#check_response_and_raise!" do
before(:each) do
@response = mock('response',
:body => 'response body')
end
it "should raise if a response has a code in the wrong range" do
bad_codes = [0, 404, 500, 403, 410, 400]
bad_codes.each do |code|
@response.stub!(:code).and_return(code)
lambda {
MonsterMash::Base.check_response_and_raise!(@response)
}.should raise_error(MonsterMash::HTTPError)
end
end
it "should not raise if a response has good codes" do
good_codes = [200, 204, 301, 302]
good_codes.each do |code|
@response.stub!(:code).and_return(code)
lambda {
MonsterMash::Base.check_response_and_raise!(@response)
}.should_not raise_error(MonsterMash::HTTPError)
end
end
it "should propagate the response object with the error" do
@response.stub!(:code).and_return(400)
error = nil
begin
MonsterMash::Base.check_response_and_raise!(@response)
rescue => e
error = e
end
error.response.should == @response
end
end
describe "#delete" do
it "should proxy to build_method" do
MockApi.should_receive(:build_method).
with(:delete, :method_name)
MockApi.delete(:method_name) { nil }
end
end
describe "#self.build_method" do
before(:all) do
unless MockApi.respond_to?(:my_method)
MockApi.build_method(:get, :my_method) do
uri 'http://google.com'
end
end
end
it "should create an instance (async parallel HTTP) method" do
api = MockApi.new(@hydra)
api.should respond_to(:my_method)
end
it "should create a class (sync serial HTTP) method" do
MockApi.should respond_to(:my_method)
end
it "should raise an error if the name is in use" do
lambda {
MockApi.build_method(:get, :my_method) { nil }
}.should raise_error(ArgumentError)
end
describe "checking validity" do
it "should raise errors if the request is not valid" do
api = MockApi.new(@hydra)
lambda {
api.my_method
}.should raise_error(MonsterMash::InvalidRequest)
end
end
context "error propagation" do
before(:all) do
MockApi.build_method(:get, :google_json2) do |search|
uri 'http://ajax.googleapis.com/ajax/services/search/web'
params({
'v' => '1.0',
'q' => search,
'rsz' => 'large'
})
cache_timeout 999999
handler do |response|
raise CustomMockError, "my error"
end
end
end
use_vcr_cassette 'google/error_propagation', :record => :new_episodes
it "should raise an error in a serial request" do
lambda {
MockApi.google_json2('david balatero')
}.should raise_error(CustomMockError)
end
it "should propagate the error to the block in parallel request" do
api = MockApi.new(@hydra)
propagated_error = nil
api.google_json2('david balatero') do |urls, error|
propagated_error = error
end
@hydra.run
propagated_error.should be_an_instance_of(CustomMockError)
end
end
describe "delegation to the request class" do
before(:all) do
MockApi.build_method(:get, :google_json_delegation) do |search|
uri 'http://ajax.googleapis.com/ajax/services/search/web'
params({
'v' => '1.0',
'q' => search,
'rsz' => 'large'
})
handler do |response|
test_class_method
end
end
end
use_vcr_cassette 'google/delegation', :record => :new_episodes
it "should allow calling class methods of the request class in serial" do
result = MockApi.google_json_delegation('balatero')
result.should == 'testvalue'
end
it "should allow calling class methods of the request class in parallel" do
saved_result = nil
api = MockApi.new(@hydra)
api.google_json_delegation('balatero') do |result, error|
if !error
saved_result = result
end
end
@hydra.run
saved_result.should == 'testvalue'
end
end
describe "a valid method" do
before(:all) do
MockApi.build_method(:get, :google_json) do |search|
uri 'http://ajax.googleapis.com/ajax/services/search/web'
params({
'v' => '1.0',
'q' => search,
'rsz' => 'large'
})
cache_timeout 999999
handler do |response|
json = JSON.parse(response.body)
json['responseData']['results'].map do |result|
result['url']
end
end
end
end
use_vcr_cassette 'google/valid', :record => :new_episodes
it "should do a serial query correctly" do
saved_urls = MockApi.google_json('balatero')
saved_urls.should have(8).things
end
it "should do a query correctly" do
saved_urls = nil
api = MockApi.new(@hydra)
api.google_json('balatero') do |urls, error|
if !error
saved_urls = urls
end
end
@hydra.run
saved_urls.should have(8).things
end
end
end
end