public
Fork of bmizerany/sinatra
Description: Classy web-development dressed in a DSL
Homepage: http://sinatra.rubyforge.org
Clone URL: git://github.com/sr/sinatra.git
redirect
sr (author)
Fri Aug 29 03:38:07 -0700 2008
commit  7948402f900d89d8a9b965aac3621b1a65936da6
tree    56c0b2720485c920b4206dbd39783b251876094b
parent  4ca3885709ba5a620e3ee24020d028c482d2b002
...
374
375
376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
378
379
...
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
0
@@ -374,6 +374,26 @@ module Sinatra
0
     end
0
     alias :header :headers        
0
 
0
+    # Immediately halt response execution by redirecting to the resource
0
+    # specified. The +path+ argument may be an absolute URL or a path
0
+    # relative to the site root. Additional arguments are passed to the
0
+    # halt.
0
+    #
0
+    # With no integer status code, a '302 Temporary Redirect' response is
0
+    # sent. To send a permanent redirect, pass an explicit status code of
0
+    # 301:
0
+    #
0
+    #   redirect '/somewhere/else', 301
0
+    #
0
+    # NOTE: No attempt is made to rewrite the path based on application
0
+    # context. The 'Location' response header is set verbatim to the value
0
+    # provided.
0
+    def redirect(path, *args)
0
+      status(302)
0
+      header 'Location' => path
0
+      stop *args
0
+    end
0
+
0
     # Set the content type of the response body (HTTP 'Content-Type' header).
0
     #
0
     # The +type+ argument may be an internet media type (e.g., 'text/html',
...
168
169
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
0
@@ -168,3 +168,55 @@ context "A Sinatra::Application with custom Content-Type" do
0
     @response.body.should.equal "one less bitch"
0
   end
0
 end
0
+
0
+context "A Sinatra::Application with redirects" do
0
+  before do
0
+    @app = Sinatra::Application.new do
0
+      get '/' do
0
+        redirect '/blake'
0
+      end
0
+
0
+      get '/blake' do
0
+        'Mizerany'
0
+      end
0
+    end
0
+
0
+    @request = Rack::MockRequest.new(@app)
0
+  end
0
+
0
+  specify "follows redirects" do
0
+    @response = @request.get('/')
0
+    @response.should.be.redirection
0
+    @response.body.should.equal ''
0
+    follow!
0
+    @response.should.be.ok
0
+    @response.body.should.equal 'Mizerany'
0
+  end
0
+
0
+  specify "renders a body with a redirect" do
0
+    Sinatra::EventContext.any_instance.expects(:foo).returns('blah')
0
+
0
+    @app.get '/foo' do
0
+      redirect 'foo', :foo
0
+    end
0
+    @request = Rack::MockRequest.new(@app)
0
+
0
+    @response = @request.get('/foo')
0
+    @response.should.be.redirection
0
+    @response['Location'].should.equal 'foo'
0
+    @response.body.should.equal 'blah'
0
+  end
0
+
0
+  specify "redirects permanently with 301 status code" do
0
+    @app.get '/perma' do
0
+      redirect 'foo', 301
0
+    end
0
+    @request = Rack::MockRequest.new(@app)
0
+
0
+    @response = @request.get('/perma')
0
+    @response.should.be.redirection
0
+    @response['Location'].should.equal 'foo'
0
+    @response.status.should.equal 301
0
+    @response.body.should.be.empty
0
+  end
0
+end
...
14
15
16
17
 
 
 
 
18
...
14
15
16
 
17
18
19
20
21
0
@@ -14,5 +14,8 @@ class Test::Unit::TestCase
0
   def context_for(*args)
0
     Sinatra::EventContext.new(env_for(*args))
0
   end
0
-  
0
+
0
+  def follow!
0
+    @response = @request.get(@response.location)
0
+  end
0
 end

Comments