Jason R. Coombs avatar Jason R. Coombs committed d6db812

Fixed failure in test_request_obj when unicode header values were passed to ntob

Comments (0)

Files changed (2)

cherrypy/_cpcompat.py

     if not isinstance(n, nativestr):
         raise TypeError("n must be a native str (got %s)" % type(n).__name__)
 
+def always_bytes(str, encoding='utf-8'):
+    if isinstance(str, unicodestr):
+        str = str.encode(encoding)
+    return str
+
 try:
     set = set
 except NameError:

cherrypy/test/webtest.py

 from unittest import *
 from unittest import _TextTestResult
 
-from cherrypy._cpcompat import basestring, ntob, py3k, HTTPConnection, HTTPSConnection, unicodestr
+from cherrypy._cpcompat import basestring, ntob, py3k, HTTPConnection, HTTPSConnection, unicodestr, always_bytes
 
 
 def interface(host):
         """Open the url with debugging support. Return status, headers, body."""
         ServerError.on = False
 
-        if isinstance(url, unicodestr):
-            url = url.encode('utf-8')
-        if isinstance(body, unicodestr):
-            body = body.encode('utf-8')
+        url = always_bytes(url)
+        body = always_bytes(body)
 
         self.url = url
         self.time = None
                 conn.putrequest(method.upper(), url)
 
             for key, value in headers:
-                conn.putheader(key, ntob(value, "Latin-1"))
+                conn.putheader(key, always_bytes(value, "Latin-1"))
             conn.endheaders()
 
             if body is not None:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.