From 0aa6d24d62f79225f52fd578c660e0d04a81d024 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Tue, 3 Jun 2008 17:09:39 -0700 Subject: [PATCH] web: add test for unicode POST when the name comes from name=, not the filename attribute --- src/allmydata/test/test_web.py | 26 ++++++++++++++++++++++++-- src/allmydata/web/directory.py | 2 ++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/allmydata/test/test_web.py b/src/allmydata/test/test_web.py index 5ae18a51..f5da33ed 100644 --- a/src/allmydata/test/test_web.py +++ b/src/allmydata/test/test_web.py @@ -254,7 +254,12 @@ class WebMixin(object): else: form.append('Content-Disposition: form-data; name="%s"' % name) form.append('') - form.append(str(value)) + if isinstance(value, unicode): + value = value.encode("utf-8") + else: + value = str(value) + assert isinstance(value, str) + form.append(value) form.append(sep) form[-1] += "--" body = "\r\n".join(form) + "\r\n" @@ -975,7 +980,6 @@ class Web(WebMixin, unittest.TestCase): def test_POST_upload_unicode(self): filename = u"n\u00e9wer.txt" # n e-acute w e r . t x t - target_url = self.public_url + "/foo/" + filename.encode("utf-8") d = self.POST(self.public_url + "/foo", t="upload", file=(filename, self.NEWFILE_CONTENTS)) fn = self._foo_node @@ -983,6 +987,24 @@ class Web(WebMixin, unittest.TestCase): d.addCallback(lambda res: self.failUnlessChildContentsAre(fn, filename, self.NEWFILE_CONTENTS)) + target_url = self.public_url + "/foo/" + filename.encode("utf-8") + d.addCallback(lambda res: self.GET(target_url)) + d.addCallback(lambda contents: self.failUnlessEqual(contents, + self.NEWFILE_CONTENTS, + contents)) + return d + + def test_POST_upload_unicode_named(self): + filename = u"n\u00e9wer.txt" # n e-acute w e r . t x t + d = self.POST(self.public_url + "/foo", t="upload", + name=filename, + file=("overridden", self.NEWFILE_CONTENTS)) + fn = self._foo_node + d.addCallback(self.failUnlessURIMatchesChild, fn, filename) + d.addCallback(lambda res: + self.failUnlessChildContentsAre(fn, filename, + self.NEWFILE_CONTENTS)) + target_url = self.public_url + "/foo/" + filename.encode("utf-8") d.addCallback(lambda res: self.GET(target_url)) d.addCallback(lambda contents: self.failUnlessEqual(contents, self.NEWFILE_CONTENTS, diff --git a/src/allmydata/web/directory.py b/src/allmydata/web/directory.py index 7a3473d9..f0218c47 100644 --- a/src/allmydata/web/directory.py +++ b/src/allmydata/web/directory.py @@ -234,6 +234,7 @@ class DirectoryNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin): req = IRequest(ctx) charset = get_arg(req, "_charset", "utf-8") contents = req.fields["file"] + assert contents.filename is None or isinstance(contents.filename, str) name = get_arg(req, "name") name = name or contents.filename if name is not None: @@ -241,6 +242,7 @@ class DirectoryNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin): if not name: # this prohibts empty, missing, and all-whitespace filenames raise WebError("upload requires a name") + assert isinstance(name, str) name = name.decode(charset) if "/" in name: raise WebError("name= may not contain a slash", http.BAD_REQUEST) -- 2.45.2