| 1 | import simplejson |
|---|
| 2 | from cherrypy.test.webtest import openURL |
|---|
| 3 | |
|---|
| 4 | headers = [ |
|---|
| 5 | ('Accept', 'text/*'), |
|---|
| 6 | ('Content-Type', 'multipart/form-data; ' |
|---|
| 7 | 'boundary=----------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6'), |
|---|
| 8 | ('User-Agent', 'Shockwave Flash'), |
|---|
| 9 | ('Host', 'www.example.com:8080'), |
|---|
| 10 | ('Content-Length', '499'), |
|---|
| 11 | ('Connection', 'Keep-Alive'), |
|---|
| 12 | ('Cache-Control', 'no-cache'), |
|---|
| 13 | ] |
|---|
| 14 | filedata = ('<?xml version="1.0" encoding="UTF-8"?>\r\n' |
|---|
| 15 | '<projectDescription>\r\n' |
|---|
| 16 | '</projectDescription>\r\n') |
|---|
| 17 | body = ( |
|---|
| 18 | '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n' |
|---|
| 19 | 'Content-Disposition: form-data; name="filename"\r\n' |
|---|
| 20 | '\r\n' |
|---|
| 21 | '.project\r\n' |
|---|
| 22 | '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n' |
|---|
| 23 | 'Content-Disposition: form-data; ' |
|---|
| 24 | 'name="filedata"; filename=".project"\r\n' |
|---|
| 25 | 'Content-Type: application/octet-stream\r\n' |
|---|
| 26 | '\r\n' |
|---|
| 27 | + filedata + |
|---|
| 28 | '\r\n' |
|---|
| 29 | '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6\r\n' |
|---|
| 30 | 'Content-Disposition: form-data; name="Upload"\r\n' |
|---|
| 31 | '\r\n' |
|---|
| 32 | 'Submit Query\r\n' |
|---|
| 33 | # Flash apps omit the trailing \r\n on the last line: |
|---|
| 34 | '------------KM7Ij5cH2KM7Ef1gL6ae0ae0cH2gL6--' |
|---|
| 35 | ) |
|---|
| 36 | |
|---|
| 37 | for url in ['/okupload', '/failupload']: |
|---|
| 38 | print "Sending multipart POST request to %s" % url |
|---|
| 39 | status, header, res_body = openURL(url, headers=headers, method="POST", |
|---|
| 40 | body=body, host="127.0.0.1", port=8080) |
|---|
| 41 | try: |
|---|
| 42 | response = simplejson.loads(res_body) |
|---|
| 43 | except ValueError: |
|---|
| 44 | print res_body |
|---|
| 45 | print "FAILED" |
|---|
| 46 | else: |
|---|
| 47 | assert response['result'] == 'success' |
|---|
| 48 | assert response['filename'] == '.project' |
|---|
| 49 | print "OK" |
|---|