def test_POST_ChunksQueue(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') here = os.path.dirname(__file__) fname = os.path.join(here, 'sample.key') with open(fname, 'rb') as f: data = f.read() stream = aiohttp.ChunksQueue(loop=self.loop) stream.feed_data(data[:100]) stream.feed_data(data[100:]) stream.feed_eof() r = self.loop.run_until_complete( client.request( 'post', url, data=stream, headers={'Content-Length': str(len(data))}, loop=self.loop)) content = self.loop.run_until_complete(r.json()) r.close() self.assertEqual(str(len(data)), content['headers']['Content-Length'])
def test_POST_STREAM_DATA(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') with open(__file__, 'rb') as f: data = f.read() fut = asyncio.Future(loop=self.loop) @asyncio.coroutine def stream(): yield from fut yield data self.loop.call_later(0.01, fut.set_result, True) r = self.loop.run_until_complete( client.request( 'post', url, data=stream(), headers={'Content-Length': str(len(data))}, loop=self.loop)) content = self.loop.run_until_complete(r.json()) r.close() self.assertEqual(str(len(data)), content['headers']['Content-Length'])
def test_POST_FILES_WITH_DATA(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') here = os.path.dirname(__file__) fname = os.path.join(here, 'sample.key') with open(fname) as f: r = self.loop.run_until_complete( client.request('post', url, loop=self.loop, data={'test': 'true', 'some': f})) content = self.loop.run_until_complete(r.json()) files = list( sorted(content['multipart-data'], key=lambda d: d['name'])) self.assertEqual(2, len(content['multipart-data'])) self.assertEqual('test', files[1]['name']) self.assertEqual('true', files[1]['data']) f.seek(0) filename = os.path.split(f.name)[-1] self.assertEqual('some', files[0]['name']) self.assertEqual(filename, files[0]['filename']) self.assertEqual(f.read(), files[0]['data']) self.assertEqual(r.status, 200) r.close()
def test_POST_STREAM_DATA(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') with open(__file__, 'rb') as f: data = f.read() fut = asyncio.Future(loop=self.loop) @asyncio.coroutine def stream(): yield from fut yield data self.loop.call_later(0.01, fut.set_result, True) r = self.loop.run_until_complete( client.request( 'post', url, data=stream(), headers={'Content-Length': str(len(data))}, loop=self.loop)) content = self.loop.run_until_complete(r.read(True)) r.close() self.assertEqual(str(len(data)), content['headers']['Content-Length'])
def test_POST_MULTIPART(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') with MultipartWriter('form-data') as writer: writer.append('foo') writer.append_json({'bar': 'баз'}) writer.append_form([('тест', '4'), ('сетс', '2')]) r = self.loop.run_until_complete( client.request('post', url, data=writer, loop=self.loop)) content = self.loop.run_until_complete(r.json()) self.assertEqual(3, len(content['multipart-data'])) self.assertEqual({ 'content-type': 'text/plain', 'data': 'foo' }, content['multipart-data'][0]) self.assertEqual( { 'content-type': 'application/json', 'data': '{"bar": "\\u0431\\u0430\\u0437"}' }, content['multipart-data'][1]) self.assertEqual( { 'content-type': 'application/x-www-form-urlencoded', 'data': '%D1%82%D0%B5%D1%81%D1%82=4&' '%D1%81%D0%B5%D1%82%D1%81=2' }, content['multipart-data'][2]) self.assertEqual(r.status, 200) r.close()
def test_HTTP_302_REDIRECT_NON_HTTP(self): with test_utils.run_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_MULTIPART(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') with MultipartWriter('form-data') as writer: writer.append('foo') writer.append_json({'bar': 'баз'}) writer.append_form([('тест', '4'), ('сетс', '2')]) r = self.loop.run_until_complete( client.request('post', url, data=writer, loop=self.loop)) content = self.loop.run_until_complete(r.json()) self.assertEqual(3, len(content['multipart-data'])) self.assertEqual({'content-type': 'text/plain', 'data': 'foo'}, content['multipart-data'][0]) self.assertEqual({'content-type': 'application/json', 'data': '{"bar": "\\u0431\\u0430\\u0437"}'}, content['multipart-data'][1]) self.assertEqual( {'content-type': 'application/x-www-form-urlencoded', 'data': '%D1%82%D0%B5%D1%81%D1%82=4&' '%D1%81%D0%B5%D1%82%D1%81=2'}, content['multipart-data'][2]) self.assertEqual(r.status, 200) r.close()
def test_POST_FILES_DEFLATE(self): with test_utils.run_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) r.close()
def test_POST_FILES_LIST_CT(self): with test_utils.run_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) r.close()
def test_POST_FILES_LIST_CT(self): with test_utils.run_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=[('some', f, 'text/plain')])) content = self.loop.run_until_complete(r.json()) 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) r.close()
def test_POST_FILES_DEFLATE(self): with test_utils.run_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, data={'some': f}, chunked=1024, compress='deflate', loop=self.loop)) content = self.loop.run_until_complete(r.json()) 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) r.close()
def test_POST_FILES_WITH_DATA(self): with test_utils.run_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', 'some': f})) content = self.loop.run_until_complete(r.json()) files = list( sorted(content['multipart-data'], key=lambda d: d['name'])) self.assertEqual(2, len(content['multipart-data'])) self.assertEqual('test', files[1]['name']) self.assertEqual('true', files[1]['data']) f.seek(0) filename = os.path.split(f.name)[-1] self.assertEqual('some', files[0]['name']) self.assertEqual(filename, files[0]['filename']) self.assertEqual(f.read(), files[0]['data']) self.assertEqual(r.status, 200) r.close()
def _test_HTTP_302_REDIRECT_NON_HTTP(self): with test_utils.run_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_session_close(self): conn = aiohttp.TCPConnector(loop=self.loop) with test_utils.run_server(self.loop, router=Functional) as httpd: r = self.loop.run_until_complete( client.request('get', httpd.url('keepalive') + '?close=1', connector=conn, loop=self.loop)) self.assertEqual(r.status, 200) content = self.loop.run_until_complete(r.json()) self.assertEqual(content['content'], 'requests=1') r.close() r = self.loop.run_until_complete( client.request('get', httpd.url('keepalive'), connector=conn, loop=self.loop)) self.assertEqual(r.status, 200) content = self.loop.run_until_complete(r.json()) self.assertEqual(content['content'], 'requests=1') r.close() conn.close()
def test_POST_FILES_WITH_DATA(self): with test_utils.run_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) r.close()
def test_POST_FILES_LIST_CT(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') here = os.path.dirname(__file__) fname = os.path.join(here, 'sample.key') with open(fname) as f: form = aiohttp.FormData() form.add_field('some', f, content_type='text/plain') r = self.loop.run_until_complete( client.request('post', url, loop=self.loop, data=form)) content = self.loop.run_until_complete(r.json()) 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) r.close()
def test_request_conn_closed(self): with test_utils.run_server(self.loop, router=Functional) as httpd: httpd['close'] = True with self.assertRaises(aiohttp.ClientHttpProcessingError): self.loop.run_until_complete( client.request('get', httpd.url('method', 'get'), loop=self.loop))
def test_POST_FILES_DEPRECATED(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') with open(__file__) as f: with self.assertWarns(DeprecationWarning): r = self.loop.run_until_complete( client.request( 'post', url, files={'some': f, 'test': b'data'}, chunked=1024, headers={'Transfer-Encoding': 'chunked'}, loop=self.loop)) content = self.loop.run_until_complete(r.json()) files = list( sorted(content['multipart-data'], key=lambda d: d['name'])) f.seek(0) filename = os.path.split(f.name)[-1] self.assertEqual(2, len(content['multipart-data'])) self.assertEqual('some', files[0]['name']) self.assertEqual(filename, files[0]['filename']) self.assertEqual(f.read(), files[0]['data']) self.assertEqual(r.status, 200) r.close()
def test_POST_STREAM_DATA(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') here = os.path.dirname(__file__) fname = os.path.join(here, 'sample.key') with open(fname, 'rb') as f: data = f.read() fut = helpers.create_future(self.loop) @asyncio.coroutine def stream(): yield from fut yield data self.loop.call_later(0.01, fut.set_result, True) r = self.loop.run_until_complete( client.request( 'post', url, data=stream(), headers={'Content-Length': str(len(data))}, loop=self.loop)) content = self.loop.run_until_complete(r.json()) r.close() self.assertEqual(str(len(data)), content['headers']['Content-Length']) self.assertEqual('application/octet-stream', content['headers']['Content-Type'])
def test_encoding2(self): with test_utils.run_server(self.loop, router=Functional) as httpd: r = self.loop.run_until_complete( client.request('get', httpd.url('encoding', 'gzip'), loop=self.loop)) self.assertEqual(r.status, 200) r.close()
def test_POST_FILES_LIST(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') here = os.path.dirname(__file__) fname = os.path.join(here, 'sample.key') with open(fname) as f: r = self.loop.run_until_complete( client.request('post', url, data=[('some', f)], loop=self.loop)) content = self.loop.run_until_complete(r.json()) 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) r.close()
def test_POST_FILES_DEPRECATED(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') here = os.path.dirname(__file__) fname = os.path.join(here, 'sample.key') with open(fname) as f: with self.assertWarns(DeprecationWarning): r = self.loop.run_until_complete( client.request( 'post', url, files={ 'some': f, 'test': b'data' }, chunked=1024, headers={'Transfer-Encoding': 'chunked'}, loop=self.loop)) content = self.loop.run_until_complete(r.json()) files = list( sorted(content['multipart-data'], key=lambda d: d['name'])) f.seek(0) filename = os.path.split(f.name)[-1] self.assertEqual(2, len(content['multipart-data'])) self.assertEqual('some', files[0]['name']) self.assertEqual(filename, files[0]['filename']) self.assertEqual(f.read(), files[0]['data']) self.assertEqual(r.status, 200) r.close()
def test_HTTP_200_OK_METHOD_ssl(self): connector = aiohttp.TCPConnector(verify_ssl=False, loop=self.loop) with test_utils.run_server(self.loop, use_ssl=True) as httpd: for meth in ('get', 'post', 'put', 'delete', 'head'): @asyncio.coroutine def go(): yield from asyncio.sleep(0.1, loop=self.loop) r = yield from client.request(meth, httpd.url('method', meth), loop=self.loop, connector=connector) content = yield from r.read() self.assertEqual(r.status, 200) if meth == 'head': self.assertEqual(b'', content) else: self.assertEqual(content, b'Test message') r.close() # let loop to make one iteration to call connection_lost # and close socket yield from asyncio.sleep(0, loop=self.loop) self.loop.run_until_complete(go()) connector.close()
def test_HTTP_200_OK_METHOD_ssl(self): connector = aiohttp.TCPConnector(verify_ssl=False, loop=self.loop) with test_utils.run_server(self.loop, use_ssl=True) as httpd: for meth in ('get', 'post', 'put', 'delete', 'head'): @asyncio.coroutine def go(): yield from asyncio.sleep(0.1, loop=self.loop) r = yield from client.request( meth, httpd.url('method', meth), loop=self.loop, connector=connector) content = yield from r.read() self.assertEqual(r.status, 200) if meth == 'head': self.assertEqual(b'', content) else: self.assertEqual(content, b'Test message') r.close() # let loop to make one iteration to call connection_lost # and close socket yield from asyncio.sleep(0, loop=self.loop) self.loop.run_until_complete(go()) connector.close()
def test_keepalive(self): from aiohttp import connector c = connector.TCPConnector(share_cookies=True, loop=self.loop) with test_utils.run_server(self.loop, router=Functional) as httpd: r = self.loop.run_until_complete( client.request('get', httpd.url('keepalive', ), connector=c, loop=self.loop)) self.assertEqual(r.status, 200) content = self.loop.run_until_complete(r.json()) self.assertEqual(content['content'], 'requests=1') r.close() r = self.loop.run_until_complete( client.request('get', httpd.url('keepalive'), connector=c, loop=self.loop)) self.assertEqual(r.status, 200) content = self.loop.run_until_complete(r.json()) self.assertEqual(content['content'], 'requests=2') r.close() c.close()
def test_timeout(self): with test_utils.run_server(self.loop, router=Functional) as httpd: httpd['noresponse'] = True self.assertRaises( asyncio.TimeoutError, self.loop.run_until_complete, client.request('get', httpd.url('method', 'get'), timeout=0.1, loop=self.loop))
def test_POST_FILES_SINGLE(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') with open(__file__) as f: with self.assertRaises(ValueError): self.loop.run_until_complete( client.request('post', url, data=f, loop=self.loop))
def test_broken_connection(self): with test_utils.run_server(self.loop, router=Functional) as httpd: r = self.loop.run_until_complete( client.request('get', httpd.url('broken'), loop=self.loop)) self.assertEqual(r.status, 200) self.assertRaises(aiohttp.ServerDisconnectedError, self.loop.run_until_complete, r.json()) r.close()
def test_request_conn_closed(self): with test_utils.run_server(self.loop, router=Functional) as httpd: httpd['close'] = True self.assertRaises( aiohttp.ClientConnectionError, self.loop.run_until_complete, client.request('get', httpd.url('method', 'get'), loop=self.loop))
def test_set_cookies(self): with test_utils.run_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') resp.close()
def test_chunked(self): with test_utils.run_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.headers.getone('TRANSFER-ENCODING'), 'chunked') content = self.loop.run_until_complete(r.json()) self.assertEqual(content['path'], '/chunked') r.close()
def test_chunked(self): with test_utils.run_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') r.close()
def test_HTTP_302_REDIRECT_GET(self): with test_utils.run_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']) r.close()
def test_broken_connection(self): with test_utils.run_server(self.loop, router=Functional) as httpd: r = self.loop.run_until_complete( client.request('get', httpd.url('broken'), loop=self.loop)) self.assertEqual(r.status, 200) self.assertRaises( aiohttp.IncompleteRead, self.loop.run_until_complete, r.json()) r.close()
def test_HTTP_302_max_redirects(self): with test_utils.run_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']) r.close()
def test_HTTP_302_REDIRECT_NON_HTTP(self): with test_utils.run_server(self.loop, router=Functional) as httpd: @asyncio.coroutine def go(): with self.assertRaises(ValueError): yield from client.request('get', httpd.url('redirect_err'), loop=self.loop) self.loop.run_until_complete(go())
def test_tcp_connector(self): with test_utils.run_server(self.loop, router=Functional) as httpd: r = self.loop.run_until_complete( client.request('get', httpd.url('method', 'get'), connector=aiohttp.TCPConnector(loop=self.loop), loop=self.loop)) content = self.loop.run_until_complete(r.content.read()) content = content.decode() self.assertEqual(r.status, 200) r.close()
def test_session_auth_header_conflict(self): with test_utils.run_server(self.loop, router=Functional) as httpd: session = client.ClientSession( loop=self.loop, auth=helpers.BasicAuth("login", "pass")) headers = {'Authorization': "Basic b3RoZXJfbG9naW46cGFzcw=="} with self.assertRaises(ValueError): self.loop.run_until_complete( session.request('get', httpd.url('method', 'get'), headers=headers)) session.close()
def test_POST_FILES_SINGLE(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') here = os.path.dirname(__file__) fname = os.path.join(here, 'sample.key') with open(fname) as f: with self.assertRaises(ValueError): self.loop.run_until_complete( client.request('post', url, data=f, loop=self.loop))
def test_HTTP_200_GET_WITH_PARAMS(self): with test_utils.run_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) r.close()
def test_tcp_connector(self): with test_utils.run_server(self.loop, router=Functional) as httpd: r = self.loop.run_until_complete( client.request( 'get', httpd.url('method', 'get'), connector=aiohttp.TCPConnector(loop=self.loop), loop=self.loop)) content = self.loop.run_until_complete(r.content.read()) content = content.decode() self.assertEqual(r.status, 200) r.close()
def test_HTTP_200_OK_METHOD_ssl(self): with test_utils.run_server(self.loop, use_ssl=True) 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, verify_ssl=False)) content = self.loop.run_until_complete(r.read()) self.assertEqual(r.status, 200) self.assertEqual(content, b'Test message') r.close()
def _test_encoding(self): with test_utils.run_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) r.close()
def test_HTTP_302_REDIRECT_GET(self): with test_utils.run_server(self.loop, router=Functional) as httpd: @asyncio.coroutine def go(): r = yield from client.request('get', httpd.url('redirect', 2), loop=self.loop) self.assertEqual(r.status, 200) self.assertEqual(2, httpd['redirects']) r.close() self.loop.run_until_complete(go())
def test_expect_continue(self): with test_utils.run_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'}, expect100=True, loop=self.loop)) self.assertEqual(r.status, 200) content = self.loop.run_until_complete(r.json()) self.assertEqual('100-continue', content['headers']['Expect']) self.assertEqual(r.status, 200) r.close()
def test_HTTP_302_REDIRECT_POST(self): with test_utils.run_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']) r.close()
def test_POST_DATA(self): with test_utils.run_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) r.close()
def test_expect_continue(self): with test_utils.run_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'}, expect100=True, loop=self.loop)) self.assertEqual(r.status, 200) content = self.loop.run_until_complete(r.read(True)) self.assertEqual('100-continue', content['headers']['Expect']) self.assertEqual(r.status, 200) r.close()
def test_POST_DATA(self): with test_utils.run_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.json()) self.assertEqual({'some': ['data']}, content['form']) self.assertEqual(r.status, 200) r.close()
def test_set_cookies(self, m_log): with test_utils.run_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(list(sorted(resp.cookies.keys())), ['c1', 'c2']) self.assertEqual(resp.cookies['c1'].value, 'cookie1') self.assertEqual(resp.cookies['c2'].value, 'cookie2') resp.close() m_log.warning.assert_called_with('Can not load response cookies: %s', mock.ANY)
def test_POST_DATA_with_explicit_formdata(self): with test_utils.run_server(self.loop, router=Functional) as httpd: url = httpd.url('method', 'post') form = aiohttp.FormData() form.add_field('name', 'text') r = self.loop.run_until_complete( client.request('post', url, data=form, loop=self.loop)) self.assertEqual(r.status, 200) content = self.loop.run_until_complete(r.json()) self.assertEqual({'name': ['text']}, content['form']) self.assertEqual(r.status, 200) r.close()