You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by mf...@apache.org on 2010/08/06 13:07:11 UTC

svn commit: r982930 - in /incubator/deltacloud/trunk/server: ./ lib/deltacloud/helpers/ lib/sinatra/ tests/ views/ views/api/ views/docs/

Author: mfojtik
Date: Fri Aug  6 11:07:10 2010
New Revision: 982930

URL: http://svn.apache.org/viewvc?rev=982930&view=rev
Log:
Generate correct URLs when not running at the root context.

This patch fixes the URLs generated by deltacloud-core when it
is proxied or deployed via Passenger or TorqueBox at a non-root
context.

It mainly consists of using url_for for all URLs and improving the
url_for logic to handle the cases where it gets applied multiple
times to the same url. This happens in a few places indirectly
because link_to also calls url_for on the passed-in URL.

Added:
    incubator/deltacloud/trunk/server/tests/url_for_test.rb
Modified:
    incubator/deltacloud/trunk/server/Rakefile
    incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb
    incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb
    incubator/deltacloud/trunk/server/server.rb
    incubator/deltacloud/trunk/server/views/api/show.html.haml
    incubator/deltacloud/trunk/server/views/docs/collection.html.haml
    incubator/deltacloud/trunk/server/views/docs/collection.xml.haml
    incubator/deltacloud/trunk/server/views/docs/index.html.haml
    incubator/deltacloud/trunk/server/views/docs/index.xml.haml
    incubator/deltacloud/trunk/server/views/docs/operation.html.haml
    incubator/deltacloud/trunk/server/views/docs/operation.xml.haml
    incubator/deltacloud/trunk/server/views/layout.html.haml

Modified: incubator/deltacloud/trunk/server/Rakefile
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/Rakefile?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/Rakefile (original)
+++ incubator/deltacloud/trunk/server/Rakefile Fri Aug  6 11:07:10 2010
@@ -37,6 +37,7 @@ Rake::TestTask.new("test") { |t|
     'tests/images_test.rb',
     'tests/instances_test.rb',
     'tests/instance_states_test.rb',
+    'tests/url_for_test.rb'
   ]
   t.verbose = true
   t.warning = false

Modified: incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb (original)
+++ incubator/deltacloud/trunk/server/lib/deltacloud/helpers/application_helper.rb Fri Aug  6 11:07:10 2010
@@ -20,8 +20,8 @@
 module ApplicationHelper
 
   def bread_crumb
-    s = "<ul class='breadcrumb'><li class='first'><a href='/'>&#948</a></li>"
-    url = request.path.split('?')  #remove extra query string parameters
+    s = "<ul class='breadcrumb'><li class='first'><a href='#{url_for('/')}'>&#948</a></li>"
+    url = request.path_info.split('?')  #remove extra query string parameters
     levels = url[0].split('/') #break up url into different levels
     levels.each_with_index do |level, index|
       unless level.blank?
@@ -30,7 +30,7 @@ module ApplicationHelper
           s += "<li class='subsequent'>#{level.gsub(/_/, ' ')}</li>\n" unless level.to_i > 0
         else
             link = levels.slice(0, index+1).join("/")
-            s += "<li class='subsequent'><a href=\"#{link}\">#{level.gsub(/_/, ' ')}</a></li>\n"
+            s += "<li class='subsequent'><a href=\"#{url_for(link)}\">#{level.gsub(/_/, ' ')}</a></li>\n"
         end
       end
     end

Modified: incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb (original)
+++ incubator/deltacloud/trunk/server/lib/sinatra/url_for.rb Fri Aug  6 11:07:10 2010
@@ -33,7 +33,13 @@ module Sinatra
         raise TypeError, "Unknown url_for mode #{mode}"
       end
       url_escape = URI.escape(url_fragment)
-      "#{base}#{url_escape}"
+      # Don't add the base fragment if url_for gets called more than once
+      # per url or the url_fragment passed in is an absolute url
+      if url_escape.match(/^#{base}/) or url_escape.match(/^http/)
+        url_escape
+      else
+        "#{base}#{url_escape}"
+      end
     end
 
     def root_url

Modified: incubator/deltacloud/trunk/server/server.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/server.rb?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/server.rb (original)
+++ incubator/deltacloud/trunk/server/server.rb Fri Aug  6 11:07:10 2010
@@ -38,7 +38,7 @@ error Deltacloud::BackendError do
 end
 
 # Redirect to /api
-get '/' do redirect '/api'; end
+get '/' do redirect url_for('/api'); end
 
 get '/api\/?' do
     @version = 0.1

Added: incubator/deltacloud/trunk/server/tests/url_for_test.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/tests/url_for_test.rb?rev=982930&view=auto
==============================================================================
--- incubator/deltacloud/trunk/server/tests/url_for_test.rb (added)
+++ incubator/deltacloud/trunk/server/tests/url_for_test.rb Fri Aug  6 11:07:10 2010
@@ -0,0 +1,50 @@
+require 'tests/common'
+
+module DeltacloudUnitTest
+  class UrlForTest < Test::Unit::TestCase
+    include Rack::Test::Methods
+
+    def app
+      Sinatra::Application
+    end
+
+    def test_it_works_for_root
+      verify_url_for("/", "/")
+    end
+
+    def test_it_works_for_root_absolute
+      verify_url_for("/", "http://localhost/", :full)
+    end
+
+    def test_it_works_with_spaces
+      verify_url_for("/url with spaces", "/url%20with%20spaces")
+    end
+
+    def test_it_works_when_given_absolute
+      verify_url_for("http://test.com", "http://test.com")
+    end
+
+    def test_it_works_when_not_at_root_context
+      verify_url_for("/", "context/", :path_only, {}, {"SCRIPT_NAME" => "context"})
+    end
+
+    def verify_url_for(url, expected_url, mode=:path_only, params={}, rack_env={})
+      # generate a unique url for each test
+      test_url = "/url_for_test/#{expected_url.hash}/#{Time.now.to_f}"
+      # Create our sinatra test endpoint
+      self.class.create_test_url_content(test_url, url, mode)
+
+      # verify the generated url matches what we expect
+      get test_url, params, rack_env
+      last_response.body.should == expected_url
+    end
+
+    def self.create_test_url_content(test_url, url_content, mode)
+      get test_url do
+        content_type "text/plain"
+          url_for(url_content, mode)
+      end
+    end
+
+  end
+end

Modified: incubator/deltacloud/trunk/server/views/api/show.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/api/show.html.haml?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/api/show.html.haml (original)
+++ incubator/deltacloud/trunk/server/views/api/show.html.haml Fri Aug  6 11:07:10 2010
@@ -12,4 +12,4 @@
             = op
   %li
     %strong
-      %a{:href => "/api/docs"} Documentation (v#{@version})
+      %a{:href => url_for("/api/docs")} Documentation (v#{@version})

Modified: incubator/deltacloud/trunk/server/views/docs/collection.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/docs/collection.html.haml?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/docs/collection.html.haml (original)
+++ incubator/deltacloud/trunk/server/views/docs/collection.html.haml Fri Aug  6 11:07:10 2010
@@ -16,7 +16,7 @@
     - @operations.keys.sort_by { |k| k.to_s }.each do |operation|
       %tr
         %td{:style => "width:15em"}
-          %a{:href => "/api/docs/#{@collection.name.to_s}/#{operation}"} #{operation}
+          %a{:href => url_for("/api/docs/#{@collection.name.to_s}/#{operation}")} #{operation}
         %td{:style => "width:10em"} #{@operations[operation].description}
 
 %h3 Features:
@@ -34,4 +34,4 @@
         %td= feature.description
         %td
           - feature.operations.each do |op|
-            %a{:href => "/api/docs/#{@collection.name.to_s}/#{op.name}"} #{op.name}
+            %a{:href => url_for("/api/docs/#{@collection.name.to_s}/#{op.name}")} #{op.name}

Modified: incubator/deltacloud/trunk/server/views/docs/collection.xml.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/docs/collection.xml.haml?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/docs/collection.xml.haml (original)
+++ incubator/deltacloud/trunk/server/views/docs/collection.xml.haml Fri Aug  6 11:07:10 2010
@@ -1,9 +1,9 @@
 %docs{:status => "unsupported"}
-  %collection{:url => "/api/docs/#{@collection.name}", :name => "#{@collection.name}"}
+  %collection{:url => url_for("/api/docs/#{@collection.name}"), :name => "#{@collection.name}"}
     %description #{@collection.description}
     %operations
       - @operations.keys.sort_by { |k| k.to_s }.each do |operation|
-        %operation{:url => "/api/#{@collection.name.to_s}", :name => "#{operation}", :href => "#{@operations[operation].path}", :method => "#{@operations[operation].method}"}
+        %operation{:url => url_for("/api/#{@collection.name.to_s}"), :name => "#{operation}", :href => url_for("#{@operations[operation].path}"), :method => "#{@operations[operation].method}"}
           %description #{@operations[operation].description}
           - @operations[operation].each_param do |param|
             %parameter{:name => "#{param.name}", :type => "#{param.type}"}

Modified: incubator/deltacloud/trunk/server/views/docs/index.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/docs/index.html.haml?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/docs/index.html.haml (original)
+++ incubator/deltacloud/trunk/server/views/docs/index.html.haml Fri Aug  6 11:07:10 2010
@@ -11,5 +11,5 @@
     - collections.keys.sort_by { |k| k.to_s }.each do |collection|
       %tr
         %td{:style => "width:15em"}
-          %a{:href => "/api/docs/#{collection}"} #{collection}
+          %a{:href => url_for("/api/docs/#{collection}")} #{collection}
         %td{:style => "width:10em"} #{collections[collection].description}

Modified: incubator/deltacloud/trunk/server/views/docs/index.xml.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/docs/index.xml.haml?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/docs/index.xml.haml (original)
+++ incubator/deltacloud/trunk/server/views/docs/index.xml.haml Fri Aug  6 11:07:10 2010
@@ -1,5 +1,5 @@
 %docs{:status => "unsupported"}
   - collections.keys.sort_by { |k| k.to_s }.each do |collection|
-    %collection{:url => "/api/docs/#{collection}"}
+    %collection{:url => url_for("/api/docs/#{collection}")}
       %name #{collection}
       %description  #{collections[collection].description}

Modified: incubator/deltacloud/trunk/server/views/docs/operation.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/docs/operation.html.haml?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/docs/operation.html.haml (original)
+++ incubator/deltacloud/trunk/server/views/docs/operation.html.haml Fri Aug  6 11:07:10 2010
@@ -1,5 +1,5 @@
 %h2
-  %a{:href => "/api/docs/#{@collection.name.to_s}"} #{@collection.name.to_s.titlecase}
+  %a{:href => url_for("/api/docs/#{@collection.name.to_s}")} #{@collection.name.to_s.titlecase}
   #{'::'}
   #{@operation.name}
 
@@ -9,7 +9,7 @@
 %h3
   URL:
   %u
-    = "/api/#{@collection.name.to_s}/#{@operation.name.to_s}"
+    = url_for("/api/#{@collection.name.to_s}/#{@operation.name.to_s}")
 %br
 %h3 Parameters:
 

Modified: incubator/deltacloud/trunk/server/views/docs/operation.xml.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/docs/operation.xml.haml?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/docs/operation.xml.haml (original)
+++ incubator/deltacloud/trunk/server/views/docs/operation.xml.haml Fri Aug  6 11:07:10 2010
@@ -1,5 +1,5 @@
 %docs{:status => "unsupported"}
-  %operation{:url => "/api/docs/#{@collection.name.to_s}", :name => "#{@operation.name.to_s}", :href => "#{@operation.path}", :method => "#{@operation.method}"}
+  %operation{:url => url_for("/api/docs/#{@collection.name.to_s}"), :name => "#{@operation.name.to_s}", :href => url_for("#{@operation.path}"), :method => "#{@operation.method}"}
     %description #{@operation.description}
     - @operation.each_param do |param|
       %parameter{:name => "#{param.name}", :type => "#{param.type}"}

Modified: incubator/deltacloud/trunk/server/views/layout.html.haml
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/server/views/layout.html.haml?rev=982930&r1=982929&r2=982930&view=diff
==============================================================================
--- incubator/deltacloud/trunk/server/views/layout.html.haml (original)
+++ incubator/deltacloud/trunk/server/views/layout.html.haml Fri Aug  6 11:07:10 2010
@@ -22,4 +22,5 @@
           Driver: #{DRIVER}
         #copyright
           Copyright 2009, 2010
-          #{link_to 'Red Hat', 'http://redhat.com'} and individual contributors.
+          %a{:href => 'http://redhat.com'} Red Hat
+          and individual contributors.