def test_click_u(self): app = webtest.TestApp(links_app) resp = app.get('/utf8/') self.assertIn(u("Тестовая страница"), resp) self.assertIn('This is foo.', resp.click(u('Менделеев'))) self.assertIn('This is baz.', resp.click(anchor=u(".*title='Поэт'.*")))
def test_parse_attrs(self): self.assertEqual(_parse_attrs("href='foo'"), {'href': 'foo'}) self.assertEqual(_parse_attrs('href="foo"'), {'href': 'foo'}) self.assertEqual(_parse_attrs('href=""'), {'href': ''}) self.assertEqual(_parse_attrs('href="foo" id="bar"'), { 'href': 'foo', 'id': 'bar' }) self.assertEqual(_parse_attrs('href="foo" id="bar"'), { 'href': 'foo', 'id': 'bar' }) self.assertEqual(_parse_attrs("href='foo' id=\"bar\" "), { 'href': 'foo', 'id': 'bar' }) self.assertEqual(_parse_attrs("href='foo' id='bar' "), { 'href': 'foo', 'id': 'bar' }) self.assertEqual(_parse_attrs("tag='foo\"'"), {'tag': 'foo"'}) self.assertEqual(_parse_attrs('value="<>&"{"'), {'value': u('<>&"{')}) self.assertEqual(_parse_attrs('value="∑"'), {'value': u('∑')}) self.assertEqual(_parse_attrs('value="€"'), {'value': u('€')})
def test_unicode_substring(self): pattern = u('\u03A3\u0393') text = u('\u03A0\u03A3\u0393\u0394') self.expectedOutcomes( self.search(pattern, text, max_subs=0), [Match(1, 3, 0)] )
def links_app(environ, start_response): req = Request(environ) status = "200 OK" responses = { '/': """ <html> <head><title>page with links</title></head> <body> <a href="/foo/">Foo</a> <a href='bar'>Bar</a> <a href='baz/' id='id_baz'>Baz</a> <a href='#' id='fake_baz'>Baz</a> <a href='javascript:alert("123")' id='js_baz'>Baz</a> <script> var link = "<a href='/boo/'>Boo</a>"; </script> <a href='/spam/'>Click me!</a> <a href='/egg/'>Click me!</a> </body> </html> """, '/foo/': '<html><body>This is foo. <a href="bar">Bar</a> </body></html>', '/foo/bar': '<html><body>This is foobar.</body></html>', '/bar': '<html><body>This is bar.</body></html>', '/baz/': '<html><body>This is baz.</body></html>', '/spam/': '<html><body>This is spam.</body></html>', '/egg/': '<html><body>Just eggs.</body></html>', '/utf8/': u(""" <html> <head><title>Тестовая страница</title></head> <body> <a href='/foo/'>Менделеев</a> <a href='/baz/' title='Поэт'>Пушкин</a> <img src='/egg/' title='Поэт'> <script> var link = "<a href='/boo/'>Злодейская ссылка</a>"; </script> </body> </html> """).encode('utf8'), } utf8_paths = ['/utf8/'] body = responses[u(req.path_info)] headers = [('Content-Type', 'text/html'), ('Content-Length', str(len(body)))] if req.path_info in utf8_paths: headers[0] = ('Content-Type', 'text/html; charset=utf-8') start_response(status, headers) return [to_bytes(body)]
def select_app_unicode(environ, start_response): req = Request(environ) status = b"200 OK" if req.method == "GET": body = u( """ <html> <head><title>form page</title></head> <body> <form method="POST" id="single_select_form"> <select id="single" name="single"> <option value="ЕКБ">Екатеринбург</option> <option value="МСК" selected="selected">Москва</option> <option value="СПБ">Санкт-Петербург</option> <option value="САМ">Самара</option> </select> <input name="button" type="submit" value="single"> </form> <form method="POST" id="multiple_select_form"> <select id="multiple" name="multiple" multiple="multiple"> <option value="8" selected="selected">Лондон</option> <option value="9">Париж</option> <option value="10">Пекин</option> <option value="11" selected="selected">Бристоль</option> </select> <input name="button" type="submit" value="multiple"> </form> </body> </html> """ ).encode("utf8") else: select_type = req.POST.get("button") if select_type == "single": selection = req.POST.get("single") elif select_type == "multiple": selection = ", ".join(req.POST.getall("multiple")) body = ( u( """ <html> <head><title>display page</title></head> <body> <p>You submitted the %(select_type)s </p> <p>You selected %(selection)s</p> </body> </html> """ ) % dict(selection=selection, select_type=select_type) ).encode("utf8") headers = [("Content-Type", "text/html; charset=utf-8"), ("Content-Length", str(len(body)))] start_response(status, headers) if not isinstance(body, binary_type): raise AssertionError("Body is not %s" % binary_type) return [body]
def links_app(environ, start_response): req = Request(environ) status = "200 OK" responses = { '/': """ <html> <head><title>page with links</title></head> <body> <a href="/foo/">Foo</a> <a href='bar'>Bar</a> <a href='baz/' id='id_baz'>Baz</a> <a href='#' id='fake_baz'>Baz</a> <a href='javascript:alert("123")' id='js_baz'>Baz</a> <script> var link = "<a href='/boo/'>Boo</a>"; </script> <a href='/spam/'>Click me!</a> <a href='/egg/'>Click me!</a> </body> </html> """, '/foo/': '<html><body>This is foo. <a href="bar">Bar</a> </body></html>', '/foo/bar': '<html><body>This is foobar.</body></html>', '/bar': '<html><body>This is bar.</body></html>', '/baz/': '<html><body>This is baz.</body></html>', '/spam/': '<html><body>This is spam.</body></html>', '/egg/': '<html><body>Just eggs.</body></html>', '/utf8/': u(""" <html> <head><title>Тестовая страница</title></head> <body> <a href='/foo/'>Менделеев</a> <a href='/baz/' title='Поэт'>Пушкин</a> <img src='/egg/' title='Поэт'> <script> var link = "<a href='/boo/'>Злодейская ссылка</a>"; </script> </body> </html> """).encode('utf8'), } utf8_paths = ['/utf8/'] body = responses[u(req.path_info)] headers = [ ('Content-Type', 'text/html'), ('Content-Length', str(len(body))) ] if req.path_info in utf8_paths: headers[0] = ('Content-Type', 'text/html; charset=utf-8') start_response(status, headers) return [to_bytes(body)]
def select_app_unicode(environ, start_response): req = Request(environ) status = b"200 OK" if req.method == "GET": body = u(""" <html> <head><title>form page</title></head> <body> <form method="POST" id="single_select_form"> <select id="single" name="single"> <option value="ЕКБ">Екатеринбург</option> <option value="МСК" selected="selected">Москва</option> <option value="СПБ">Санкт-Петербург</option> <option value="САМ">Самара</option> </select> <input name="button" type="submit" value="single"> </form> <form method="POST" id="multiple_select_form"> <select id="multiple" name="multiple" multiple="multiple"> <option value="8" selected="selected">Лондон</option> <option value="9">Париж</option> <option value="10">Пекин</option> <option value="11" selected="selected">Бристоль</option> </select> <input name="button" type="submit" value="multiple"> </form> </body> </html> """).encode('utf8') else: select_type = req.POST.get("button") if select_type == "single": selection = req.POST.get("single") elif select_type == "multiple": selection = ", ".join(req.POST.getall("multiple")) body = (u(""" <html> <head><title>display page</title></head> <body> <p>You submitted the %(select_type)s </p> <p>You selected %(selection)s</p> </body> </html> """) % dict(selection=selection, select_type=select_type)).encode('utf8') headers = [ ('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', str(len(body)))] # PEP 3333 requires native strings: headers = [(str(k), str(v)) for k, v in headers] start_response(status, headers) if not isinstance(body, bytes): raise AssertionError('Body is not %s' % bytes) return [body]
def test_parse_attrs(self): self.assertEqual(_parse_attrs("href='foo'"), {'href': 'foo'}) self.assertEqual(_parse_attrs('href="foo"'), {'href': 'foo'}) self.assertEqual(_parse_attrs('href=""'), {'href': ''}) self.assertEqual(_parse_attrs('href="foo" id="bar"'), {'href': 'foo', 'id': 'bar'}) self.assertEqual(_parse_attrs('href="foo" id="bar"'), {'href': 'foo', 'id': 'bar'}) self.assertEqual(_parse_attrs("href='foo' id=\"bar\" "), {'href': 'foo', 'id': 'bar'}) self.assertEqual(_parse_attrs("href='foo' id='bar' "), {'href': 'foo', 'id': 'bar'}) self.assertEqual(_parse_attrs("tag='foo\"'"), {'tag': 'foo"'}) self.assertEqual( _parse_attrs('value="<>&"{"'), {'value': u('<>&"{')}) self.assertEqual(_parse_attrs('value="∑"'), {'value': u('∑')}) self.assertEqual(_parse_attrs('value="€"'), {'value': u('€')})
def test_click_utf8(self): app = webtest.TestApp(links_app, use_unicode=False) resp = app.get('/utf8/') self.assertEqual(resp.charset, 'utf-8') if not PY3: # No need to deal with that in Py3 self.assertIn(u("Тестовая страница").encode('utf8'), resp) self.assertIn(u("Тестовая страница"), resp) target = u('Менделеев').encode('utf8') self.assertIn('This is foo.', resp.click(target)) # should skip the img tag anchor = u(".*title='Поэт'.*") anchor_re = anchor.encode('utf8') self.assertIn('This is baz.', resp.click(anchor=anchor_re))
def test_unicode_encodings(self): needle = u('PATTERN') haystack = u('---PATERN---') for encoding in ['ascii', 'latin-1', 'latin1', 'utf-8', 'utf-16']: with self.subTest(encoding=encoding): with tempfile.NamedTemporaryFile(mode='wb', delete=False) as f: filename = f.name f.write(haystack.encode(encoding)) self.addCleanup(os.remove, filename) with io.open(filename, 'r', encoding=encoding) as f: self.assertEqual( find_near_matches_in_file(needle, f, max_l_dist=1), [Match(3, 9, 1, u('PATERN'))], )
def test_unicode_encodings(self): needle = u('PATTERN') haystack = u('---PATERN---') for encoding in ['ascii', 'latin-1', 'latin1', 'utf-8', 'utf-16']: with self.subTest(encoding=encoding): with tempfile.NamedTemporaryFile(mode='wb', delete=False) as f: filename = f.name f.write(haystack.encode(encoding)) self.addCleanup(os.remove, filename) with io.open(filename, 'r', encoding=encoding) as f: self.assertEqual( find_near_matches_in_file(needle, f, max_l_dist=1), [Match(3, 9, 1)], )
def test_app_error(self): res = self.app.get('/') res.charset = 'utf-8' res.text = u('°C') AppError('%s %s %s %s', res.status, '', res.request.url, res) res.charset = None AppError('%s %s %s %s', res.status, '', res.request.url, res)
def application(environ, start_response): req = webob.Request(environ) resp = webob.Response() if req.method == 'GET': filename = req.path_info.strip('/') or 'index.html' if filename in ('302',): redirect = req.params['redirect'] resp = exc.HTTPFound(location=redirect) return resp(environ, start_response) if filename.isdigit(): resp.status = filename filename = 'index.html' filename = os.path.join(files, 'html', filename) if os.path.isfile(filename): kw = dict(message=req.params.get('message', ''), redirect=req.params.get('redirect', '')) resp.unicode_body = u(open(filename).read()) % kw _, ext = os.path.splitext(filename) if ext == '.html': resp.content_type = 'text/html' elif ext == '.js': resp.content_type = 'text/javascript' elif ext == '.json': resp.content_type = 'application/json' else: redirect = req.params.get('redirect', '') if redirect: resp = exc.HTTPFound(location=redirect) else: resp.body = req.body return resp(environ, start_response)
def input_unicode_app(environ, start_response): req = Request(environ) status = "200 OK" body =\ u(""" <html> <head><title>form page</title></head> <body> <form method="POST" id="text_input_form"> <input name="foo" type="text" value="Хармс"> <input name="button" type="submit" value="Сохранить"> </form> <form method="POST" id="radio_input_form"> <input name="foo" type="radio" value="Хармс"> <input name="foo" type="radio" value="Блок" checked> <input name="button" type="submit" value="Сохранить"> </form> <form method="POST" id="checkbox_input_form"> <input name="foo" type="checkbox" value="Хармс" checked> <input name="button" type="submit" value="Ура"> </form> </body> </html> """).encode('utf8') headers = [ ('Content-Type', 'text/html; charset=utf-8'), ('Content-Length', str(len(body)))] start_response(status, headers) return [body]
def test_print_stderr(self): res = self.app.get('/') res.charset = 'utf-8' res.text = u('°C') print_stderr(str(res)) res.charset = None print_stderr(str(res))
def test_upload_invalid_content(self): app = webtest.TestApp(SingleUploadFileApp()) res = app.get("/") single_form = res.forms["file_upload_form"] single_form.set("file-field", ("my_file.dat", 1)) try: single_form.submit("button") except ValueError: e = sys.exc_info()[1] self.assertEquals(six.text_type(e), u("File content must be %s not %s" % (binary_type, int)))
def test_exception_repr(self): res = self.app.get('/') res.charset = 'utf-8' res.text = u('°C') if not PY3: unicode(AssertionError(res)) str(AssertionError(res)) res.charset = None if not PY3: unicode(AssertionError(res)) str(AssertionError(res))
def test_upload_invalid_content(self): app = webtest.TestApp(SingleUploadFileApp()) res = app.get('/') single_form = res.forms["file_upload_form"] single_form.set("file-field", ('my_file.dat', 1)) try: single_form.submit("button") except ValueError: e = sys.exc_info()[1] self.assertEquals( six.text_type(e), u('File content must be %s not %s' % (binary_type, int)))
def test_input_unicode(self): app = self.callFUT('form_unicode_inputs.html') res = app.get('/form.html') self.assertEqual(res.status_int, 200) self.assertTrue(res.content_type.startswith('text/html')) self.assertEqual(res.charset.lower(), 'utf-8') form = res.forms['text_input_form'] self.assertEqual(form['foo'].value, u('Хармс')) self.assertEqual(form.submit_fields(), [('foo', u('Хармс'))]) form = res.forms['radio_input_form'] self.assertEqual(form['foo'].value, u('Блок')) self.assertEqual(form.submit_fields(), [('foo', u('Блок'))]) form = res.forms['checkbox_input_form'] self.assertEqual(form['foo'].value, u('Хармс')) self.assertEqual(form.submit_fields(), [('foo', u('Хармс'))]) form = res.forms['password_input_form'] self.assertEqual(form['foo'].value, u('Хармс')) self.assertEqual(form.submit_fields(), [('foo', u('Хармс'))])
def test_input_unicode(self): app = webtest.TestApp(input_unicode_app) res = app.get('/') self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, 'text/html') self.assertEqual(res.charset, 'utf-8') form = res.forms['text_input_form'] self.assertEqual(form['foo'].value, u('Хармс')) self.assertEqual(form.submit_fields(), [('foo', u('Хармс'))]) form = res.forms['radio_input_form'] self.assertEqual(form['foo'].value, u('Блок')) self.assertEqual(form.submit_fields(), [('foo', u('Блок'))]) form = res.forms['checkbox_input_form'] self.assertEqual(form['foo'].value, u('Хармс')) self.assertEqual(form.submit_fields(), [('foo', u('Хармс'))]) form = res.forms['password_input_form'] self.assertEqual(form['foo'].value, u('Хармс')) self.assertEqual(form.submit_fields(), [('foo', u('Хармс'))])
def test_input_unicode(self): app = self.callFUT("form_unicode_inputs.html") res = app.get("/form.html") self.assertEqual(res.status_int, 200) self.assertTrue(res.content_type.startswith("text/html")) self.assertEqual(res.charset.lower(), "utf-8") form = res.forms["text_input_form"] self.assertEqual(form["foo"].value, u("Хармс")) self.assertEqual(form.submit_fields(), [("foo", u("Хармс"))]) form = res.forms["radio_input_form"] self.assertEqual(form["foo"].value, u("Блок")) self.assertEqual(form.submit_fields(), [("foo", u("Блок"))]) form = res.forms["checkbox_input_form"] self.assertEqual(form["foo"].value, u("Хармс")) self.assertEqual(form.submit_fields(), [("foo", u("Хармс"))]) form = res.forms["password_input_form"] self.assertEqual(form["foo"].value, u("Хармс")) self.assertEqual(form.submit_fields(), [("foo", u("Хармс"))])
def test_unicode_select(self): app = webtest.TestApp(select_app_unicode) res = app.get('/') single_form = res.forms["single_select_form"] self.assertEqual(single_form["single"].value, u("МСК")) display = single_form.submit("button") self.assertIn(u("<p>You selected МСК</p>"), display, display) res = app.get('/') single_form = res.forms["single_select_form"] self.assertEqual(single_form["single"].value, u("МСК")) single_form.set("single", u("СПБ")) self.assertEqual(single_form["single"].value, u("СПБ")) display = single_form.submit("button") self.assertIn(u("<p>You selected СПБ</p>"), display, display)
def test_input_unicode(self): app = webtest.TestApp(input_unicode_app) res = app.get('/') self.assertEqual(res.status_int, 200) self.assertEqual(res.content_type, 'text/html') self.assertEqual(res.charset, 'utf-8') form = res.forms['text_input_form'] self.assertEqual(form['foo'].value, u('Хармс')) self.assertEqual(form.submit_fields(), [('foo', u('Хармс'))]) form = res.forms['radio_input_form'] self.assertEqual(form['foo'].value, u('Блок')) self.assertEqual(form.submit_fields(), [('foo', u('Блок'))]) form = res.forms['checkbox_input_form'] self.assertEqual(form['foo'].value, u('Хармс')) self.assertEqual(form.submit_fields(), [('foo', u('Хармс'))])
def test_post_unicode(self): res = self.app.post( '/', params=dict(a=u('é')), content_type='application/x-www-form-urlencoded;charset=utf8') res.mustcontain('a=%C3%A9')
def test_unicode_subsequence(self): self.assertEqual( self.search(u('\u03A3\u0393'), u('\u03A0\u03A3\u0393\u0394')), [1])
def test_file_unicode(f): self.assertEqual( find_near_matches_in_file(u(needle), f, max_l_dist=1), [Match(3, 9, 1, u('PATERN'))])
def test_file_unicode(f): self.assertEqual(find_near_matches_in_file(u(needle), f, max_l_dist=1), [Match(3, 9, 1)])
def test_button_submit_by_value(self): form = self.callFUT(formid='multiple_buttons_form') display = form.submit('action', value='activate') self.assertIn(u("action=activate"), display, display)
def test_button_submit(self): form = self.callFUT(formid="multiple_buttons_form") display = form.submit("action") self.assertIn(u("action=deactivate"), display, display)
def test_unicode_subsequence(self): self.assertEqual(self.search(u('\u03A3\u0393'), u('\u03A0\u03A3\u0393\u0394')), [1])
def test_print_unicode(): print_stderr(u('°C'))
def test_post_unicode(self): res = self.app.post('/', params=dict(a=u('é')), content_type='application/x-www-form-urlencoded;charset=utf8') res.mustcontain('a=%C3%A9')