Fix PUT uploads

PUT uploads were broken on python 3.9 and were lacking tests.
fix-weird-encoding-problem-in-tests
Paweł Chojnacki 3 years ago committed by Sebastian Lohff
parent 65fcac5c49
commit 6537c054e5

@ -637,7 +637,7 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
http://host:8080/testfile will cause the file to be named testfile. If http://host:8080/testfile will cause the file to be named testfile. If
no filename is given, a random name will be generated. no filename is given, a random name will be generated.
Files can be uploaded with e.g. curl -X POST -d @file <url> . Files can be uploaded with e.g. curl -T file <url> .
""" """
length = self.getContentLength() length = self.getContentLength()
if length < 0: if length < 0:
@ -654,11 +654,11 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
return return
# Sometimes clients want to be told to continue with their transfer # Sometimes clients want to be told to continue with their transfer
if self.headers.getheader("Expect") == "100-continue": if self.headers.get("Expect") == "100-continue":
self.send_response(100) self.send_response(100)
self.end_headers() self.end_headers()
target = open(cleanFileName, "w") target = open(cleanFileName, "wb")
bytesLeft = int(self.headers['Content-Length']) bytesLeft = int(self.headers['Content-Length'])
while bytesLeft > 0: while bytesLeft > 0:
bytesToRead = min(self.blockSize, bytesLeft) bytesToRead = min(self.blockSize, bytesLeft)

@ -312,6 +312,13 @@ def test_upload(run_servefile, tmp_path):
with open(str(uploaddir / 'haiku.txt(1)')) as f: with open(str(uploaddir / 'haiku.txt(1)')) as f:
assert f.read() == data assert f.read() == data
# upload file using PUT
r = make_request("/haiku.txt", method='put', data=data)
assert r.status_code == 201
assert 'OK!' in r.text
with open(str(uploaddir / 'haiku.txt(2)')) as f:
assert f.read() == data
def test_upload_size_limit(run_servefile, tmp_path): def test_upload_size_limit(run_servefile, tmp_path):
uploaddir = tmp_path / 'upload' uploaddir = tmp_path / 'upload'

Loading…
Cancel
Save