Commits

Mikhail Korobov  committed 25ac478

Switch to Unicode strings

  • Participants
  • Parent commits 3fc51e3

Comments (0)

Files changed (2)

File tests/test_click.py

 
 
 def test_click_utf8():
-    app = webtest.TestApp(links_app)
+    app = webtest.TestApp(links_app, use_unicode=False)
     resp = app.get('/utf8/')
     assert resp.charset == 'utf-8'
     assert u"Тестовая страница".encode('utf8') in resp
     assert 'This is baz.' in resp.click(anchor=anchor_re)
 
 
-#def test_click_unicode():
-#    app = webtest.TestApp(links_app)
-#    resp = app.get('/utf8/')
-#
-#    assert u"Тестовая страница" in resp
-#    assert 'This is foo.' in resp.click(u'Менделеев')
-#    assert 'This is baz.' in resp.click(anchor=u".*title='Поэт'.*")
+def test_click_unicode():
+    app = webtest.TestApp(links_app)
+    resp = app.get('/utf8/')
+
+    assert u"Тестовая страница" in resp
+    assert 'This is foo.' in resp.click(u'Менделеев')
+    assert 'This is baz.' in resp.click(anchor=u".*title='Поэт'.*")
 
 
 

File webtest/__init__.py

     # for py.test
     disabled = True
 
-    def __init__(self, app, extra_environ=None, relative_to=None):
+    def __init__(self, app, extra_environ=None, relative_to=None, use_unicode=True):
         """
         Wraps a WSGI application in a more convenient interface for
         testing.
         if extra_environ is None:
             extra_environ = {}
         self.extra_environ = extra_environ
+        self.use_unicode = use_unicode
         self.reset()
 
     def reset(self):
             start_time = time.time()
             ## FIXME: should it be an option to not catch exc_info?
             res = req.get_response(app, catch_exc_info=True)
+            res._use_unicode = self.use_unicode
             end_time = time.time()
         finally:
             sys.stdout = old_stdout
                     page.
                     """)
 
+    @property
+    def testbody(self):
+        if getattr(self, '_use_unicode', True) and self.charset:
+            return self.unicode_body
+        return self.body
+
     _tag_re = re.compile(r'<(/?)([:a-z0-9_\-]*)(.*?)>', re.S|re.I)
 
     def _parse_forms(self):
         forms = self._forms_indexed = {}
         form_texts = []
         started = None
-        for match in self._tag_re.finditer(self.body):
+        for match in self._tag_re.finditer(self.testbody):
             end = match.group(1) == '/'
             tag = match.group(2).lower()
             if tag != 'form':
             if end:
                 assert started, (
                     "</form> unexpected at %s" % match.start())
-                form_texts.append(self.body[started:match.end()])
+                form_texts.append(self.testbody[started:match.end()])
                 started = None
             else:
                 assert not started, (
                     "Nested form tags at %s" % match.start())
                 started = match.start()
         assert not started, (
-            "Danging form: %r" % self.body[started:])
+            "Danging form: %r" % self.testbody[started:])
         for i, text in enumerate(form_texts):
             form = Form(self, text)
             forms[i] = form
                              re.I+re.S)
         _script_re = re.compile(r'<script.*?>.*?</script>', re.I|re.S)
         bad_spans = []
-        for match in _script_re.finditer(self.body):
+        for match in _script_re.finditer(self.testbody):
             bad_spans.append((match.start(), match.end()))
 
         def printlog(s):
 
         found_links = []
         total_links = 0
-        for match in _tag_re.finditer(self.body):
+        for match in _tag_re.finditer(self.testbody):
             found_bad = False
             for bad_start, bad_end in bad_spans:
                 if (match.start() > bad_start
         except ImportError:
             raise ImportError(
                 "You must have BeautifulSoup installed to use response.html")
-        soup = BeautifulSoup(self.body)
+        soup = BeautifulSoup(self.testbody)
         return soup
 
     html = property(html, doc=html.__doc__)
                 except ImportError:
                     raise ImportError(
                         "You must have ElementTree installed (or use Python 2.5) to use response.xml")
+        # ElementTree can't parse unicode => use `body` instead of `testbody`
         return ElementTree.XML(self.body)
 
     xml = property(xml, doc=xml.__doc__)
             fromstring = etree.HTML
         ## FIXME: would be nice to set xml:base, in some fashion
         if self.content_type == 'text/html':
-            return fromstring(self.body)
+            return fromstring(self.testbody)
         else:
-            return etree.XML(self.body)
+            return etree.XML(self.testbody)
 
     lxml = property(lxml, doc=lxml.__doc__)
 
         except ImportError:
             raise ImportError(
                 "You must have simplejson installed to use response.json")
-        return loads(self.body)
+        return loads(self.testbody)
 
     json = property(json, doc=json.__doc__)
 
     disabled = True
     ResponseClass = TestResponse
 
+
 ########################################
 ## Form objects
 ########################################