From facd4f143bb3f0bb44d89b6ff11e61a0b7c36427 Mon Sep 17 00:00:00 2001 From: Stormwind Date: Fri, 30 Nov 2012 18:58:32 +0100 Subject: Add redirect to response Now a relocation can be done, by calling redirect with the URL you want relocate to. This will set the Location header and set the status code to 302. --- lib/zero/response.rb | 9 +++++++++ spec/unit/response/response_spec.rb | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/zero/response.rb b/lib/zero/response.rb index 24c1aaf..a3e1651 100644 --- a/lib/zero/response.rb +++ b/lib/zero/response.rb @@ -67,5 +67,14 @@ module Zero self.header['Content-Type'] = value end + # Sets the Location header to the given URL and the status code to 302. + # + # @param [String] location Redirect URL + # + def redirect(location) + self.status = 302 + self.header['Location'] = location + end + end end diff --git a/spec/unit/response/response_spec.rb b/spec/unit/response/response_spec.rb index 9d2097a..8edc48d 100644 --- a/spec/unit/response/response_spec.rb +++ b/spec/unit/response/response_spec.rb @@ -125,4 +125,14 @@ describe Zero::Response do end end + describe '#redirect' do + it "sets the status to 302 and the given Location URL in header" do + subject.redirect 'http://foo.bar/relocated/thingy' + value = subject.to_a + + value[0].should eq(302) + value[1]['Location'].should eq('http://foo.bar/relocated/thingy') + end + end + end \ No newline at end of file -- cgit v1.2.3-70-g09d2