cehoffman-sinatra-respond_to 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +24 -9
- data/Rakefile +16 -0
- data/VERSION.yml +1 -1
- data/lib/sinatra/respond_to.rb +38 -15
- data/spec/app/public/static.txt +1 -0
- data/spec/app/test_app.rb +53 -0
- data/spec/app/unreachable_static.txt +1 -0
- data/spec/app/views/layout.html.haml +2 -0
- data/spec/app/views/resource.html.haml +1 -0
- data/spec/app/views/resource.js.erb +3 -0
- data/spec/app/views/resource.xml.builder +3 -0
- data/spec/extension_spec.rb +340 -0
- data/spec/spec_helper.rb +18 -0
- metadata +15 -4
data/README.markdown
CHANGED
@@ -5,10 +5,10 @@
|
|
5
5
|
|
6
6
|
require 'sinatra'
|
7
7
|
require 'sinatra/respond_to'
|
8
|
-
|
8
|
+
|
9
9
|
get '/posts' do
|
10
10
|
@posts = Post.recent
|
11
|
-
|
11
|
+
|
12
12
|
respond_to do |wants|
|
13
13
|
wants.html { haml :posts } # => views/posts.html.haml, also sets content_type to text/html
|
14
14
|
wants.rss { haml :posts } # => views/posts.rss.haml, also sets content_type to application/rss+xml
|
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
get '/post/:id' do
|
20
20
|
@post = Post.find(params[:id])
|
21
|
-
|
21
|
+
|
22
22
|
respond_to do |wants|
|
23
23
|
wants.html { haml :post } # => views/post.html.haml, also sets content_type to text/html
|
24
24
|
wants.xhtml { haml :post } # => views/post.xhtml.haml, also sets content_type to application/xhtml+xml
|
@@ -26,10 +26,10 @@
|
|
26
26
|
wants.js { erb :post } # => views/post.js.erb, also sets content_type to application/javascript
|
27
27
|
end
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
get '/comments/:id' do
|
31
31
|
@comment = Comment.find(params[:id])
|
32
|
-
|
32
|
+
|
33
33
|
respond_to do |wants|
|
34
34
|
wants.html { haml :comment } # => views/comment.html.haml, also sets content_type to text/html
|
35
35
|
wants.json { @comment.to_json } # => sets content_type to application/json
|
@@ -37,6 +37,20 @@
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
+
To change the character set of the response, there is a <tt>charset</tt> helper. For example
|
41
|
+
|
42
|
+
get '/iso-8859-1' do
|
43
|
+
charset 'iso-8859-1'
|
44
|
+
"This is now sent using iso-8859-1 character set"
|
45
|
+
end
|
46
|
+
|
47
|
+
get '/respond_to-mixed' do
|
48
|
+
respond_to do |wants|
|
49
|
+
wants.html { charset 'iso-8859-1'; "Some html in iso-8859-1" }
|
50
|
+
wants.xml { builder :utf-8-xml } # => this is returned in the default character set
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
40
54
|
## Configuration
|
41
55
|
|
42
56
|
There a few options available for configuring the default behavior of respond_to using Sinatra's
|
@@ -61,16 +75,17 @@ Due to the way respond\_to works, all incoming requests have the extension strip
|
|
61
75
|
This causes routes like the following to fail.
|
62
76
|
|
63
77
|
get '/style.css' do
|
78
|
+
content_type :css, :charset => 'utf-8'
|
64
79
|
sass :style # => renders views/style.sass
|
65
80
|
end
|
66
|
-
|
67
|
-
They need to be changed to the following
|
81
|
+
|
82
|
+
They need to be changed to the following. Note that you no longer have to set the content\_type or charset.
|
68
83
|
|
69
84
|
get '/style' do
|
70
85
|
sass :style # => renders views/style.css.sass
|
71
86
|
end
|
72
|
-
|
73
|
-
If you want to ensure the route only gets called for css requests try this
|
87
|
+
|
88
|
+
If you want to ensure the route only gets called for css requests try this. This will use sinatra's built in accept header matching.
|
74
89
|
|
75
90
|
get '/style', :provides => :css do
|
76
91
|
sass :style
|
data/Rakefile
CHANGED
@@ -11,4 +11,20 @@ begin
|
|
11
11
|
end
|
12
12
|
rescue LoadError
|
13
13
|
puts "Jewler not available. Install it with sugo gem install technicalpickles-jeweler -s http://gems.github.com"
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
require 'spec/rake/spectask'
|
18
|
+
desc "Run specs"
|
19
|
+
Spec::Rake::SpecTask.new do |t|
|
20
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
21
|
+
t.spec_opts = %w(-fp --color)
|
22
|
+
|
23
|
+
t.rcov = true
|
24
|
+
t.rcov_opts << '--text-summary'
|
25
|
+
t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
|
26
|
+
t.rcov_opts << '--exclude' << '.gem,pkg,spec'
|
27
|
+
end
|
28
|
+
rescue LoadError
|
29
|
+
puts "RSpec not available. Install it with sudo gem install rspec."
|
14
30
|
end
|
data/VERSION.yml
CHANGED
data/lib/sinatra/respond_to.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
1
3
|
# Simple note, accept header parsing was looked at but deamed
|
2
4
|
# too much of an irregularity to deal with. Problems with the header
|
3
5
|
# differences from IE, Firefox, Safari, and every other UA causes
|
@@ -38,12 +40,14 @@ module Sinatra
|
|
38
40
|
#
|
39
41
|
# and the format will automatically be available in as <tt>format</tt>
|
40
42
|
app.before do
|
41
|
-
unless options.static? && options.public? && ["GET", "HEAD"].include?(request.request_method) && static_file?(
|
43
|
+
unless options.static? && options.public? && ["GET", "HEAD"].include?(request.request_method) && static_file?(request.path_info)
|
42
44
|
request.path_info.gsub! %r{\.([^\./]+)$}, ''
|
43
45
|
format $1 || options.default_content
|
44
46
|
|
45
47
|
# For the oh so common case of actually wanting Javascript from an XmlHttpRequest
|
46
48
|
format :js if request.xhr? && options.assume_xhr_is_js?
|
49
|
+
|
50
|
+
charset options.default_charset if TEXT_MIME_TYPES.include? format
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
@@ -89,18 +93,23 @@ module Sinatra
|
|
89
93
|
|
90
94
|
app.error MissingTemplate do
|
91
95
|
content_type :html, :charset => 'utf-8'
|
96
|
+
response.status = 500 # If I can find out how to reference the error code from the exception, I would
|
92
97
|
|
93
98
|
engine = request.env['sinatra.error'].message[/\.([^\.]+)$/, 1]
|
99
|
+
engine = 'haml' unless ['haml', 'builder', 'erb'].include? engine
|
100
|
+
|
94
101
|
path = request.path_info[/([^\/]+)$/, 1]
|
95
102
|
path = "root" if path.nil? || path.empty?
|
96
|
-
|
103
|
+
|
104
|
+
format = engine == 'builder' ? 'xml' : 'html'
|
105
|
+
|
97
106
|
layout = case engine
|
98
107
|
when 'haml' then "!!!\n%html\n %body= yield"
|
99
108
|
when 'erb' then "<html>\n <body>\n <%= yield %>\n </body>\n</html>"
|
100
109
|
when 'builder' then "builder do |xml|\n xml << yield\nend"
|
101
110
|
end
|
102
111
|
|
103
|
-
layout = "<small>app
|
112
|
+
layout = "<small>app.#{format}.#{engine}</small>\n<pre>#{escape_html(layout)}</pre>"
|
104
113
|
|
105
114
|
(<<-HTML).gsub(/^ {10}/, '')
|
106
115
|
<!DOCTYPE html>
|
@@ -120,7 +129,7 @@ module Sinatra
|
|
120
129
|
<div id="c">
|
121
130
|
Try this:<br />
|
122
131
|
#{layout if layout}
|
123
|
-
<small>#{path}
|
132
|
+
<small>#{path}.#{format}.#{engine}</small>
|
124
133
|
<pre>Hello World!</pre>
|
125
134
|
<small>application.rb</small>
|
126
135
|
<pre>#{request.request_method.downcase} '#{request.path_info}' do\n respond_to do |wants|\n wants.#{engine == 'builder' ? 'xml' : 'html'} { #{engine} :#{path}#{",\n#{' '*32}layout => :app" if layout} }\n end\nend</pre>
|
@@ -135,7 +144,7 @@ module Sinatra
|
|
135
144
|
app.class_eval do
|
136
145
|
private
|
137
146
|
def render_with_format(*args)
|
138
|
-
args[1] = "#{args[1]}.#{format}".to_sym
|
147
|
+
args[1] = "#{args[1]}.#{format}".to_sym if args[1].is_a?(::Symbol)
|
139
148
|
render_without_format *args
|
140
149
|
rescue Errno::ENOENT
|
141
150
|
raise MissingTemplate, "#{args[1]}.#{args[0]}"
|
@@ -154,23 +163,43 @@ module Sinatra
|
|
154
163
|
|
155
164
|
module Helpers
|
156
165
|
def format(val=nil)
|
157
|
-
|
166
|
+
unless val.nil?
|
167
|
+
new_mime_type = media_type(val.to_sym)
|
168
|
+
fail "Unknown media type #{val}\nTry registering the extension with a mime type" if new_mime_type.nil?
|
169
|
+
|
170
|
+
request.env['sinatra.respond_to.format'] = val.to_sym
|
171
|
+
old_mime_type, params = response['Content-Type'].split(';', 2)
|
172
|
+
response['Content-Type'] = [new_mime_type, params].join(';')
|
173
|
+
end
|
174
|
+
|
158
175
|
request.env['sinatra.respond_to.format']
|
159
176
|
end
|
160
177
|
|
161
178
|
def static_file?(path)
|
162
179
|
return false unless path =~ /.*[^\/]$/
|
163
180
|
public_dir = File.expand_path(options.public)
|
164
|
-
path = File.expand_path(File.join(public_dir, unescape(
|
181
|
+
path = File.expand_path(File.join(public_dir, unescape(path)))
|
165
182
|
return false if path[0, public_dir.length] != public_dir
|
166
|
-
|
167
|
-
|
183
|
+
File.file?(path)
|
184
|
+
end
|
185
|
+
|
186
|
+
def charset(val=nil)
|
187
|
+
fail "Content-Type must be set in order to specify a charset" if response['Content-Type'].nil?
|
188
|
+
|
189
|
+
if response['Content-Type'] =~ /charset=[^ ;,]+/
|
190
|
+
response['Content-Type'].gsub!(/charset=[^ ;,]+/, (val == '' && '') || "charset=#{val}")
|
191
|
+
else
|
192
|
+
response['Content-Type'] += ";charset=#{val}"
|
193
|
+
end unless val.nil?
|
194
|
+
|
195
|
+
response['Content-Type'][/charset=([^ ;,]+)/, 1]
|
168
196
|
end
|
169
197
|
|
170
198
|
def respond_to(&block)
|
171
199
|
wants = {}
|
172
200
|
def wants.method_missing(type, *args, &block)
|
173
201
|
Sinatra::Base.send(:fail, "Unknown media type for respond_to: #{type}\nTry registering the extension with a mime type") if Sinatra::Base.media_type(type).nil?
|
202
|
+
options = args.pop if args.last.is_a?(::Hash)
|
174
203
|
self[type] = block
|
175
204
|
end
|
176
205
|
|
@@ -178,12 +207,6 @@ module Sinatra
|
|
178
207
|
|
179
208
|
handler = wants[format]
|
180
209
|
raise UnhandledFormat if handler.nil?
|
181
|
-
|
182
|
-
opts = [format]
|
183
|
-
opts << {:charset => options.default_charset} if TEXT_MIME_TYPES.include? format && response['Content-Type'] !~ /charset=/
|
184
|
-
|
185
|
-
content_type *opts
|
186
|
-
|
187
210
|
handler.call
|
188
211
|
end
|
189
212
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
A static file
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'erb'
|
3
|
+
require 'haml'
|
4
|
+
require 'builder'
|
5
|
+
|
6
|
+
class TestApp < Sinatra::Base
|
7
|
+
register Sinatra::RespondTo
|
8
|
+
|
9
|
+
set :views, File.join(File.dirname(__FILE__), 'views')
|
10
|
+
set :public, File.join(File.dirname(__FILE__), 'public')
|
11
|
+
|
12
|
+
get '/resource' do
|
13
|
+
respond_to do |wants|
|
14
|
+
wants.html { haml :resource }
|
15
|
+
wants.json { "We got some json" }
|
16
|
+
wants.xml { builder :resource }
|
17
|
+
wants.js { erb :resource }
|
18
|
+
wants.png { }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
get '/default_charset' do
|
23
|
+
respond_to do |wants|
|
24
|
+
wants.html { "Should set charcter set to default_charset" }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
get '/iso-8859-1' do
|
29
|
+
respond_to do |wants|
|
30
|
+
wants.html { charset 'iso-8859-1'; "Should have character set of iso-8859-1" }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
get '/normal-no-respond_to' do
|
35
|
+
"Just some plain old text"
|
36
|
+
end
|
37
|
+
|
38
|
+
get '/style.css' do
|
39
|
+
"This route should fail"
|
40
|
+
end
|
41
|
+
|
42
|
+
get '/style-no-extension', :provides => :css do
|
43
|
+
"Should succeed only when browser accepts text/css"
|
44
|
+
end
|
45
|
+
|
46
|
+
get '/missing-template' do
|
47
|
+
respond_to do |wants|
|
48
|
+
wants.html { haml :missing }
|
49
|
+
wants.xml { builder :missing }
|
50
|
+
wants.js { erb :missing }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Unreachable static file
|
@@ -0,0 +1 @@
|
|
1
|
+
Hello from HTML
|
@@ -0,0 +1,340 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe Sinatra::RespondTo do
|
4
|
+
describe "options" do
|
5
|
+
it "should initialize with :default_charset set to 'utf-8'" do
|
6
|
+
TestApp.default_charset.should == 'utf-8'
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should initialize with :default_content set to :html" do
|
10
|
+
TestApp.default_content.should == :html
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should initialize with :assume_xhr_is_js set to true" do
|
14
|
+
TestApp.assume_xhr_is_js be_true
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "assume_xhr_is_js" do
|
19
|
+
it "should set the content type to application/javascript for an XMLHttpRequest" do
|
20
|
+
header 'HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'
|
21
|
+
|
22
|
+
get '/resource'
|
23
|
+
|
24
|
+
last_response['Content-Type'].should =~ %r{#{Rack::Mime.mime_type(".js")}}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should not set the content type to application/javascript for an XMLHttpRequest when assume_xhr_is_js is false" do
|
28
|
+
TestApp.disable :assume_xhr_is_js
|
29
|
+
header 'HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'
|
30
|
+
get '/resource'
|
31
|
+
|
32
|
+
last_response['Content-Type'].should_not =~ %r{#{Rack::Mime.mime_type(".js")}}
|
33
|
+
|
34
|
+
# Put back the option, no side effects here
|
35
|
+
TestApp.enable :assume_xhr_is_js
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "extension routing" do
|
40
|
+
it "breaks routes expecting an extension" do
|
41
|
+
# In test_app.rb the route is defined as get '/style.css' instead of get '/style'
|
42
|
+
get "/style.css"
|
43
|
+
|
44
|
+
last_response.should_not be_ok
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should pick the default content option for routes with out an extension, and render haml templates" do
|
48
|
+
get "/resource"
|
49
|
+
|
50
|
+
last_response.body.should =~ %r{\s*<html>\s*<body>Hello from HTML</body>\s*</html>\s*}
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should render for a template using builder" do
|
54
|
+
get "/resource.xml"
|
55
|
+
|
56
|
+
last_response.body.should =~ %r{\s*<root>Some XML</root>\s*}
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should render for a template using erb" do
|
60
|
+
get "/resource.js"
|
61
|
+
|
62
|
+
last_response.body.should =~ %r{'Hiya from javascript'}
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should return string literals in block" do
|
66
|
+
get "/resource.json"
|
67
|
+
|
68
|
+
last_response.body.should =~ %r{We got some json}
|
69
|
+
end
|
70
|
+
|
71
|
+
# This will fail if the above is failing
|
72
|
+
it "should set the appropriate content-type for route with an extension" do
|
73
|
+
get "/resource.xml"
|
74
|
+
|
75
|
+
last_response['Content-Type'].should =~ %r{#{Rack::Mime.mime_type('.xml')}}
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should set the character set to the default character set" do
|
79
|
+
get "/default_charset"
|
80
|
+
|
81
|
+
last_response['Content-Type'].should =~ %r{charset=#{TestApp.default_charset}}
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should honor a change in character set in block" do
|
85
|
+
get "/iso-8859-1"
|
86
|
+
|
87
|
+
last_response['Content-Type'].should =~ %r{charset=iso-8859-1}
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should not set the character set when requesting a non text resource" do
|
91
|
+
get "/resource.png"
|
92
|
+
|
93
|
+
last_response['Content-Type'].should_not =~ /charset/
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should return not found when path does not exist" do
|
97
|
+
get "/nonexistant-path.txt"
|
98
|
+
|
99
|
+
last_response.status.should == 404
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "for static files" do
|
103
|
+
before(:all) do
|
104
|
+
TestApp.enable :static
|
105
|
+
end
|
106
|
+
|
107
|
+
after(:all) do
|
108
|
+
TestApp.disable :static
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should allow serving static files from public directory" do
|
112
|
+
get '/static.txt'
|
113
|
+
|
114
|
+
last_response.body.should == "A static file"
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should only serve files when static routing is enabled" do
|
118
|
+
TestApp.disable :static
|
119
|
+
get '/static.txt'
|
120
|
+
|
121
|
+
last_response.should_not be_ok
|
122
|
+
last_response.body.should_not == "A static file"
|
123
|
+
|
124
|
+
TestApp.enable :static
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should not allow serving static files from outside the public directory" do
|
128
|
+
get '/../unreachable_static.txt'
|
129
|
+
|
130
|
+
last_response.should_not be_ok
|
131
|
+
last_response.body.should_not == "Unreachable static file"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "routes not using respond_to" do
|
137
|
+
it "should set the default content type when no extension" do
|
138
|
+
get "/normal-no-respond_to"
|
139
|
+
|
140
|
+
last_response['Content-Type'].should =~ %r{#{Rack::Mime.mime_type(".#{TestApp.default_content}")}}
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should set the default character when no extension" do
|
144
|
+
get "/normal-no-respond_to"
|
145
|
+
|
146
|
+
last_response['Content-Type'].should =~ %r{charset=#{TestApp.default_charset}}
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should set the appropriate content type when given an extension" do
|
150
|
+
get "/normal-no-respond_to.css"
|
151
|
+
|
152
|
+
last_response['Content-Type'].should =~ %r{#{Rack::Mime.mime_type(".css")}}
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should set the default charset when given an extension" do
|
156
|
+
get "/normal-no-respond_to.css"
|
157
|
+
|
158
|
+
last_response['Content-Type'].should =~ %r{#{Rack::Mime.mime_type(".css")}}
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "error pages in development:" do
|
163
|
+
describe Sinatra::RespondTo::MissingTemplate do
|
164
|
+
it "should return 500 status when looking for a missing template" do
|
165
|
+
get '/missing-template'
|
166
|
+
|
167
|
+
last_response.status.should == 500
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should return 500 status when looking for a missing template in production" do
|
171
|
+
TestApp.set :environment, :production
|
172
|
+
get '/missing-template'
|
173
|
+
|
174
|
+
last_response.status.should == 500
|
175
|
+
|
176
|
+
TestApp.set :environment, :development
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should provide a helpful error message for a missing template when in development" do
|
180
|
+
get '/missing-template'
|
181
|
+
|
182
|
+
last_response.body.should =~ /missing\.html\.haml/
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should show the /__sinatra__/500.png" do
|
186
|
+
get '/missing-template'
|
187
|
+
|
188
|
+
last_response.body.should =~ %r{src='/__sinatra__/500.png'}
|
189
|
+
end
|
190
|
+
|
191
|
+
it "should provide a contextual code example for the template engine" do
|
192
|
+
# Haml
|
193
|
+
get '/missing-template'
|
194
|
+
|
195
|
+
last_response.body.should =~ %r{app.html.haml}
|
196
|
+
last_response.body.should =~ %r{missing-template.html.haml}
|
197
|
+
last_response.body.should =~ %r{get '/missing-template' do respond_to do |wants| wants.html \{ haml :missing-template, layout => :app \} end end}
|
198
|
+
|
199
|
+
# ERB
|
200
|
+
get '/missing-template.js'
|
201
|
+
|
202
|
+
last_response.body.should =~ %r{app.html.erb}
|
203
|
+
last_response.body.should =~ %r{missing-template.html.erb}
|
204
|
+
last_response.body.should =~ %r{get '/missing-template' do respond_to do |wants| wants.html \{ erb :missing-template, layout => :app \} end end}
|
205
|
+
|
206
|
+
# Builder
|
207
|
+
get '/missing-template.xml'
|
208
|
+
|
209
|
+
last_response.body.should =~ %r{app.xml.builder}
|
210
|
+
last_response.body.should =~ %r{missing-template.xml.builder}
|
211
|
+
last_response.body.should =~ %r{get '/missing-template' do respond_to do |wants| wants.xml \{ builder :missing-template, layout => :app \} end end}
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
describe Sinatra::RespondTo::UnhandledFormat do
|
216
|
+
it "should return with a 404 when an extension is not supported" do
|
217
|
+
get '/missing-template.txt'
|
218
|
+
|
219
|
+
last_response.status.should == 404
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should return with a 404 when an extension is not support in production" do
|
223
|
+
TestApp.set :environment, :production
|
224
|
+
get '/missing-template.txt'
|
225
|
+
|
226
|
+
last_response.status.should == 404
|
227
|
+
|
228
|
+
TestApp.set :environment, :development
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should provide a helpful error message for an unhandled format" do
|
232
|
+
get '/missing-template.txt'
|
233
|
+
|
234
|
+
last_response.body.should =~ %r{get '/missing-template' do respond_to do |wants| wants.txt \{ "Hello World" \} end end}
|
235
|
+
end
|
236
|
+
|
237
|
+
it "should show the /__sinatra__/404.png" do
|
238
|
+
get '/missing-template.txt'
|
239
|
+
|
240
|
+
last_response.body.should =~ %r{src='/__sinatra__/404.png'}
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
describe "helpers:" do
|
246
|
+
include Sinatra::RespondTo::Helpers
|
247
|
+
|
248
|
+
before(:each) do
|
249
|
+
stub!(:response).and_return({'Content-Type' => 'text/html'})
|
250
|
+
end
|
251
|
+
|
252
|
+
describe "charset" do
|
253
|
+
it "should set the working charset when called with a non blank string" do
|
254
|
+
response['Content-Type'].should_not =~ /charset/
|
255
|
+
|
256
|
+
charset 'utf-8'
|
257
|
+
|
258
|
+
response['Content-Type'].split(';').should include("charset=utf-8")
|
259
|
+
end
|
260
|
+
|
261
|
+
it "should remove the charset when called with a blank string" do
|
262
|
+
charset 'utf-8'
|
263
|
+
charset ''
|
264
|
+
|
265
|
+
response['Content-Type'].should_not =~ /charset/
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should return the current charset when called with nothing" do
|
269
|
+
charset 'utf-8'
|
270
|
+
|
271
|
+
charset.should == 'utf-8'
|
272
|
+
end
|
273
|
+
|
274
|
+
it "should fail when the response does not have a Content-Type" do
|
275
|
+
response.delete('Content-Type')
|
276
|
+
|
277
|
+
lambda { charset }.should raise_error
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
describe "format" do
|
282
|
+
before(:each) do
|
283
|
+
stub!(:request).and_return(Rack::Request.new({}))
|
284
|
+
end
|
285
|
+
|
286
|
+
def media_type(sym)
|
287
|
+
Sinatra::Base.media_type(sym)
|
288
|
+
end
|
289
|
+
|
290
|
+
it "should set the correct mime type when given an extension" do
|
291
|
+
format :xml
|
292
|
+
|
293
|
+
response['Content-Type'].split(';').should include(Rack::Mime.mime_type(".xml"))
|
294
|
+
end
|
295
|
+
|
296
|
+
it "should fail when set to an unknown extension type" do
|
297
|
+
lambda { format :bogus }.should raise_error
|
298
|
+
end
|
299
|
+
|
300
|
+
it "should return the current mime type extension" do
|
301
|
+
format :js
|
302
|
+
|
303
|
+
format.should == :js
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
describe "static_file?" do
|
308
|
+
before(:all) do
|
309
|
+
TestApp.enable :static
|
310
|
+
@static_folder = "/static folder/"
|
311
|
+
@reachable_static_file = "/static.txt"
|
312
|
+
@unreachable_static_file = "/../unreachable_static.txt"
|
313
|
+
end
|
314
|
+
|
315
|
+
after(:all) do
|
316
|
+
TestApp.disable :static
|
317
|
+
end
|
318
|
+
|
319
|
+
def options
|
320
|
+
TestApp
|
321
|
+
end
|
322
|
+
|
323
|
+
def unescape(path)
|
324
|
+
Rack::Utils.unescape(path)
|
325
|
+
end
|
326
|
+
|
327
|
+
it "should return true if the request path points to a file in the public directory" do
|
328
|
+
static_file?(@reachable_static_file).should be_true
|
329
|
+
end
|
330
|
+
|
331
|
+
it "should return false when pointing to files outside of the public directory" do
|
332
|
+
static_file?(@unreachable_static_file).should be_false
|
333
|
+
end
|
334
|
+
|
335
|
+
it "should return false when the path is for a folder" do
|
336
|
+
static_file?(@static_folder).should be_false
|
337
|
+
end
|
338
|
+
end
|
339
|
+
end
|
340
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'spec'
|
5
|
+
require 'rack/test'
|
6
|
+
|
7
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'sinatra', 'respond_to')
|
8
|
+
require File.join(File.dirname(__FILE__), 'app', 'test_app')
|
9
|
+
|
10
|
+
Spec::Runner.configure do |config|
|
11
|
+
def app
|
12
|
+
@app = Rack::Builder.new do
|
13
|
+
run TestApp
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
config.include Rack::Test::Methods
|
18
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cehoffman-sinatra-respond_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Hoffman
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-05-
|
12
|
+
date: 2009-05-13 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -37,6 +37,15 @@ files:
|
|
37
37
|
- Rakefile
|
38
38
|
- VERSION.yml
|
39
39
|
- lib/sinatra/respond_to.rb
|
40
|
+
- spec/app/public/static.txt
|
41
|
+
- spec/app/test_app.rb
|
42
|
+
- spec/app/unreachable_static.txt
|
43
|
+
- spec/app/views/layout.html.haml
|
44
|
+
- spec/app/views/resource.html.haml
|
45
|
+
- spec/app/views/resource.js.erb
|
46
|
+
- spec/app/views/resource.xml.builder
|
47
|
+
- spec/extension_spec.rb
|
48
|
+
- spec/spec_helper.rb
|
40
49
|
has_rdoc: true
|
41
50
|
homepage: http://github.com/cehoffman/sinatra-respond_to
|
42
51
|
post_install_message:
|
@@ -63,5 +72,7 @@ rubygems_version: 1.2.0
|
|
63
72
|
signing_key:
|
64
73
|
specification_version: 2
|
65
74
|
summary: A respond_to style Rails block for baked-in web service support in Sinatra
|
66
|
-
test_files:
|
67
|
-
|
75
|
+
test_files:
|
76
|
+
- spec/app/test_app.rb
|
77
|
+
- spec/extension_spec.rb
|
78
|
+
- spec/spec_helper.rb
|