rack-respond_to 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/LICENSE +19 -0
- data/Manifest +14 -0
- data/README.md +66 -0
- data/Rakefile +38 -0
- data/TODO +0 -0
- data/docs.watchr +26 -0
- data/examples/recommended_use.ru +40 -0
- data/examples/simple_app.ru +28 -0
- data/lib/rack/respond_to.rb +193 -0
- data/rack-respond_to.gemspec +16 -0
- data/specs.watchr +21 -0
- data/test/test_helper.rb +20 -0
- data/test/test_respond_to.rb +188 -0
- metadata +87 -0
data/.gitignore
ADDED
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/Manifest
ADDED
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
Summary
|
2
|
+
-------
|
3
|
+
Rack convenience middleware that allows triggering different actions based on
|
4
|
+
requested media type. Standalone version of the equivalent Rails functionality.
|
5
|
+
|
6
|
+
Features
|
7
|
+
--------
|
8
|
+
* Based on familiar API (Rails)
|
9
|
+
* Cascades down priority list of accepted media types
|
10
|
+
* Handles wildcard media types
|
11
|
+
* Simple to use
|
12
|
+
* Simple code (~50 LOCs)
|
13
|
+
* Flexible (standalone use)
|
14
|
+
* Decently documented (examples/ dir, source docs/rdocs)
|
15
|
+
* Compatible with other media type handling middleware (uses Rack::AcceptMediaTypes)
|
16
|
+
|
17
|
+
Install
|
18
|
+
-------
|
19
|
+
`gem install rack-respond_to`
|
20
|
+
|
21
|
+
Example
|
22
|
+
-------
|
23
|
+
require 'rack'
|
24
|
+
require 'rack/respond_to'
|
25
|
+
|
26
|
+
class App
|
27
|
+
include Rack::RespondTo #mixes in #respond_to
|
28
|
+
|
29
|
+
def call(env)
|
30
|
+
# Pass in the env, and RespondTo will retrieve the requested media types
|
31
|
+
Rack::RespondTo.env = env
|
32
|
+
|
33
|
+
# Alternatively, to use standalone you can also assign the media types
|
34
|
+
# directly (this will take precedence over the env)
|
35
|
+
#Rack::RespondTo.media_types = ['text/html']
|
36
|
+
|
37
|
+
body = respond_to do |format|
|
38
|
+
format.html { '<em>html</em>' }
|
39
|
+
format.xml { '<body>xml</body>' }
|
40
|
+
end
|
41
|
+
|
42
|
+
[200, {'Content-Type' => Rack::RespondTo.selected_media_type}, [body]]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
run App.new
|
47
|
+
|
48
|
+
See the examples/simple_app.ru for an executable example.
|
49
|
+
|
50
|
+
Tips
|
51
|
+
----
|
52
|
+
Use together with `Rack::AbstractFormat` to respond to routes based on url
|
53
|
+
extensions. For example, if you want `example.com/foo.xml` to trigger the
|
54
|
+
`format.xml` block (`Rack::AbstractFormat` moves the extension's media type
|
55
|
+
into `HTTP_ACCEPT` and makes it the highest ranked).
|
56
|
+
|
57
|
+
`gem install rack-abstract-format`
|
58
|
+
|
59
|
+
See examples/recommended_use.ru for a complete example.
|
60
|
+
|
61
|
+
Links
|
62
|
+
-----
|
63
|
+
|
64
|
+
* code: http://github.com/mynyml/rack-respond_to
|
65
|
+
* docs: http://docs.github.com/mynyml/rack-respond_to
|
66
|
+
* bugs: http://github.com/mynyml/rack-respond_to/issues
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
def gem_opt
|
2
|
+
defined?(Gem) ? "-rubygems" : ""
|
3
|
+
end
|
4
|
+
|
5
|
+
# --------------------------------------------------
|
6
|
+
# Tests
|
7
|
+
# --------------------------------------------------
|
8
|
+
task(:default => :"test:all")
|
9
|
+
|
10
|
+
namespace(:test) do
|
11
|
+
desc "Run tests"
|
12
|
+
task(:all) do
|
13
|
+
exit system("ruby #{gem_opt} test/test_respond_to.rb")
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Run all tests on multiple ruby versions (requires rvm)"
|
17
|
+
task(:portability) do
|
18
|
+
versions = %w( 1.8.6 1.8.7 1.9 1.9.2 )
|
19
|
+
versions.each do |version|
|
20
|
+
system <<-BASH
|
21
|
+
bash -c 'source ~/.rvm/scripts/rvm;
|
22
|
+
rvm use #{version};
|
23
|
+
echo "--------- #{version} ----------";
|
24
|
+
rake -s test:all'
|
25
|
+
BASH
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# --------------------------------------------------
|
31
|
+
# Docs
|
32
|
+
# --------------------------------------------------
|
33
|
+
desc "Generate YARD Documentation"
|
34
|
+
task(:yardoc) do
|
35
|
+
require 'yard'
|
36
|
+
YARD::CLI::Yardoc.run *%w( -o doc/yard --readme README - LICENSE )
|
37
|
+
end
|
38
|
+
|
data/TODO
ADDED
File without changes
|
data/docs.watchr
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Run me with:
|
2
|
+
# $ watchr docs.watchr
|
3
|
+
|
4
|
+
require 'yard'
|
5
|
+
# --------------------------------------------------
|
6
|
+
# Watchr Rules
|
7
|
+
# --------------------------------------------------
|
8
|
+
watch( 'lib/.*\.rb' ) { yard }
|
9
|
+
watch( 'README.md' ) { yard }
|
10
|
+
watch( 'LICENSE' ) { yard }
|
11
|
+
|
12
|
+
# --------------------------------------------------
|
13
|
+
# Signal Handling
|
14
|
+
# --------------------------------------------------
|
15
|
+
Signal.trap('QUIT') { yard } # Ctrl-\
|
16
|
+
Signal.trap('INT' ) { abort("\n") } # Ctrl-C
|
17
|
+
|
18
|
+
# --------------------------------------------------
|
19
|
+
# Helpers
|
20
|
+
# --------------------------------------------------
|
21
|
+
def yard
|
22
|
+
print "Updating yardocs... "; STDOUT.flush
|
23
|
+
YARD::CLI::Yardoc.run *%w( -o doc/yard --readme README.md --markup rdoc - LICENSE )
|
24
|
+
print "done\n"
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# install required dependency:
|
2
|
+
# $sudo gem install rack-abstract-format
|
3
|
+
#
|
4
|
+
# run with:
|
5
|
+
# $rackup examples/recommended_use.ru -p 8080
|
6
|
+
#
|
7
|
+
# and request:
|
8
|
+
# localhost:8080/foo.html
|
9
|
+
# localhost:8080/foo.xml
|
10
|
+
#
|
11
|
+
require 'pathname'
|
12
|
+
$:.unshift Pathname(__FILE__).dirname.parent + 'lib'
|
13
|
+
|
14
|
+
require 'rack'
|
15
|
+
require 'rack/abstract_format'
|
16
|
+
require 'rack/respond_to'
|
17
|
+
|
18
|
+
class App
|
19
|
+
include Rack::RespondTo
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
Rack::RespondTo.env = env
|
23
|
+
request = Rack::Request.new(env)
|
24
|
+
|
25
|
+
body = case request.path_info
|
26
|
+
when '/'
|
27
|
+
"try /foo<em>.html</em> and /foo<em>.xml</em>"
|
28
|
+
when '/foo'
|
29
|
+
respond_to do |format|
|
30
|
+
format.html { '<em>html</em>' }
|
31
|
+
format.xml { '<body>xml</body>' }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
[200, {'Content-Type' => Rack::RespondTo.selected_media_type || 'text/html'}, [body || '']]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
use Rack::AbstractFormat
|
40
|
+
run App.new
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# run me with:
|
2
|
+
# $rackup examples/simple_app.ru
|
3
|
+
#
|
4
|
+
require 'pathname'
|
5
|
+
$:.unshift Pathname(__FILE__).dirname.parent + 'lib'
|
6
|
+
|
7
|
+
require 'rack'
|
8
|
+
require 'rack/respond_to'
|
9
|
+
|
10
|
+
class App
|
11
|
+
include Rack::RespondTo
|
12
|
+
|
13
|
+
def call(env)
|
14
|
+
Rack::RespondTo.media_types = %w( application/xml )
|
15
|
+
|
16
|
+
body = case env['PATH_INFO']
|
17
|
+
when '/'
|
18
|
+
respond_to do |format|
|
19
|
+
format.html { '<em>html</em>' }
|
20
|
+
format.xml { '<body>xml</body>' }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
[200, {'Content-Type' => Rack::RespondTo.selected_media_type || ''}, [body || '']]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
run App.new
|
@@ -0,0 +1,193 @@
|
|
1
|
+
require 'rack/accept_media_types'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
|
5
|
+
# Based on Rails's API, and sinatra-respond_to (http://github.com/cehoffman/sinatra-respond_to)
|
6
|
+
#
|
7
|
+
# See examples/ directory for code examples.
|
8
|
+
#
|
9
|
+
module RespondTo
|
10
|
+
class << self
|
11
|
+
# Assign the environment directly to fetch the requested media types from
|
12
|
+
# env['HTTP_ACCEPT'] ('Accept:' request header).
|
13
|
+
#
|
14
|
+
# ===== Example
|
15
|
+
#
|
16
|
+
# def call(env)
|
17
|
+
# Rack::RespondTo.env = env
|
18
|
+
# #...
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
attr_accessor :env
|
22
|
+
|
23
|
+
# If used completely standalone, you can assign the requested media types
|
24
|
+
# directly.
|
25
|
+
#
|
26
|
+
# ===== Example
|
27
|
+
#
|
28
|
+
# RespondTo.media_types = ['application/xml']
|
29
|
+
#
|
30
|
+
attr_accessor :media_types
|
31
|
+
alias :mime_types= :media_types=
|
32
|
+
alias :mime_types :media_types
|
33
|
+
|
34
|
+
# Contains the media type that was responded to. Set after the respond_to
|
35
|
+
# block is called.
|
36
|
+
#
|
37
|
+
# Useful for setting the response's Content-Type:
|
38
|
+
#
|
39
|
+
# [200, {'Content-Type' => RespondTo.selected_media_type}, [body]]
|
40
|
+
#
|
41
|
+
attr_accessor :selected_media_type
|
42
|
+
alias :selected_mime_type= :selected_media_type=
|
43
|
+
alias :selected_mime_type :selected_media_type
|
44
|
+
|
45
|
+
def included(base) #:nodoc:
|
46
|
+
base.extend(ClassMethods)
|
47
|
+
base.class_eval do
|
48
|
+
include InstanceMethods
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Cast format to media type
|
53
|
+
#
|
54
|
+
# ===== Example
|
55
|
+
#
|
56
|
+
# RespondTo::MediaType('html') #=> 'text/html'
|
57
|
+
# RespondTo::MediaType('htm') #=> 'text/html'
|
58
|
+
#
|
59
|
+
def MediaType(format)
|
60
|
+
Rack::Mime.mime_type(format.sub(/^\./,'').insert(0,'.'))
|
61
|
+
end
|
62
|
+
alias :MimeType :MediaType
|
63
|
+
|
64
|
+
# Requested media types, in preferencial order
|
65
|
+
#
|
66
|
+
# ===== Examples
|
67
|
+
#
|
68
|
+
# RespondTo.env['HTTP_ACCEPT'] #=> 'text/html,application/xml'
|
69
|
+
# RespondTo.media_types #=> ['text/html', 'application/xml']
|
70
|
+
#
|
71
|
+
# RespondTo.env['HTTP_ACCEPT'] #=> 'text/html;q=0.7,application/xml;q=0.9,application/json;q=0.8'
|
72
|
+
# RespondTo.media_types #=> ['application/xml', 'application/json', 'text/html']
|
73
|
+
#
|
74
|
+
def media_types
|
75
|
+
@media_types || accept_list
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
def accept_list
|
80
|
+
self.env.nil? ? [] : Rack::AcceptMediaTypes.new(self.env['HTTP_ACCEPT'] || '')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
module InstanceMethods
|
85
|
+
# Delegates to the equivalent class method.
|
86
|
+
def respond_to(&block)
|
87
|
+
self.class.respond_to(&block)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
module ClassMethods
|
92
|
+
|
93
|
+
# Allows defining different actions and returns the one which corresponds
|
94
|
+
# to the highest ranking value in the RespondTo.media_types list.
|
95
|
+
#
|
96
|
+
# If no handler is defined for the highest ranking value, respond_to will
|
97
|
+
# cascade down the RespondTo.media_types list until it finds a match.
|
98
|
+
# Returns nil if there is no match.
|
99
|
+
#
|
100
|
+
# Wildcard media types (*/*, text/*, etc.) will trigger the first
|
101
|
+
# matching format definition, so order matters if you expect the Accept
|
102
|
+
# header to contain any (a nil Accept header, for instance, will be
|
103
|
+
# turned into '*/*' as per rfc2616-sec14.1). Simply define the 'default'
|
104
|
+
# handler first (usually html), and it will work like a charm.
|
105
|
+
#
|
106
|
+
# ===== Examples
|
107
|
+
#
|
108
|
+
# RespondTo.media_types = ['text/html', 'application/xml']
|
109
|
+
#
|
110
|
+
# respond_to do |format|
|
111
|
+
# format.html { 'html' }
|
112
|
+
# format.xml { 'xml' }
|
113
|
+
# end
|
114
|
+
# #=> 'html'
|
115
|
+
#
|
116
|
+
# RespondTo.media_types = ['text/html', 'application/xml']
|
117
|
+
#
|
118
|
+
# respond_to do |format|
|
119
|
+
# format.xml { 'xml' }
|
120
|
+
# format.txt { 'txt' }
|
121
|
+
# end
|
122
|
+
# #=> 'xml'
|
123
|
+
#
|
124
|
+
# RespondTo.media_types = ['text/html', 'application/json']
|
125
|
+
#
|
126
|
+
# respond_to do |format|
|
127
|
+
# format.xml { 'xml' }
|
128
|
+
# format.txt { 'txt' }
|
129
|
+
# end
|
130
|
+
# #=> nil
|
131
|
+
#
|
132
|
+
# RespondTo.media_types = ['*/*']
|
133
|
+
#
|
134
|
+
# respond_to do |format|
|
135
|
+
# format.html { 'html' }
|
136
|
+
# format.xml { 'xml' }
|
137
|
+
# end
|
138
|
+
# #=> 'html'
|
139
|
+
#
|
140
|
+
# RespondTo.media_types = ['*/*']
|
141
|
+
#
|
142
|
+
# respond_to do |format|
|
143
|
+
# format.xml { 'xml' }
|
144
|
+
# format.html { 'html' }
|
145
|
+
# end
|
146
|
+
# #=> 'xml'
|
147
|
+
#
|
148
|
+
# RespondTo.media_types = ['text/*']
|
149
|
+
#
|
150
|
+
# respond_to do |format|
|
151
|
+
# format.xml { 'xml' } # application/xml (skip)
|
152
|
+
# format.html { 'html' } # text/html (match)
|
153
|
+
# format.txt { 'txt' }
|
154
|
+
# end
|
155
|
+
# #=> 'html'
|
156
|
+
#
|
157
|
+
def respond_to
|
158
|
+
format = Format.new
|
159
|
+
yield format
|
160
|
+
type, handler = Helpers.match(RespondTo.media_types, format)
|
161
|
+
RespondTo.selected_media_type = type
|
162
|
+
handler.nil? ? nil : handler.call
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
# Helper methods, kept in a seperate namespace to avoid pollution.
|
167
|
+
module Helpers #:nodoc:
|
168
|
+
extend self
|
169
|
+
|
170
|
+
# TODO refactor
|
171
|
+
def match(media_types, format)
|
172
|
+
selected = []
|
173
|
+
accepted_types = media_types.map {|type| Regexp.escape(type).gsub(/\\\*/,'.*') }
|
174
|
+
accepted_types.each do |at|
|
175
|
+
format.each do |ht, handler|
|
176
|
+
(selected = [ht, handler]) and break if ht.match(at)
|
177
|
+
end
|
178
|
+
break unless selected.empty?
|
179
|
+
end
|
180
|
+
selected
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
# NOTE
|
185
|
+
# Array instead of hash because order matters (wildcard type matches first
|
186
|
+
# handler)
|
187
|
+
class Format < Array #:nodoc:
|
188
|
+
def method_missing(format, *args, &handler)
|
189
|
+
self << [RespondTo::MediaType(format.to_s), handler]
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
spec = Gem::Specification.new do |s|
|
2
|
+
s.name = "rack-respond_to"
|
3
|
+
s.version = "0.9.8"
|
4
|
+
s.summary = "Rack middleware port of Rails's respond_to feature"
|
5
|
+
s.description = "Rack middleware port of Rails's respond_to feature."
|
6
|
+
s.author = "Martin Aumont"
|
7
|
+
s.email = "mynyml@gmail.com"
|
8
|
+
s.homepage = "http://github.com/mynyml/rack-respond_to"
|
9
|
+
s.rubyforge_project = "rack-respond_to"
|
10
|
+
s.has_rdoc = false
|
11
|
+
s.require_path = "lib"
|
12
|
+
s.files = File.read("Manifest").strip.split("\n")
|
13
|
+
|
14
|
+
s.add_dependency 'rack-accept-media-types', '>= 0.6'
|
15
|
+
s.add_development_dependency 'minitest'
|
16
|
+
end
|
data/specs.watchr
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Run me with:
|
2
|
+
# $ watchr specs.watchr
|
3
|
+
|
4
|
+
# --------------------------------------------------
|
5
|
+
# Watchr Rules
|
6
|
+
# --------------------------------------------------
|
7
|
+
watch( '^(lib|test)/.*\.rb' ) { rake }
|
8
|
+
|
9
|
+
# --------------------------------------------------
|
10
|
+
# Signal Handling
|
11
|
+
# --------------------------------------------------
|
12
|
+
Signal.trap('QUIT') { rake } # Ctrl-\
|
13
|
+
Signal.trap('INT' ) { abort("\n") } # Ctrl-C
|
14
|
+
|
15
|
+
# --------------------------------------------------
|
16
|
+
# Helpers
|
17
|
+
# --------------------------------------------------
|
18
|
+
def rake(task='')
|
19
|
+
system "rake -s #{task}"
|
20
|
+
end
|
21
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'rack'
|
4
|
+
begin
|
5
|
+
require 'ruby-debug'
|
6
|
+
require 'phocus'
|
7
|
+
require 'redgreen'
|
8
|
+
rescue LoadError, RuntimeError
|
9
|
+
end
|
10
|
+
|
11
|
+
root = Pathname(__FILE__).dirname.parent.expand_path
|
12
|
+
$:.unshift(root.join('lib'))
|
13
|
+
|
14
|
+
require 'rack/respond_to'
|
15
|
+
|
16
|
+
class MiniTest::Unit::TestCase
|
17
|
+
def self.test(name, &block)
|
18
|
+
define_method(:"test_#{name.gsub(/\s/,'_')}", &block)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
|
3
|
+
class App
|
4
|
+
include Rack::RespondTo
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestRespondTo < MiniTest::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
Rack::RespondTo.selected_media_type = nil
|
11
|
+
Rack::RespondTo.media_types = nil
|
12
|
+
Rack::RespondTo.env = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
## api
|
16
|
+
|
17
|
+
test "env accessor" do
|
18
|
+
env = {'HTTP_ACCEPT' => 'text/html,application/xml'}
|
19
|
+
Rack::RespondTo.env = env
|
20
|
+
assert_equal env, Rack::RespondTo.env
|
21
|
+
end
|
22
|
+
|
23
|
+
test "media types accessor" do
|
24
|
+
Rack::RespondTo.media_types = %w( application/xml )
|
25
|
+
assert_equal %w( application/xml ), Rack::RespondTo.media_types
|
26
|
+
assert_equal %w( application/xml ), Rack::RespondTo.mime_types #alias
|
27
|
+
end
|
28
|
+
|
29
|
+
test "selected media type reader" do
|
30
|
+
Rack::RespondTo.media_types = %w( application/xml )
|
31
|
+
body = App.respond_to do |format|
|
32
|
+
format.xml { 'xml' }
|
33
|
+
end
|
34
|
+
assert_equal 'application/xml', Rack::RespondTo.selected_media_type
|
35
|
+
assert_equal 'application/xml', Rack::RespondTo.selected_mime_type #alias
|
36
|
+
end
|
37
|
+
|
38
|
+
test "mixin injects respond_to class method" do
|
39
|
+
assert App.respond_to?(:respond_to)
|
40
|
+
end
|
41
|
+
|
42
|
+
test "mixin injects respond_to instance method" do
|
43
|
+
assert App.new.respond_to?(:respond_to)
|
44
|
+
end
|
45
|
+
|
46
|
+
## requested media types
|
47
|
+
|
48
|
+
test "explicitly specified media types take precedence over header's" do
|
49
|
+
Rack::RespondTo.env = {'HTTP_ACCEPT' => 'text/html'}
|
50
|
+
Rack::RespondTo.media_types = %w( text/plain )
|
51
|
+
assert_equal %w( text/plain ), Rack::RespondTo.media_types
|
52
|
+
end
|
53
|
+
|
54
|
+
## respond_to
|
55
|
+
|
56
|
+
test "respond_to returns block for highest ranking format" do
|
57
|
+
Rack::RespondTo.env = {'HTTP_ACCEPT' => 'application/xml;q=0.8,text/plain;q=0.9'}
|
58
|
+
body = App.respond_to do |format|
|
59
|
+
format.xml { 'xml' }
|
60
|
+
format.txt { 'txt' }
|
61
|
+
end
|
62
|
+
assert_equal 'txt', body
|
63
|
+
end
|
64
|
+
|
65
|
+
test "cascades down the Accept header's list to find suitable type" do
|
66
|
+
Rack::RespondTo.env = {'HTTP_ACCEPT' => 'text/html,text/plain;q=0.9'}
|
67
|
+
body = App.respond_to do |format|
|
68
|
+
format.xml { 'xml' }
|
69
|
+
format.txt { 'txt' }
|
70
|
+
end
|
71
|
+
assert_equal 'txt', body
|
72
|
+
end
|
73
|
+
|
74
|
+
test "respond_to with no matching format" do
|
75
|
+
Rack::RespondTo.media_types = %w( text/html )
|
76
|
+
body = App.respond_to do |format|
|
77
|
+
format.xml { 'xml' }
|
78
|
+
format.txt { 'txt' }
|
79
|
+
end
|
80
|
+
assert_equal nil, body
|
81
|
+
end
|
82
|
+
|
83
|
+
test "respond_to with empty type list" do
|
84
|
+
Rack::RespondTo.media_types = []
|
85
|
+
body = App.respond_to do |format|
|
86
|
+
format.xml { 'xml' }
|
87
|
+
format.txt { 'txt' }
|
88
|
+
end
|
89
|
+
assert_equal nil, body
|
90
|
+
end
|
91
|
+
|
92
|
+
test "respond_to with nil type list" do
|
93
|
+
Rack::RespondTo.media_types = nil
|
94
|
+
body = App.respond_to do |format|
|
95
|
+
format.xml { 'xml' }
|
96
|
+
format.txt { 'txt' }
|
97
|
+
end
|
98
|
+
assert_equal nil, body
|
99
|
+
end
|
100
|
+
|
101
|
+
test "respond_to handles synonymous formats" do
|
102
|
+
Rack::RespondTo.media_types = %w( text/html )
|
103
|
+
|
104
|
+
body = App.respond_to do |format|
|
105
|
+
format.htm { 'htm' } # htm/html
|
106
|
+
format.xml { 'xml' }
|
107
|
+
end
|
108
|
+
assert_equal 'htm', body
|
109
|
+
|
110
|
+
body = App.respond_to do |format|
|
111
|
+
format.html { 'html' } # htm/html
|
112
|
+
format.json { 'json' }
|
113
|
+
end
|
114
|
+
assert_equal 'html', body
|
115
|
+
end
|
116
|
+
|
117
|
+
test "respond_to handles types with special chars" do
|
118
|
+
Rack::RespondTo.media_types = %w( application/rss+xml )
|
119
|
+
body = App.respond_to do |format|
|
120
|
+
format.rss { 'rss' }
|
121
|
+
end
|
122
|
+
assert_equal 'rss', body
|
123
|
+
|
124
|
+
Rack::RespondTo.media_types = %w( video/x-msvideo )
|
125
|
+
body = App.respond_to do |format|
|
126
|
+
format.avi { 'avi' }
|
127
|
+
end
|
128
|
+
assert_equal 'avi', body
|
129
|
+
|
130
|
+
Rack::RespondTo.media_types = %w( application/x-bzip2 )
|
131
|
+
body = App.respond_to do |format|
|
132
|
+
format.bz2 { 'bz2' }
|
133
|
+
end
|
134
|
+
assert_equal 'bz2', body
|
135
|
+
end
|
136
|
+
|
137
|
+
test "repond_to sets selected media type" do
|
138
|
+
Rack::RespondTo.media_types = %w( text/html text/plain )
|
139
|
+
assert_equal nil, Rack::RespondTo.selected_media_type
|
140
|
+
body = App.respond_to do |format|
|
141
|
+
format.xml { 'xml' }
|
142
|
+
format.txt { 'txt' }
|
143
|
+
end
|
144
|
+
assert_equal 'txt', body
|
145
|
+
assert_equal 'text/plain', Rack::RespondTo.selected_media_type
|
146
|
+
end
|
147
|
+
|
148
|
+
## wildcard media types
|
149
|
+
|
150
|
+
test "wildcard type matches first handler" do
|
151
|
+
Rack::RespondTo.media_types = %w( */* )
|
152
|
+
|
153
|
+
body = App.respond_to do |format|
|
154
|
+
format.xml { 'xml' }
|
155
|
+
format.txt { 'txt' }
|
156
|
+
end
|
157
|
+
assert_equal 'xml', body
|
158
|
+
|
159
|
+
body = App.respond_to do |format|
|
160
|
+
format.txt { 'txt' }
|
161
|
+
format.xml { 'xml' }
|
162
|
+
end
|
163
|
+
assert_equal 'txt', body
|
164
|
+
end
|
165
|
+
|
166
|
+
test "wildcard subtype matches first handler for type" do
|
167
|
+
Rack::RespondTo.media_types = %w( text/* )
|
168
|
+
|
169
|
+
body = App.respond_to do |format|
|
170
|
+
format.xml { 'xml' }
|
171
|
+
format.txt { 'txt' }
|
172
|
+
format.htm { 'htm' }
|
173
|
+
end
|
174
|
+
assert_equal 'txt', body
|
175
|
+
|
176
|
+
body = App.respond_to do |format|
|
177
|
+
format.xml { 'xml' }
|
178
|
+
format.htm { 'htm' }
|
179
|
+
format.txt { 'txt' }
|
180
|
+
end
|
181
|
+
assert_equal 'htm', body
|
182
|
+
|
183
|
+
body = App.respond_to do |format|
|
184
|
+
format.xml { 'xml' }
|
185
|
+
end
|
186
|
+
assert_equal nil, body
|
187
|
+
end
|
188
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-respond_to
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Aumont
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-29 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rack-accept-media-types
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0.6"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: minitest
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Rack middleware port of Rails's respond_to feature.
|
36
|
+
email: mynyml@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- LICENSE
|
46
|
+
- Manifest
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- TODO
|
50
|
+
- docs.watchr
|
51
|
+
- examples/recommended_use.ru
|
52
|
+
- examples/simple_app.ru
|
53
|
+
- lib/rack/respond_to.rb
|
54
|
+
- rack-respond_to.gemspec
|
55
|
+
- specs.watchr
|
56
|
+
- test/test_helper.rb
|
57
|
+
- test/test_respond_to.rb
|
58
|
+
has_rdoc: false
|
59
|
+
homepage: http://github.com/mynyml/rack-respond_to
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project: rack-respond_to
|
82
|
+
rubygems_version: 1.3.5
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Rack middleware port of Rails's respond_to feature
|
86
|
+
test_files: []
|
87
|
+
|