josh-rack-test 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,368 @@
1
+ require "spec_helper"
2
+
3
+ describe Rack::Test::Session do
4
+ describe "initialization" do
5
+ it "supports being initialized with a Rack::MockSession app" do
6
+ session = Rack::Test::Session.new(Rack::MockSession.new(app))
7
+ session.request("/").should be_ok
8
+ end
9
+
10
+ it "supports being initialized with an app" do
11
+ session = Rack::Test::Session.new(app)
12
+ session.request("/").should be_ok
13
+ end
14
+ end
15
+
16
+ describe "#request" do
17
+ it "requests the URI using GET by default" do
18
+ request "/"
19
+ last_request.should be_get
20
+ last_response.should be_ok
21
+ end
22
+
23
+ it "returns a response" do
24
+ request("/").should be_ok
25
+ end
26
+
27
+ it "uses the provided env" do
28
+ request "/", "X-Foo" => "bar"
29
+ last_request.env["X-Foo"].should == "bar"
30
+ end
31
+
32
+ it "defaults to GET" do
33
+ request "/"
34
+ last_request.env["REQUEST_METHOD"].should == "GET"
35
+ end
36
+
37
+ it "defaults the REMOTE_ADDR to 127.0.0.1" do
38
+ request "/"
39
+ last_request.env["REMOTE_ADDR"].should == "127.0.0.1"
40
+ end
41
+
42
+ it "sets rack.test to true in the env" do
43
+ request "/"
44
+ last_request.env["rack.test"].should == true
45
+ end
46
+
47
+ it "defaults to port 80" do
48
+ request "/"
49
+ last_request.env["SERVER_PORT"].should == "80"
50
+ end
51
+
52
+ it "defaults to example.org" do
53
+ request "/"
54
+ last_request.env["SERVER_NAME"].should == "example.org"
55
+ end
56
+
57
+ it "yields the response to a given block" do
58
+ request "/" do |response|
59
+ response.should be_ok
60
+ end
61
+ end
62
+
63
+ it "supports sending :params" do
64
+ request "/", :params => { "foo" => "bar" }
65
+ last_request.GET["foo"].should == "bar"
66
+ end
67
+
68
+ it "doesn't follow redirects by default" do
69
+ request "/redirect"
70
+ last_response.should be_redirect
71
+ last_response.body.should be_empty
72
+ end
73
+
74
+ it "stringify and upcases method" do
75
+ request "/", :method => :post, :input => "foo"
76
+ last_request.env["rack.input"].read.should == "foo"
77
+ end
78
+
79
+ context "when input is given" do
80
+ it "should send the input" do
81
+ request "/", :method => "POST", :input => "foo"
82
+ last_request.env["rack.input"].read.should == "foo"
83
+ end
84
+
85
+ it "should not send a multipart request" do
86
+ request "/", :method => "POST", :input => "foo"
87
+ last_request.env["CONTENT_TYPE"].should_not == "application/x-www-form-urlencoded"
88
+ end
89
+ end
90
+
91
+ context "for a POST specified with :method" do
92
+ it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
93
+ request "/", :method => "POST"
94
+ last_request.env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
95
+ end
96
+ end
97
+
98
+ context "for a POST specified with REQUEST_METHOD" do
99
+ it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
100
+ request "/", "REQUEST_METHOD" => "POST"
101
+ last_request.env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
102
+ end
103
+ end
104
+
105
+ context "when CONTENT_TYPE is specified in the env" do
106
+ it "does not overwrite the CONTENT_TYPE" do
107
+ request "/", "CONTENT_TYPE" => "application/xml"
108
+ last_request.env["CONTENT_TYPE"].should == "application/xml"
109
+ end
110
+ end
111
+
112
+ context "when the URL is https://" do
113
+ it "sets SERVER_PORT to 443" do
114
+ get "https://example.org/"
115
+ last_request.env["SERVER_PORT"].should == "443"
116
+ end
117
+
118
+ it "sets HTTPS to on" do
119
+ get "https://example.org/"
120
+ last_request.env["HTTPS"].should == "on"
121
+ end
122
+ end
123
+
124
+ context "for a XHR" do
125
+ it "sends XMLHttpRequest for the X-Requested-With header" do
126
+ request "/", :xhr => true
127
+ last_request.env["X-Requested-With"].should == "XMLHttpRequest"
128
+ end
129
+ end
130
+ end
131
+
132
+ describe "#header" do
133
+ it "sets a header to be sent with requests" do
134
+ header "User-Agent", "Firefox"
135
+ request "/"
136
+
137
+ last_request.env["User-Agent"].should == "Firefox"
138
+ end
139
+
140
+ it "persists across multiple requests" do
141
+ header "User-Agent", "Firefox"
142
+ request "/"
143
+ request "/"
144
+
145
+ last_request.env["User-Agent"].should == "Firefox"
146
+ end
147
+
148
+ it "overwrites previously set headers" do
149
+ header "User-Agent", "Firefox"
150
+ header "User-Agent", "Safari"
151
+ request "/"
152
+
153
+ last_request.env["User-Agent"].should == "Safari"
154
+ end
155
+
156
+ it "can be used to clear a header" do
157
+ header "User-Agent", "Firefox"
158
+ header "User-Agent", nil
159
+ request "/"
160
+
161
+ last_request.env.should_not have_key("User-Agent")
162
+ end
163
+
164
+ it "is overridden by headers sent during the request" do
165
+ header "User-Agent", "Firefox"
166
+ request "/", "User-Agent" => "Safari"
167
+
168
+ last_request.env["User-Agent"].should == "Safari"
169
+ end
170
+ end
171
+
172
+ describe "#authorize" do
173
+ it "sets the HTTP_AUTHORIZATION header" do
174
+ authorize "bryan", "secret"
175
+ request "/"
176
+
177
+ last_request.env["HTTP_AUTHORIZATION"].should == "Basic YnJ5YW46c2VjcmV0\n"
178
+ end
179
+
180
+ it "includes the header for subsequent requests" do
181
+ basic_authorize "bryan", "secret"
182
+ request "/"
183
+ request "/"
184
+
185
+ last_request.env["HTTP_AUTHORIZATION"].should == "Basic YnJ5YW46c2VjcmV0\n"
186
+ end
187
+ end
188
+
189
+ describe "follow_redirect!" do
190
+ it "follows redirects" do
191
+ get "/redirect"
192
+ follow_redirect!
193
+
194
+ last_response.should_not be_redirect
195
+ last_response.body.should == "You've been redirected"
196
+ end
197
+
198
+ it "does not include params when following the redirect" do
199
+ get "/redirect", { "foo" => "bar" }
200
+ follow_redirect!
201
+
202
+ last_request.GET.should == {}
203
+ end
204
+
205
+ it "raises an error if the last_response is not set" do
206
+ lambda {
207
+ follow_redirect!
208
+ }.should raise_error(Rack::Test::Error)
209
+ end
210
+
211
+ it "raises an error if the last_response is not a redirect" do
212
+ get "/"
213
+
214
+ lambda {
215
+ follow_redirect!
216
+ }.should raise_error(Rack::Test::Error)
217
+ end
218
+ end
219
+
220
+ describe "#last_request" do
221
+ it "returns the most recent request" do
222
+ request "/"
223
+ last_request.env["PATH_INFO"].should == "/"
224
+ end
225
+
226
+ it "raises an error if no requests have been issued" do
227
+ lambda {
228
+ last_request
229
+ }.should raise_error(Rack::Test::Error)
230
+ end
231
+ end
232
+
233
+ describe "#last_response" do
234
+ it "returns the most recent response" do
235
+ request "/"
236
+ last_response["Content-Type"].should == "text/html"
237
+ end
238
+
239
+ it "raises an error if no requests have been issued" do
240
+ lambda {
241
+ last_response
242
+ }.should raise_error
243
+ end
244
+ end
245
+
246
+ describe "after_request" do
247
+ it "runs callbacks after each request" do
248
+ ran = false
249
+
250
+ rack_mock_session.after_request do
251
+ ran = true
252
+ end
253
+
254
+ get "/"
255
+ ran.should == true
256
+ end
257
+
258
+ it "runs multiple callbacks" do
259
+ count = 0
260
+
261
+ 2.times do
262
+ rack_mock_session.after_request do
263
+ count += 1
264
+ end
265
+ end
266
+
267
+ get "/"
268
+ count.should == 2
269
+ end
270
+ end
271
+
272
+ describe "#get" do
273
+ it_should_behave_like "any #verb methods"
274
+
275
+ def verb
276
+ "get"
277
+ end
278
+
279
+ it "uses the provided params hash" do
280
+ get "/", :foo => "bar"
281
+ last_request.GET.should == { "foo" => "bar" }
282
+ end
283
+
284
+ it "sends params with parens in names" do
285
+ get "/", "foo(1i)" => "bar"
286
+ last_request.GET["foo(1i)"].should == "bar"
287
+ end
288
+
289
+ it "supports params with encoding sensitive names" do
290
+ get "/", "foo bar" => "baz"
291
+ last_request.GET["foo bar"].should == "baz"
292
+ end
293
+
294
+ it "supports params with nested encoding sensitive names" do
295
+ get "/", "boo" => {"foo bar" => "baz"}
296
+ last_request.GET.should == {"boo" => {"foo bar" => "baz"}}
297
+ end
298
+
299
+ it "accepts params in the path" do
300
+ get "/?foo=bar"
301
+ last_request.GET.should == { "foo" => "bar" }
302
+ end
303
+ end
304
+
305
+ describe "#head" do
306
+ it_should_behave_like "any #verb methods"
307
+
308
+ def verb
309
+ "head"
310
+ end
311
+ end
312
+
313
+ describe "#post" do
314
+ it_should_behave_like "any #verb methods"
315
+
316
+ def verb
317
+ "post"
318
+ end
319
+
320
+ it "uses the provided params hash" do
321
+ post "/", :foo => "bar"
322
+ last_request.POST.should == { "foo" => "bar" }
323
+ end
324
+
325
+ it "supports params with encoding sensitive names" do
326
+ post "/", "foo bar" => "baz"
327
+ last_request.POST["foo bar"].should == "baz"
328
+ end
329
+
330
+ it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
331
+ post "/"
332
+ last_request.env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
333
+ end
334
+
335
+ it "accepts a body" do
336
+ post "/", "Lobsterlicious!"
337
+ last_request.body.read.should == "Lobsterlicious!"
338
+ end
339
+
340
+ context "when CONTENT_TYPE is specified in the env" do
341
+ it "does not overwrite the CONTENT_TYPE" do
342
+ post "/", {}, { "CONTENT_TYPE" => "application/xml" }
343
+ last_request.env["CONTENT_TYPE"].should == "application/xml"
344
+ end
345
+ end
346
+ end
347
+
348
+ describe "#put" do
349
+ it_should_behave_like "any #verb methods"
350
+
351
+ def verb
352
+ "put"
353
+ end
354
+
355
+ it "accepts a body" do
356
+ put "/", "Lobsterlicious!"
357
+ last_request.body.read.should == "Lobsterlicious!"
358
+ end
359
+ end
360
+
361
+ describe "#delete" do
362
+ it_should_behave_like "any #verb methods"
363
+
364
+ def verb
365
+ "delete"
366
+ end
367
+ end
368
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1 @@
1
+ -x gems,spec
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,48 @@
1
+ require "rubygems"
2
+ require "spec"
3
+
4
+ gem "rack", "~> 1.0.0"
5
+
6
+ require "rack/test"
7
+ require File.dirname(__FILE__) + "/fixtures/fake_app"
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.include Rack::Test::Methods
11
+
12
+ def app
13
+ Rack::Lint.new(Rack::Test::FakeApp.new)
14
+ end
15
+
16
+ end
17
+
18
+ describe "any #verb methods", :shared => true do
19
+ it "requests the URL using VERB" do
20
+ send(verb, "/")
21
+
22
+ last_request.env["REQUEST_METHOD"].should == verb.upcase
23
+ last_response.should be_ok
24
+ end
25
+
26
+ it "uses the provided env" do
27
+ send(verb, "/", {}, { "User-Agent" => "Rack::Test" })
28
+ last_request.env["User-Agent"].should == "Rack::Test"
29
+ end
30
+
31
+ it "yields the response to a given block" do
32
+ yielded = false
33
+
34
+ send(verb, "/") do |response|
35
+ response.should be_ok
36
+ yielded = true
37
+ end
38
+
39
+ yielded.should be_true
40
+ end
41
+
42
+ context "for a XHR" do
43
+ it "sends XMLHttpRequest for the X-Requested-With header" do
44
+ send(verb, "/", {}, { :xhr => true })
45
+ last_request.env["X-Requested-With"].should == "XMLHttpRequest"
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: josh-rack-test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.1
5
+ platform: ruby
6
+ authors:
7
+ - Bryan Helmkamp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: bryan@brynary.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - MIT-LICENSE.txt
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - History.txt
29
+ - MIT-LICENSE.txt
30
+ - README.rdoc
31
+ - Rakefile
32
+ - lib/rack/mock_session.rb
33
+ - lib/rack/test.rb
34
+ - lib/rack/test/cookie_jar.rb
35
+ - lib/rack/test/methods.rb
36
+ - lib/rack/test/mock_digest_request.rb
37
+ - lib/rack/test/uploaded_file.rb
38
+ - lib/rack/test/utils.rb
39
+ - rack-test.gemspec
40
+ - spec/fixtures/config.ru
41
+ - spec/fixtures/fake_app.rb
42
+ - spec/fixtures/foo.txt
43
+ - spec/rack/test/cookie_spec.rb
44
+ - spec/rack/test/digest_auth_spec.rb
45
+ - spec/rack/test/multipart_spec.rb
46
+ - spec/rack/test/utils_spec.rb
47
+ - spec/rack/test_spec.rb
48
+ - spec/rcov.opts
49
+ - spec/spec.opts
50
+ - spec/spec_helper.rb
51
+ has_rdoc: false
52
+ homepage: http://github.com/brynary/rack-test
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project: rack-test
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Simple testing API built on Rack
77
+ test_files:
78
+ - spec/fixtures/fake_app.rb
79
+ - spec/rack/test/cookie_spec.rb
80
+ - spec/rack/test/digest_auth_spec.rb
81
+ - spec/rack/test/multipart_spec.rb
82
+ - spec/rack/test/utils_spec.rb
83
+ - spec/rack/test_spec.rb
84
+ - spec/spec_helper.rb