# -*- coding: utf-8 -*-
from webob import Request
import webtest
from webtest.compat import to_bytes
from tests.compat import unittest
from tests.compat import u
def input_app(environ, start_response):
req = Request(environ)
status = "200 OK"
body = to_bytes(
"""
form page
""")
headers = [
('Content-Type', 'text/html'),
('Content-Length', str(len(body)))]
start_response(status, headers)
return [body]
def input_app_without_default(environ, start_response):
req = Request(environ)
status = "200 OK"
body = to_bytes(
"""
form page
""")
headers = [
('Content-Type', 'text/html'),
('Content-Length', str(len(body)))]
start_response(status, headers)
return [body]
def input_unicode_app(environ, start_response):
req = Request(environ)
status = "200 OK"
body =\
u("""
form page
""").encode('utf8')
headers = [
('Content-Type', 'text/html; charset=utf-8'),
('Content-Length', str(len(body)))]
start_response(status, headers)
return [body]
class TestInput(unittest.TestCase):
def test_input(self):
app = webtest.TestApp(input_app)
res = app.get('/')
self.assertEqual(res.status_int, 200)
self.assertEqual(res.headers['content-type'], 'text/html')
self.assertEqual(res.content_type, 'text/html')
form = res.forms['text_input_form']
self.assertEqual(form['foo'].value, 'bar')
self.assertEqual(form.submit_fields(), [('foo', 'bar')])
form = res.forms['radio_input_form']
self.assertEqual(form['foo'].value, 'baz')
self.assertEqual(form.submit_fields(), [('foo', 'baz')])
form = res.forms['checkbox_input_form']
self.assertEqual(form['foo'].value, 'bar')
self.assertEqual(form.submit_fields(), [('foo', 'bar')])
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_input_no_default(self):
app = webtest.TestApp(input_app_without_default)
res = app.get('/')
self.assertEqual(res.status_int, 200)
self.assertEqual(res.headers['content-type'], 'text/html')
self.assertEqual(res.content_type, 'text/html')
form = res.forms['text_input_form']
self.assertEqual(form['foo'].value, '')
self.assertEqual(form.submit_fields(), [('foo', '')])
form = res.forms['radio_input_form']
self.assertTrue(form['foo'].value is None)
self.assertEqual(form.submit_fields(), [])
form = res.forms['checkbox_input_form']
self.assertTrue(form['foo'].value is None)
self.assertEqual(form.submit_fields(), [])
class TestFormLint(unittest.TestCase):
def test_form_lint(self):
form = webtest.Form(None, '''''')
self.assertRaises(AttributeError, form.lint)
form = webtest.Form(None, '''''')
self.assertRaises(AttributeError, form.lint)
form = webtest.Form(None, '''''')
form.lint()
form = webtest.Form(None, '''''')
form.lint()