You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by tv...@apache.org on 2013/09/03 23:03:00 UTC

[2/3] git commit: [#6619] make h.absurl() have proper value in tasks

[#6619] make h.absurl() have proper value in tasks

* add hostname to taskd fake request
* add hostname for tests
* request.host_url is better, it doesn't include port numbers
* add tests


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/f1253f6a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/f1253f6a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/f1253f6a

Branch: refs/heads/master
Commit: f1253f6a03cd48d2d6c088fd4c0de681cb9311ad
Parents: 208f608
Author: Dave Brondsema <db...@slashdotmedia.com>
Authored: Thu Aug 29 19:16:19 2013 +0000
Committer: Tim Van Steenburgh <tv...@gmail.com>
Committed: Tue Sep 3 21:01:57 2013 +0000

----------------------------------------------------------------------
 Allura/allura/command/taskd.py      | 10 ++++++----
 Allura/allura/lib/app_globals.py    |  4 ++--
 Allura/allura/lib/helpers.py        | 20 +++++++++++++-------
 Allura/allura/tests/test_helpers.py | 12 ++++++++++++
 4 files changed, 33 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1253f6a/Allura/allura/command/taskd.py
----------------------------------------------------------------------
diff --git a/Allura/allura/command/taskd.py b/Allura/allura/command/taskd.py
index 50402f0..87dbfa4 100644
--- a/Allura/allura/command/taskd.py
+++ b/Allura/allura/command/taskd.py
@@ -27,7 +27,7 @@ import sys
 import faulthandler
 import pylons
 from setproctitle import setproctitle, getproctitle
-
+import tg
 from paste.deploy import loadapp
 from paste.deploy.converters import asint
 from webob import Request
@@ -134,10 +134,12 @@ class TaskdCommand(base.Command):
                             only=only)
                     if self.task:
                         with(proctitle("taskd:{0}:{1}".format(
-                            self.task.task_name, self.task._id))):
+                                self.task.task_name, self.task._id))):
                             # Build the (fake) request
-                            r = Request.blank('/--%s--/%s/' % (self.task.task_name, self.task._id),
-                                              {'task': self.task,
+                            request_path = '/--%s--/%s/' % (self.task.task_name, self.task._id)
+                            r = Request.blank(request_path,
+                                              base_url=tg.config['base_url'].rstrip('/') + request_path,
+                                              environ={'task': self.task,
                                                })
                             list(wsgi_app(r.environ, start_response))
                             self.task = None

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1253f6a/Allura/allura/lib/app_globals.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/app_globals.py b/Allura/allura/lib/app_globals.py
index 276af63..c1c0b32 100644
--- a/Allura/allura/lib/app_globals.py
+++ b/Allura/allura/lib/app_globals.py
@@ -471,9 +471,9 @@ class Globals(object):
     def url(self, base, **kw):
         params = urlencode(kw)
         if params:
-            return '%s://%s%s?%s' % (request.scheme, request.host, base, params)
+            return '%s%s?%s' % (request.host_url, base, params)
         else:
-            return '%s://%s%s' % (request.scheme, request.host, base)
+            return '%s%s' % (request.host_url, base)
 
     def postload_contents(self):
         text = '''

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1253f6a/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index 3715de6..adfb339 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -360,18 +360,24 @@ class DateTimeConverter(FancyValidator):
     def _from_python(self, value, state):
         return value.isoformat()
 
+
 def absurl(url):
-    if url is None: return None
-    if '://' in url: return url
-    # some __json__ methods call absurl
-    # and in tests request is not set so exception raises
-    # this check prevents it
+    """
+    Given a root-relative URL, return a full URL including protocol and host
+    """
+    if url is None:
+        return None
+    if '://' in url:
+        return url
     try:
-        host = request.scheme + '://' + request.host
+        # try request first, so we can get proper http/https value
+        host = request.host_url
     except TypeError:
-        host = ''
+        # for tests, etc
+        host = tg.config['base_url'].rstrip('/')
     return host + url
 
+
 def diff_text(t1, t2, differ=None):
     t1_lines = t1.replace('\r', '').split('\n')
     t2_lines = t2.replace('\r', '').split('\n')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f1253f6a/Allura/allura/tests/test_helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_helpers.py b/Allura/allura/tests/test_helpers.py
index 3c79e61..3d4ad96 100644
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -25,6 +25,7 @@ from pylons import tmpl_context as c
 from nose.tools import eq_, assert_equals
 from IPython.testing.decorators import skipif, module_not_available
 from datadiff import tools as dd
+from webob import Request
 
 from allura import model as M
 from allura.lib import helpers as h
@@ -382,3 +383,14 @@ class TestUrlOpen(TestCase):
         urlopen.side_effect = side_effect
         self.assertRaises(HTTPError, h.urlopen, 'myurl')
         self.assertEqual(urlopen.call_count, 1)
+
+
+def test_absurl_no_request():
+    assert_equals(h.absurl('/p/test/foobar'), 'http://localhost/p/test/foobar')
+
+
+@patch.object(h, 'request',
+              new=Request.blank('/p/test/foobar', base_url='https://www.mysite.com/p/test/foobar'))
+def test_absurl_with_request():
+    assert_equals(h.absurl('/p/test/foobar'), 'https://www.mysite.com/p/test/foobar')
+