-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstub_spec.rb
284 lines (228 loc) · 8.17 KB
/
stub_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
273
274
275
276
277
278
279
280
281
282
283
284
describe WebStub::Stub do
before do
@stub = WebStub::Stub.new(:get, "http://www.yahoo.com/")
end
it "holds the method and the path" do
@stub.should.not.be.nil
end
it "allows all valid HTTP methods" do
WebStub::Stub::METHODS.each do |method|
lambda { WebStub::Stub.new(method, "http://www.yahoo.com/") }.should.not.raise(ArgumentError)
end
end
it "does not allow invalid HTTP methods" do
lambda { WebStub::Stub.new("invalid", "http://www.yahoo.com/") }.should.raise(ArgumentError)
end
it "does not have a response error by default" do
@stub.response_error.should.be.nil
end
describe "#error?" do
describe "by default" do
it "returns false" do
@stub.error?.should.be.false
end
end
describe "after calling to_fail" do
it "returns true" do
@stub.to_fail(code: NSURLErrorUnsupportedURL)
@stub.error?.should.be.true
end
end
end
describe "callback" do
before do
@called = 0
@stub.with_callback do |headers,body|
@called += 1
headers.should == "headers"
body.should == "body"
end
end
it "calls the callback method" do
@stub.callback.should.not.be.nil
end
it "should call the callback" do
@stub.do_callback("headers", "body")
@called.should == 1
end
end
describe "#matches?" do
it "returns true when provided an identical stub" do
@stub.matches?(:get, "http://www.yahoo.com/").should.be.true
end
it "returns false when the URL differs" do
@stub.matches?(:get, "http://www.google.com/").should.be.false
end
it "returns false when the method differs" do
@stub.matches?(:post, "http://www.yahoo.com/").should.be.false
end
describe "canonicalizing URLs" do
it "lowercases the scheme" do
@stub.matches?(:get, "HTTP://www.yahoo.com/").should.be.true
end
it "lowercases the hostname" do
@stub.matches?(:get, "http://WWW.YAHOo.COM/").should.be.true
end
it "ignores default HTTP port" do
@stub.matches?(:get, "http://www.yahoo.com:80/").should.be.true
end
it "ignores default HTTPS port" do
stub = WebStub::Stub.new(:get, "https://www.yahoo.com/")
stub.matches?(:get, "https://www.yahoo.com:443/").should.be.true
end
it "ignores a path with \"/\"" do
@stub.matches?(:get, "http://www.yahoo.com:80/").should.be.true
end
it 'strips any trailing ?' do
@stub.matches?(:get, "http://www.yahoo.com/?").should.be.true
end
it 'strips any trailing #' do
@stub.matches?(:get, "http://www.yahoo.com/#").should.be.true
end
end
describe "query string" do
before do
@stub = WebStub::Stub.new(:get, "http://www.yahoo.com/search?count=1&q=whiskey")
end
it "returns true when the query string matches" do
@stub.matches?(:get, "http://www.yahoo.com/search?count=1&q=whiskey").should.be.true
end
it "strips any trailing &" do
@stub.matches?(:get, "http://www.yahoo.com/search?count=1&q=whiskey&").should.be.true
end
it "does not depend on the order of query parameters" do
@stub.matches?(:get, "http://www.yahoo.com/search?q=whiskey&count=1").should.be.true
end
end
describe "body" do
describe "with a dictionary" do
before do
@stub = WebStub::Stub.new(:post, "http://www.yahoo.com/search").
with(body: { :q => "query"})
end
it "returns false when the body does not match" do
@stub.matches?(:post, "http://www.yahoo.com/search", { :body => {}}).should.be.false
end
it "returns true when the body matches (with string keys)" do
@stub.matches?(:post, "http://www.yahoo.com/search", { :body => { "q" => "query" }}).should.be.true
end
end
describe "with a string" do
before do
@stub = WebStub::Stub.new(:post, "http://www.yahoo.com/search").
with(body: "raw body")
end
it "returns true when an identical body is provided" do
@stub.matches?(:post, "http://www.yahoo.com/search", { :body => "raw body" }).should.be.true
end
it "returns false when a dictionary is provided" do
@stub.matches?(:post, "http://www.yahoo.com/search", { :body => { "q" => "query" }}).should.be.false
end
it "returns false without a body" do
@stub.matches?(:post, "http://www.yahoo.com/search").should.be.false
end
end
end
describe "headers" do
before do
@stub = WebStub::Stub.new(:get, "http://www.yahoo.com/search").
with(headers: { "Authorization" => "secret" })
end
it "returns true when the headers are included" do
@stub.matches?(:get, "http://www.yahoo.com/search",
headers: { "X-Extra" => "42", "Authorization" => "secret" }).should.be.true
end
it "returns false when any of the headers are absent" do
@stub.matches?(:get, "http://www.yahoo.com/search",
headers: { "X-Extra" => "42" }).should.be.false
end
end
end
describe "#requested?" do
describe "by default" do
it "returns false" do
@stub.should.not.be.requested
end
end
describe "after incrementing the request count" do
before do
@stub.requests += 1
end
it "returns true" do
@stub.should.be.requested
end
end
end
describe "#response_body" do
it "returns the response body" do
@stub.response_body.should == ""
end
end
describe "#to_fail" do
it "rejects an empty options Hash" do
lambda { @stub.to_fail({}) }.should.raise(ArgumentError)
end
it "builds an NSError using the option specified by code" do
@stub.to_fail(code: NSURLErrorUnsupportedURL)
@stub.response_error.domain.should == NSURLErrorDomain
@stub.response_error.code.should == NSURLErrorUnsupportedURL
end
it "accepts an arbitrary NSError using the error option" do
error = NSError.errorWithDomain(0, code: 123, userInfo: nil)
@stub.to_fail(error: error)
@stub.response_error.should == error
end
it "returns self" do
@stub.to_fail(code: 123).should == @stub
end
end
describe "#to_redirect" do
it "requires the :url option" do
lambda { @stub.to_redirect }.should.raise(ArgumentError)
end
it "sets the Location header to the specified URL" do
@stub.to_redirect(url: "http://example.org/")
@stub.response_headers.should.include("Location")
end
it "sets the status code to 301" do
@stub.to_redirect(url: "http://example.org/")
@stub.response_status_code.should == 301
end
it "returns the stub" do
@stub.to_redirect(url: "http://example.org/").should == @stub
end
end
describe "#to_return" do
it "sets the response body" do
@stub.to_return(body: "hello")
@stub.response_body.should.be == "hello"
end
it "allows JSON results by passing :json with a string" do
@stub.to_return(json: '{"value":42}')
@stub.response_headers["Content-Type"].should == "application/json"
@stub.response_body.should == '{"value":42}'
end
it "allows JSON results by passing :json with a hash" do
@stub.to_return(json: {:value => 42})
@stub.response_headers["Content-Type"].should == "application/json"
@stub.response_body.should == '{"value":42}'
end
it "allows JSON results by passing :json with an array" do
@stub.to_return(json: [{:value => 42}])
@stub.response_headers["Content-Type"].should == "application/json"
@stub.response_body.should == '[{"value":42}]'
end
it "sets a delay time" do
@stub.to_return(body: "{}", delay: 0.5)
@stub.response_delay.should == 0.5
end
it "sets response headers" do
@stub.to_return(body: "{}", headers: { "Content-Type" => "application/json" })
@stub.response_headers.should.be == { "Content-Type" => "application/json" }
end
it "sets the response status code" do
@stub.to_return(body: "{}", status_code: 400)
@stub.response_status_code.should.be == 400
end
end
end