summaryrefslogtreecommitdiff
path: root/tests/test_fileapp.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_fileapp.py')
-rw-r--r--tests/test_fileapp.py71
1 files changed, 36 insertions, 35 deletions
diff --git a/tests/test_fileapp.py b/tests/test_fileapp.py
index d5b2a95..bdd7510 100644
--- a/tests/test_fileapp.py
+++ b/tests/test_fileapp.py
@@ -1,30 +1,38 @@
# (c) 2005 Ian Bicking, Clark C. Evans and contributors
# This module is part of the Python Paste Project and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
-import string
import time
+import random
+import os
+import tempfile
try:
# Python 3
from email.utils import parsedate_tz, mktime_tz
except ImportError:
# Python 2
from rfc822 import parsedate_tz, mktime_tz
+import six
+from paste import fileapp
from paste.fileapp import *
from paste.fixture import *
+# NOTE(haypo): don't use string.letters because the order of lower and upper
+# case letters changes when locale.setlocale() is called for the first time
+LETTERS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+
def test_data():
- harness = TestApp(DataApp('mycontent'))
+ harness = TestApp(DataApp(b'mycontent'))
res = harness.get("/")
assert 'application/octet-stream' == res.header('content-type')
assert '9' == res.header('content-length')
assert "<Response 200 OK 'mycontent'>" == repr(res)
- harness.app.set_content("bingles")
+ harness.app.set_content(b"bingles")
assert "<Response 200 OK 'bingles'>" == repr(harness.get("/"))
def test_cache():
def build(*args,**kwargs):
- app = DataApp("SomeContent")
+ app = DataApp(b"SomeContent")
app.cache_control(*args,**kwargs)
return TestApp(app).get("/")
res = build()
@@ -48,7 +56,7 @@ def test_cache():
def test_disposition():
def build(*args,**kwargs):
- app = DataApp("SomeContent")
+ app = DataApp(b"SomeContent")
app.content_disposition(*args,**kwargs)
return TestApp(app).get("/")
res = build()
@@ -73,7 +81,7 @@ def test_disposition():
assert False, "should be an exception"
def test_modified():
- harness = TestApp(DataApp('mycontent'))
+ harness = TestApp(DataApp(b'mycontent'))
res = harness.get("/")
assert "<Response 200 OK 'mycontent'>" == repr(res)
last_modified = res.header('last-modified')
@@ -84,21 +92,20 @@ def test_modified():
assert "<Response 304 Not Modified ''>" == repr(res)
res = harness.get("/",status=400,
headers={'if-modified-since': 'garbage'})
- assert 400 == res.status and "ill-formed timestamp" in res.body
+ assert 400 == res.status and b"ill-formed timestamp" in res.body
res = harness.get("/",status=400,
headers={'if-modified-since':
'Thu, 22 Dec 2030 01:01:01 GMT'})
- assert 400 == res.status and "check your system clock" in res.body
+ assert 400 == res.status and b"check your system clock" in res.body
def test_file():
- import random, string, os
tempfile = "test_fileapp.%s.txt" % (random.random())
- content = string.letters * 20
- file = open(tempfile,"w")
- file.write(content)
- file.close()
+ content = LETTERS * 20
+ if six.PY3:
+ content = content.encode('utf8')
+ with open(tempfile, "wb") as fp:
+ fp.write(content)
try:
- from paste import fileapp
app = fileapp.FileApp(tempfile)
res = TestApp(app).get("/")
assert len(content) == int(res.header('content-length'))
@@ -113,7 +120,7 @@ def test_file():
res = TestApp(app).get("/",headers={'Cache-Control': 'max-age=0'})
assert len(content)+10 == int(res.header('content-length'))
assert 'text/plain' == res.header('content-type')
- assert content + "0123456789" == res.body
+ assert content + b"0123456789" == res.body
assert app.content # we are still cached
file = open(tempfile,"a+")
file.write("X" * fileapp.CACHE_SIZE) # exceed the cashe size
@@ -123,15 +130,12 @@ def test_file():
newsize = fileapp.CACHE_SIZE + len(content)+12
assert newsize == int(res.header('content-length'))
assert newsize == len(res.body)
- assert res.body.startswith(content) and res.body.endswith('XYZ')
+ assert res.body.startswith(content) and res.body.endswith(b'XYZ')
assert not app.content # we are no longer cached
finally:
- import os
os.unlink(tempfile)
def test_dir():
- import os
- import tempfile
tmpdir = tempfile.mkdtemp()
try:
tmpfile = os.path.join(tmpdir, 'file')
@@ -141,13 +145,12 @@ def test_dir():
fp.close()
os.mkdir(tmpsubdir)
try:
- from paste import fileapp
app = fileapp.DirectoryApp(tmpdir)
for path in ['/', '', '//', '/..', '/.', '/../..']:
assert TestApp(app).get(path, status=403).status == 403, ValueError(path)
for path in ['/~', '/foo', '/dir', '/dir/']:
assert TestApp(app).get(path, status=404).status == 404, ValueError(path)
- assert TestApp(app).get('/file').body == 'abcd'
+ assert TestApp(app).get('/file').body == b'abcd'
finally:
os.remove(tmpfile)
os.rmdir(tmpsubdir)
@@ -171,44 +174,43 @@ def _excercize_range(build,content):
assert res.body == content[:10]
assert res.header('content-length') == '10'
res = build("bytes=%d-" % (len(content)-1), status=206)
- assert res.body == 'Z'
+ assert res.body == b'Z'
assert res.header('content-length') == '1'
res = build("bytes=%d-%d" % (3,17), status=206)
assert res.body == content[3:18]
assert res.header('content-length') == '15'
def test_range():
- content = string.letters * 5
- def build(range, status=200):
+ content = LETTERS * 5
+ if six.PY3:
+ content = content.encode('utf8')
+ def build(range, status=206):
app = DataApp(content)
return TestApp(app).get("/",headers={'Range': range}, status=status)
_excercize_range(build,content)
build('bytes=0-%d' % (len(content)+1), 416)
def test_file_range():
- from paste import fileapp
- import random, string, os
tempfile = "test_fileapp.%s.txt" % (random.random())
- content = string.letters * (1+(fileapp.CACHE_SIZE / len(string.letters)))
+ content = LETTERS * (1+(fileapp.CACHE_SIZE // len(LETTERS)))
+ if six.PY3:
+ content = content.encode('utf8')
assert len(content) > fileapp.CACHE_SIZE
- file = open(tempfile,"w")
- file.write(content)
- file.close()
+ with open(tempfile, "wb") as fp:
+ fp.write(content)
try:
- def build(range, status=200):
+ def build(range, status=206):
app = fileapp.FileApp(tempfile)
return TestApp(app).get("/",headers={'Range': range},
status=status)
_excercize_range(build,content)
- for size in (13,len(string.letters),len(string.letters)-1):
+ for size in (13,len(LETTERS), len(LETTERS)-1):
fileapp.BLOCK_SIZE = size
_excercize_range(build,content)
finally:
- import os
os.unlink(tempfile)
def test_file_cache():
- from paste import fileapp
filename = os.path.join(os.path.dirname(__file__),
'urlparser_data', 'secured.txt')
app = TestApp(fileapp.FileApp(filename))
@@ -229,7 +231,6 @@ def test_file_cache():
status=400)
def test_methods():
- from paste import fileapp
filename = os.path.join(os.path.dirname(__file__),
'urlparser_data', 'secured.txt')
app = TestApp(fileapp.FileApp(filename))