Exemple #1
0
 def test_leaf(self):
     """
     The resource identifies as a leaf, it cannot be given child resources
     and cannot locate them either.
     """
     resource = twisted_adapter_resource()
     with ExpectedException(NotImplementedError):
         resource.putChild('path', Resource())
     self.assertThat(
         resource.getChildWithDefault('path', fake_twisted_request()),
         Is(resource))
Exemple #2
0
 def test_body_status(self):
     """
     Write a response body and status to the Twisted request.
     """
     resource = twisted_adapter_resource(
         [handler(lambda _: ok(b'Hello world!', status=201))])
     req = fake_twisted_request()
     self.assertThat(resource.render(req), Equals(NOT_DONE_YET))
     self.assertThat(req.code, Equals(201))
     req.channel.transport.written.seek(0)
     self.assertThat(req.channel.transport.written.read(),
                     Contains(b'Hello world!'))
Exemple #3
0
 def test_error(self):
     """
     If an exception is unhandled, set the response body and status
     accordingly.
     """
     f = Failure(RuntimeError('Nope'))
     resource = twisted_adapter_resource([before(constantly(f))])
     req = fake_twisted_request()
     self.assertThat(resource.render(req), Equals(NOT_DONE_YET))
     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))
Exemple #4
0
 def test_response_headers(self):
     """
     Write response headers to the Twisted request.
     """
     resource = twisted_adapter_resource([
         handler(lambda _: ok(b'',
                              headers={
                                  b'X-Foo': [b'foo'],
                                  b'X-Bar': b'bar'
                              }))
     ])
     req = fake_twisted_request()
     self.assertThat(resource.render(req), Equals(NOT_DONE_YET))
     self.assertThat(req.responseHeaders.getRawHeaders(b'X-Foo'),
                     Equals([b'foo']))
     self.assertThat(req.responseHeaders.getRawHeaders(b'X-Bar'),
                     Equals([b'bar']))
Exemple #5
0
    def test_twisted_request(self):
        """
        Rendering the resource returns a successful deferred, inserts a
        `TWISTED_REQUEST` value into the context and calls ``finish`` on the
        request.
        """
        def _spy(res):
            def _spy_inner(context):
                res.append(context)
                return context

            return before(_spy_inner)

        requests = []
        request = fake_twisted_request()
        resource = twisted_adapter_resource([_spy(requests)])
        self.assertThat(resource.render(request), Equals(NOT_DONE_YET))
        self.assertThat(requests, MatchesListwise([Contains(TWISTED_REQUEST)]))
        self.assertThat(next(request.finish.counter), Equals(1))