[Jifty-commit] r1643 - in jifty/trunk: lib/Jifty t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Jul 21 15:46:50 EDT 2006


Author: zev
Date: Fri Jul 21 15:46:50 2006
New Revision: 1643

Added:
   jifty/trunk/t/09-url.t   (contents, props changed)
Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/LetMe.pm
   jifty/trunk/lib/Jifty/Request.pm
   jifty/trunk/lib/Jifty/Web.pm

Log:
 r11795 at truegrounds:  zev | 2006-07-21 15:46:38 -0400
 * added a path option to Jifty::Web->url
 * added url tests


Modified: jifty/trunk/lib/Jifty/LetMe.pm
==============================================================================
--- jifty/trunk/lib/Jifty/LetMe.pm	(original)
+++ jifty/trunk/lib/Jifty/LetMe.pm	Fri Jul 21 15:46:50 2006
@@ -231,7 +231,7 @@
 
 sub as_url {
     my $self = shift;
-    return Jifty->web->url . $self->base_path. $self->as_encoded_token;
+    return Jifty->web->url(path => $self->base_path . $self->as_encoded_token);
 
 }
 

Modified: jifty/trunk/lib/Jifty/Request.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Request.pm	(original)
+++ jifty/trunk/lib/Jifty/Request.pm	Fri Jul 21 15:46:50 2006
@@ -507,7 +507,7 @@
     );
 
     # Set us up with the new continuation
-    Jifty->web->_redirect( Jifty::Web->url . $path
+    Jifty->web->_redirect( Jifty->web->url(path => $path)
                       . ( $path =~ /\?/ ? "&" : "?" ) . "J:C="
                       . $c->id );
 }

Modified: jifty/trunk/lib/Jifty/Web.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web.pm	(original)
+++ jifty/trunk/lib/Jifty/Web.pm	Fri Jul 21 15:46:50 2006
@@ -106,29 +106,43 @@
 =head3 url
 
 Returns the root url of this Jifty application.  This is pulled from
-the configuration file.  Takes an optional named parameter C<scheme>
-to specify the scheme.
+the configuration file.  Takes an optional named path which will
+form the path part of the resulting URL.
 
 =cut
 
 sub url {
     my $self = shift;
     my %args = (scheme => undef,
+                path => undef,
                 @_);
 
-    my $url  = Jifty->config->framework("Web")->{BaseURL};
-    my $port = Jifty->config->framework("Web")->{Port};
-   
     if ($args{'scheme'}) {
         $self->log->error("Jifty->web->url no longer accepts a 'scheme' argument");
     }
-    my $uri = URI->new($url);
-    $uri->port($port);
-
+    
+    my $uri;
     if ($ENV{'HTTP_HOST'}) {
-        return $uri->scheme ."://".$ENV{'HTTP_HOST'};
+      my $host = $ENV{HTTP_HOST};
+      if ($host !~ m{^http://}) {
+        $host = 'http://' . $host;
+      }
+      $uri = URI->new($host);
+    } else {
+      my $url  = Jifty->config->framework("Web")->{BaseURL};
+      my $port = Jifty->config->framework("Web")->{Port};
+   
+      $uri = URI->new($url);
+      $uri->port($port);
     }
 
+    if (defined $args{path}) {
+      my $path = $args{path};
+      # strip off leading '/' because ->canonical provides one
+      $path =~ s{^/}{};
+      $uri->path($path);
+    }
+    
     return $uri->canonical;
 }
 

Added: jifty/trunk/t/09-url.t
==============================================================================
--- (empty file)
+++ jifty/trunk/t/09-url.t	Fri Jul 21 15:46:50 2006
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+=head1 DESCRIPTION
+
+Tests that URLs constructed with Jifty->web->url are correct
+
+=cut
+
+use Jifty::Test tests => 4;
+
+like(Jifty->web->url, qr{^http://localhost:\d+/$}, 'basic call works');
+like(Jifty->web->url(path => 'foo/bar'), qr{^http://localhost:\d+/foo/bar$}, 'path works');
+
+$ENV{HTTP_HOST} = 'example.com';
+
+is(Jifty->web->url, 'http://example.com/', 'setting hostname via env works');
+is(Jifty->web->url(path => 'foo/bar'), 'http://example.com/foo/bar', 'hostname via env and path works');


More information about the Jifty-commit mailing list