rack-content_type_validator 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rack/content_type_validator.rb +19 -11
- data/test/rack/content_type_validator_test.rb +21 -8
- metadata +4 -4
@@ -13,9 +13,8 @@ module Rack
|
|
13
13
|
|
14
14
|
def call(env)
|
15
15
|
if match_path?(env['PATH_INFO']) && match_method?(env['REQUEST_METHOD'])
|
16
|
-
|
17
|
-
|
18
|
-
unless valid_content_type?(content_type) && valid_charset?(content_type)
|
16
|
+
|
17
|
+
unless valid_content_type?(env['CONTENT_TYPE'])
|
19
18
|
response = Response.new
|
20
19
|
response.status = 415
|
21
20
|
response.write(error_message)
|
@@ -43,18 +42,27 @@ module Rack
|
|
43
42
|
end
|
44
43
|
|
45
44
|
def valid_content_type?(content_type)
|
46
|
-
|
45
|
+
valid_mime_type?(@expected_params[:mime_type], content_type) &&
|
46
|
+
valid_params?(@expected_params, content_type)
|
47
|
+
end
|
48
|
+
|
49
|
+
def valid_mime_type?(expected_mime_type, content_type)
|
50
|
+
return true unless expected_mime_type
|
47
51
|
|
48
|
-
|
49
|
-
|
52
|
+
mime_type = content_type ? content_type[/^([\w\/]+)\b/, 1] : nil
|
53
|
+
mime_type == expected_mime_type
|
50
54
|
end
|
51
55
|
|
52
|
-
def
|
53
|
-
|
56
|
+
def valid_params?(expected_params, content_type)
|
57
|
+
expected_params = expected_params.dup
|
58
|
+
expected_params.delete(:mime_type)
|
59
|
+
return true unless expected_params
|
54
60
|
|
55
|
-
|
56
|
-
|
57
|
-
|
61
|
+
expected_params.all? do |key, value|
|
62
|
+
param = content_type ? content_type[/\b#{key}=([^;\s]*)/, 1] : nil
|
63
|
+
param.gsub!('"', '') if param
|
64
|
+
param == value
|
65
|
+
end
|
58
66
|
end
|
59
67
|
|
60
68
|
def error_message
|
@@ -21,7 +21,7 @@ class ContentTypeValidatorTest < Test::Unit::TestCase
|
|
21
21
|
env = build_env("PUT", "/request_path", 'application/json')
|
22
22
|
status, headers, body = middleware.call(env)
|
23
23
|
|
24
|
-
assert_equal
|
24
|
+
assert_equal 200, status
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_incorrect_content_type
|
@@ -33,7 +33,7 @@ class ContentTypeValidatorTest < Test::Unit::TestCase
|
|
33
33
|
env = build_env("PUT", path, 'application/json')
|
34
34
|
status, headers, body = middleware.call(env)
|
35
35
|
|
36
|
-
assert_equal
|
36
|
+
assert_equal 415, status
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_correct_content_type
|
@@ -45,7 +45,7 @@ class ContentTypeValidatorTest < Test::Unit::TestCase
|
|
45
45
|
env = build_env("POST", path, 'application/json')
|
46
46
|
status, headers, body = middleware.call(env)
|
47
47
|
|
48
|
-
assert_equal
|
48
|
+
assert_equal 200, status
|
49
49
|
end
|
50
50
|
|
51
51
|
def test_correct_content_type_and_incorrect_charset
|
@@ -57,7 +57,7 @@ class ContentTypeValidatorTest < Test::Unit::TestCase
|
|
57
57
|
env = build_env("POST", path, 'text/plain; charset=us-ascii')
|
58
58
|
status, headers, body = middleware.call(env)
|
59
59
|
|
60
|
-
assert_equal
|
60
|
+
assert_equal 415, status
|
61
61
|
end
|
62
62
|
|
63
63
|
def test_correct_content_type_and_charset
|
@@ -69,7 +69,7 @@ class ContentTypeValidatorTest < Test::Unit::TestCase
|
|
69
69
|
env = build_env("POST", path, 'application/json; charset=UTF-8')
|
70
70
|
status, headers, body = middleware.call(env)
|
71
71
|
|
72
|
-
assert_equal
|
72
|
+
assert_equal 200, status
|
73
73
|
end
|
74
74
|
|
75
75
|
def test_correct_content_type_and_charset_not_given
|
@@ -81,7 +81,7 @@ class ContentTypeValidatorTest < Test::Unit::TestCase
|
|
81
81
|
env = build_env("POST", path, 'application/json; charset=UTF-8')
|
82
82
|
status, headers, body = middleware.call(env)
|
83
83
|
|
84
|
-
assert_equal
|
84
|
+
assert_equal 200, status
|
85
85
|
end
|
86
86
|
|
87
87
|
def test_regexp_on_path
|
@@ -91,7 +91,7 @@ class ContentTypeValidatorTest < Test::Unit::TestCase
|
|
91
91
|
env = build_env("POST", "/content/1/comments", 'application/json; charset=UTF-8')
|
92
92
|
status, headers, body = middleware.call(env)
|
93
93
|
|
94
|
-
assert_equal
|
94
|
+
assert_equal 200, status
|
95
95
|
end
|
96
96
|
|
97
97
|
def test_charset_with_quotes
|
@@ -101,7 +101,20 @@ class ContentTypeValidatorTest < Test::Unit::TestCase
|
|
101
101
|
env = build_env("POST", "/content/1/comments", 'application/xml; charset="UTF-8"')
|
102
102
|
status, headers, body = middleware.call(env)
|
103
103
|
|
104
|
-
assert_equal
|
104
|
+
assert_equal 200, status
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_others_params_like_type
|
108
|
+
expected_params = {:type => "application/xml"}
|
109
|
+
middleware = build_middleware(:post, /comments/, expected_params)
|
110
|
+
|
111
|
+
env = build_env("POST", "/content/1/comments", 'multipart/related; type="application/json"')
|
112
|
+
status, headers, body = middleware.call(env)
|
113
|
+
|
114
|
+
assert_equal 415, status
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_multi
|
105
118
|
|
106
119
|
end
|
107
120
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-content_type_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
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: 2011-01-
|
19
|
+
date: 2011-01-08 00:00:00 -02:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|