SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [\"\"] being inserted in URLs - using Sinatra

    I'm following a very short tutorial for creating a simple URL shortener (http://blogs.sitepoint.com/2010/09/2...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:
    Code:
    $ 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
    Code:
    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
    Code:
    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

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,707
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    OP reports that the problem is resolved.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •