rack-raw-upload 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -2,6 +2,7 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  json (1.4.6)
5
+ json (1.4.6-java)
5
6
  rack (1.2.1)
6
7
  rack-test (0.5.6)
7
8
  rack (>= 1.0)
@@ -9,6 +10,7 @@ GEM
9
10
  shoulda (2.11.3)
10
11
 
11
12
  PLATFORMS
13
+ java
12
14
  ruby
13
15
 
14
16
  DEPENDENCIES
data/README.md CHANGED
@@ -13,7 +13,7 @@ Additionally, it can be told explicitly to perform the conversion, using the hea
13
13
 
14
14
  Rack::RawUpload expects that requests will:
15
15
 
16
- 1. be POST requests
16
+ 1. be POST or PUT requests
17
17
  2. set the mimetype `application/octet-stream`
18
18
 
19
19
 
@@ -3,7 +3,7 @@ require 'tmpdir' # Needed in 1.8.7 to access Dir::tmpdir
3
3
  module Rack
4
4
  class RawUpload
5
5
 
6
- VERSION = '1.0.5'
6
+ VERSION = '1.0.6'
7
7
 
8
8
  def initialize(app, opts = {})
9
9
  @app = app
@@ -35,7 +35,9 @@ module Rack
35
35
  # the garbage collector will unlink this file.
36
36
  # in this case, only the path to the 'original' tempfile is used
37
37
  # and the physical file will be deleted, if the gc runs.
38
- tempfile = open(tempfile.path, "r+:BINARY")
38
+ tempfile2 = relink_file(tempfile)
39
+ tempfile.close
40
+ tempfile = tempfile2
39
41
  end
40
42
  tempfile << env['rack.input'].read
41
43
  tempfile.flush
@@ -89,5 +91,20 @@ module Rack
89
91
  true
90
92
  end
91
93
  end
94
+
95
+ def relink_file(file)
96
+ new_name = file.path + random_string
97
+ ::File.link(file.path, new_name)
98
+ ret = ::File.open(new_name, "r+")
99
+ ret.binmode
100
+ ret
101
+ rescue SystemCallError
102
+ # The randomly chosen file name was taken. Try again.
103
+ retry
104
+ end
105
+
106
+ def random_string
107
+ (0...8).map{65.+(rand(25)).chr}.join
108
+ end
92
109
  end
93
110
  end
@@ -3,6 +3,7 @@ require 'rack/test'
3
3
  require 'shoulda'
4
4
  require 'rack-raw-upload'
5
5
  require 'json'
6
+ require 'digest'
6
7
 
7
8
  class RawUploadTest < Test::Unit::TestCase
8
9
  include Rack::Test::Methods
@@ -17,17 +18,31 @@ class RawUploadTest < Test::Unit::TestCase
17
18
 
18
19
  def setup
19
20
  @middleware_opts = {}
20
- @path = __FILE__
21
+ @path = File.join(File.dirname(__FILE__), %w{data me-in-shanghai.jpg})
21
22
  @filename = File.basename(@path)
22
23
  @file = File.open(@path)
23
24
  end
24
25
 
25
26
  def upload(env = {})
26
27
  env = {
27
- 'REQUEST_METHOD' => 'POST',
28
28
  'CONTENT_TYPE' => 'application/octet-stream',
29
+ 'rack.input' => @file
30
+ }.merge(env)
31
+ do_request(env)
32
+ end
33
+
34
+ def post(env = {})
35
+ env = {
36
+ 'CONTENT_TYPE' => 'multipart/form-data',
37
+ 'rack.input' => StringIO.new('things=stuff')
38
+ }.merge(env)
39
+ do_request(env)
40
+ end
41
+
42
+ def do_request(env = {})
43
+ env = {
44
+ 'REQUEST_METHOD' => 'POST',
29
45
  'PATH_INFO' => '/some/path',
30
- 'rack.input' => @file,
31
46
  }.merge(env)
32
47
  request(env['PATH_INFO'], env)
33
48
  end
@@ -49,19 +64,19 @@ class RawUploadTest < Test::Unit::TestCase
49
64
  end
50
65
 
51
66
  should "not work with Content-Type 'application/x-www-form-urlencoded'" do
52
- upload('CONTENT_TYPE' => 'application/x-www-form-urlencoded')
67
+ post('CONTENT_TYPE' => 'application/x-www-form-urlencoded')
53
68
  assert_successful_non_upload
54
69
  end
55
70
 
56
71
  should "not work with Content-Type 'multipart/form-data'" do
57
- upload('CONTENT_TYPE' => 'multipart/form-data')
72
+ post('CONTENT_TYPE' => 'multipart/form-data')
58
73
  assert_successful_non_upload
59
74
  end
60
75
 
61
76
  # "stuff" should be something like "boundary=----WebKitFormBoundaryeKPeU4p65YgercgO",
62
77
  # but if I do that here, Rack tries to be clever and the test breaks
63
78
  should "not work with Content-Type 'multipart/form-data; stuff'" do
64
- upload('CONTENT_TYPE' => 'multipart/form-data; stuff')
79
+ post('CONTENT_TYPE' => 'multipart/form-data; stuff')
65
80
  assert_successful_non_upload
66
81
  end
67
82
 
@@ -84,7 +99,7 @@ class RawUploadTest < Test::Unit::TestCase
84
99
 
85
100
  context "with X-File-Upload: smart" do
86
101
  should "perform a file upload if appropriate" do
87
- upload('CONTENT_TYPE' => 'multipart/form-data', 'HTTP_X_FILE_UPLOAD' => 'smart')
102
+ post('CONTENT_TYPE' => 'multipart/form-data', 'HTTP_X_FILE_UPLOAD' => 'smart')
88
103
  assert_successful_non_upload
89
104
  end
90
105
 
@@ -115,7 +130,7 @@ class RawUploadTest < Test::Unit::TestCase
115
130
  end
116
131
 
117
132
  should "stay put when `X-File-Upload: smart` and the request is not an upload" do
118
- upload('CONTENT_TYPE' => 'multipart/form-data', 'HTTP_X_FILE_UPLOAD' => 'smart')
133
+ post('CONTENT_TYPE' => 'multipart/form-data', 'HTTP_X_FILE_UPLOAD' => 'smart')
119
134
  assert_successful_non_upload
120
135
  end
121
136
  end
@@ -192,14 +207,14 @@ class RawUploadTest < Test::Unit::TestCase
192
207
  def assert_file_uploaded
193
208
  file = File.open(@path)
194
209
  received = last_request.POST["file"]
195
- assert_equal file.gets, received[:tempfile].gets
210
+ assert_files_equal file, received[:tempfile]
196
211
  assert last_response.ok?
197
212
  end
198
213
 
199
214
  def assert_file_uploaded_as(file_type)
200
215
  file = File.open(@path)
201
216
  received = last_request.POST["file"]
202
- assert_equal file.gets, received[:tempfile].gets
217
+ assert_files_equal file, received[:tempfile]
203
218
  assert_equal file_type, received[:type]
204
219
  assert last_response.ok?
205
220
  end
@@ -208,4 +223,10 @@ class RawUploadTest < Test::Unit::TestCase
208
223
  assert ! last_request.POST.has_key?('file')
209
224
  assert last_response.ok?
210
225
  end
226
+
227
+ def assert_files_equal(f1, f2)
228
+ f1.seek(0)
229
+ f2.seek(0)
230
+ assert_equal Digest::MD5.hexdigest(f1.read), Digest::MD5.hexdigest(f2.read)
231
+ end
211
232
  end
metadata CHANGED
@@ -1,89 +1,70 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rack-raw-upload
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.6
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 5
10
- version: 1.0.5
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Pablo Brasero
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-07-06 00:00:00 +01:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2011-07-30 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: json
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2157245860 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 3
30
- segments:
31
- - 0
32
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
33
22
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: rake
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *2157245860
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &2157245420 !ruby/object:Gem::Requirement
39
28
  none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
47
33
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: rack-test
51
34
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *2157245420
36
+ - !ruby/object:Gem::Dependency
37
+ name: rack-test
38
+ requirement: &2157245000 !ruby/object:Gem::Requirement
53
39
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
61
44
  type: :development
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: shoulda
65
45
  prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *2157245000
47
+ - !ruby/object:Gem::Dependency
48
+ name: shoulda
49
+ requirement: &2157271180 !ruby/object:Gem::Requirement
67
50
  none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
75
55
  type: :development
76
- version_requirements: *id004
77
- description: Middleware that converts files uploaded with mimetype application/octet-stream into normal form input, so Rack applications can read these as normal, rather than as raw input.
56
+ prerelease: false
57
+ version_requirements: *2157271180
58
+ description: Middleware that converts files uploaded with mimetype application/octet-stream
59
+ into normal form input, so Rack applications can read these as normal, rather than
60
+ as raw input.
78
61
  email: pablobm@gmail.com
79
62
  executables: []
80
-
81
63
  extensions: []
82
-
83
- extra_rdoc_files:
64
+ extra_rdoc_files:
84
65
  - LICENSE
85
66
  - README.md
86
- files:
67
+ files:
87
68
  - lib/rack/raw_upload.rb
88
69
  - lib/rack-raw-upload.rb
89
70
  - test/raw_upload_test.rb
@@ -91,41 +72,31 @@ files:
91
72
  - README.md
92
73
  - Gemfile
93
74
  - Gemfile.lock
94
- has_rdoc: true
95
75
  homepage: https://github.com/newbamboo/rack-raw-upload
96
76
  licenses: []
97
-
98
77
  post_install_message:
99
- rdoc_options:
78
+ rdoc_options:
100
79
  - --charset=UTF-8
101
80
  - --main
102
81
  - README.rdoc
103
- require_paths:
82
+ require_paths:
104
83
  - lib
105
- required_ruby_version: !ruby/object:Gem::Requirement
84
+ required_ruby_version: !ruby/object:Gem::Requirement
106
85
  none: false
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- hash: 3
111
- segments:
112
- - 0
113
- version: "0"
114
- required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
91
  none: false
116
- requirements:
117
- - - ">="
118
- - !ruby/object:Gem::Version
119
- hash: 3
120
- segments:
121
- - 0
122
- version: "0"
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
123
96
  requirements: []
124
-
125
97
  rubyforge_project:
126
- rubygems_version: 1.6.2
98
+ rubygems_version: 1.8.6
127
99
  signing_key:
128
100
  specification_version: 3
129
101
  summary: Rack Raw Upload middleware
130
102
  test_files: []
131
-