def test_POST_FILES_WITH_DATA(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            with open(__file__) as f:
                r = self.loop.run_until_complete(
                    client.request('post', url, loop=self.loop,
                                   data={'test': 'true'}, files={'some': f}))

                content = self.loop.run_until_complete(r.read(True))

                self.assertEqual(2, len(content['multipart-data']))
                self.assertEqual(
                    'test', content['multipart-data'][0]['name'])
                self.assertEqual(
                    'true', content['multipart-data'][0]['data'])

                f.seek(0)
                filename = os.path.split(f.name)[-1]
                self.assertEqual(
                    'some', content['multipart-data'][1]['name'])
                self.assertEqual(
                    filename, content['multipart-data'][1]['filename'])
                self.assertEqual(
                    f.read(), content['multipart-data'][1]['data'])
                self.assertEqual(r.status, 200)
Ejemplo n.º 2
0
    def test_create_connection_sock(self):
        with test_utils.run_test_server(self.loop) as httpd:
            sock = None
            infos = self.loop.run_until_complete(
                self.loop.getaddrinfo(
                    *httpd.address, type=socket.SOCK_STREAM))
            for family, type, proto, cname, address in infos:
                try:
                    sock = socket.socket(family=family, type=type, proto=proto)
                    sock.setblocking(False)
                    self.loop.run_until_complete(
                        self.loop.sock_connect(sock, address))
                except:
                    pass
                else:
                    break
            else:
                assert False, 'Can not create socket.'

            f = self.loop.create_connection(sock=sock)
            tr = self.loop.run_until_complete(f)
            pr = MyProto(tr, create_future=True)
            self.assertTrue(isinstance(tr, transports.Transport))
            self.assertTrue(isinstance(pr, protocols.Protocol))
            self.loop.run_until_complete(pr.done)
            self.assertTrue(pr.nbytes > 0)
            tr.close()
    def test_POST_FILES_LIST_CT(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            with open(__file__) as f:
                r = self.loop.run_until_complete(
                    client.request('post',
                                   url,
                                   loop=self.loop,
                                   files=[('some', f, 'text/plain')]))

                content = self.loop.run_until_complete(r.read(True))

                f.seek(0)
                filename = os.path.split(f.name)[-1]

                self.assertEqual(1, len(content['multipart-data']))
                self.assertEqual('some', content['multipart-data'][0]['name'])
                self.assertEqual(filename,
                                 content['multipart-data'][0]['filename'])
                self.assertEqual(f.read(),
                                 content['multipart-data'][0]['data'])
                self.assertEqual('text/plain',
                                 content['multipart-data'][0]['content-type'])
                self.assertEqual(r.status, 200)
 def test_HTTP_302_REDIRECT_NON_HTTP(self):
     with test_utils.run_test_server(self.loop, router=Functional) as httpd:
         self.assertRaises(
             ValueError,
             self.loop.run_until_complete,
             client.request('get', httpd.url('redirect_err'),
                            loop=self.loop))
    def test_POST_FILES_WITH_DATA(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            with open(__file__) as f:
                r = self.loop.run_until_complete(
                    client.request('post',
                                   url,
                                   loop=self.loop,
                                   data={'test': 'true'},
                                   files={'some': f}))

                content = self.loop.run_until_complete(r.read(True))

                self.assertEqual(2, len(content['multipart-data']))
                self.assertEqual('test', content['multipart-data'][0]['name'])
                self.assertEqual('true', content['multipart-data'][0]['data'])

                f.seek(0)
                filename = os.path.split(f.name)[-1]
                self.assertEqual('some', content['multipart-data'][1]['name'])
                self.assertEqual(filename,
                                 content['multipart-data'][1]['filename'])
                self.assertEqual(f.read(),
                                 content['multipart-data'][1]['data'])
                self.assertEqual(r.status, 200)
 def test_HTTP_302_REDIRECT_NON_HTTP(self):
     with test_utils.run_test_server(self.loop, router=Functional) as httpd:
         self.assertRaises(
             ValueError, self.loop.run_until_complete,
             client.request('get',
                            httpd.url('redirect_err'),
                            loop=self.loop))
    def test_POST_FILES_DEFLATE(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            with open(__file__) as f:
                r = self.loop.run_until_complete(
                    client.request('post',
                                   url,
                                   files={'some': f},
                                   chunked=1024,
                                   compress='deflate',
                                   loop=self.loop))

                content = self.loop.run_until_complete(r.read(True))

                f.seek(0)
                filename = os.path.split(f.name)[-1]

                self.assertEqual('deflate', content['compression'])
                self.assertEqual(1, len(content['multipart-data']))
                self.assertEqual('some', content['multipart-data'][0]['name'])
                self.assertEqual(filename,
                                 content['multipart-data'][0]['filename'])
                self.assertEqual(f.read(),
                                 content['multipart-data'][0]['data'])
                self.assertEqual(r.status, 200)
Ejemplo n.º 8
0
 def test_open_connection_error(self):
     with test_utils.run_test_server(self.loop) as httpd:
         f = streams.open_connection(*httpd.address, loop=self.loop)
         reader, writer = self.loop.run_until_complete(f)
         writer._protocol.connection_lost(ZeroDivisionError())
         f = reader.read()
         with self.assertRaises(ZeroDivisionError):
             self.loop.run_until_complete(f)
 def test_chunked(self):
     with test_utils.run_test_server(self.loop, router=Functional) as httpd:
         r = self.loop.run_until_complete(
             client.request('get', httpd.url('chunked'), loop=self.loop))
         self.assertEqual(r.status, 200)
         self.assertEqual(r['Transfer-Encoding'], 'chunked')
         content = self.loop.run_until_complete(r.read(True))
         self.assertEqual(content['path'], '/chunked')
    def test_set_cookies(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            resp = self.loop.run_until_complete(
                client.request('get', httpd.url('cookies'), loop=self.loop))
            self.assertEqual(resp.status, 200)

            self.assertEqual(resp.cookies['c1'].value, 'cookie1')
            self.assertEqual(resp.cookies['c2'].value, 'cookie2')
    def test_HTTP_302_REDIRECT_GET(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get', httpd.url('redirect', 2),
                               loop=self.loop))

            self.assertEqual(r.status, 200)
            self.assertEqual(2, httpd['redirects'])
 def test_chunked(self):
     with test_utils.run_test_server(self.loop, router=Functional) as httpd:
         r = self.loop.run_until_complete(
             client.request('get', httpd.url('chunked'), loop=self.loop))
         self.assertEqual(r.status, 200)
         self.assertEqual(r['Transfer-Encoding'], 'chunked')
         content = self.loop.run_until_complete(r.read(True))
         self.assertEqual(content['path'], '/chunked')
Ejemplo n.º 13
0
 def test_create_connection_local_addr_in_use(self):
     with test_utils.run_test_server(self.loop) as httpd:
         f = self.loop.create_connection(
             *httpd.address, local_addr=httpd.address)
         with self.assertRaises(socket.error) as cm:
             self.loop.run_until_complete(f)
         self.assertEqual(cm.exception.errno, errno.EADDRINUSE)
         self.assertIn(str(httpd.address), cm.exception.strerror)
    def test_set_cookies(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            resp = self.loop.run_until_complete(
                client.request('get', httpd.url('cookies'), loop=self.loop))
            self.assertEqual(resp.status, 200)

            self.assertEqual(resp.cookies['c1'].value, 'cookie1')
            self.assertEqual(resp.cookies['c2'].value, 'cookie2')
    def test_HTTP_302_max_redirects(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get', httpd.url('redirect', 5),
                               max_redirects=2, loop=self.loop))

            self.assertEqual(r.status, 302)
            self.assertEqual(2, httpd['redirects'])
 def test_request_conn_closed(self):
     with test_utils.run_test_server(self.loop, router=Functional) as httpd:
         httpd['close'] = True
         self.assertRaises(
             tulip.http.HttpException, self.loop.run_until_complete,
             client.request('get',
                            httpd.url('method', 'get'),
                            loop=self.loop))
    def test_HTTP_302_REDIRECT_GET(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get', httpd.url('redirect', 2),
                               loop=self.loop))

            self.assertEqual(r.status, 200)
            self.assertEqual(2, httpd['redirects'])
 def test_request_conn_closed(self):
     with test_utils.run_test_server(self.loop, router=Functional) as httpd:
         httpd['close'] = True
         self.assertRaises(
             tulip.http.HttpException,
             self.loop.run_until_complete,
             client.request('get', httpd.url('method', 'get'),
                            loop=self.loop))
 def test_timeout(self):
     with test_utils.run_test_server(self.loop, router=Functional) as httpd:
         httpd['noresponse'] = True
         self.assertRaises(
             tulip.TimeoutError,
             self.loop.run_until_complete,
             client.request('get', httpd.url('method', 'get'),
                            timeout=0.1, loop=self.loop))
 def test_timeout(self):
     with test_utils.run_test_server(self.loop, router=Functional) as httpd:
         httpd['noresponse'] = True
         self.assertRaises(
             tulip.TimeoutError, self.loop.run_until_complete,
             client.request('get',
                            httpd.url('method', 'get'),
                            timeout=0.1,
                            loop=self.loop))
    def test_HTTP_302_max_redirects(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get',
                               httpd.url('redirect', 5),
                               max_redirects=2,
                               loop=self.loop))

            self.assertEqual(r.status, 302)
            self.assertEqual(2, httpd['redirects'])
    def test_HTTP_200_GET_WITH_PARAMS(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get', httpd.url('method', 'get'),
                               params={'q': 'test'}, loop=self.loop))
            content = self.loop.run_until_complete(r.content.read())
            content = content.decode()

            self.assertIn('"query": "q=test"', content)
            self.assertEqual(r.status, 200)
Ejemplo n.º 23
0
 def test_create_connection_local_addr(self):
     with test_utils.run_test_server(self.loop) as httpd:
         port = find_unused_port()
         f = self.loop.create_connection(
             *httpd.address, local_addr=(httpd.address[0], port))
         tr = self.loop.run_until_complete(f)
         pr = MyProto(tr, create_future=True)
         expected = pr.transport.get_extra_info('socket').getsockname()[1]
         self.assertEqual(port, expected)
         tr.close()
Ejemplo n.º 24
0
 def test_create_connection(self):
     with test_utils.run_test_server(self.loop) as httpd:
         f = self.loop.create_connection(*httpd.address)
         tr = self.loop.run_until_complete(f)
         pr = MyProto(tr, create_future=True)
         self.assertTrue(isinstance(tr, transports.Transport))
         self.assertTrue(isinstance(pr, protocols.Protocol))
         self.loop.run_until_complete(pr.done)
         self.assertTrue(pr.nbytes > 0)
         tr.close()
    def test_POST_DATA(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')
            r = self.loop.run_until_complete(
                client.request('post', url, data={'some': 'data'},
                               loop=self.loop))
            self.assertEqual(r.status, 200)

            content = self.loop.run_until_complete(r.read(True))
            self.assertEqual({'some': ['data']}, content['form'])
            self.assertEqual(r.status, 200)
    def test_HTTP_302_REDIRECT_POST(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('post', httpd.url('redirect', 2),
                               data={'some': 'data'}, loop=self.loop))
            content = self.loop.run_until_complete(r.content.read())
            content = content.decode()

            self.assertEqual(r.status, 200)
            self.assertIn('"method": "POST"', content)
            self.assertEqual(2, httpd['redirects'])
    def test_encoding(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get', httpd.url('encoding', 'deflate'),
                               loop=self.loop))
            self.assertEqual(r.status, 200)

            r = self.loop.run_until_complete(
                client.request('get', httpd.url('encoding', 'gzip'),
                               loop=self.loop))
            self.assertEqual(r.status, 200)
Ejemplo n.º 28
0
 def test_open_connection(self):
     with test_utils.run_test_server(self.loop) as httpd:
         f = streams.open_connection(*httpd.address, loop=self.loop)
         reader, writer = self.loop.run_until_complete(f)
         writer.write(b'GET / HTTP/1.0\r\n\r\n')
         f = reader.readline()
         data = self.loop.run_until_complete(f)
         self.assertEqual(data, b'HTTP/1.0 200 OK\r\n')
         f = reader.read()
         data = self.loop.run_until_complete(f)
         self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
Ejemplo n.º 29
0
 def test_open_connection_no_loop_ssl(self):
     with test_utils.run_test_server(self.loop, use_ssl=True) as httpd:
         try:
             events.set_event_loop(self.loop)
             f = streams.open_connection(*httpd.address, ssl=True)
             reader, writer = self.loop.run_until_complete(f)
         finally:
             events.set_event_loop(None)
         writer.write(b'GET / HTTP/1.0\r\n\r\n')
         f = reader.read()
         data = self.loop.run_until_complete(f)
         self.assertTrue(data.endswith(b'\r\n\r\nTest message'))
    def test_HTTP_200_GET_WITH_MIXED_PARAMS(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get',
                               httpd.url('method', 'get') + '?test=true',
                               params={'q': 'test'},
                               loop=self.loop))
            content = self.loop.run_until_complete(r.content.read())
            content = content.decode()

            self.assertIn('"query": "test=true&q=test"', content)
            self.assertEqual(r.status, 200)
    def test_cookies(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            c = http.cookies.Morsel()
            c.set('test3', '456', '456')

            r = self.loop.run_until_complete(
                client.request(
                    'get', httpd.url('method', 'get'), loop=self.loop,
                    cookies={'test1': '123', 'test2': c}))
            self.assertEqual(r.status, 200)

            content = self.loop.run_until_complete(r.content.read())
            self.assertIn(b'"Cookie": "test1=123; test3=456"', bytes(content))
    def test_encoding(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get',
                               httpd.url('encoding', 'deflate'),
                               loop=self.loop))
            self.assertEqual(r.status, 200)

            r = self.loop.run_until_complete(
                client.request('get',
                               httpd.url('encoding', 'gzip'),
                               loop=self.loop))
            self.assertEqual(r.status, 200)
    def test_HTTP_302_REDIRECT_POST(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('post',
                               httpd.url('redirect', 2),
                               data={'some': 'data'},
                               loop=self.loop))
            content = self.loop.run_until_complete(r.content.read())
            content = content.decode()

            self.assertEqual(r.status, 200)
            self.assertIn('"method": "POST"', content)
            self.assertEqual(2, httpd['redirects'])
    def test_POST_DATA(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')
            r = self.loop.run_until_complete(
                client.request('post',
                               url,
                               data={'some': 'data'},
                               loop=self.loop))
            self.assertEqual(r.status, 200)

            content = self.loop.run_until_complete(r.read(True))
            self.assertEqual({'some': ['data']}, content['form'])
            self.assertEqual(r.status, 200)
Ejemplo n.º 35
0
 def test_create_ssl_connection(self):
     with test_utils.run_test_server(
             self.loop, use_ssl=True) as httpd:
         f = self.loop.create_connection(*httpd.address, ssl=True)
         tr = self.loop.run_until_complete(f)
         pr = MyProto(tr, create_future=True)
         self.assertTrue(isinstance(tr, transports.Transport))
         self.assertTrue(isinstance(pr, protocols.Protocol))
         self.assertTrue('ssl' in tr.__class__.__name__.lower())
         self.assertTrue(
             hasattr(tr.get_extra_info('socket'), 'getsockname'))
         self.loop.run_until_complete(pr.done)
         self.assertTrue(pr.nbytes > 0)
         tr.close()
    def test_HTTP_200_OK_METHOD(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            for meth in ('get', 'post', 'put', 'delete', 'head'):
                r = self.loop.run_until_complete(
                    client.request(meth, httpd.url('method', meth),
                                   loop=self.loop))
                content1 = self.loop.run_until_complete(r.read())
                content2 = self.loop.run_until_complete(r.read())
                content = content1.decode()

                self.assertEqual(r.status, 200)
                self.assertIn('"method": "%s"' % meth.upper(), content)
                self.assertEqual(content1, content2)
                r.close()
    def test_HTTP_200_OK_METHOD(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            for meth in ('get', 'post', 'put', 'delete', 'head'):
                r = self.loop.run_until_complete(
                    client.request(meth,
                                   httpd.url('method', meth),
                                   loop=self.loop))
                content1 = self.loop.run_until_complete(r.read())
                content2 = self.loop.run_until_complete(r.read())
                content = content1.decode()

                self.assertEqual(r.status, 200)
                self.assertIn('"method": "%s"' % meth.upper(), content)
                self.assertEqual(content1, content2)
                r.close()
Ejemplo n.º 38
0
    def test_sock_client_ops(self):
        with test_utils.run_test_server(self.loop) as httpd:
            sock = socket.socket()
            sock.setblocking(False)
            self.loop.run_until_complete(
                self.loop.sock_connect(sock, httpd.address))
            self.loop.run_until_complete(
                self.loop.sock_sendall(sock, b'GET / HTTP/1.0\r\n\r\n'))
            data = self.loop.run_until_complete(
                self.loop.sock_recv(sock, 1024))
            # consume data
            self.loop.run_until_complete(
                self.loop.sock_recv(sock, 1024))
            sock.close()

        self.assertTrue(re.match(rb'HTTP/1.0 200 OK', data), data)
    def test_use_global_loop(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            try:
                tulip.set_event_loop(self.loop)
                r = self.loop.run_until_complete(
                    client.request('get', httpd.url('method', 'get')))
            finally:
                tulip.set_event_loop(None)
            content1 = self.loop.run_until_complete(r.read())
            content2 = self.loop.run_until_complete(r.read())
            content = content1.decode()

            self.assertEqual(r.status, 200)
            self.assertIn('"method": "GET"', content)
            self.assertEqual(content1, content2)
            r.close()
    def test_use_global_loop(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            try:
                tulip.set_event_loop(self.loop)
                r = self.loop.run_until_complete(
                    client.request('get', httpd.url('method', 'get')))
            finally:
                tulip.set_event_loop(None)
            content1 = self.loop.run_until_complete(r.read())
            content2 = self.loop.run_until_complete(r.read())
            content = content1.decode()

            self.assertEqual(r.status, 200)
            self.assertIn('"method": "GET"', content)
            self.assertEqual(content1, content2)
            r.close()
    def test_cookies(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            c = http.cookies.Morsel()
            c.set('test3', '456', '456')

            r = self.loop.run_until_complete(
                client.request('get',
                               httpd.url('method', 'get'),
                               loop=self.loop,
                               cookies={
                                   'test1': '123',
                                   'test2': c
                               }))
            self.assertEqual(r.status, 200)

            content = self.loop.run_until_complete(r.content.read())
            self.assertIn(b'"Cookie": "test1=123; test3=456"', bytes(content))
    def test_POST_FILES_IO(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            data = io.BytesIO(b'data')

            r = self.loop.run_until_complete(
                client.request('post', url, files=[data], loop=self.loop))

            content = self.loop.run_until_complete(r.read(True))

            self.assertEqual(1, len(content['multipart-data']))
            self.assertEqual(
                {'content-type': 'application/octet-stream',
                 'data': 'data',
                 'filename': 'unknown',
                 'name': 'unknown'}, content['multipart-data'][0])
            self.assertEqual(r.status, 200)
    def test_session_cookies(self):
        from tulip.http import session
        s = session.Session()

        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            s.update_cookies({'test': '1'})
            r = self.loop.run_until_complete(
                client.request('get', httpd.url('cookies'),
                               session=s, loop=self.loop))
            self.assertEqual(r.status, 200)
            content = self.loop.run_until_complete(r.read(True))

            self.assertEqual(content['headers']['Cookie'], 'test=1')
            r.close()

            cookies = sorted([(k, v.value) for k, v in s.cookies.items()])
            self.assertEqual(
                cookies, [('c1', 'cookie1'), ('c2', 'cookie2'), ('test', '1')])
    def test_keepalive(self):
        from tulip.http import session
        s = session.Session()

        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get', httpd.url('keepalive',),
                               session=s, loop=self.loop))
            self.assertEqual(r.status, 200)
            content = self.loop.run_until_complete(r.read(True))
            self.assertEqual(content['content'], 'requests=1')
            r.close()

            r = self.loop.run_until_complete(
                client.request('get', httpd.url('keepalive'),
                               session=s, loop=self.loop))
            self.assertEqual(r.status, 200)
            content = self.loop.run_until_complete(r.read(True))
            self.assertEqual(content['content'], 'requests=2')
            r.close()
    def test_POST_FILES_IO(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            data = io.BytesIO(b'data')

            r = self.loop.run_until_complete(
                client.request('post', url, files=[data], loop=self.loop))

            content = self.loop.run_until_complete(r.read(True))

            self.assertEqual(1, len(content['multipart-data']))
            self.assertEqual(
                {
                    'content-type': 'application/octet-stream',
                    'data': 'data',
                    'filename': 'unknown',
                    'name': 'unknown'
                }, content['multipart-data'][0])
            self.assertEqual(r.status, 200)
    def test_session_cookies(self):
        from tulip.http import session
        s = session.Session()

        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            s.update_cookies({'test': '1'})
            r = self.loop.run_until_complete(
                client.request('get',
                               httpd.url('cookies'),
                               session=s,
                               loop=self.loop))
            self.assertEqual(r.status, 200)
            content = self.loop.run_until_complete(r.read(True))

            self.assertEqual(content['headers']['Cookie'], 'test=1')
            r.close()

            cookies = sorted([(k, v.value) for k, v in s.cookies.items()])
            self.assertEqual(cookies, [('c1', 'cookie1'), ('c2', 'cookie2'),
                                       ('test', '1')])
    def test_POST_FILES_STR(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            with open(__file__) as f:
                r = self.loop.run_until_complete(
                    client.request('post', url, files=[('some', f.read())],
                                   loop=self.loop))

                content = self.loop.run_until_complete(r.read(True))

                f.seek(0)
                self.assertEqual(1, len(content['multipart-data']))
                self.assertEqual(
                    'some', content['multipart-data'][0]['name'])
                self.assertEqual(
                    'some', content['multipart-data'][0]['filename'])
                self.assertEqual(
                    f.read(), content['multipart-data'][0]['data'])
                self.assertEqual(r.status, 200)
    def test_POST_FILES_STR(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            with open(__file__) as f:
                r = self.loop.run_until_complete(
                    client.request('post',
                                   url,
                                   files=[('some', f.read())],
                                   loop=self.loop))

                content = self.loop.run_until_complete(r.read(True))

                f.seek(0)
                self.assertEqual(1, len(content['multipart-data']))
                self.assertEqual('some', content['multipart-data'][0]['name'])
                self.assertEqual('some',
                                 content['multipart-data'][0]['filename'])
                self.assertEqual(f.read(),
                                 content['multipart-data'][0]['data'])
                self.assertEqual(r.status, 200)
    def test_POST_FILES(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            with open(__file__) as f:
                r = self.loop.run_until_complete(
                    client.request(
                        'post', url, files={'some': f}, chunked=1024,
                        headers={'Transfer-Encoding': 'chunked'},
                        loop=self.loop))
                content = self.loop.run_until_complete(r.read(True))

                f.seek(0)
                filename = os.path.split(f.name)[-1]

                self.assertEqual(1, len(content['multipart-data']))
                self.assertEqual(
                    'some', content['multipart-data'][0]['name'])
                self.assertEqual(
                    filename, content['multipart-data'][0]['filename'])
                self.assertEqual(
                    f.read(), content['multipart-data'][0]['data'])
                self.assertEqual(r.status, 200)
    def test_session_close(self):
        from tulip.http import session
        s = session.Session()

        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            r = self.loop.run_until_complete(
                client.request('get',
                               httpd.url('keepalive') + '?close=1',
                               session=s,
                               loop=self.loop))
            self.assertEqual(r.status, 200)
            content = self.loop.run_until_complete(r.read(True))
            self.assertEqual(content['content'], 'requests=1')
            r.close()

            r = self.loop.run_until_complete(
                client.request('get',
                               httpd.url('keepalive'),
                               session=s,
                               loop=self.loop))
            self.assertEqual(r.status, 200)
            content = self.loop.run_until_complete(r.read(True))
            self.assertEqual(content['content'], 'requests=1')
            r.close()
    def test_POST_FILES_LIST_CT(self):
        with test_utils.run_test_server(self.loop, router=Functional) as httpd:
            url = httpd.url('method', 'post')

            with open(__file__) as f:
                r = self.loop.run_until_complete(
                    client.request('post', url, loop=self.loop,
                                   files=[('some', f, 'text/plain')]))

                content = self.loop.run_until_complete(r.read(True))

                f.seek(0)
                filename = os.path.split(f.name)[-1]

                self.assertEqual(1, len(content['multipart-data']))
                self.assertEqual(
                    'some', content['multipart-data'][0]['name'])
                self.assertEqual(
                    filename, content['multipart-data'][0]['filename'])
                self.assertEqual(
                    f.read(), content['multipart-data'][0]['data'])
                self.assertEqual(
                    'text/plain', content['multipart-data'][0]['content-type'])
                self.assertEqual(r.status, 200)