示例#1
0
 def _getReq(self):
     d = DummyChannel()
     d.site.resource.putChild('newrender', NewRenderResource())
     d.transport.port = 81
     request = server.Request(d, 1)
     request.setHost('example.com', 81)
     request.gotLength(0)
     return request
示例#2
0
 def testPrePathURLSetSSLHost(self):
     d = DummyChannel()
     d.transport.port = 81
     request = server.Request(d, 1)
     request.setHost('foo.com', 81, 1)
     request.gotLength(0)
     request.requestReceived('GET', '/foo/bar', 'HTTP/1.0')
     self.assertEqual(request.prePathURL(), 'https://foo.com:81/foo/bar')
示例#3
0
 def test_get_client_proxy_ip_with_x_forwarded(self):
     d = DummyChannel()
     request = server.Request(d, False)
     request.gotLength(0)
     request.requestHeaders.setRawHeaders(b"x-forwarded-for",
                                          [b"80.80.80.80"])
     request.requestReceived(b'GET', b'/foo%2Fbar', b'HTTP/1.0')
     self.assertEqual(request.getClientProxyIP(), '192.168.1.1')
示例#4
0
 def testPrePathURLSSLPort(self):
     d = DummyChannel()
     d.transport.port = 443
     request = server.Request(d, 1)
     request.setHost(b'example.com', 443)
     request.gotLength(0)
     request.requestReceived(b'GET', b'/foo/bar', b'HTTP/1.0')
     self.assertEqual(request.prePathURL(), b'http://example.com:443/foo/bar')
示例#5
0
 def testPrePathURLSSLNonDefault(self):
     d = DummyChannel()
     d.transport = DummyChannel.SSL()
     d.transport.port = 81
     request = server.Request(d, 1)
     request.setHost(b'example.com', 81)
     request.gotLength(0)
     request.requestReceived(b'GET', b'/foo/bar', b'HTTP/1.0')
     self.assertEqual(request.prePathURL(), b'https://example.com:81/foo/bar')
示例#6
0
 def send(action, args):
     args['action'] = [action];
     request = server.Request(Channel(self.site), True)
     request.site = self.site
     request.args = args
     request.method = 'GET'
     d = resource.wrap_http(request)
     d.addCallback(collect)
     return d
示例#7
0
 def testPrePathURLHTTPPortAndSSL(self):
     d = DummyChannel()
     d.transport = DummyChannel.SSL()
     d.transport.port = 80
     request = server.Request(d, 1)
     request.setHost('example.com', 80)
     request.gotLength(0)
     request.requestReceived('GET', '/foo/bar', 'HTTP/1.0')
     self.assertEqual(request.prePathURL(), 'https://example.com:80/foo/bar')
示例#8
0
 def testUICaching(self):
     ui_caching = True
     site = CacheControlledSite(ui_caching, self._resource)
     request = server.Request(DummyChannel(), False)
     request.prepath = [b""]
     request.postpath = [b""]
     site.getResourceFor(request)
     self.assertTrue(
         request.responseHeaders.getRawHeaders("cache-control") ==
         ["max-age={}".format(FRESHNESS_TIME_SECS)])
示例#9
0
    def test04_handle(self):
        resource = CardstoriesResource(self.service)
        self.site = CardstoriesSite(resource, {}, [])
        request = server.Request(self.Channel(self.site), True)

        request.method = 'GET'
        self.assertEquals('handle', resource.handle(True, request))

        request.method = 'POST'
        self.assertEquals('handle', resource.handle(True, request))
示例#10
0
 def testNoUICaching(self):
     ui_caching = False
     site = CacheControlledSite(ui_caching, self._resource)
     request = server.Request(DummyChannel(), False)
     request.prepath = [b""]
     request.postpath = [b""]
     site.getResourceFor(request)
     self.assertTrue(
         request.responseHeaders.getRawHeaders("cache-control") ==
         ["no-store, must-revalidate"])
示例#11
0
 def test01_wrap_http(self):
     resource = CardstoriesResource(self.service)
     self.site = CardstoriesSite(resource, {}, [])
     request = server.Request(self.Channel(self.site), True)
     request.site = self.site
     request.method = 'GET'
     d = resource.wrap_http(request)
     def finish(result):
         self.assertSubstring('\r\n\r\n"handle"', request.transport.getvalue())
     d.addCallback(finish)
     return d
示例#12
0
 def _getReq(self):
     """
     Generate a dummy request for use by C{_computeAllowedMethod} tests.
     """
     d = DummyChannel()
     d.site.resource.putChild(b'gettableresource', GettableResource())
     d.transport.port = 81
     request = server.Request(d, 1)
     request.setHost(b'example.com', 81)
     request.gotLength(0)
     return request
示例#13
0
 def testRoot(self):
     rr = RootResource()
     rr.putChild(b'', rr)
     rr.putChild(b'bar', resource.Resource())
     chan = self.createServer(rr)
     for url in [b'/', b'/bar', b'/bar/baz', b'/bar/']:
         request = server.Request(chan, 1)
         request.setHost(b'example.com', 81)
         request.gotLength(0)
         request.requestReceived(b'GET', url, b'HTTP/1.0')
         self.assertEqual(request.getRootURL(), b"http://example.com/")
示例#14
0
 def test_prePathURLQuoting(self):
     """
     L{Request.prePathURL} quotes special characters in the URL segments to
     preserve the original meaning.
     """
     d = DummyChannel()
     request = server.Request(d, 1)
     request.setHost(b'example.com', 80)
     request.gotLength(0)
     request.requestReceived(b'GET', b'/foo%2Fbar', b'HTTP/1.0')
     self.assertEqual(request.prePathURL(), b'http://example.com/foo%2Fbar')
示例#15
0
 def test_connectionLost(self):
     """
     L{server.Request.connectionLost} triggers all finish notification
     Deferreds and cleans up per-request state.
     """
     d = DummyChannel()
     request = server.Request(d, 1)
     finished = request.notifyFinish()
     request.connectionLost(error.ConnectionDone("Connection done"))
     self.assertIdentical(request.channel, None)
     return self.assertFailure(finished, error.ConnectionDone)
示例#16
0
 def test01_wrap_http_disconnected(self):
     resource = CardstoriesResource(self.service)
     self.site = CardstoriesSite(resource, {}, [])
     request = server.Request(self.Channel(self.site), True)
     request.site = self.site
     request.method = 'GET'
     request._disconnected = True
     d = resource.wrap_http(request)
     def finish(result):
         self.assertEquals('', request.transport.getvalue())
     d.addCallback(finish)
     return d
示例#17
0
    def test_inMemoryStringSharedSecretSourceHMAC(self):

        users = [{
            "username": "******",
            "password": "******"
        }, {
            "username": "******",
            "canonicalUsername": "******",
            "password": "******"
        }]

        authenticator = auth.DefaultAuthenticator(
            auth.InMemoryStringSharedSecretSource(users))

        host = "example.com"
        port = 8080
        body = "hi!"
        args = {}

        request = server.Request(DummyChannel(), False)
        request.gotLength(len(body))
        request.content = StringIO()
        request.content.write(body)
        request.content.seek(0)
        request.args = args
        request.setHost(host, port, False)
        request.uri = "example.com/test"
        request.path = "/test"
        request.method = "GET"
        request.clientproto = 'HTTP/1.1'

        import httpsig

        key_id = "alice"
        secret = "wonderland"

        hs = httpsig.HeaderSigner(key_id, secret, algorithm="hmac-sha256")
        signed_headers_dict = hs.sign(
            {
                "Date": "Tue, 01 Jan 2014 01:01:01 GMT",
                "Host": "example.com"
            },
            method="GET",
            path="/test")

        request.requestHeaders = Headers(
            {x: [y]
             for x, y in signed_headers_dict.iteritems()})

        authDeferred = authenticator.auth_HMAC("alice", request)

        return authDeferred
示例#18
0
 def test03_wrap_http_fail(self):
     resource = CardstoriesResource(self.service)
     fail_string = 'FAIL STRING'
     def fail(result, request):
         raise Exception(fail_string)
     resource.handle = fail
     self.site = CardstoriesSite(resource, {}, [])
     request = server.Request(self.Channel(self.site), True)
     request.site = self.site
     d = resource.wrap_http(request)
     def finish(result):
         self.assertSubstring(fail_string, request.transport.getvalue())
     d.addCallback(finish)
     return d
示例#19
0
    def test04_handle_non_internal(self):
        def mock_handle(result, args, internal_request=False):
            return internal_request
        self.service.handle = mock_handle

        resource = CardstoriesResource(self.service)
        self.site = CardstoriesSite(resource, {}, [])
        request = server.Request(self.Channel(self.site), True)

        request.method = 'GET'
        self.assertEquals(False, resource.handle(True, request))

        request.method = 'POST'
        self.assertEquals(False, resource.handle(True, request))
示例#20
0
 def test00_render(self):
     self.site = CardstoriesSite(CardstoriesTree(self.service), {}, [])
     r = server.Request(self.Channel(self.site), True)
     r.site = r.channel.site
     input = ''
     r.gotLength(len(input))
     r.handleContentChunk(input)
     r.queued = 0
     d = r.notifyFinish()
     def finish(result):
         self.assertSubstring('\r\n\r\n"handle"', r.transport.getvalue())
     d.addCallback(finish)
     r.requestReceived('GET', '/resource', '')
     return d
示例#21
0
 def testSimple(self):
     r = resource.Resource()
     r.isLeaf = 0
     rr = RootResource()
     r.putChild('foo', rr)
     rr.putChild('', rr)
     rr.putChild('bar', resource.Resource())
     chan = self.createServer(r)
     for url in ['/foo/', '/foo/bar', '/foo/bar/baz', '/foo/bar/']:
         request = server.Request(chan, 1)
         request.setHost('example.com', 81)
         request.gotLength(0)
         request.requestReceived('GET', url, 'HTTP/1.0')
         self.assertEqual(request.getRootURL(), "http://example.com/foo")
示例#22
0
 def test_nonEncoding(self):
     """
     L{server.GzipEncoderFactory} doesn't return a L{server._GzipEncoder} if
     the I{Accept-Encoding} header doesn't mention gzip support.
     """
     request = server.Request(self.channel, False)
     request.gotLength(0)
     request.requestHeaders.setRawHeaders(b"Accept-Encoding", [b"foo,bar"])
     request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
     data = self.channel.transport.written.getvalue()
     self.assertIn(b"Content-Length", data)
     self.assertNotIn(b"Content-Encoding: gzip\r\n", data)
     body = data[data.find(b"\r\n\r\n") + 4:]
     self.assertEqual(b"Some data", body)
示例#23
0
 def test02_wrap_http_plugin_postprocess_fail(self):
     plugins = CardstoriesPlugins({ 'plugins-dir': '..',
                                    'plugins': 'plugin_fail'})
     plugins.load(True)
     resource = CardstoriesResource(self.service)
     self.site = CardstoriesSite(resource, { 'plugins-post-process': 'plugin_fail' }, plugins.plugins)
     request = server.Request(self.Channel(self.site), True)
     request.site = self.site
     request.method = 'GET'
     d = resource.wrap_http(request)
     def finish(result):
         self.assertSubstring('Internal Server Error', request.transport.getvalue())
         self.assertSubstring('POSTPROCESS', request.transport.getvalue())
     d.addCallback(finish)
     return d
示例#24
0
 def _getReq(self, resource=None):
     """
     Create a request object with a stub channel and install the
     passed resource at /newrender. If no resource is passed,
     create one.
     """
     d = DummyChannel()
     if resource is None:
         resource = NewRenderResource()
     d.site.resource.putChild(b'newrender', resource)
     d.transport.port = 81
     request = server.Request(d, 1)
     request.setHost(b'example.com', 81)
     request.gotLength(0)
     return request
示例#25
0
 def test_encoding(self):
     """
     If the client request passes a I{Accept-Encoding} header which mentions
     gzip, L{server._GzipEncoder} automatically compresses the data.
     """
     request = server.Request(self.channel, False)
     request.gotLength(0)
     request.requestHeaders.setRawHeaders(b"Accept-Encoding",
                                          [b"gzip,deflate"])
     request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
     data = self.channel.transport.written.getvalue()
     self.assertNotIn(b"Content-Length", data)
     self.assertIn(b"Content-Encoding: gzip\r\n", data)
     body = data[data.find(b"\r\n\r\n") + 4:]
     self.assertEqual(b"Some data",
                       zlib.decompress(body, 16 + zlib.MAX_WBITS))
示例#26
0
    def test_interfaces(self):
        """
        L{server.GzipEncoderFactory} implements the
        L{iweb._IRequestEncoderFactory} and its C{encoderForRequest} returns an
        instance of L{server._GzipEncoder} which implements
        L{iweb._IRequestEncoder}.
        """
        request = server.Request(self.channel, False)
        request.gotLength(0)
        request.requestHeaders.setRawHeaders(b"Accept-Encoding",
                                             [b"gzip,deflate"])
        factory = server.GzipEncoderFactory()
        self.assertTrue(verifyObject(iweb._IRequestEncoderFactory, factory))

        encoder = factory.encoderForRequest(request)
        self.assertTrue(verifyObject(iweb._IRequestEncoder, encoder))
示例#27
0
    def test_processingFailedDisplayTraceback(self):
        """
        L{Request.processingFailed} when the site has C{displayTracebacks} set
        to C{True} writes out the failure.
        """
        d = DummyChannel()
        request = server.Request(d, 1)
        request.site = server.Site(resource.Resource())
        request.site.displayTracebacks = True
        fail = failure.Failure(Exception("Oh no!"))
        request.processingFailed(fail)

        self.assertIn(b"Oh no!", request._transport.getvalue())

        # Since we didn't "handle" the exception, flush it to prevent a test
        # failure
        self.assertEqual(1, len(self.flushLoggedErrors()))
示例#28
0
 def test_multipleAccept(self):
     """
     If there are multiple I{Accept-Encoding} header,
     L{server.GzipEncoderFactory} reads them properly to detect if gzip is
     supported.
     """
     request = server.Request(self.channel, False)
     request.gotLength(0)
     request.requestHeaders.setRawHeaders(b"Accept-Encoding",
                                          [b"deflate", b"gzip"])
     request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
     data = self.channel.transport.written.getvalue()
     self.assertNotIn(b"Content-Length", data)
     self.assertIn(b"Content-Encoding: gzip\r\n", data)
     body = data[data.find(b"\r\n\r\n") + 4:]
     self.assertEqual(b"Some data",
                      zlib.decompress(body, 16 + zlib.MAX_WBITS))
示例#29
0
 def test02_transparent_transform(self):
     self.site = CardstoriesSite(CardstoriesTree(self.service),
                                 { 'plugins-pre-process': 'example',
                                   'plugins-post-process': 'example' },
                                 [ example.Plugin(self.service, [ AnotherPlugin() ]) ])
     r = server.Request(Channel(self.site), True)
     r.site = r.channel.site
     input = ''
     r.gotLength(len(input))
     r.handleContentChunk(input)
     r.queued = 0
     d = r.notifyFinish()
     def finish(result):
         self.assertSubstring('\r\n\r\n{"arg1": ["val10X", "val11X"], "arg2": ["val20X"], "MORE": "YES", "echo": ["yesX"]}', r.transport.getvalue())
     d.addCallback(finish)
     r.requestReceived('GET', '/resource?action=echo&echo=yes&arg1=val10&arg1=val11&arg2=val20', '')
     return d
示例#30
0
 def test_alreadyEncoded(self):
     """
     If the content is already encoded and the I{Content-Encoding} header is
     set, L{server.GzipEncoderFactory} properly appends gzip to it.
     """
     request = server.Request(self.channel, False)
     request.gotLength(0)
     request.requestHeaders.setRawHeaders(b"Accept-Encoding",
                                          [b"deflate", b"gzip"])
     request.responseHeaders.setRawHeaders(b"Content-Encoding",
                                          [b"deflate"])
     request.requestReceived(b'GET', b'/foo', b'HTTP/1.0')
     data = self.channel.transport.written.getvalue()
     self.assertNotIn(b"Content-Length", data)
     self.assertIn(b"Content-Encoding: deflate,gzip\r\n", data)
     body = data[data.find(b"\r\n\r\n") + 4:]
     self.assertEqual(b"Some data",
                      zlib.decompress(body, 16 + zlib.MAX_WBITS))