From 8fab4d97e0c7814803a7bed2f88c778398d72a25 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Fri, 23 Oct 2015 09:23:49 +0200
Subject: [PATCH] LiveTest: set SERVER_URL in the test request so that tests
 run with same URL as the host

---
 product/ERP5Type/Tool/ComponentTool.py         |  2 ++
 product/ERP5Type/tests/ERP5TypeLiveTestCase.py | 11 ++++++++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/product/ERP5Type/Tool/ComponentTool.py b/product/ERP5Type/Tool/ComponentTool.py
index 23eeda14e3..02e0e0d2fc 100644
--- a/product/ERP5Type/Tool/ComponentTool.py
+++ b/product/ERP5Type/Tool/ComponentTool.py
@@ -295,6 +295,7 @@ class Test(ERP5TypeTestCase):
     debug = int(debug) and True or False
     run_only = self._getCommaSeparatedParameterList(run_only)
     verbosity = verbose and 2 or 1
+    request_server_url = self.REQUEST.get('SERVER_URL')
 
     try:
       live_test_running = True
@@ -304,6 +305,7 @@ class Test(ERP5TypeTestCase):
                              run_only=run_only,
                              debug=debug,
                              stream=global_stream,
+                             request_server_url=request_server_url,
                              verbosity=verbosity)
       except ImportError:
         import traceback
diff --git a/product/ERP5Type/tests/ERP5TypeLiveTestCase.py b/product/ERP5Type/tests/ERP5TypeLiveTestCase.py
index d3de127572..99a023968d 100644
--- a/product/ERP5Type/tests/ERP5TypeLiveTestCase.py
+++ b/product/ERP5Type/tests/ERP5TypeLiveTestCase.py
@@ -51,6 +51,7 @@ from Products.ERP5Type.tests import ProcessingNodeTestCase as\
                                     ProcessingNodeTestCaseModule
 ProcessingNodeTestCaseModule.patchActivityTool = lambda: None
 
+_request_server_url = None
 
 class ERP5TypeLiveTestCase(ERP5TypeTestCaseMixin):
     """ERP5TypeLiveTestCase is the default class for *all* tests
@@ -109,6 +110,11 @@ class ERP5TypeLiveTestCase(ERP5TypeTestCaseMixin):
       with patches._requests_lock:
         patches._requests[thread.get_ident()] = request
 
+      # Make live tests run under the same server URL than the host instance.
+      if _request_server_url:
+        request['SERVER_URL'] = _request_server_url
+        request._resetURLS()
+
       self.portal = portal
       return portal
 
@@ -184,7 +190,7 @@ class ERP5TypeLiveTestLoader(ERP5TypeTestLoader):
         return super(ERP5TypeLiveTestLoader, self).loadTestsFromName(name,
                                                                      module)
 
-def runLiveTest(test_list, verbosity=1, stream=None, **kw):
+def runLiveTest(test_list, verbosity=1, stream=None, request_server_url=None, **kw):
   from Products.ERP5Type.tests.runUnitTest import DebugTestResult
   from StringIO import StringIO
   # Add path of the TestTemplateItem folder of the instance
@@ -197,6 +203,9 @@ def runLiveTest(test_list, verbosity=1, stream=None, **kw):
     product_test_list.extend(glob(os.path.join(product_path, '*', 'tests')))
   current_syspath = set(sys.path)
 
+  global _request_server_url
+  _request_server_url = request_server_url
+
   sys.path.extend(path for path in product_test_list
                   if path not in current_syspath)
 
-- 
2.30.9