public
Description: A Ruby web application framework
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack.git
Click here to lend your support to: mack and make a donation at www.pledgie.com !
file upload testing [#15 state:resolved]
dsutedja (author)
Fri Aug 15 14:00:00 -0700 2008
commit  f634e81de2541bfbfbf8232592c5409ad06b4112
tree    de23838b8f10a555a627dbff0b95135bc09653f2
parent  e8b236b2ef78a838448edc36d4141985620bceb6
...
1
 
2
3
4
...
16
17
18
 
19
20
21
...
 
1
2
3
4
...
16
17
18
19
20
21
22
0
@@ -1,4 +1,4 @@
0
-* [#68] Removed const redefined warnings if mack-l10n is used along with mack-activerecord
0
+* [#15] File upload testing support (ability to do multipart form post in testing)
0
 * [#88] Implemented pending SQLite3 tests in mack-activerecord
0
 * [#68] Transactional support in mack-AR
0
 * [#87] Refactored out common ORM code from mack-active_record and mack-data_mapper into mack-orm
0
@@ -16,6 +16,7 @@
0
 * [#74] Added optional feature to disable initialization logging.
0
 * [#73] Tests no longer use the functional/unit directories
0
 * [#69] Added ViewHelperGenerator and ControllerHelperGenerator
0
+* [#68] Removed const redefined warnings if mack-l10n is used along with mack-activerecord
0
 * [#67] Added test:setup tasks for active_record and data_mapper
0
 * [#39] HAML 2.0.2 support
0
 * [#28] Refactored out encryption into the mack-encryption gem.
...
7
8
9
 
10
11
12
13
 
 
 
 
 
 
 
14
15
 
16
17
18
...
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
23
24
25
26
0
@@ -7,12 +7,20 @@ module Mack
0
       attr_reader :file_name
0
       attr_reader :content
0
       attr_reader :size
0
+      attr_reader :mime
0
       
0
       def initialize(path)
0
         @path = path
0
         @file_name = File.basename(path)
0
+        extension = File.extname(path)
0
+        extension = extension.gsub!(".", "")
0
+        if extension and !extension.empty?
0
+          @mime     = Mack::Utils::MimeTypes.instance.get(extension) if extension
0
+        else
0
+          @mime     = "application/octet-stream"
0
+        end
0
         raw_content = File.read(path)
0
-        @content = Base64.encode64(raw_content)
0
+        @content = Base64.encode64(raw_content).strip
0
         @size    = @content.size
0
       end
0
     end
...
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
...
182
183
184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
186
187
...
86
87
88
 
89
90
91
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
94
 
95
96
97
...
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
0
@@ -86,33 +86,12 @@ module Mack
0
       def build_file(path)
0
         return Mack::Testing::FileWrapper.new(path)
0
       end
0
-    
0
+          
0
       # Performs a 'post' request for the specified uri.
0
       def post(uri, options = {})
0
         if options[:multipart]
0
-          form_input = ""
0
-          boundary = "--Mack-boundary\r\n"
0
-          options.each_pair do |k, v|
0
-            if v.kind_of?(Mack::Testing::FileWrapper)
0
-              form_input += boundary
0
-              form_input += "content-disposition: form-data; name=\"#{k}\"; filename=\"#{v.file_name}\"\r\n"
0
-              form_input += "Content-Type: application/octet-stream\r\n\r\n"
0
-              form_input += "#{v.content}\r\n"
0
-            elsif k != :multipart 
0
-              form_input += boundary
0
-              form_input += "content-disposition: form-data; name=\"#{k}\"\r\n"
0
-              form_input += "Content-Type: text/plain\r\n\r\n"
0
-              form_input += "#{v}\r\n"
0
-            end
0
-          end
0
-          
0
-          form_input += boundary + "\r\n"
0
-          # request.env_for("/",
0
-          #                 "CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
0
-          #                 "CONTENT_LENGTH" => form_input.size)
0
-          # build_response(request.post(uri, build_request_options({:input => form_input})))
0
+          form_input = build_multipart_data(options)
0
           build_response(request.post(uri, build_request_options({"CONTENT_TYPE" => "multipart/form-data, boundary=Mack-boundary", "CONTENT_LENGTH" => form_input.size, :input => form_input})))
0
-          # build_response(request.post(uri, build_request_options({:content_type => "multipart/form-data, boundary=Mack-boundary", :content_length => form_input.size, :input => form_input})))
0
         else
0
           build_response(request.post(uri, build_request_options({:input => options.to_params})))
0
         end
0
@@ -182,6 +161,28 @@ module Mack
0
       end
0
     
0
       private
0
+      
0
+      def build_multipart_data(options)
0
+        form_input = ""
0
+        boundary = "--Mack-boundary\r\n"
0
+        options.each_pair do |k, v|
0
+          if v.kind_of?(Mack::Testing::FileWrapper)
0
+            form_input += boundary
0
+            form_input += "content-disposition: form-data; name=\"#{k}\"; filename=\"#{v.file_name}\"\r\n"
0
+            form_input += "Content-Type: #{v.mime}\r\n\r\n"
0
+            form_input += "#{v.content}\r\n"
0
+          elsif k != :multipart 
0
+            form_input += boundary
0
+            form_input += "content-disposition: form-data; name=\"#{k}\"\r\n"
0
+            form_input += "Content-Type: text/plain\r\n\r\n"
0
+            form_input += "#{v}\r\n"
0
+          end
0
+        end
0
+        form_input += boundary
0
+        return form_input
0
+      end
0
+      
0
+      
0
       def test_cookies
0
         @test_cookies = {} if @test_cookies.nil?
0
         @test_cookies
...
49
50
51
52
53
54
55
 
 
 
 
 
 
 
 
56
57
58
...
49
50
51
 
52
53
 
54
55
56
57
58
59
60
61
62
63
64
0
@@ -49,10 +49,16 @@ class TstAnotherController
0
   end
0
   
0
   def do_upload
0
-    debugger
0
     file = request.file("file0")
0
     @saved_file_name = file.file_name
0
-    redirect_to(upload_successful_url)
0
+    @album = params[:album]
0
+  end
0
+  
0
+  def upload_multiple
0
+    file = request.file("file0")
0
+    @saved_file1 = file.file_name.dup
0
+    file = request.file("file1")
0
+    @saved_file2 = file.file_name.dup
0
   end
0
   
0
   def regardless_of_string_or_symbol
...
25
26
27
28
 
29
30
31
...
25
26
27
 
28
29
30
31
0
@@ -25,7 +25,7 @@ Mack::Routes.build do |r|
0
     map.kill_kenny_no_meth "/tst_another/kill_kenny", :action => :kill_kenny
0
     map.kill_kenny_bad "/tst_another/kill_kenny_bad", :action => :kill_kenny_bad
0
     map.upload_file "/tst_another/do_upload", :action => :do_upload
0
-    map.upload_successful "/tst_another/upload_successful"
0
+    map.upload_multiple "/tst_another/upload_multiple", :action => :upload_multiple
0
   end
0
   
0
   r.with_options(:controller => "vtt/view_template") do |map|
...
2
3
4
5
6
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
9
10
...
2
3
4
 
 
 
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -2,8 +2,19 @@ require 'pathname'
0
 require Pathname(__FILE__).dirname.expand_path.parent.parent + 'spec_helper'
0
 
0
 describe "File Upload Request" do
0
-  it "should handle multipart content properly" do
0
-    post upload_file_url, :multipart => true, :file0 => build_file(File.join(File.dirname(__FILE__), "images", "homer_brain.jpg"))
0
-    pp response
0
+  it "should generate proper multipart content" do
0
+    post upload_file_url, :multipart => true, :file0 => build_file(File.join(File.dirname(__FILE__), "images", "homer_brain.jpg")), :album => 'simpsons'
0
+    assigns(:saved_file_name).should_not be_nil
0
+    assigns(:saved_file_name).should == "homer_brain.jpg"
0
+    assigns(:album).should_not be_nil
0
+    assigns(:album).should == "simpsons"
0
+  end
0
+  
0
+  it "should be able to upload multiple files" do  
0
+    post upload_multiple_url, :multipart => true, :file0 => build_file(File.join(File.dirname(__FILE__), "images", "homer_brain.jpg")), :file1 => build_file(File.join(File.dirname(__FILE__), "images", "homer_brain2.jpg"))
0
+    assigns(:saved_file1).should_not be_nil
0
+    assigns(:saved_file2).should_not be_nil
0
+    assigns(:saved_file1).should == "homer_brain.jpg"
0
+    assigns(:saved_file2).should == "homer_brain2.jpg"
0
   end
0
 end
0
\ No newline at end of file

Comments