Merge pull request #1233 from tahoe-lafs/3874.fix-testing-helper-encoding

Fix a testing helper
This commit is contained in:
Jean-Paul Calderone 2022-12-02 10:27:58 -05:00 committed by GitHub
commit daecfc63b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 56 deletions

0
newsfragments/3874.minor Normal file
View File

View File

@ -9,18 +9,7 @@
""" """
Tests for the allmydata.testing helpers Tests for the allmydata.testing helpers
Ported to Python 3.
""" """
from __future__ import absolute_import
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
from twisted.internet.defer import ( from twisted.internet.defer import (
inlineCallbacks, inlineCallbacks,
@ -56,10 +45,12 @@ from testtools.matchers import (
IsInstance, IsInstance,
MatchesStructure, MatchesStructure,
AfterPreprocessing, AfterPreprocessing,
Contains,
) )
from testtools.twistedsupport import ( from testtools.twistedsupport import (
succeeded, succeeded,
) )
from twisted.web.http import GONE
class FakeWebTest(SyncTestCase): class FakeWebTest(SyncTestCase):
@ -144,7 +135,8 @@ class FakeWebTest(SyncTestCase):
def test_download_missing(self): def test_download_missing(self):
""" """
Error if we download a capability that doesn't exist The response to a request to download a capability that doesn't exist
is 410 (GONE).
""" """
http_client = create_tahoe_treq_client() http_client = create_tahoe_treq_client()
@ -157,7 +149,11 @@ class FakeWebTest(SyncTestCase):
resp, resp,
succeeded( succeeded(
MatchesStructure( MatchesStructure(
code=Equals(500) code=Equals(GONE),
content=AfterPreprocessing(
lambda m: m(),
succeeded(Contains(b"No data for")),
),
) )
) )
) )

View File

@ -6,20 +6,12 @@
# 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
from __future__ import annotations
import hashlib import hashlib
@ -54,6 +46,7 @@ import allmydata.uri
from allmydata.util import ( from allmydata.util import (
base32, base32,
) )
from ..util.dictutil import BytesKeyDict
__all__ = ( __all__ = (
@ -147,7 +140,7 @@ class _FakeTahoeUriHandler(Resource, object):
isLeaf = True isLeaf = True
data = attr.ib(default=attr.Factory(dict)) data: BytesKeyDict = attr.ib(default=attr.Factory(BytesKeyDict))
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 +202,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 +214,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]

View File

@ -1,21 +1,6 @@
""" """
Tools to mess with dicts. Tools to mess with dicts.
Ported to Python 3.
""" """
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.utils import PY2
if PY2:
# IMPORTANT: We deliberately don't import dict. The issue is that we're
# subclassing dict, so we'd end up exposing Python 3 dict APIs to lots of
# code that doesn't support it.
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, list, object, range, str, max, min # noqa: F401
from six import ensure_str
class DictOfSets(dict): class DictOfSets(dict):
def add(self, key, value): def add(self, key, value):
@ -104,7 +89,7 @@ def _make_enforcing_override(K, method_name):
raise TypeError("{} must be of type {}".format( raise TypeError("{} must be of type {}".format(
repr(key), self.KEY_TYPE)) repr(key), self.KEY_TYPE))
return getattr(dict, method_name)(self, key, *args, **kwargs) return getattr(dict, method_name)(self, key, *args, **kwargs)
f.__name__ = ensure_str(method_name) f.__name__ = method_name
setattr(K, method_name, f) setattr(K, method_name, f)
for _method_name in ["__setitem__", "__getitem__", "setdefault", "get", for _method_name in ["__setitem__", "__getitem__", "setdefault", "get",
@ -113,11 +98,6 @@ for _method_name in ["__setitem__", "__getitem__", "setdefault", "get",
del _method_name del _method_name
if PY2:
# No need for enforcement, can use either bytes or unicode as keys and it's
# fine.
BytesKeyDict = UnicodeKeyDict = dict
else:
class BytesKeyDict(_TypedKeyDict): class BytesKeyDict(_TypedKeyDict):
"""Keys should be bytes.""" """Keys should be bytes."""