mynyml-rack-supported-media-types 0.8

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright © 2009 Martin Aumont (mynyml)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,66 @@
1
+ # --------------------------------------------------
2
+ # based on thin's Rakefile (http://github.com/macournoyer/thin)
3
+ # --------------------------------------------------
4
+ require 'rake/gempackagetask'
5
+ require 'rake/rdoctask'
6
+ require 'pathname'
7
+ require 'yaml'
8
+
9
+ RUBY_1_9 = RUBY_VERSION =~ /^1\.9/
10
+ WIN = (RUBY_PLATFORM =~ /mswin|cygwin/)
11
+ SUDO = (WIN ? "" : "sudo")
12
+
13
+ def gem
14
+ RUBY_1_9 ? 'gem19' : 'gem'
15
+ end
16
+
17
+ def all_except(res)
18
+ Dir['**/*'].reject do |path|
19
+ Array(res).any? {|re| path.match(re) }
20
+ end
21
+ end
22
+
23
+ spec = Gem::Specification.new do |s|
24
+ s.name = 'rack-supported-media-types'
25
+ s.version = '0.8'
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 '415 Unsuported Media Type' status when unsuported type is requested."
28
+ s.author = "Martin Aumont"
29
+ s.email = 'mynyml@gmail.com'
30
+ s.homepage = ''
31
+ s.has_rdoc = true
32
+ s.require_path = "lib"
33
+ s.files = all_except([/doc/, /pkg/])
34
+ end
35
+
36
+ desc "Generate rdoc documentation."
37
+ Rake::RDocTask.new("rdoc") { |rdoc|
38
+ rdoc.rdoc_dir = 'doc/rdoc'
39
+ rdoc.title = "Rack::SupportedMediaTypes"
40
+ rdoc.options << '--line-numbers' << '--inline-source'
41
+ rdoc.options << '--charset' << 'utf-8'
42
+ rdoc.rdoc_files.include('README')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ }
45
+
46
+ Rake::GemPackageTask.new(spec) do |p|
47
+ p.gem_spec = spec
48
+ end
49
+
50
+ desc "Remove package products"
51
+ task :clean => :clobber_package
52
+
53
+ desc "Update the gemspec for GitHub's gem server"
54
+ task :gemspec do
55
+ Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
56
+ end
57
+
58
+ desc "Install gem"
59
+ task :install => [:clobber, :package] do
60
+ sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
61
+ end
62
+
63
+ desc "Uninstall gem"
64
+ task :uninstall => :clean do
65
+ sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
66
+ end
@@ -0,0 +1,24 @@
1
+ module Rack
2
+ class SupportedMediaTypes
3
+ def initialize(app, mime_types)
4
+ @app, @mime_types = app, mime_types
5
+ end
6
+
7
+ def call(env)
8
+ type = requested_mime_type(env)
9
+ if @mime_types.include?(type)
10
+ @app.call(env)
11
+ else
12
+ Rack::Response.new([], 415).finish
13
+ end
14
+ end
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
23
+ end
24
+ end
@@ -0,0 +1,22 @@
1
+ require 'pathname'
2
+ require 'test/unit'
3
+ require 'rubygems'
4
+ require 'rack'
5
+ require 'phocus/test_unit'
6
+ begin
7
+ require 'ruby-debug'
8
+ rescue LoadError, RuntimeError
9
+ end
10
+
11
+ root = Pathname(__FILE__).dirname.parent.expand_path
12
+ $:.unshift(root.join('lib'))
13
+
14
+ require 'rack/supported_media_types'
15
+
16
+ class Test::Unit::TestCase
17
+ def self.test(name, &block)
18
+ name = :"test_#{name.gsub(/\s/,'_')}"
19
+ define_method(name, &block)
20
+ end
21
+ end
22
+
@@ -0,0 +1,49 @@
1
+ require 'test/test_helper'
2
+
3
+ InnerApp = Rack::Builder.new {
4
+ use Rack::Lint
5
+ run lambda {|env| [200, {'Content-Type' => 'text/html'}, ['content']] }
6
+ }.to_app
7
+
8
+ class SupportedMediaTypeTest < Test::Unit::TestCase
9
+
10
+ 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')
18
+
19
+ assert_equal 415, response.status
20
+ assert response.body.empty?
21
+ end
22
+
23
+ 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')
31
+
32
+ assert_equal 200, response.status
33
+ end
34
+
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
+
41
+ client = Rack::MockRequest.new(app)
42
+
43
+ response = client.get('/', 'request.format' => 'html')
44
+ assert_equal 200, response.status
45
+
46
+ response = client.get('/', 'request.format' => 'text/html')
47
+ assert_equal 200, response.status
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mynyml-rack-supported-media-types
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.8"
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-01 21:00:00 -07: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
+ - lib
30
+ - lib/rack
31
+ - lib/rack/supported_media_types.rb
32
+ - LICENSE
33
+ - README
34
+ has_rdoc: true
35
+ homepage: ""
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: Rack middleware to specify an app's supported media types.
60
+ test_files: []
61
+