rack-test 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,46 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Rack::Test::Session do
4
- context "HTTP Digest authentication" do
5
-
6
- def app
7
- app = Rack::Auth::Digest::MD5.new(Rack::Test::FakeApp.new) do |username|
8
- { 'alice' => 'correct-password' }[username]
9
- end
10
- app.realm = 'WallysWorld'
11
- app.opaque = 'this-should-be-secret'
12
- app
13
- end
14
-
15
- it 'incorrectly authenticates GETs' do
16
- digest_authorize 'foo', 'bar'
17
- get '/'
18
- last_response.should be_challenge
19
- end
20
-
21
- it "correctly authenticates GETs" do
22
- digest_authorize "alice", "correct-password"
23
- response = get "/"
24
- response.should be_ok
25
- end
26
-
27
- it "correctly authenticates GETs with params" do
28
- digest_authorize "alice", "correct-password"
29
- response = get "/", "foo" => "bar"
30
- response.should be_ok
31
- end
32
-
33
- it "correctly authenticates POSTs" do
34
- digest_authorize "alice", "correct-password"
35
- response = post "/"
36
- response.should be_ok
37
- end
38
-
39
- it "returns a re-challenge if authenticating incorrectly" do
40
- digest_authorize "alice", "incorrect-password"
41
- response = get "/"
42
- response.should be_challenge
43
- end
44
-
45
- end
46
- end
@@ -1,145 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require "spec_helper"
4
-
5
- describe Rack::Test::Session do
6
-
7
- def test_file_path
8
- File.dirname(__FILE__) + "/../../fixtures/foo.txt"
9
- end
10
-
11
- def second_test_file_path
12
- File.dirname(__FILE__) + "/../../fixtures/bar.txt"
13
- end
14
-
15
- def uploaded_file
16
- Rack::Test::UploadedFile.new(test_file_path)
17
- end
18
-
19
- def second_uploaded_file
20
- Rack::Test::UploadedFile.new(second_test_file_path)
21
- end
22
-
23
- context "uploading a file" do
24
- it "sends the multipart/form-data content type" do
25
- post "/", "photo" => uploaded_file
26
- last_request.env["CONTENT_TYPE"].should include("multipart/form-data;")
27
- end
28
-
29
- it "sends regular params" do
30
- post "/", "photo" => uploaded_file, "foo" => "bar"
31
- last_request.POST["foo"].should == "bar"
32
- end
33
-
34
- it "sends nested params" do
35
- post "/", "photo" => uploaded_file, "foo" => {"bar" => "baz"}
36
- last_request.POST["foo"]["bar"].should == "baz"
37
- end
38
-
39
- it "sends multiple nested params" do
40
- post "/", "photo" => uploaded_file, "foo" => {"bar" => {"baz" => "bop"}}
41
- last_request.POST["foo"]["bar"]["baz"].should == "bop"
42
- end
43
-
44
- it "sends params with arrays" do
45
- post "/", "photo" => uploaded_file, "foo" => ["1", "2"]
46
- last_request.POST["foo"].should == ["1", "2"]
47
- end
48
-
49
- it "sends params with encoding sensitive values" do
50
- post "/", "photo" => uploaded_file, "foo" => "bar? baz"
51
- last_request.POST["foo"].should == "bar? baz"
52
- end
53
-
54
- it "sends params encoded as ISO-8859-1" do
55
- post "/", "photo" => uploaded_file, "foo" => "bar", "utf8" => "☃"
56
- last_request.POST["foo"].should == "bar"
57
-
58
- if Rack::Test.encoding_aware_strings?
59
- last_request.POST["utf8"].should == "☃"
60
- else
61
- last_request.POST["utf8"].should == "\xE2\x98\x83"
62
- end
63
- end
64
-
65
- it "sends params with parens in names" do
66
- post "/", "photo" => uploaded_file, "foo(1i)" => "bar"
67
- last_request.POST["foo(1i)"].should == "bar"
68
- end
69
-
70
- it "sends params with encoding sensitive names" do
71
- post "/", "photo" => uploaded_file, "foo bar" => "baz"
72
- last_request.POST["foo bar"].should == "baz"
73
- end
74
-
75
- it "sends files with the filename" do
76
- post "/", "photo" => uploaded_file
77
- last_request.POST["photo"][:filename].should == "foo.txt"
78
- end
79
-
80
- it "sends files with the text/plain MIME type by default" do
81
- post "/", "photo" => uploaded_file
82
- last_request.POST["photo"][:type].should == "text/plain"
83
- end
84
-
85
- it "sends files with the right name" do
86
- post "/", "photo" => uploaded_file
87
- last_request.POST["photo"][:name].should == "photo"
88
- end
89
-
90
- it "allows overriding the content type" do
91
- post "/", "photo" => Rack::Test::UploadedFile.new(test_file_path, "image/jpeg")
92
- last_request.POST["photo"][:type].should == "image/jpeg"
93
- end
94
-
95
- it "sends files with a Content-Length in the header" do
96
- post "/", "photo" => uploaded_file
97
- last_request.POST["photo"][:head].should include("Content-Length: 4")
98
- end
99
-
100
- it "sends files as Tempfiles" do
101
- post "/", "photo" => uploaded_file
102
- last_request.POST["photo"][:tempfile].should be_a(::Tempfile)
103
- end
104
- end
105
-
106
-
107
- context "uploading two files" do
108
- it "sends the multipart/form-data content type" do
109
- post "/", "photos" => [uploaded_file, second_uploaded_file]
110
- last_request.env["CONTENT_TYPE"].should include("multipart/form-data;")
111
- end
112
-
113
- it "sends files with the filename" do
114
- post "/", "photos" => [uploaded_file, second_uploaded_file]
115
- last_request.POST["photos"].collect{|photo| photo[:filename]}.should == ["foo.txt", "bar.txt"]
116
- end
117
-
118
- it "sends files with the text/plain MIME type by default" do
119
- post "/", "photos" => [uploaded_file, second_uploaded_file]
120
- last_request.POST["photos"].collect{|photo| photo[:type]}.should == ["text/plain", "text/plain"]
121
- end
122
-
123
- it "sends files with the right names" do
124
- post "/", "photos" => [uploaded_file, second_uploaded_file]
125
- last_request.POST["photos"].all?{|photo| photo[:name].should == "photos[]" }
126
- end
127
-
128
- it "allows mixed content types" do
129
- image_file = Rack::Test::UploadedFile.new(test_file_path, "image/jpeg")
130
-
131
- post "/", "photos" => [uploaded_file, image_file]
132
- last_request.POST["photos"].collect{|photo| photo[:type]}.should == ["text/plain", "image/jpeg"]
133
- end
134
-
135
- it "sends files with a Content-Length in the header" do
136
- post "/", "photos" => [uploaded_file, second_uploaded_file]
137
- last_request.POST["photos"].all?{|photo| photo[:head].should include("Content-Length: 4") }
138
- end
139
-
140
- it "sends both files as Tempfiles" do
141
- post "/", "photos" => [uploaded_file, second_uploaded_file]
142
- last_request.POST["photos"].all?{|photo| photo[:tempfile].should be_a(::Tempfile) }
143
- end
144
- end
145
- end
@@ -1,24 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Rack::Test::UploadedFile do
4
- def test_file_path
5
- File.dirname(__FILE__) + "/../../fixtures/foo.txt"
6
- end
7
-
8
- it "responds to things that Tempfile responds to" do
9
- uploaded_file = Rack::Test::UploadedFile.new(test_file_path)
10
-
11
- uploaded_file.should respond_to(:close)
12
- uploaded_file.should respond_to(:close!)
13
- uploaded_file.should respond_to(:delete)
14
- uploaded_file.should respond_to(:length)
15
- uploaded_file.should respond_to(:open)
16
- uploaded_file.should respond_to(:path)
17
- uploaded_file.should respond_to(:size)
18
- uploaded_file.should respond_to(:unlink)
19
- uploaded_file.should respond_to(:read)
20
- uploaded_file.should respond_to(:original_filename)
21
- uploaded_file.should respond_to(:tempfile) # Allows calls to params[:file].tempfile
22
- end
23
-
24
- end
@@ -1,193 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Rack::Test::Utils do
4
- include Rack::Test::Utils
5
-
6
- describe "build_nested_query" do
7
- it "converts empty strings to =" do
8
- build_nested_query("").should == "="
9
- end
10
-
11
- it "converts nil to an empty string" do
12
- build_nested_query(nil).should == ""
13
- end
14
-
15
- it "converts hashes with nil values" do
16
- build_nested_query(:a => nil).should == "a"
17
- end
18
-
19
- it "converts hashes" do
20
- build_nested_query(:a => 1).should == "a=1"
21
- end
22
-
23
- it "converts hashes with multiple keys" do
24
- hash = { :a => 1, :b => 2 }
25
- ["a=1&b=2", "b=2&a=1"].should include(build_nested_query(hash))
26
- end
27
-
28
- it "converts arrays with one element" do
29
- build_nested_query(:a => [1]).should == "a[]=1"
30
- end
31
-
32
- it "converts arrays with multiple elements" do
33
- build_nested_query(:a => [1, 2]).should == "a[]=1&a[]=2"
34
- end
35
-
36
- it "converts arrays with brackets '[]' in the name" do
37
- build_nested_query("a[]" => [1, 2]).should == "a%5B%5D=1&a%5B%5D=2"
38
- end
39
-
40
- it "converts nested hashes" do
41
- build_nested_query(:a => { :b => 1 }).should == "a[b]=1"
42
- end
43
-
44
- it "converts arrays nested in a hash" do
45
- build_nested_query(:a => { :b => [1, 2] }).should == "a[b][]=1&a[b][]=2"
46
- end
47
-
48
- it "converts arrays of hashes" do
49
- build_nested_query(:a => [{ :b => 2}, { :c => 3}]).should == "a[][b]=2&a[][c]=3"
50
- end
51
- end
52
-
53
- describe "build_multipart" do
54
- it "builds multipart bodies" do
55
- files = Rack::Test::UploadedFile.new(multipart_file("foo.txt"))
56
- data = build_multipart("submit-name" => "Larry", "files" => files)
57
-
58
- options = {
59
- "CONTENT_TYPE" => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
60
- "CONTENT_LENGTH" => data.length.to_s,
61
- :input => StringIO.new(data)
62
- }
63
- env = Rack::MockRequest.env_for("/", options)
64
- params = Rack::Utils::Multipart.parse_multipart(env)
65
- check params["submit-name"].should == "Larry"
66
- check params["files"][:filename].should == "foo.txt"
67
- params["files"][:tempfile].read.should == "bar\n"
68
- end
69
-
70
- it "builds multipart bodies from array of files" do
71
- files = [Rack::Test::UploadedFile.new(multipart_file("foo.txt")), Rack::Test::UploadedFile.new(multipart_file("bar.txt"))]
72
- data = build_multipart("submit-name" => "Larry", "files" => files)
73
-
74
- options = {
75
- "CONTENT_TYPE" => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
76
- "CONTENT_LENGTH" => data.length.to_s,
77
- :input => StringIO.new(data)
78
- }
79
- env = Rack::MockRequest.env_for("/", options)
80
- params = Rack::Utils::Multipart.parse_multipart(env)
81
- check params["submit-name"].should == "Larry"
82
-
83
- check params["files"][0][:filename].should == "foo.txt"
84
- params["files"][0][:tempfile].read.should == "bar\n"
85
-
86
- check params["files"][1][:filename].should == "bar.txt"
87
- params["files"][1][:tempfile].read.should == "baz\n"
88
- end
89
-
90
- it "builds nested multipart bodies" do
91
- files = Rack::Test::UploadedFile.new(multipart_file("foo.txt"))
92
- data = build_multipart("people" => [{"submit-name" => "Larry", "files" => files}], "foo" => ['1', '2'])
93
-
94
- options = {
95
- "CONTENT_TYPE" => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
96
- "CONTENT_LENGTH" => data.length.to_s,
97
- :input => StringIO.new(data)
98
- }
99
- env = Rack::MockRequest.env_for("/", options)
100
- params = Rack::Utils::Multipart.parse_multipart(env)
101
- check params["people"][0]["submit-name"].should == "Larry"
102
- check params["people"][0]["files"][:filename].should == "foo.txt"
103
- params["people"][0]["files"][:tempfile].read.should == "bar\n"
104
- check params["foo"].should == ["1", "2"]
105
- end
106
-
107
- it "builds nested multipart bodies with an array of hashes" do
108
- files = Rack::Test::UploadedFile.new(multipart_file("foo.txt"))
109
- data = build_multipart("files" => files, "foo" => [{"id" => "1", "name" => 'Dave'}, {"id" => "2", "name" => 'Steve'}])
110
-
111
- options = {
112
- "CONTENT_TYPE" => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
113
- "CONTENT_LENGTH" => data.length.to_s,
114
- :input => StringIO.new(data)
115
- }
116
- env = Rack::MockRequest.env_for("/", options)
117
- params = Rack::Utils::Multipart.parse_multipart(env)
118
- check params["files"][:filename].should == "foo.txt"
119
- params["files"][:tempfile].read.should == "bar\n"
120
- check params["foo"].should == [{"id" => "1", "name" => "Dave"}, {"id" => "2", "name" => "Steve"}]
121
- end
122
-
123
- it "builds nested multipart bodies with arbitrarily nested array of hashes" do
124
- files = Rack::Test::UploadedFile.new(multipart_file("foo.txt"))
125
- data = build_multipart("files" => files, "foo" => {"bar" => [{"id" => "1", "name" => 'Dave'},
126
- {"id" => "2", "name" => 'Steve', "qux" => [{"id" => '3', "name" => 'mike'},
127
- {"id" => '4', "name" => 'Joan'}]}]})
128
-
129
- options = {
130
- "CONTENT_TYPE" => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
131
- "CONTENT_LENGTH" => data.length.to_s,
132
- :input => StringIO.new(data)
133
- }
134
- env = Rack::MockRequest.env_for("/", options)
135
- params = Rack::Utils::Multipart.parse_multipart(env)
136
- check params["files"][:filename].should == "foo.txt"
137
- params["files"][:tempfile].read.should == "bar\n"
138
- check params["foo"].should == {"bar" => [{"id" => "1", "name" => "Dave"},
139
- {"id" => "2", "name" => "Steve", "qux" => [{"id" => '3', "name" => 'mike'},
140
- {"id" => '4', "name" => 'Joan'}]}]}
141
- end
142
-
143
- it 'does not break with params that look nested, but are not' do
144
- files = Rack::Test::UploadedFile.new(multipart_file("foo.txt"))
145
- data = build_multipart("foo[]" => "1", "bar[]" => {"qux" => "2"}, "files[]" => files)
146
-
147
- options = {
148
- "CONTENT_TYPE" => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
149
- "CONTENT_LENGTH" => data.length.to_s,
150
- :input => StringIO.new(data)
151
- }
152
- env = Rack::MockRequest.env_for("/", options)
153
- params = Rack::Utils::Multipart.parse_multipart(env)
154
- check params["files"][0][:filename].should == "foo.txt"
155
- params["files"][0][:tempfile].read.should == "bar\n"
156
- check params["foo"][0].should == "1"
157
- check params["bar"][0].should == {"qux" => "2"}
158
- end
159
-
160
- it 'allows for nested files' do
161
- files = Rack::Test::UploadedFile.new(multipart_file("foo.txt"))
162
- data = build_multipart("foo" => [{"id" => "1", "data" => files},
163
- {"id" => "2", "data" => ["3", "4"]}])
164
-
165
- options = {
166
- "CONTENT_TYPE" => "multipart/form-data; boundary=#{Rack::Test::MULTIPART_BOUNDARY}",
167
- "CONTENT_LENGTH" => data.length.to_s,
168
- :input => StringIO.new(data)
169
- }
170
- env = Rack::MockRequest.env_for("/", options)
171
- params = Rack::Utils::Multipart.parse_multipart(env)
172
- check params["foo"][0]["id"].should == "1"
173
- check params["foo"][0]["data"][:filename].should == "foo.txt"
174
- params["foo"][0]["data"][:tempfile].read.should == "bar\n"
175
- check params["foo"][1].should == {"id" => "2", "data" => ["3", "4"]}
176
- end
177
-
178
- it "returns nil if no UploadedFiles were used" do
179
- data = build_multipart("people" => [{"submit-name" => "Larry", "files" => "contents"}])
180
- data.should be_nil
181
- end
182
-
183
- it "raises ArgumentErrors if params is not a Hash" do
184
- lambda {
185
- build_multipart("foo=bar")
186
- }.should raise_error(ArgumentError, "value must be a Hash")
187
- end
188
-
189
- def multipart_file(name)
190
- File.join(File.dirname(__FILE__), "..", "..", "fixtures", name.to_s)
191
- end
192
- end
193
- end
@@ -1,550 +0,0 @@
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 "allows HTTP_HOST to be set" do
33
- request "/", "HTTP_HOST" => "www.example.ua"
34
- last_request.env['HTTP_HOST'].should == "www.example.ua"
35
- end
36
-
37
- it "sets HTTP_HOST with port for non-default ports" do
38
- request "http://foo.com:8080"
39
- last_request.env["HTTP_HOST"].should == "foo.com:8080"
40
- request "https://foo.com:8443"
41
- last_request.env["HTTP_HOST"].should == "foo.com:8443"
42
- end
43
-
44
- it "sets HTTP_HOST without port for default ports" do
45
- request "http://foo.com"
46
- last_request.env["HTTP_HOST"].should == "foo.com"
47
- request "http://foo.com:80"
48
- last_request.env["HTTP_HOST"].should == "foo.com"
49
- request "https://foo.com:443"
50
- last_request.env["HTTP_HOST"].should == "foo.com"
51
- end
52
-
53
- it "defaults to GET" do
54
- request "/"
55
- last_request.env["REQUEST_METHOD"].should == "GET"
56
- end
57
-
58
- it "defaults the REMOTE_ADDR to 127.0.0.1" do
59
- request "/"
60
- last_request.env["REMOTE_ADDR"].should == "127.0.0.1"
61
- end
62
-
63
- it "sets rack.test to true in the env" do
64
- request "/"
65
- last_request.env["rack.test"].should == true
66
- end
67
-
68
- it "defaults to port 80" do
69
- request "/"
70
- last_request.env["SERVER_PORT"].should == "80"
71
- end
72
-
73
- it "defaults to example.org" do
74
- request "/"
75
- last_request.env["SERVER_NAME"].should == "example.org"
76
- end
77
-
78
- it "yields the response to a given block" do
79
- request "/" do |response|
80
- response.should be_ok
81
- end
82
- end
83
-
84
- it "supports sending :params" do
85
- request "/", :params => { "foo" => "bar" }
86
- last_request.GET["foo"].should == "bar"
87
- end
88
-
89
- it "doesn't follow redirects by default" do
90
- request "/redirect"
91
- last_response.should be_redirect
92
- last_response.body.should be_empty
93
- end
94
-
95
- it "allows passing :input in for POSTs" do
96
- request "/", :method => :post, :input => "foo"
97
- last_request.env["rack.input"].read.should == "foo"
98
- end
99
-
100
- it "converts method names to a uppercase strings" do
101
- request "/", :method => :put
102
- last_request.env["REQUEST_METHOD"].should == "PUT"
103
- end
104
-
105
- it "prepends a slash to the URI path" do
106
- request "foo"
107
- last_request.env["PATH_INFO"].should == "/foo"
108
- end
109
-
110
- it "accepts params and builds query strings for GET requests" do
111
- request "/foo?baz=2", :params => {:foo => {:bar => "1"}}
112
- last_request.GET.should == { "baz" => "2", "foo" => { "bar" => "1" }}
113
- end
114
-
115
- it "parses query strings with repeated variable names correctly" do
116
- request "/foo?bar=2&bar=3"
117
- last_request.GET.should == { "bar" => "3" }
118
- end
119
-
120
- it "accepts raw input in params for GET requests" do
121
- request "/foo?baz=2", :params => "foo[bar]=1"
122
- last_request.GET.should == { "baz" => "2", "foo" => { "bar" => "1" }}
123
- end
124
-
125
- it "does not rewrite a GET query string when :params is not supplied" do
126
- request "/foo?a=1&b=2&c=3&e=4&d=5+%20"
127
- last_request.query_string.should == "a=1&b=2&c=3&e=4&d=5+%20"
128
- end
129
-
130
- it "accepts params and builds url encoded params for POST requests" do
131
- request "/foo", :method => :post, :params => {:foo => {:bar => "1"}}
132
- last_request.env["rack.input"].read.should == "foo[bar]=1"
133
- end
134
-
135
- it "accepts raw input in params for POST requests" do
136
- request "/foo", :method => :post, :params => "foo[bar]=1"
137
- last_request.env["rack.input"].read.should == "foo[bar]=1"
138
- end
139
-
140
- context "when the response body responds_to?(:close)" do
141
- class CloseableBody
142
- def initialize
143
- @closed = false
144
- end
145
-
146
- def each
147
- return if @closed
148
- yield "Hello, World!"
149
- end
150
-
151
- def close
152
- @closed = true
153
- end
154
- end
155
-
156
- it "closes response's body" do
157
- body = CloseableBody.new
158
- body.should_receive(:close)
159
-
160
- app = lambda do |env|
161
- [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, body]
162
- end
163
-
164
- session = Rack::Test::Session.new(Rack::MockSession.new(app))
165
- session.request("/")
166
- end
167
-
168
- it "closes response's body after iteration" do
169
- app = lambda do |env|
170
- [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, CloseableBody.new]
171
- end
172
-
173
- session = Rack::Test::Session.new(Rack::MockSession.new(app))
174
- session.request("/")
175
- session.last_response.body.should == "Hello, World!"
176
- end
177
- end
178
-
179
- context "when input is given" do
180
- it "sends the input" do
181
- request "/", :method => "POST", :input => "foo"
182
- last_request.env["rack.input"].read.should == "foo"
183
- end
184
-
185
- it "does not send a multipart request" do
186
- request "/", :method => "POST", :input => "foo"
187
- last_request.env["CONTENT_TYPE"].should_not == "application/x-www-form-urlencoded"
188
- end
189
- end
190
-
191
- context "for a POST specified with :method" do
192
- it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
193
- request "/", :method => "POST"
194
- last_request.env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
195
- end
196
- end
197
-
198
- context "for a POST specified with REQUEST_METHOD" do
199
- it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
200
- request "/", "REQUEST_METHOD" => "POST"
201
- last_request.env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
202
- end
203
- end
204
-
205
- context "when CONTENT_TYPE is specified in the env" do
206
- it "does not overwrite the CONTENT_TYPE" do
207
- request "/", "CONTENT_TYPE" => "application/xml"
208
- last_request.env["CONTENT_TYPE"].should == "application/xml"
209
- end
210
- end
211
-
212
- context "when the URL is https://" do
213
- it "sets rack.url_scheme to https" do
214
- get "https://example.org/"
215
- last_request.env["rack.url_scheme"].should == "https"
216
- end
217
-
218
- it "sets SERVER_PORT to 443" do
219
- get "https://example.org/"
220
- last_request.env["SERVER_PORT"].should == "443"
221
- end
222
-
223
- it "sets HTTPS to on" do
224
- get "https://example.org/"
225
- last_request.env["HTTPS"].should == "on"
226
- end
227
- end
228
-
229
- context "for a XHR" do
230
- it "sends XMLHttpRequest for the X-Requested-With header" do
231
- request "/", :xhr => true
232
- last_request.env["HTTP_X_REQUESTED_WITH"].should == "XMLHttpRequest"
233
- last_request.should be_xhr
234
- end
235
- end
236
- end
237
-
238
- describe "#header" do
239
- it "sets a header to be sent with requests" do
240
- header "User-Agent", "Firefox"
241
- request "/"
242
-
243
- last_request.env["HTTP_USER_AGENT"].should == "Firefox"
244
- end
245
-
246
- it "sets a Content-Type to be sent with requests" do
247
- header "Content-Type", "application/json"
248
- request "/"
249
-
250
- last_request.env["CONTENT_TYPE"].should == "application/json"
251
- end
252
-
253
- it "sets a Host to be sent with requests" do
254
- header "Host", "www.example.ua"
255
- request "/"
256
-
257
- last_request.env["HTTP_HOST"].should == "www.example.ua"
258
- end
259
-
260
- it "persists across multiple requests" do
261
- header "User-Agent", "Firefox"
262
- request "/"
263
- request "/"
264
-
265
- last_request.env["HTTP_USER_AGENT"].should == "Firefox"
266
- end
267
-
268
- it "overwrites previously set headers" do
269
- header "User-Agent", "Firefox"
270
- header "User-Agent", "Safari"
271
- request "/"
272
-
273
- last_request.env["HTTP_USER_AGENT"].should == "Safari"
274
- end
275
-
276
- it "can be used to clear a header" do
277
- header "User-Agent", "Firefox"
278
- header "User-Agent", nil
279
- request "/"
280
-
281
- last_request.env.should_not have_key("HTTP_USER_AGENT")
282
- end
283
-
284
- it "is overridden by headers sent during the request" do
285
- header "User-Agent", "Firefox"
286
- request "/", "HTTP_USER_AGENT" => "Safari"
287
-
288
- last_request.env["HTTP_USER_AGENT"].should == "Safari"
289
- end
290
- end
291
-
292
- describe "#env" do
293
- it "sets the env to be sent with requests" do
294
- env "rack.session", {:csrf => 'token'}
295
- request "/"
296
-
297
- last_request.env["rack.session"].should == {:csrf => 'token'}
298
- end
299
-
300
- it "persists across multiple requests" do
301
- env "rack.session", {:csrf => 'token'}
302
- request "/"
303
- request "/"
304
-
305
- last_request.env["rack.session"].should == {:csrf => 'token'}
306
- end
307
-
308
- it "overwrites previously set envs" do
309
- env "rack.session", {:csrf => 'token'}
310
- env "rack.session", {:some => :thing}
311
- request "/"
312
-
313
- last_request.env["rack.session"].should == {:some => :thing}
314
- end
315
-
316
- it "can be used to clear a env" do
317
- env "rack.session", {:csrf => 'token'}
318
- env "rack.session", nil
319
- request "/"
320
-
321
- last_request.env.should_not have_key("X_CSRF_TOKEN")
322
- end
323
-
324
- it "is overridden by envs sent during the request" do
325
- env "rack.session", {:csrf => 'token'}
326
- request "/", "rack.session" => {:some => :thing}
327
-
328
- last_request.env["rack.session"].should == {:some => :thing}
329
- end
330
- end
331
-
332
- describe "#authorize" do
333
- it "sets the HTTP_AUTHORIZATION header" do
334
- authorize "bryan", "secret"
335
- request "/"
336
-
337
- last_request.env["HTTP_AUTHORIZATION"].should == "Basic YnJ5YW46c2VjcmV0\n"
338
- end
339
-
340
- it "includes the header for subsequent requests" do
341
- basic_authorize "bryan", "secret"
342
- request "/"
343
- request "/"
344
-
345
- last_request.env["HTTP_AUTHORIZATION"].should == "Basic YnJ5YW46c2VjcmV0\n"
346
- end
347
- end
348
-
349
- describe "follow_redirect!" do
350
- it "follows redirects" do
351
- get "/redirect"
352
- follow_redirect!
353
-
354
- last_response.should_not be_redirect
355
- last_response.body.should == "You've been redirected"
356
- last_request.env["HTTP_REFERER"].should eql("http://example.org/redirect")
357
- end
358
-
359
- it "does not include params when following the redirect" do
360
- get "/redirect", { "foo" => "bar" }
361
- follow_redirect!
362
-
363
- last_request.GET.should == {}
364
- end
365
-
366
- it "raises an error if the last_response is not set" do
367
- lambda {
368
- follow_redirect!
369
- }.should raise_error(Rack::Test::Error)
370
- end
371
-
372
- it "raises an error if the last_response is not a redirect" do
373
- get "/"
374
-
375
- lambda {
376
- follow_redirect!
377
- }.should raise_error(Rack::Test::Error)
378
- end
379
- end
380
-
381
- describe "#last_request" do
382
- it "returns the most recent request" do
383
- request "/"
384
- last_request.env["PATH_INFO"].should == "/"
385
- end
386
-
387
- it "raises an error if no requests have been issued" do
388
- lambda {
389
- last_request
390
- }.should raise_error(Rack::Test::Error)
391
- end
392
- end
393
-
394
- describe "#last_response" do
395
- it "returns the most recent response" do
396
- request "/"
397
- last_response["Content-Type"].should == "text/html;charset=utf-8"
398
- end
399
-
400
- it "raises an error if no requests have been issued" do
401
- lambda {
402
- last_response
403
- }.should raise_error
404
- end
405
- end
406
-
407
- describe "after_request" do
408
- it "runs callbacks after each request" do
409
- ran = false
410
-
411
- rack_mock_session.after_request do
412
- ran = true
413
- end
414
-
415
- get "/"
416
- ran.should == true
417
- end
418
-
419
- it "runs multiple callbacks" do
420
- count = 0
421
-
422
- 2.times do
423
- rack_mock_session.after_request do
424
- count += 1
425
- end
426
- end
427
-
428
- get "/"
429
- count.should == 2
430
- end
431
- end
432
-
433
- describe "#get" do
434
- it_should_behave_like "any #verb methods"
435
-
436
- def verb
437
- "get"
438
- end
439
-
440
- it "uses the provided params hash" do
441
- get "/", :foo => "bar"
442
- last_request.GET.should == { "foo" => "bar" }
443
- end
444
-
445
- it "sends params with parens in names" do
446
- get "/", "foo(1i)" => "bar"
447
- last_request.GET["foo(1i)"].should == "bar"
448
- end
449
-
450
- it "supports params with encoding sensitive names" do
451
- get "/", "foo bar" => "baz"
452
- last_request.GET["foo bar"].should == "baz"
453
- end
454
-
455
- it "supports params with nested encoding sensitive names" do
456
- get "/", "boo" => {"foo bar" => "baz"}
457
- last_request.GET.should == {"boo" => {"foo bar" => "baz"}}
458
- end
459
-
460
- it "accepts params in the path" do
461
- get "/?foo=bar"
462
- last_request.GET.should == { "foo" => "bar" }
463
- end
464
- end
465
-
466
- describe "#head" do
467
- it_should_behave_like "any #verb methods"
468
-
469
- def verb
470
- "head"
471
- end
472
- end
473
-
474
- describe "#post" do
475
- it_should_behave_like "any #verb methods"
476
-
477
- def verb
478
- "post"
479
- end
480
-
481
- it "uses the provided params hash" do
482
- post "/", :foo => "bar"
483
- last_request.POST.should == { "foo" => "bar" }
484
- end
485
-
486
- it "supports params with encoding sensitive names" do
487
- post "/", "foo bar" => "baz"
488
- last_request.POST["foo bar"].should == "baz"
489
- end
490
-
491
- it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
492
- post "/"
493
- last_request.env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
494
- end
495
-
496
- it "accepts a body" do
497
- post "/", "Lobsterlicious!"
498
- last_request.body.read.should == "Lobsterlicious!"
499
- end
500
-
501
- context "when CONTENT_TYPE is specified in the env" do
502
- it "does not overwrite the CONTENT_TYPE" do
503
- post "/", {}, { "CONTENT_TYPE" => "application/xml" }
504
- last_request.env["CONTENT_TYPE"].should == "application/xml"
505
- end
506
- end
507
- end
508
-
509
- describe "#put" do
510
- it_should_behave_like "any #verb methods"
511
-
512
- def verb
513
- "put"
514
- end
515
-
516
- it "accepts a body" do
517
- put "/", "Lobsterlicious!"
518
- last_request.body.read.should == "Lobsterlicious!"
519
- end
520
- end
521
-
522
- describe "#patch" do
523
- it_should_behave_like "any #verb methods"
524
-
525
- def verb
526
- "patch"
527
- end
528
-
529
- it "accepts a body" do
530
- patch "/", "Lobsterlicious!"
531
- last_request.body.read.should == "Lobsterlicious!"
532
- end
533
- end
534
-
535
- describe "#delete" do
536
- it_should_behave_like "any #verb methods"
537
-
538
- def verb
539
- "delete"
540
- end
541
- end
542
-
543
- describe "#options" do
544
- it_should_behave_like "any #verb methods"
545
-
546
- def verb
547
- "options"
548
- end
549
- end
550
- end