mynyml-rack-supported-media-types 0.9 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +8 -3
- data/Rakefile +2 -2
- data/examples/recommended.ru +1 -1
- data/examples/simple.ru +2 -2
- data/lib/rack/supported_media_types.rb +17 -6
- data/rack-supported-media-types.gemspec +3 -3
- data/test/test_supported_media_types.rb +6 -6
- metadata +3 -3
data/README
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
===== Summary
|
2
2
|
|
3
3
|
Rack middleware to specify an app's supported media types.
|
4
|
-
Returns '
|
4
|
+
Returns '406 Not Acceptable' status when unsuported type is requested.
|
5
5
|
|
6
6
|
The requested type is assumed to be the first content-type in the Accept header's list.
|
7
7
|
|
@@ -17,7 +17,7 @@ will give you:
|
|
17
17
|
|
18
18
|
GET /foo
|
19
19
|
Accept: text/html,text/plain
|
20
|
-
#=>
|
20
|
+
#=> 406 Not Acceptable
|
21
21
|
|
22
22
|
GET /foo
|
23
23
|
Accept: application/xml,text/html
|
@@ -46,7 +46,7 @@ Rack::SupportedMediaTypes.
|
|
46
46
|
will result in:
|
47
47
|
|
48
48
|
GET /foo.html
|
49
|
-
#=>
|
49
|
+
#=> 406 Not Acceptable
|
50
50
|
|
51
51
|
GET /foo.xml
|
52
52
|
#=> 200 OK
|
@@ -58,3 +58,8 @@ will result in:
|
|
58
58
|
Accept header)
|
59
59
|
|
60
60
|
see examples/recommended.ru for example code.
|
61
|
+
|
62
|
+
===== Links
|
63
|
+
|
64
|
+
source:: http://github.com/mynyml/rack-supported-media-types
|
65
|
+
rdocs:: http://docs.github.com/mynyml/rack-supported-media-types
|
data/Rakefile
CHANGED
@@ -22,9 +22,9 @@ end
|
|
22
22
|
|
23
23
|
spec = Gem::Specification.new do |s|
|
24
24
|
s.name = 'rack-supported-media-types'
|
25
|
-
s.version = '0.9'
|
25
|
+
s.version = '0.9.2'
|
26
26
|
s.summary = "Rack middleware to specify an app's supported media types."
|
27
|
-
s.description = "Rack middleware to specify an app's supported media types. Returns '
|
27
|
+
s.description = "Rack middleware to specify an app's supported media types. Returns '406 Not Acceptable' status when unsuported type is requested."
|
28
28
|
s.author = "Martin Aumont"
|
29
29
|
s.email = 'mynyml@gmail.com'
|
30
30
|
s.homepage = ''
|
data/examples/recommended.ru
CHANGED
data/examples/simple.ru
CHANGED
@@ -17,7 +17,7 @@ require root + 'lib/rack/supported_media_types'
|
|
17
17
|
|
18
18
|
class App
|
19
19
|
def call(env)
|
20
|
-
[200, {'Content-Type' => 'text/html'}, ['now try <strong>/foo</strong> (will return
|
20
|
+
[200, {'Content-Type' => 'text/html'}, ['now try <strong>/foo</strong> (will return 406)']]
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -31,7 +31,7 @@ map '/' do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
map '/foo' do
|
34
|
-
# ... this will abort the request and return
|
34
|
+
# ... this will abort the request and return 406 Not Acceptable
|
35
35
|
use Rack::SupportedMediaTypes, %w( application/xml application/json )
|
36
36
|
run App.new
|
37
37
|
end
|
@@ -1,25 +1,36 @@
|
|
1
1
|
module Rack
|
2
2
|
class SupportedMediaTypes
|
3
3
|
|
4
|
+
#--
|
5
|
+
# NOTE
|
6
|
+
# Ths reason for adding nil to the list of accepted mime types:
|
7
|
+
#
|
8
|
+
# "If no Accept header field is present, then it is assumed that the client accepts all media types"
|
9
|
+
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
10
|
+
#
|
11
|
+
# Counter intuitive?
|
12
|
+
#
|
4
13
|
def initialize(app, mime_types)
|
5
|
-
@app, @mime_types = app, mime_types
|
14
|
+
@app, @mime_types = app, mime_types.push(nil)
|
6
15
|
end
|
7
16
|
|
8
17
|
#--
|
9
|
-
# return any headers with
|
18
|
+
# return any headers with 406?
|
10
19
|
def call(env)
|
11
20
|
@mime_types.include?(accept(env)) ?
|
12
21
|
@app.call(env) :
|
13
|
-
Rack::Response.new([],
|
22
|
+
Rack::Response.new([], 406).finish
|
14
23
|
end
|
15
24
|
|
16
25
|
private
|
17
26
|
#--
|
18
27
|
# First content-type in Accept header's list, without params
|
19
28
|
def accept(env)
|
20
|
-
|
21
|
-
|
22
|
-
|
29
|
+
header = env['HTTP_ACCEPT']
|
30
|
+
|
31
|
+
(header.nil? || header.empty?) ?
|
32
|
+
header :
|
33
|
+
header.split(',').first.split(';').first
|
23
34
|
end
|
24
35
|
end
|
25
36
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-supported-media-types
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Aumont
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-05 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: Rack middleware to specify an app's supported media types. Returns '
|
16
|
+
description: Rack middleware to specify an app's supported media types. Returns '406 Not Acceptable' status when unsuported type is requested.
|
17
17
|
email: mynyml@gmail.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -5,11 +5,11 @@ SMT = Rack::SupportedMediaTypes
|
|
5
5
|
|
6
6
|
class SupportedMediaTypesTest < Test::Unit::TestCase
|
7
7
|
|
8
|
-
test "returns
|
8
|
+
test "returns 406 Not Acceptable" do
|
9
9
|
app = SMT.new(App, %w( application/xml application/json ))
|
10
10
|
|
11
11
|
response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/html')
|
12
|
-
assert_equal
|
12
|
+
assert_equal 406, response.status
|
13
13
|
assert response.body.empty?
|
14
14
|
end
|
15
15
|
|
@@ -28,7 +28,7 @@ class SupportedMediaTypesTest < Test::Unit::TestCase
|
|
28
28
|
assert_equal 200, response.status
|
29
29
|
|
30
30
|
response = client.get('/', 'HTTP_ACCEPT' => 'application/xml,text/html')
|
31
|
-
assert_equal
|
31
|
+
assert_equal 406, response.status
|
32
32
|
end
|
33
33
|
|
34
34
|
test "ignores content-type params" do
|
@@ -38,12 +38,12 @@ class SupportedMediaTypesTest < Test::Unit::TestCase
|
|
38
38
|
assert_equal 200, response.status
|
39
39
|
end
|
40
40
|
|
41
|
-
test "nil Accept header" do
|
41
|
+
test "nil Accept header means all types accepted" do
|
42
42
|
app = SMT.new(App, %w( application/xml application/json ))
|
43
43
|
|
44
44
|
assert_nothing_raised do
|
45
45
|
response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => nil)
|
46
|
-
assert_equal
|
46
|
+
assert_equal 200, response.status
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -52,7 +52,7 @@ class SupportedMediaTypesTest < Test::Unit::TestCase
|
|
52
52
|
|
53
53
|
assert_nothing_raised do
|
54
54
|
response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => '')
|
55
|
-
assert_equal
|
55
|
+
assert_equal 406, response.status
|
56
56
|
end
|
57
57
|
end
|
58
58
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mynyml-rack-supported-media-types
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.9.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Aumont
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-04 21:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description: Rack middleware to specify an app's supported media types. Returns '
|
16
|
+
description: Rack middleware to specify an app's supported media types. Returns '406 Not Acceptable' status when unsuported type is requested.
|
17
17
|
email: mynyml@gmail.com
|
18
18
|
executables: []
|
19
19
|
|