| 1 |
"""Basic tests for the CherryPy core: request handling.""" |
|---|
| 2 |
|
|---|
| 3 |
from cherrypy.test import test |
|---|
| 4 |
test.prefer_parent_path() |
|---|
| 5 |
|
|---|
| 6 |
import cherrypy |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
def setup_server(): |
|---|
| 10 |
|
|---|
| 11 |
from cherrypy.lib import safemime |
|---|
| 12 |
safemime.init() |
|---|
| 13 |
|
|---|
| 14 |
class Root: |
|---|
| 15 |
|
|---|
| 16 |
def flashupload(self, Filedata, Upload, Filename): |
|---|
| 17 |
return ("Upload: %r, Filename: %r, Filedata: %r" % |
|---|
| 18 |
(Upload, Filename, Filedata.file.read())) |
|---|
| 19 |
flashupload.exposed = True |
|---|
| 20 |
flashupload._cp_config = {'tools.safe_multipart.on': True} |
|---|
| 21 |
|
|---|
| 22 |
cherrypy.config.update({ |
|---|
| 23 |
'environment': 'test_suite', |
|---|
| 24 |
'server.max_request_body_size': 0, |
|---|
| 25 |
}) |
|---|
| 26 |
cherrypy.tree.mount(Root()) |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
from cherrypy.test import helper |
|---|
| 32 |
|
|---|
| 33 |
class SafeMultipartHandlingTest(helper.CPWebCase): |
|---|
| 34 |
|
|---|
| 35 |
def test_Flash_Upload(self): |
|---|
| 36 |
headers = [ |
|---|
| 37 |
('Accept', 'text/*'), |
|---|
| 38 |
('Content-Type', 'multipart/form-data; ' |
|---|
| 39 |
'boundary=----------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6'), |
|---|
| 40 |
('User-Agent', 'Shockwave Flash'), |
|---|
| 41 |
('Host', 'www.example.com:8080'), |
|---|
| 42 |
('Content-Length', '499'), |
|---|
| 43 |
('Connection', 'Keep-Alive'), |
|---|
| 44 |
('Cache-Control', 'no-cache'), |
|---|
| 45 |
] |
|---|
| 46 |
filedata = ('<?xml version="1.0" encoding="UTF-8"?>\r\n' |
|---|
| 47 |
'<projectDescription>\r\n' |
|---|
| 48 |
'</projectDescription>\r\n') |
|---|
| 49 |
body = ( |
|---|
| 50 |
'------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n' |
|---|
| 51 |
'Content-Disposition: form-data; name="Filename"\r\n' |
|---|
| 52 |
'\r\n' |
|---|
| 53 |
'.project\r\n' |
|---|
| 54 |
'------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n' |
|---|
| 55 |
'Content-Disposition: form-data; ' |
|---|
| 56 |
'name="Filedata"; filename=".project"\r\n' |
|---|
| 57 |
'Content-Type: application/octet-stream\r\n' |
|---|
| 58 |
'\r\n' |
|---|
| 59 |
+ filedata + |
|---|
| 60 |
'\r\n' |
|---|
| 61 |
'------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n' |
|---|
| 62 |
'Content-Disposition: form-data; name="Upload"\r\n' |
|---|
| 63 |
'\r\n' |
|---|
| 64 |
'Submit Query\r\n' |
|---|
| 65 |
|
|---|
| 66 |
'------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6--' |
|---|
| 67 |
) |
|---|
| 68 |
self.getPage('/flashupload', headers, "POST", body) |
|---|
| 69 |
self.assertBody("Upload: 'Submit Query', Filename: '.project', " |
|---|
| 70 |
"Filedata: %r" % filedata) |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
if __name__ == '__main__': |
|---|
| 74 |
setup_server() |
|---|
| 75 |
helper.testmain() |
|---|