diff MoinMoin/support/werkzeug/test.py @ 4689:bd3f5ccf6fb6

updated werkzeug
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 19 Apr 2009 00:46:15 +0200
parents 06a744121699
children 7cb92118a93e
line wrap: on
line diff
--- a/MoinMoin/support/werkzeug/test.py	Sun Apr 19 00:38:06 2009 +0200
+++ b/MoinMoin/support/werkzeug/test.py	Sun Apr 19 00:46:15 2009 +0200
@@ -22,7 +22,7 @@
 from werkzeug._internal import _empty_stream
 from werkzeug.wrappers import BaseRequest
 from werkzeug.utils import create_environ, run_wsgi_app, get_current_url, \
-     url_encode, url_decode, FileStorage
+     url_encode, url_decode, FileStorage, get_host
 from werkzeug.datastructures import FileMultiDict, MultiDict, \
      CombinedMultiDict, Headers
 
@@ -628,16 +628,33 @@
         if self.cookie_jar is not None:
             self.cookie_jar.extract_wsgi(environ, rv[2])
 
-        if rv[0].status_code in (301, 302, 303, 305, 307) and follow_redirects:
-            redirect = urlparse.urlunsplit(urlparse.urlsplit(
-                dict(rv[2])['Location'])[:-2] + ('', ''))
+        # handle redirects
+        redirect_chain = []
+        status_code = int(rv[1].split(None, 1)[0])
+        while status_code in (301, 302, 303, 305, 307) and follow_redirects:
+            redirect = dict(rv[2])['Location']
+            host = get_host(create_environ('/', redirect))
+            if get_host(environ).split(':', 1)[0] != host:
+                raise RuntimeError('%r does not support redirect to '
+                                   'external targets' % self.__class__)
+
+            scheme, netloc, script_root, qs, anchor = urlparse.urlsplit(redirect)
+            redirect_chain.append((redirect, status_code))
+
             kwargs.update({
-                'base_url':         redirect,
+                'base_url':         urlparse.urlunsplit((scheme, host,
+                                    script_root, '', '')).rstrip('/') + '/',
+                'query_string':     qs,
                 'as_tuple':         as_tuple,
                 'buffered':         buffered,
-                'follow_redirects': True
+                'follow_redirects': False
             })
-            return self.open(*args, **kwargs)
+            rv = self.open(*args, **kwargs)
+            status_code = int(rv[1].split(None, 1)[0])
+
+            # Prevent loops
+            if redirect_chain[-1] in redirect_chain[0:-1]:
+                break
 
         response = self.response_wrapper(*rv)
         if as_tuple: