Beispiel #1
0
 def test_error(self):
     """
     If an exception is unhandled, set the response body and status
     accordingly.
     """
     f = Failure(RuntimeError('Nope'))
     resource = nevow_adapter_resource([before(constantly(f))])
     req = fake_nevow_request()
     self.assertThat(resource.renderHTTP(req), succeeded(Equals(b'')))
     req.channel.transport.written.seek(0)
     self.assertThat(req.channel.transport.written.read(),
                     Contains(b'Internal server error: exception'))
     self.assertThat(req.code, Equals(500))
Beispiel #2
0
class TerminateWhenTests(TestCase):
    """
    Tests for `terminate_when`.
    """
    always = constantly(True)
    never = constantly(False)

    def test_empty(self):
        """
        Adding the first terminator creates the vector if necessary.
        """
        context = empty_context
        self.assertThat(terminate_when(context, self.always),
                        ContainsDict({TERMINATORS: Equals(v(self.always))}))

    def test_non_empty(self):
        """
        Adding a non-first terminator.
        """
        context = terminate_when(empty_context, self.always)
        self.assertThat(
            terminate_when(context, self.never),
            ContainsDict({TERMINATORS: Equals(v(self.always, self.never))}))
Beispiel #3
0
def route(path, method, interceptors, name=None):
    """
    Construct a route description.

    :param unicode path: Rooted route path to match, may include identifiers
    and wildcards (Rails-like syntax). For example: ``/users/:id/*rest``
    :param unicode method: Request method to match, with the special case of
    ``'ANY'`` to match any method.
    :type interceptors: Iterable[`Interceptor`] or Callable[[pmap], Any]
    :param interceptors: Interceptors to enqueue when matching this route, or a
    single function accepting the ``REQUEST`` value and returning a
    ``RESPONSE`` value.
    :param unicode name: Route name, derived from the last interceptor's name
    if ``None``.
    :rtype: Route
    :return: Fully specified route to match.
    """
    if isinstance(path, bytes):
        path = path.decode('utf-8')
    interceptors, name = _conform_interceptor(interceptors, name)
    if isinstance(name, bytes):
        name = name.decode('utf-8')
    iri = URL.from_text(path).to_iri()
    if not iri.rooted:
        raise ValueError('Route must be a rooted path', iri)
    parsed = _parse_path(iri.path)
    return Route(
        name=name,
        path=path,
        method=method,
        interceptors=interceptors,
        priority=parsed.priority,
        path_re=_path_regex(parsed.parts, parsed.constraints),
        path_parts=parsed.parts,
        path_params=parsed.params,
        path_constraints=parsed.constraints,
        matcher=constantly(None))
Beispiel #4
0
 def test_constantly(self):
     """
     Return the initial constant value regardless of any arguments passed.
     """
     f = constantly(42)
     self.assertThat([f(), f(1), f(1, 2), f(1, b=2)], AllMatch(Equals(42)))