Exemplo n.º 1
0
 def test_renderRealRequest(self):
     """
     The request managed by L{WebSocketsResource.render} doesn't contain
     unnecessary HTTP headers like I{Content-Type}.
     """
     channel = DummyChannel()
     channel.transport = StringTransportWithDisconnection()
     channel.transport.protocol = channel
     request = Request(channel, False)
     headers = {
         b"upgrade": b"Websocket",
         b"connection": b"Upgrade",
         b"sec-websocket-key": b"secure",
         b"sec-websocket-version": b"13"
     }
     for key, value in headers.items():
         request.requestHeaders.setRawHeaders(key, [value])
     request.method = b"GET"
     request.clientproto = b"HTTP/1.1"
     result = self.resource.render(request)
     self.assertEqual(NOT_DONE_YET, result)
     self.assertEqual(
         [(b"Connection", [b"Upgrade"]),
          (b"Sec-Websocket-Accept", [b"oYBv54i42V5dw6KnZqOFroecUTc="]),
          (b"Upgrade", [b"WebSocket"])],
         sorted(request.responseHeaders.getAllRawHeaders()))
     self.assertThat(
         channel.transport.value(),
         StartsWith(b"HTTP/1.1 101 Switching Protocols\r\n"
                    b"Transfer-Encoding: chunked\r\n"))
     self.assertEqual(101, request.code)
     self.assertIdentical(None, request.transport)
Exemplo n.º 2
0
def make_request(method, path):
    req = Request(FakeChannel(), None)
    req.prepath = req.postpath = None
    req.method = method
    req.path = path
    resource = site.getChildWithDefault(path, req)
    return resource.render(req)
Exemplo n.º 3
0
 def test_renderRealRequest(self):
     """
     The request managed by L{WebSocketsResource.render} doesn't contain
     unnecessary HTTP headers like I{Content-Type} or I{Transfer-Encoding}.
     """
     channel = DummyChannel()
     channel.transport = StringTransportWithDisconnection()
     channel.transport.protocol = channel
     request = Request(channel, False)
     headers = {
         "upgrade": "Websocket",
         "connection": "Upgrade",
         "sec-websocket-key": "secure",
         "sec-websocket-version": "13"
     }
     for key, value in headers.items():
         request.requestHeaders.setRawHeaders(key, [value])
     request.method = "GET"
     request.clientproto = "HTTP/1.1"
     result = self.resource.render(request)
     self.assertEqual(NOT_DONE_YET, result)
     self.assertEqual(
         [("Connection", ["Upgrade"]), ("Upgrade", ["WebSocket"]),
          ("Sec-Websocket-Accept", ["oYBv54i42V5dw6KnZqOFroecUTc="])],
         list(request.responseHeaders.getAllRawHeaders()))
     self.assertEqual(
         "HTTP/1.1 101 Switching Protocols\r\n"
         "Connection: Upgrade\r\n"
         "Upgrade: WebSocket\r\n"
         "Sec-Websocket-Accept: oYBv54i42V5dw6KnZqOFroecUTc=\r\n\r\n",
         channel.transport.value())
     self.assertEqual(101, request.code)
     self.assertIdentical(None, request.transport)
Exemplo n.º 4
0
 def test_renderRealRequest(self):
     """
     The request managed by L{WebSocketsResource.render} doesn't contain
     unnecessary HTTP headers like I{Content-Type} or I{Transfer-Encoding}.
     """
     channel = DummyChannel()
     channel.transport = StringTransportWithDisconnection()
     channel.transport.protocol = channel
     request = Request(channel, False)
     headers = {
         "upgrade": "Websocket",
         "connection": "Upgrade",
         "sec-websocket-key": "secure",
         "sec-websocket-version": "13"}
     for key, value in headers.items():
         request.requestHeaders.setRawHeaders(key, [value])
     request.method = "GET"
     request.clientproto = "HTTP/1.1"
     result = self.resource.render(request)
     self.assertEqual(NOT_DONE_YET, result)
     self.assertEqual(
         [("Connection", ["Upgrade"]),
          ("Upgrade", ["WebSocket"]),
          ("Sec-Websocket-Accept", ["oYBv54i42V5dw6KnZqOFroecUTc="])],
         list(request.responseHeaders.getAllRawHeaders()))
     self.assertEqual(
         "HTTP/1.1 101 Switching Protocols\r\n"
         "Connection: Upgrade\r\n"
         "Upgrade: WebSocket\r\n"
         "Sec-Websocket-Accept: oYBv54i42V5dw6KnZqOFroecUTc=\r\n\r\n",
         channel.transport.value())
     self.assertEqual(101, request.code)
     self.assertIdentical(None, request.transport)
Exemplo n.º 5
0
 def test_redirectToUnicodeURL(self) :
     """
     L{redirectTo} will raise TypeError if unicode object is passed in URL
     """
     request = Request(DummyChannel(), True)
     request.method = b'GET'
     targetURL = u'http://target.example.com/4321'
     self.assertRaises(TypeError, redirectTo, targetURL, request)
Exemplo n.º 6
0
 def test_redirectToUnicodeURL(self):
     """
     L{redirectTo} will raise TypeError if unicode object is passed in URL
     """
     request = Request(DummyChannel(), True)
     request.method = b"GET"
     targetURL = "http://target.example.com/4321"
     self.assertRaises(TypeError, redirectTo, targetURL, request)
Exemplo n.º 7
0
def makeRequest(method, path, post_data=None):
    req = Request(FakeChannel(), None)
    req.prepath = req.postpath = None
    req.method = method
    req.path = path
    req.content = StringIO(post_data)
    resource = site.getChildWithDefault(path, req)
    return resource.render(req)
Exemplo n.º 8
0
def getRequest(method, url):
    import warnings
    msg = 'These tests will be removed in a future release of the txrestserver'
    warnings.warn(msg, DeprecationWarning)

    req = Request(FakeChannel(), None)
    req.method = method
    req.path = url
    return req
Exemplo n.º 9
0
    def test_requestFeature(self):
        self.log.addFeature(request)
        req = Request(DummyChannel(), False)
        req.method = 'GET'
        req.uri = '/foo'

        self.log.request(req).info('handling request')
        self.assertIn('method=GET', self.out.getvalue())
        self.assertIn('uri=/foo', self.out.getvalue())
Exemplo n.º 10
0
 def test_error_in_read_function(self):
     req = Request(DummyChannel(), 1)
     req.setResponseCode(200)
     req.method = 'GET'
     def error_function(a, b):
         raise Exception("boom")
     self.api.read_GET = error_function
     self.api._handle_error = mock.Mock()
     result, body = self.api.resource_renderer('res', req)
     self.api._handle_error.assert_called_with(req, 500, "ReadError",
         "Error %r in resource reading function." % Exception('boom'))
Exemplo n.º 11
0
 def test_headersAndCode(self):
     """
     L{redirectTo} will set the C{Location} and C{Content-Type} headers on
     its request, and set the response code to C{FOUND}, so the browser will
     be redirected.
     """
     request = Request(DummyChannel(), True)
     request.method = 'GET'
     targetURL = "http://target.example.com/4321"
     redirectTo(targetURL, request)
     self.assertEqual(request.code, FOUND)
     self.assertEqual(request.responseHeaders.getRawHeaders('location'),
                      [targetURL])
     self.assertEqual(request.responseHeaders.getRawHeaders('content-type'),
                      ['text/html; charset=utf-8'])
Exemplo n.º 12
0
    def get_request(self, method, uri, clientproto, headers, data, response):
        self.transport = response.transport

        r = Request(self, False)
        r.method = method
        r.clientproto = clientproto
        r.path = uri
        r.client = response.client
        r.host = self.host
        r.prepath = []
        r.postpath = map(unquote, string.split(r.path[1:], "/"))
        r.content = StringIO.StringIO(data)
        r.transport = response.transport

        return r
Exemplo n.º 13
0
 def test_headersAndCode(self):
     """
     L{redirectTo} will set the C{Location} and C{Content-Type} headers on
     its request, and set the response code to C{FOUND}, so the browser will
     be redirected.
     """
     request = Request(DummyChannel(), True)
     request.method = b'GET'
     targetURL = b"http://target.example.com/4321"
     redirectTo(targetURL, request)
     self.assertEqual(request.code, FOUND)
     self.assertEqual(
         request.responseHeaders.getRawHeaders(b'location'), [targetURL])
     self.assertEqual(
         request.responseHeaders.getRawHeaders(b'content-type'),
         [b'text/html; charset=utf-8'])
Exemplo n.º 14
0
    def doLocationTest(self, requestPath: bytes):
        """
        Render a response to a request with path *requestPath*

        @param requestPath: A slash-separated path like C{b'/foo/bar'}.

        @returns: The value of the I{Location} header.
        """
        request = Request(DummyChannel(), True)
        request.method = b"GET"
        request.prepath = requestPath.lstrip(b"/").split(b"/")

        resource = ParentRedirect()
        resource.render(request)

        [location] = request.responseHeaders.getRawHeaders(b"Location")
        return location
Exemplo n.º 15
0
def getRequest(method, url):
    req = Request(FakeChannel(), None)
    req.method = method
    req.path = url
    return req
Exemplo n.º 16
0
 def test_resource_renderer(self):
     req = Request(DummyChannel(), 1)
     req.method = 'GET'
     result, body = self.api.resource_renderer('res', req)
     self.assertEqual(result.code, 200)
     self.assertEqual(body, 'res::SimpleAPIResource')