Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add redirection handling

  • Loading branch information...
commit 3739e8ab6461601b772e2f4b1d53e0433cca1ae2 1 parent e00ee64
@joshuaclayton authored
View
5 app/controllers/peas_controller.rb
@@ -2,5 +2,8 @@ class PeasController < InheritedResources::Base
respond_to :html
actions :create, :show
- def redir; end
+ def redir
+ pea = Pea.find_by_key(params[:pea_key])
+ redirect_to(pea ? pea.long_url : root_path)
+ end
end
View
12 features/step_definitions/pea_steps.rb
@@ -1,4 +1,16 @@
+Given /^a pea exists for the URL "([^\"]*)"$/ do |long_url|
+ Pea.create(:long_url => long_url)
+end
+
+Given /^the pea for "([^\"]*)" has the key "([^\"]*)"$/ do |long_url, key|
+ Pea.find_by_long_url(long_url).update_attribute :key, key
+end
+
Then /^I should see a short URL for "([^\"]*)"$/ do |long_url|
pea = Pea.find_by_long_url(long_url)
assert_select("a[href=?]", peapod_url(pea.key))
end
+
+When /^I visit the short URL for "([^\"]*)"$/ do |key|
+ visit peapod_url(key)
+end
View
5 features/step_definitions/webrat_steps.rb
@@ -183,6 +183,11 @@
URI.parse(current_url).path.should == path_to(page_name)
end
+Then /^I should be redirected to "([^\"]*)"$/ do |url|
+ response.redirect?.should be_true
+ response.location.should == url
+end
+
Then /^show me the page$/ do
save_and_open_page
end
View
16 features/view_shortened_urls.feature
@@ -0,0 +1,16 @@
+Feature: View shortened URLs
+ In order to visit the long URL
+ As a visitor
+ I should be able to visit the short URL and be redirected
+
+ Scenario: Visit a shortened URL
+ Given a pea exists for the URL "http://www.google.com"
+ And the pea for "http://www.google.com" has the key "abc"
+ When I visit the short URL for "abc"
+ Then I should be redirected to "http://www.google.com"
+
+ Scenario: Visit a nonexistant shortened URL
+ Given a pea exists for the URL "http://www.google.com"
+ And the pea for "http://www.google.com" has the key "abc"
+ When I visit the short URL for "abcd"
+ Then I should be on the homepage
Please sign in to comment.
Something went wrong with that request. Please try again.