Commits

Mikhail Korobov  committed 64f0615

Tests for binary file uploads

  • Participants
  • Parent commits 69d6bb9

Comments (0)

Files changed (1)

File tests/test_file_upload.py

 import os.path
+import struct
 
 from webob import Request
 import webtest
     start_response(status, headers)
     return [body]
 
+
+def upload_binary_app(environ, start_response):
+    req = Request(environ)
+    status = "200 OK"
+    if req.method == "GET":
+        body ="""
+<html>
+    <head><title>form page</title></head>
+    <body>
+        <form method="POST" id="binary_upload_form"
+              enctype="multipart/form-data">
+            <input name="binary-file-field" type="file" />
+            <input name="button" type="submit" value="binary" />
+        </form>
+    </body>
+</html>
+"""
+    else:
+        uploaded_files = req.POST.getall("binary-file-field")
+        data = [str(n) for n in struct.unpack('255h', uploaded_files[0].value)]
+        body = """
+<html>
+    <head><title>display page</title></head>
+    <body>
+        %s
+    </body>
+</html>
+""" % ','.join(data)
+    headers = [
+        ('Content-Type', 'text/html; charset=utf-8'),
+        ('Content-Length', str(len(body)))]
+    start_response(status, headers)
+    return [body]
+
+
 def multiple_upload_file_app(environ, start_response):
     req = Request(environ)
     status = "200 OK"
 """
         body = body_head + "".join(file_parts) + body_foot
     headers = [
-        ('Content-Type', 'text/html'),
+        ('Content-Type', 'text/html; charset=utf-8'),
         ('Content-Length', str(len(body)))]
     start_response(status, headers)
     return [body]
     assert res.status_int == 200
     assert res.headers['content-type'] == 'text/html; charset=utf-8'
     assert res.content_type == 'text/html'
+    assert res.charset == 'utf-8'
 
     single_form = res.forms["file_upload_form"]
     single_form.set("file-field", (uploaded_file_name,))
     assert "<p>with contents: '%s'</p>" % uploaded_file_contents in display, \
         display
 
+
 def test_file_upload_with_filename_and_contents():
     uploaded_file_name = \
         os.path.join(os.path.dirname(__file__), "__init__.py")
     assert "<p>with contents: '%s'</p>" % uploaded_file_contents in display, \
         display
 
+
+def test_file_upload_binary():
+    binary_data = struct.pack('255h', *range(0,255))
+    app = webtest.TestApp(upload_binary_app)
+    res = app.get('/')
+    single_form = res.forms["binary_upload_form"]
+    single_form.set("binary-file-field", ('my_file.dat', binary_data))
+    display = single_form.submit("button")
+    assert ','.join([str(n) for n in range(0,255)]) in display, display
+
+
 def test_multiple_file_uploads_with_filename_and_contents():
     uploaded_file1_name = \
         os.path.join(os.path.dirname(__file__), "__init__.py")
     app = webtest.TestApp(multiple_upload_file_app)
     res = app.get('/')
     assert res.status_int == 200
-    assert res.headers['content-type'] == 'text/html'
+    assert res.headers['content-type'] == 'text/html; charset=utf-8'
     assert res.content_type == 'text/html'
 
     single_form = res.forms["file_upload_form"]