Take care with str vs bytes in the implementation

Also replace the intentional BAD_REQUEST with GONE for this case.
This commit is contained in:
Jean-Paul Calderone 2022-11-29 10:51:07 -05:00
parent d7fe25f7c7
commit 02aeb68f17

View File

@ -6,22 +6,13 @@
# This file is part of Tahoe-LAFS. # This file is part of Tahoe-LAFS.
# #
# See the docs/about.rst file for licensing information. # See the docs/about.rst file for licensing information.
"""Test-helpers for clients that use the WebUI.
Ported to Python 3.
""" """
from __future__ import absolute_import Test-helpers for clients that use the WebUI.
from __future__ import division """
from __future__ import print_function
from __future__ import unicode_literals
from future.utils import PY2
if PY2:
from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
import hashlib import hashlib
from typing import Dict
import attr import attr
@ -147,7 +138,7 @@ class _FakeTahoeUriHandler(Resource, object):
isLeaf = True isLeaf = True
data = attr.ib(default=attr.Factory(dict)) data: Dict[bytes, bytes] = attr.ib(default=attr.Factory(dict))
capability_generators = attr.ib(default=attr.Factory(dict)) capability_generators = attr.ib(default=attr.Factory(dict))
def _generate_capability(self, kind): def _generate_capability(self, kind):
@ -209,7 +200,7 @@ class _FakeTahoeUriHandler(Resource, object):
capability = None capability = None
for arg, value in uri.query: for arg, value in uri.query:
if arg == u"uri": if arg == u"uri":
capability = value capability = value.encode("utf-8")
# it's legal to use the form "/uri/<capability>" # it's legal to use the form "/uri/<capability>"
if capability is None and request.postpath and request.postpath[0]: if capability is None and request.postpath and request.postpath[0]:
capability = request.postpath[0] capability = request.postpath[0]
@ -221,10 +212,9 @@ class _FakeTahoeUriHandler(Resource, object):
# the user gave us a capability; if our Grid doesn't have any # the user gave us a capability; if our Grid doesn't have any
# data for it, that's an error. # data for it, that's an error.
capability = capability.encode('ascii')
if capability not in self.data: if capability not in self.data:
request.setResponseCode(http.BAD_REQUEST) request.setResponseCode(http.GONE)
return u"No data for '{}'".format(capability.decode('ascii')) return u"No data for '{}'".format(capability.decode('ascii')).encode("utf-8")
return self.data[capability] return self.data[capability]