From 6537c054e53b16b3d5c457a68d1e83b99ae016a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chojnacki?= Date: Sun, 30 May 2021 00:22:49 +0200 Subject: [PATCH] Fix PUT uploads PUT uploads were broken on python 3.9 and were lacking tests. --- servefile/servefile.py | 6 +++--- tests/test_servefile.py | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/servefile/servefile.py b/servefile/servefile.py index 8bdbc34..3fbc18d 100755 --- a/servefile/servefile.py +++ b/servefile/servefile.py @@ -637,7 +637,7 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler): http://host:8080/testfile will cause the file to be named testfile. If no filename is given, a random name will be generated. - Files can be uploaded with e.g. curl -X POST -d @file . + Files can be uploaded with e.g. curl -T file . """ length = self.getContentLength() if length < 0: @@ -654,11 +654,11 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler): return # 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.end_headers() - target = open(cleanFileName, "w") + target = open(cleanFileName, "wb") bytesLeft = int(self.headers['Content-Length']) while bytesLeft > 0: bytesToRead = min(self.blockSize, bytesLeft) diff --git a/tests/test_servefile.py b/tests/test_servefile.py index 3d0cf61..cf76cf6 100644 --- a/tests/test_servefile.py +++ b/tests/test_servefile.py @@ -312,6 +312,13 @@ def test_upload(run_servefile, tmp_path): with open(str(uploaddir / 'haiku.txt(1)')) as f: 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): uploaddir = tmp_path / 'upload'