Exemple #1
0
    def test_del_closes_socket(self):
        timer = api.exc_after(0.5, api.TimeoutError)

        def accept_once(listener):
            # delete/overwrite the original conn
            # object, only keeping the file object around
            # closing the file object should close everything
            try:
                conn, addr = listener.accept()
                conn = conn.makeGreenFile()
                conn.write('hello\n')
                conn.close()
                self.assertRaises(socket.error, conn.write, 'a')
            finally:
                listener.close()

        server = api.tcp_listener(('0.0.0.0', 0))
        killer = api.spawn(accept_once, server)
        client = api.connect_tcp(('127.0.0.1', server.getsockname()[1]))
        fd = client.makeGreenFile()
        client.close()
        assert fd.read() == 'hello\n'
        assert fd.read() == ''

        timer.cancel()
Exemple #2
0
 def did_it_work(server):
     client = api.connect_tcp(('127.0.0.1', server.getsockname()[1]))
     fd = client.makeGreenFile()
     client.close()
     assert fd.readline() == 'hello\n'
     assert fd.read() == ''
     fd.close()
 def did_it_work(server):
     client = api.connect_tcp(('127.0.0.1', server.getsockname()[1]))
     fd = client.makeGreenFile()
     client.close()
     assert fd.readline() == 'hello\n'    
     assert fd.read() == ''
     fd.close()
    def test_010_no_chunked_http_1_0(self):
        self.site.application = chunked_app
        sock = api.connect_tcp(('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write(
            'GET / HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\n\r\n')
        self.assert_('Transfer-Encoding: chunked' not in fd.read())
    def test_010_no_chunked_http_1_0(self):
        self.site.application = chunked_app
        sock = api.connect_tcp(
            ('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.0\r\nHost: localhost\r\nConnection: close\r\n\r\n')
        self.assert_('Transfer-Encoding: chunked' not in fd.read())
    def test_002_keepalive(self):
        sock = api.connect_tcp(('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        read_http(sock)
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        read_http(sock)
        fd.close()
Exemple #7
0
 def _getServer(self, host, port):
     destn = (host, port)
     try:
         return self.servers[destn]
     except KeyError:
         server = evtapi.connect_tcp(destn)
         server = CallOnClose(server, self.onclose)
         self.servers[destn] = server
         return server
Exemple #8
0
 def _getServer(self,host,port):
     destn = (host,port)
     try:
       return self.servers[destn]
     except KeyError:
       server = evtapi.connect_tcp(destn)
       server = CallOnClose(server,self.onclose)
       self.servers[destn] = server
       return server
    def test_003_passing_non_int_to_read(self):
        # This should go in greenio_test
        sock = api.connect_tcp(('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        cancel = api.exc_after(1, RuntimeError)
        self.assertRaises(TypeError, fd.read, "This shouldn't work")
        cancel.cancel()
        fd.close()
    def test_001_server(self):
        sock = api.connect_tcp(('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.0\r\nHost: localhost\r\n\r\n')
        result = fd.read()
        fd.close()
        ## The server responds with the maximum version it supports
        self.assert_(result.startswith('HTTP'), result)
        self.assert_(result.endswith('hello world'))
    def test_002_keepalive(self):
        sock = api.connect_tcp(
            ('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        read_http(sock)
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        read_http(sock)
        fd.close()
    def test_001_server(self):
        sock = api.connect_tcp(
            ('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.0\r\nHost: localhost\r\n\r\n')
        result = fd.read()
        fd.close()
        ## The server responds with the maximum version it supports
        self.assert_(result.startswith('HTTP'), result)
        self.assert_(result.endswith('hello world'))
    def test_003_passing_non_int_to_read(self):
        # This should go in greenio_test
        sock = api.connect_tcp(
            ('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        cancel = api.exc_after(1, RuntimeError)
        self.assertRaises(TypeError, fd.read, "This shouldn't work")
        cancel.cancel()
        fd.close()
    def test_004_close_keepalive(self):
        sock = api.connect_tcp(
            ('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        read_http(sock)
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
        read_http(sock)
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        self.assertRaises(ConnectionClosed, read_http, sock)
        fd.close()
Exemple #15
0
    def test_004_close_keepalive(self):
        sock = api.connect_tcp(
            ('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        read_http(sock)
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
        read_http(sock)
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        self.assertRaises(ConnectionClosed, read_http, sock)
        fd.close()
    def test_008_correctresponse(self):
        sock = api.connect_tcp(('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        response_line_200, _, _ = read_http(sock)
        fd.write('GET /notexist HTTP/1.1\r\nHost: localhost\r\n\r\n')
        response_line_404, _, _ = read_http(sock)
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        response_line_test, _, _ = read_http(sock)
        self.assertEqual(response_line_200, response_line_test)
        fd.close()
 def test_006_reject_long_urls(self):
     sock = api.connect_tcp(('127.0.0.1', 12346))
     path_parts = []
     for ii in range(3000):
         path_parts.append('path')
     path = '/'.join(path_parts)
     request = 'GET /%s HTTP/1.0\r\nHost: localhost\r\n\r\n' % path
     fd = sock.makeGreenFile()
     fd.write(request)
     result = fd.readline()
     status = result.split(' ')[1]
     self.assertEqual(status, '414')
     fd.close()
    def test_008_correctresponse(self):
        sock = api.connect_tcp(
            ('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        response_line_200,_,_ = read_http(sock)
        fd.write('GET /notexist HTTP/1.1\r\nHost: localhost\r\n\r\n')
        response_line_404,_,_ = read_http(sock)
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\n\r\n')
        response_line_test,_,_ = read_http(sock)
        self.assertEqual(response_line_200,response_line_test)
        fd.close()
 def test_006_reject_long_urls(self):
     sock = api.connect_tcp(
         ('127.0.0.1', 12346))
     path_parts = []
     for ii in range(3000):
         path_parts.append('path')
     path = '/'.join(path_parts)
     request = 'GET /%s HTTP/1.0\r\nHost: localhost\r\n\r\n' % path
     fd = sock.makeGreenFile()
     fd.write(request)
     result = fd.readline()
     status = result.split(' ')[1]
     self.assertEqual(status, '414')
     fd.close()
    def test_011_multiple_chunks(self):
        self.site.application = big_chunks
        sock = api.connect_tcp(
            ('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write('GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
        headers = fd.readuntil('\r\n\r\n')
        self.assert_('Transfer-Encoding: chunked' in headers)
        chunks = 0
        chunklen = int(fd.readline(), 16)
        while chunklen:
            chunks += 1
            chunk = fd.read(chunklen)
            fd.readline()
            chunklen = int(fd.readline(), 16)
        self.assert_(chunks > 1)
    def test_011_multiple_chunks(self):
        self.site.application = big_chunks
        sock = api.connect_tcp(('127.0.0.1', 12346))

        fd = sock.makeGreenFile()
        fd.write(
            'GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n')
        headers = fd.readuntil('\r\n\r\n')
        self.assert_('Transfer-Encoding: chunked' in headers)
        chunks = 0
        chunklen = int(fd.readline(), 16)
        while chunklen:
            chunks += 1
            chunk = fd.read(chunklen)
            fd.readline()
            chunklen = int(fd.readline(), 16)
        self.assert_(chunks > 1)
    def test_007_get_arg(self):
        # define a new handler that does a get_arg as well as a read_body
        def new_app(env, start_response):
            body = env['wsgi.input'].read()
            a = cgi.parse_qs(body).get('a', [1])[0]
            start_response('200 OK', [('Content-type', 'text/plain')])
            return ['a is %s, body is %s' % (a, body)]

        self.site.application = new_app
        sock = api.connect_tcp(('127.0.0.1', 12346))
        request = '\r\n'.join(('POST / HTTP/1.0', 'Host: localhost',
                               'Content-Length: 3', '', 'a=a'))
        fd = sock.makeGreenFile()
        fd.write(request)

        # send some junk after the actual request
        fd.write('01234567890123456789')
        reqline, headers, body = read_http(sock)
        self.assertEqual(body, 'a is a, body is a=a')
        fd.close()
Exemple #23
0
    def test_connect_ssl(self):
        def accept_once(listenfd):
            try:
                conn, addr = listenfd.accept()
                fl = conn.makeGreenFile('w')
                fl.write('hello\r\n')
                fl.close()
                conn.close()
            finally:
                listenfd.close()

        server = api.ssl_listener(('0.0.0.0', 0), self.certificate_file,
                                  self.private_key_file)
        api.spawn(accept_once, server)

        client = util.wrap_ssl(
            api.connect_tcp(('127.0.0.1', server.getsockname()[1])))
        client = client.makeGreenFile()

        assert client.readline() == 'hello\r\n'
        assert client.read() == ''
        client.close()
Exemple #24
0
    def test_connect_tcp(self):
        def accept_once(listenfd):
            try:
                conn, addr = listenfd.accept()
                fd = conn.makeGreenFile()
                conn.close()
                fd.write('hello\n')
                fd.close()
            finally:
                listenfd.close()

        server = api.tcp_listener(('0.0.0.0', 0))
        api.spawn(accept_once, server)

        client = api.connect_tcp(('127.0.0.1', server.getsockname()[1]))
        fd = client.makeGreenFile()
        client.close()
        assert fd.readline() == 'hello\n'

        assert fd.read() == ''
        fd.close()

        check_hub()
 def test_del_closes_socket(self):
     timer = api.exc_after(0.5, api.TimeoutError)
     def accept_once(listener):
         # delete/overwrite the original conn
         # object, only keeping the file object around
         # closing the file object should close everything
         try:
             conn, addr = listener.accept()
             conn = conn.makeGreenFile()
             conn.write('hello\n')
             conn.close()
             self.assertRaises(socket.error, conn.write, 'a')
         finally:
             listener.close()
     server = api.tcp_listener(('0.0.0.0', 0))
     killer = api.spawn(accept_once, server)
     client = api.connect_tcp(('127.0.0.1', server.getsockname()[1]))
     fd = client.makeGreenFile()
     client.close()
     assert fd.read() == 'hello\n'    
     assert fd.read() == ''
     
     timer.cancel()
    def test_connect_ssl(self):
        def accept_once(listenfd):
            try:
                conn, addr = listenfd.accept()
                fl = conn.makeGreenFile('w')
                fl.write('hello\r\n')
                fl.close()
                conn.close()
            finally:
                listenfd.close()

        server = api.ssl_listener(('0.0.0.0', 0),
                                  self.certificate_file,
                                  self.private_key_file)
        api.spawn(accept_once, server)

        client = util.wrap_ssl(
            api.connect_tcp(('127.0.0.1', server.getsockname()[1])))
        client = client.makeGreenFile()

        assert client.readline() == 'hello\r\n'
        assert client.read() == ''
        client.close()
    def test_connect_tcp(self):
        def accept_once(listenfd):
            try:
                conn, addr = listenfd.accept()
                fd = conn.makeGreenFile()
                conn.close()
                fd.write('hello\n')
                fd.close()
            finally:
                listenfd.close()

        server = api.tcp_listener(('0.0.0.0', 0))
        api.spawn(accept_once, server)

        client = api.connect_tcp(('127.0.0.1', server.getsockname()[1]))
        fd = client.makeGreenFile()
        client.close()
        assert fd.readline() == 'hello\n'

        assert fd.read() == ''
        fd.close()

        check_hub()
Exemple #28
0
    def test_007_get_arg(self):
        # define a new handler that does a get_arg as well as a read_body
        def new_handle_request(req):
            a = req.get_arg('a')
            body = req.read_body()
            req.write('a is %s, body is %s' % (a, body))
        self.site.handle_request = new_handle_request

        sock = api.connect_tcp(
            ('127.0.0.1', 12346))
        request = '\r\n'.join((
            'POST /%s HTTP/1.0',
            'Host: localhost',
            'Content-Length: 3',
            '',
            'a=a'))
        fd = sock.makeGreenFile()
        fd.write(request)

        # send some junk after the actual request
        fd.write('01234567890123456789')
        reqline, headers, body = read_http(sock)
        self.assertEqual(body, 'a is a, body is a=a')
        fd.close()
    def test_007_get_arg(self):
        # define a new handler that does a get_arg as well as a read_body
        def new_app(env, start_response):
            body = env['wsgi.input'].read()
            a = cgi.parse_qs(body).get('a', [1])[0]
            start_response('200 OK', [('Content-type', 'text/plain')])
            return ['a is %s, body is %s' % (a, body)]
        self.site.application = new_app
        sock = api.connect_tcp(
            ('127.0.0.1', 12346))
        request = '\r\n'.join((
            'POST / HTTP/1.0',
            'Host: localhost',
            'Content-Length: 3',
            '',
            'a=a'))
        fd = sock.makeGreenFile()
        fd.write(request)

        # send some junk after the actual request
        fd.write('01234567890123456789')
        reqline, headers, body = read_http(sock)
        self.assertEqual(body, 'a is a, body is a=a')
        fd.close()
Exemple #30
0
 def __init__(self, hostname, port):
     self.socket = api.connect_tcp((hostname, port))
     self.open = True
     self.buffer = ""
     self.transcript = []
Exemple #31
0
 def connect_tcp(self, address):
     self.log.info('Connecting to server at address %r', address)
     return api.connect_tcp(address)
Exemple #32
0
 def connect_tcp(self, address):
     self.log.info('Connecting to server at address %r', address)
     return api.connect_tcp(address)