I’m following a very short tutorial for creating a simple URL shortener (http://blogs.sitepoint.com/2010/09/28/a-ten-minute-url-shortener/), but it’s already kind of dated. Anyway, I followed the code and kept getting this problem, then tried just copying the entire code off the site and pasting it in and still got the same problem.
Here’s the RSpec errors:
$ rspec my-url-shortener-test.rb
...FF
Failures:
1) My URL Shortener redirects to home
Failure/Error: last_response.headers["location"].should == "http://blogs.sitepoint.com/"
expected: "http://blogs.sitepoint.com/",
got: "http://blogs.sitepoint.com/[\\"\\"]" (using ==)
# ./my-url-shortener-test.rb:28:in `block (2 levels) in <top (required)>'
2) My URL Shortener unrecognised path redirects
Failure/Error: last_response.headers["location"].should == "http://blogs.sitepoint.com/foo/bar"
expected: "http://blogs.sitepoint.com/foo/bar",
got: "http://blogs.sitepoint.com/[\\"foo/bar\\"]" (using ==)
# ./my-url-shortener-test.rb:33:in `block (2 levels) in <top (required)>'
Finished in 0.01581 seconds
5 examples, 2 failures
Here are the two files I’m working with:
my-url-shortener-test.rb
require_relative "my-url-shortener"
require "rspec"
require "rack/test"
set :environment, :test
describe "My URL Shortener" do
include Rack::Test::Methods
def app
Sinatra::Application
end
it "redirects to a blog post" do
get "/123" # 123 (base 36) == 1371 (base 10)
last_response.status.should == 301
last_response.headers["location"].should == "http://blogs.sitepoint.com/?p=1371"
end
it "redirect to blog post with lowercase digits" do
get "/a" # a (base 36) == 10 (base 10)
last_response.status.should == 301
last_response.headers["location"].should == "http://blogs.sitepoint.com/?p=10"
end
it "redirect to blog post with uppercase digits" do
get "/A" # A (base 36) == 10 (base 10)
last_response.status.should == 301
last_response.headers["location"].should == "http://blogs.sitepoint.com/?p=10"
end
it "redirects to home" do
get "/"
last_response.status.should == 301
last_response.headers["location"].should == "http://blogs.sitepoint.com/"
end
it "unrecognised path redirects" do
get "/foo/bar"
last_response.status.should == 301
last_response.headers["location"].should == "http://blogs.sitepoint.com/foo/bar"
end
end
my-url-shortener.rb
require "sinatra"
get %r{^/([0-9a-zA-Z]+)$} do
postid = params[:captures][0].to_i(36)
redirect "http://blogs.sitepoint.com/?p=#{postid}", 301
end
get "/*" do
path = params["splat"]
redirect "http://blogs.sitepoint.com/#{path}", 301
end
So, for some reason, the URLs are getting [\“\”] added to them, and I don’t know why :(. Any help is appreciated