mynyml-rack-supported-media-types 0.8 → 0.9

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 CHANGED
@@ -0,0 +1,60 @@
1
+ ===== Summary
2
+
3
+ Rack middleware to specify an app's supported media types.
4
+ Returns '415 Unsuported Media Type' status when unsuported type is requested.
5
+
6
+ The requested type is assumed to be the first content-type in the Accept header's list.
7
+
8
+ ===== Example
9
+
10
+ require 'rack'
11
+ require 'rack/supported_media_types'
12
+
13
+ use Rack::SupportedMediaTypes, %w( application/xml application/json )
14
+ run App.new
15
+
16
+ will give you:
17
+
18
+ GET /foo
19
+ Accept: text/html,text/plain
20
+ #=> 415 Unsupported Media Type
21
+
22
+ GET /foo
23
+ Accept: application/xml,text/html
24
+ #=> 200 OK
25
+
26
+ GET /foo
27
+ Accept: application/json,text/html
28
+ #=> 200 OK
29
+
30
+ see examples/simple.ru for example code.
31
+
32
+ ===== Tip
33
+
34
+ To read the requested type from the url's extension instead of the Accept
35
+ header (which is a more realistic use case), use Rack::AbstractFormat before
36
+ Rack::SupportedMediaTypes.
37
+
38
+ require 'rack'
39
+ require 'rack/abstract_format'
40
+ require 'rack/supported_media_types'
41
+
42
+ use Rack::AbstractFormat
43
+ use Rack::SupportedMediaTypes, %w( application/xml application/json )
44
+ run App.new
45
+
46
+ will result in:
47
+
48
+ GET /foo.html
49
+ #=> 415 Unsupported Media Type
50
+
51
+ GET /foo.xml
52
+ #=> 200 OK
53
+
54
+ GET /foo.json
55
+ #=> 200 OK
56
+
57
+ (Rack::AbstractFormat prepends the media type for the url's extension to the
58
+ Accept header)
59
+
60
+ see examples/recommended.ru for example code.
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ end
22
22
 
23
23
  spec = Gem::Specification.new do |s|
24
24
  s.name = 'rack-supported-media-types'
25
- s.version = '0.8'
25
+ s.version = '0.9'
26
26
  s.summary = "Rack middleware to specify an app's supported media types."
27
27
  s.description = "Rack middleware to specify an app's supported media types. Returns '415 Unsuported Media Type' status when unsuported type is requested."
28
28
  s.author = "Martin Aumont"
@@ -0,0 +1,34 @@
1
+ # run me with:
2
+ # $rackup examples/recommended.ru -p 3000
3
+ #
4
+ require 'pathname'
5
+ require 'rubygems'
6
+ require 'rack'
7
+ require 'rack/abstract_format'
8
+
9
+ root = Pathname(__FILE__).dirname.parent.expand_path
10
+ require root + 'lib/rack/supported_media_types'
11
+
12
+ class App
13
+ def call(env)
14
+ body = <<-BODY
15
+ <pre>
16
+ try using extensions:
17
+
18
+ - /foo.html
19
+ - /foo.xml
20
+ - /foo.json
21
+ - /foo.txt
22
+ - ...
23
+
24
+ app will only accept .html and .xml requests. others will return 415
25
+ (check server's output in console to see it)
26
+ </pre>
27
+ BODY
28
+ [200, {'Content-Type' => 'text/html'}, [body]]
29
+ end
30
+ end
31
+
32
+ use Rack::AbstractFormat
33
+ use Rack::SupportedMediaTypes, %w( text/html application/xml )
34
+ run App.new
@@ -0,0 +1,37 @@
1
+ # run me with:
2
+ # $rackup examples/simple.ru -p 3000
3
+ #
4
+ # then point your browser to:
5
+ # localhost:3000/
6
+ # localhost:3000/foo
7
+ #
8
+ # and look for the server's output in the console (pay attention to status
9
+ # codes)
10
+ #
11
+ require 'pathname'
12
+ require 'rubygems'
13
+ require 'rack'
14
+
15
+ root = Pathname(__FILE__).dirname.parent.expand_path
16
+ require root + 'lib/rack/supported_media_types'
17
+
18
+ class App
19
+ def call(env)
20
+ [200, {'Content-Type' => 'text/html'}, ['now try <strong>/foo</strong> (will return 415)']]
21
+ end
22
+ end
23
+
24
+ # given that the request comes from a webbrowser, and that it's Accept header's
25
+ # first value is text/html...
26
+
27
+ map '/' do
28
+ # ... this will allow it through to the app
29
+ use Rack::SupportedMediaTypes, %w( text/html application/xml )
30
+ run App.new
31
+ end
32
+
33
+ map '/foo' do
34
+ # ... this will abort the request and return 415 Unsupported Media Type
35
+ use Rack::SupportedMediaTypes, %w( application/xml application/json )
36
+ run App.new
37
+ end
@@ -1,24 +1,25 @@
1
1
  module Rack
2
2
  class SupportedMediaTypes
3
+
3
4
  def initialize(app, mime_types)
4
5
  @app, @mime_types = app, mime_types
5
6
  end
6
7
 
8
+ #--
9
+ # return any headers with 415?
7
10
  def call(env)
8
- type = requested_mime_type(env)
9
- if @mime_types.include?(type)
10
- @app.call(env)
11
- else
11
+ @mime_types.include?(accept(env)) ?
12
+ @app.call(env) :
12
13
  Rack::Response.new([], 415).finish
13
- end
14
14
  end
15
15
 
16
- def requested_mime_type(env)
17
- format = env['request.format']
18
-
19
- Rack::Mime::MIME_TYPES.values.include?(format) ?
20
- format :
21
- Rack::Mime.mime_type(format.sub(/^\./,'').insert(0,'.'), nil)
22
- end
16
+ private
17
+ #--
18
+ # First content-type in Accept header's list, without params
19
+ def accept(env)
20
+ if env['HTTP_ACCEPT'] && !env['HTTP_ACCEPT'].empty?
21
+ env['HTTP_ACCEPT'].split(',').first.split(';').first
22
+ end
23
+ end
23
24
  end
24
25
  end
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-supported-media-types
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.9"
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-04 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rack middleware to specify an app's supported media types. Returns '415 Unsuported Media Type' status when unsuported type is requested.
17
+ email: mynyml@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - test
27
+ - test/test_supported_media_types.rb
28
+ - test/test_helper.rb
29
+ - examples
30
+ - examples/simple.ru
31
+ - examples/recommended.ru
32
+ - lib
33
+ - lib/rack
34
+ - lib/rack/supported_media_types.rb
35
+ - LICENSE
36
+ - rack-supported-media-types.gemspec
37
+ - README
38
+ has_rdoc: true
39
+ homepage: ""
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.3
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Rack middleware to specify an app's supported media types.
66
+ test_files: []
67
+
data/test/test_helper.rb CHANGED
@@ -2,9 +2,9 @@ require 'pathname'
2
2
  require 'test/unit'
3
3
  require 'rubygems'
4
4
  require 'rack'
5
- require 'phocus/test_unit'
6
5
  begin
7
6
  require 'ruby-debug'
7
+ require 'phocus/test_unit'
8
8
  rescue LoadError, RuntimeError
9
9
  end
10
10
 
@@ -1,49 +1,58 @@
1
1
  require 'test/test_helper'
2
2
 
3
- InnerApp = Rack::Builder.new {
4
- use Rack::Lint
5
- run lambda {|env| [200, {'Content-Type' => 'text/html'}, ['content']] }
6
- }.to_app
3
+ App = lambda {|env| [200, {'Content-Type' => 'text/html'}, ['content']] }
4
+ SMT = Rack::SupportedMediaTypes
7
5
 
8
- class SupportedMediaTypeTest < Test::Unit::TestCase
6
+ class SupportedMediaTypesTest < Test::Unit::TestCase
9
7
 
10
8
  test "returns 415 Unsuported Media Type" do
11
- app = Rack::Builder.new {
12
- use Rack::SupportedMediaTypes, %w( application/xml text/html )
13
- run InnerApp
14
- }.to_app
15
-
16
- client = Rack::MockRequest.new(app)
17
- response = client.get('/', 'request.format' => '.txt')
9
+ app = SMT.new(App, %w( application/xml application/json ))
18
10
 
11
+ response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/html')
19
12
  assert_equal 415, response.status
20
13
  assert response.body.empty?
21
14
  end
22
15
 
23
16
  test "lets request through when media type is supported" do
24
- app = Rack::Builder.new {
25
- use Rack::SupportedMediaTypes, %w( text/plain application/xml )
26
- run InnerApp
27
- }.to_app
28
-
29
- client = Rack::MockRequest.new(app)
30
- response = client.get('/', 'request.format' => '.txt')
17
+ app = SMT.new(App, %w( text/html application/xml ))
31
18
 
19
+ response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'text/html')
32
20
  assert_equal 200, response.status
33
21
  end
34
22
 
35
- test "normalizes request format" do
36
- app = Rack::Builder.new {
37
- use Rack::SupportedMediaTypes, %w( text/html )
38
- run InnerApp
39
- }.to_app
40
-
23
+ test "requested type is assumed to be first type in Accept header's list" do
24
+ app = SMT.new(App, %w( text/html ))
41
25
  client = Rack::MockRequest.new(app)
42
26
 
43
- response = client.get('/', 'request.format' => 'html')
27
+ response = client.get('/', 'HTTP_ACCEPT' => 'text/html,application/xml')
44
28
  assert_equal 200, response.status
45
29
 
46
- response = client.get('/', 'request.format' => 'text/html')
30
+ response = client.get('/', 'HTTP_ACCEPT' => 'application/xml,text/html')
31
+ assert_equal 415, response.status
32
+ end
33
+
34
+ test "ignores content-type params" do
35
+ app = SMT.new(App, %w( application/xml application/json ))
36
+
37
+ response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => 'application/xml;q=0.9')
47
38
  assert_equal 200, response.status
48
39
  end
40
+
41
+ test "nil Accept header" do
42
+ app = SMT.new(App, %w( application/xml application/json ))
43
+
44
+ assert_nothing_raised do
45
+ response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => nil)
46
+ assert_equal 415, response.status
47
+ end
48
+ end
49
+
50
+ test "empty Accept header" do
51
+ app = SMT.new(App, %w( application/xml application/json ))
52
+
53
+ assert_nothing_raised do
54
+ response = Rack::MockRequest.new(app).get('/', 'HTTP_ACCEPT' => '')
55
+ assert_equal 415, response.status
56
+ end
57
+ end
49
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: "0.8"
4
+ version: "0.9"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Aumont
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-01 21:00:00 -07:00
12
+ date: 2009-06-03 21:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,10 +26,14 @@ files:
26
26
  - test
27
27
  - test/test_supported_media_types.rb
28
28
  - test/test_helper.rb
29
+ - examples
30
+ - examples/simple.ru
31
+ - examples/recommended.ru
29
32
  - lib
30
33
  - lib/rack
31
34
  - lib/rack/supported_media_types.rb
32
35
  - LICENSE
36
+ - rack-supported-media-types.gemspec
33
37
  - README
34
38
  has_rdoc: true
35
39
  homepage: ""