rack-test 0.6.3 → 0.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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