Permalink
Browse files

added test for redirection

  • Loading branch information...
1 parent 17c749a commit 30dbc651340a8c6f230d24253228f83e9e8fa7bc @adamkirkwood committed Feb 4, 2012
Showing with 26 additions and 8 deletions.
  1. +2 −4 models/url.rb
  2. +20 −2 recall.rb
  3. +4 −2 spec/recall_spec.rb
View
@@ -5,6 +5,8 @@
TOKEN_KEY = "url"
class Url
+ attr_reader :token, :url
+
def initialize(params = {})
@url = params[:url] if params[:url]
@token = params[:token] if params[:token]
@@ -29,10 +31,6 @@ def save
end
end
- def token
- @token
- end
-
def self.token_key(token)
"#{TOKEN_KEY}:#{token}"
end
View
@@ -1,12 +1,30 @@
require 'sinatra'
+require 'json'
+require './models/url'
+
get '/' do
'Hello World!'
end
-post '/:url' do
-
+post '/new' do
+ content_type :json
+
if !params[:url]
status 400
+ return { :error => " 'url' parameter is missing" }.to_json
+ else
+ @url = Url.new(:url => params[:url])
+ @url.save
+ status 200
+ return { :token => @url.token, :short_url => "http://fmnt.in/#{@url.token}", :original_url => @url.expanded }.to_json
+ end
+end
+
+get '/:token' do
+ if @short_url = Url.find_by_token(params[:token])
+ redirect @short_url, 301
+ else
+ raise Sinatra::NotFound
end
end
View
@@ -64,8 +64,10 @@ def app
end
it "should return the original URL given a valid token" do
- @expanded_url = Url.find_by_token('a5c2a')
- @expanded_url.should == 'http://www.fitmentfreak.com'
+ get '/a5c2a'
+ last_response.should be_redirect
+ follow_redirect!
+ last_request.url.should == 'http://www.fitmentfreak.com/'
end
end
end

0 comments on commit 30dbc65

Please sign in to comment.