diff options
| author | Martin Groenemeyer <mg@lugensa.com> | 2012-05-29 22:47:52 +0200 |
|---|---|---|
| committer | Martin Groenemeyer <mg@lugensa.com> | 2012-05-29 22:47:52 +0200 |
| commit | 1a79b9fa9146cf1ccdb9a140c0007b20dccd777b (patch) | |
| tree | 613ea64376a374153a4167c044c8847a99a73a2b /tests | |
| parent | a7b642239566edbeb33fd58965ad3f3b169952e3 (diff) | |
| download | webtest-1a79b9fa9146cf1ccdb9a140c0007b20dccd777b.tar.gz | |
- new test for field order
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_field_order.py | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/tests/test_field_order.py b/tests/test_field_order.py new file mode 100644 index 0000000..1f004c1 --- /dev/null +++ b/tests/test_field_order.py @@ -0,0 +1,137 @@ +import collections +import os.path +import struct +from tests.compat import unittest +from webtest.compat import to_bytes +from webtest.compat import to_string +from webtest.compat import join_bytes +from webtest.compat import binary_type +from webtest.compat import PY3 +from webob import Request +import webtest + + +def deform_upload_file_app(environ, start_response): + req = Request(environ) + status = "200 OK" + if req.method == "GET": + body = to_bytes( +""" +<html> + <head><title>form page</title></head> + <body> + <form + id="deform" + action="" + method="POST" + enctype="multipart/form-data" + accept-charset="utf-8"> + + <input type="hidden" name="_charset_" /> + <input type="hidden" name="__formid__" value="deform"/> + <input type="text" name="title" value="" id="deformField1"/> + <input type="hidden" name="__start__" value="fileupload:mapping"/> + <input type="file" name="fileupload" id="deformField2"/> + <input type="hidden" name="__end__" value="fileupload:mapping"/> + <textarea id="deformField3" name="description" rows="10" cols="60"></textarea> + <button + id="deformSubmit" + name="Submit" + type="submit" + value="Submit"> + Submit + </button> + </form> + </body> +</html> +""") + else: + body_head = to_bytes( +""" +<html> + <head><title>display page</title></head> + <body> +""") + + body_parts = [] + for (name, value) in req.POST.items(): + if hasattr(value, 'filename'): + body_parts.append("%s:%s:%s\n" % ( + to_string(name), + to_string(value.filename), + to_string(value.value))) + else: + body_parts.append("%s:%s\n" % ( + to_string(name), to_string(value))) + + body_foot = to_bytes( +""" </body> +</html> +""") + body = body_head + join_bytes("", body_parts) + body_foot + headers = [ + ('Content-Type', 'text/html; charset=utf-8'), + ('Content-Length', str(len(body)))] + start_response(status, headers) + assert(isinstance(body, binary_type)) + return [body] + + +class TestFieldOrder(unittest.TestCase): + + def test_submit_with_file_upload(self): + uploaded_file_name = 'test.txt' + uploaded_file_contents = 'test content file upload' + if PY3: + uploaded_file_contents = to_bytes(uploaded_file_contents) + + app = webtest.TestApp(deform_upload_file_app) + res = app.get('/') + self.assertEqual(res.status_int, 200) + self.assertEqual( + res.headers['content-type'], 'text/html; charset=utf-8') + self.assertEqual(res.content_type, 'text/html') + self.assertEqual(res.charset, 'utf-8') + + single_form = res.forms["deform"] + single_form.set("title", "testtitle") + single_form.set("fileupload", + (uploaded_file_name, uploaded_file_contents)) + single_form.set("description", "testdescription") + display = single_form.submit("Submit") + self.assertTrue( +"""Submit:Submit +_charset_: +__formid__:deform +title:testtitle +__start__:fileupload:mapping +fileupload:test.txt:test content file upload +__end__:fileupload:mapping +description:testdescription""" in display) + + def test_post_with_file_upload(self): + uploaded_file_name = 'test.txt' + uploaded_file_contents = 'test content file upload' + if PY3: + uploaded_file_contents = to_bytes(uploaded_file_contents) + + app = webtest.TestApp(deform_upload_file_app) + display = app.post("/", collections.OrderedDict([ + ('_charset_', ''), + ('__formid__', 'deform'), + ('title', 'testtitle'), + ('__start__', 'fileupload:mapping'), + ('fileupload', webtest.Upload(uploaded_file_name, uploaded_file_contents)), + ('__end__', 'fileupload:mapping'), + ('description', 'testdescription'), + ('Submit', 'Submit')])) + + self.assertTrue( +"""_charset_: +__formid__:deform +title:testtitle +__start__:fileupload:mapping +fileupload:test.txt:test content file upload +__end__:fileupload:mapping +description:testdescription +Submit:Submit""" in display) |
