rack-multipart_related 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -48,7 +48,13 @@ Using this middleware, the hash above is parsed and rebuilt like the code below:
48
48
  {
49
49
  "user" => {
50
50
  "name" => "Jhon",
51
- "avatar" => <File:/var/folders/Iu/IuwHUNlZE8OaYMACfwiapE+++TI/-Tmp-/RackMultipart20101217-30578-bt18q9-0>, # The binary content of image
51
+ "avatar" => {
52
+ :type => "image/png",
53
+ :filename =>"image.png",
54
+ :tempfile => <File:/var/folders/Iu/IuwHUNlZE8OaYMACfwiapE+++TI/-Tmp-/RackMultipart20101217-30578-bt18q9-0>, # The binary content of image
55
+ :head => "Content-Type: image/gif\r\nContent-Disposition: inline; name=\"avatar_image\"; filename=\"image.png\"\r\n",
56
+ :name =>"avatar_image"
57
+ }
52
58
  }
53
59
  }
54
60
 
@@ -13,13 +13,16 @@ module Rack
13
13
 
14
14
  if content_type =~ /^multipart\/related/ni
15
15
 
16
- start_part = content_type[/.* start=(?:"((?:\\.|[^\"])*)"|([^;\s]*))/ni, 1]
17
- start_part_type = content_type[/.* type=(?:"((?:\\.|[^\"])*)"|([^;\s]*))/ni, 1]
16
+ start_part = content_type[/.* start=(?:"((?:\\.|[^\"])*)"|([^;\s]*))/ni] && ($1 || $2)
17
+ start_part_type = content_type[/.* type=(?:"((?:\\.|[^\"])*)"|([^;\s]*))/ni] && ($1 || $2)
18
18
 
19
19
  if start_part_type == "application/json"
20
20
 
21
21
  params = env["rack.request.form_hash"]
22
- json_data = ::JSON.parse(get_attribute(params, start_part)[:tempfile].read)
22
+ start_part_attribute = get_attribute(params, start_part)
23
+ json_data = ::JSON.parse(start_part_attribute[:tempfile].read)
24
+ start_part_attribute[:tempfile].rewind
25
+ env['START_CONTENT_TYPE'] ||= start_part_attribute[:type]
23
26
 
24
27
  new_params = handle_atributtes_with_part_refs(json_data, params)
25
28
  env["rack.request.form_hash"] = new_params
@@ -73,4 +73,49 @@ class MultipartRelatedTest < Test::Unit::TestCase
73
73
  assert last_response.ok?
74
74
  assert_equal last_request.env["rack.request.form_hash"], expected_request_form_hash_after_middleware
75
75
  end
76
+
77
+ def test_multipart_related_with_start_and_type_without_quotes
78
+ jsonfile = make_tempfile("json", '{"user": {"name": "Jhon", "avatar": "cid:avatar_image"} }')
79
+
80
+ request_form_hash = {
81
+ "json" => {
82
+ :type => "application/json; charset=UTF-8",
83
+ :tempfile => jsonfile,
84
+ :head => "Content-Type: application/json; charset=UTF-8\r\nContent-Disposition: inline; name=\"json\"\r\n",
85
+ :name => "json"
86
+ },
87
+ "avatar_image" => {
88
+ :type => "image/png",
89
+ :filename =>"image.png",
90
+ :tempfile => imagefile,
91
+ :head => "Content-Type: image/gif\r\nContent-Disposition: inline; name=\"avatar_image\"; filename=\"image.png\"\r\n",
92
+ :name =>"avatar_image"
93
+ }
94
+ }
95
+
96
+ expected_request_form_hash_after_middleware = {
97
+ "user" => {
98
+ "name" => "Jhon",
99
+ "avatar" => {
100
+ :type => "image/png",
101
+ :filename =>"image.png",
102
+ :tempfile => imagefile,
103
+ :head => "Content-Type: image/gif\r\nContent-Disposition: inline; name=\"avatar_image\"; filename=\"image.png\"\r\n",
104
+ :name =>"avatar_image"
105
+ }
106
+ }
107
+ }
108
+
109
+ env = {
110
+ 'REQUEST_METHOD' => 'POST',
111
+ 'CONTENT_TYPE' => 'multipart/related; boundary="the_boundary"; type=application/json; start=json',
112
+ 'PATH_INFO' => '/some/path',
113
+ 'rack.request.form_hash' => request_form_hash
114
+ }
115
+
116
+ request(env['PATH_INFO'], env)
117
+
118
+ assert last_response.ok?
119
+ assert_equal last_request.env["rack.request.form_hash"], expected_request_form_hash_after_middleware
120
+ end
76
121
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-multipart_related
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lucas Fais
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-12-27 00:00:00 -02:00
19
+ date: 2010-12-30 00:00:00 -02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency