def go(): server = yield from self.loop.create_server( Proto, '127.0.0.1') addr = server.sockets[0].getsockname() connector = aiohttp.TCPConnector(loop=self.loop) url = 'http://{}:{}/'.format(*addr) r = yield from client.request('GET', url, connector=connector, loop=self.loop) yield from r.read() self.assertEqual(1, len(connector._conns)) with self.assertRaises(aiohttp.ClientError): yield from client.request('GET', url, connector=connector, loop=self.loop) self.assertEqual(0, len(connector._conns)) connector.close() server.close() yield from server.wait_closed()
def go(): _, srv, url = yield from self.create_server("GET", "/", handler) connector = aiohttp.TCPConnector(loop=self.loop) r = yield from client.request("GET", url, connector=connector, loop=self.loop) yield from r.read() r2 = yield from client.request("GET", url, connector=connector, loop=self.loop) yield from r2.read() self.assertEqual(1, len(connector._conns)) connector.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_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_request_conn_closed(self): with 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 callback(req): tokens = yield from google_oauth.callback(req) accessToken = tokens['access_token'] r = yield from client.request('get', 'https://www.googleapis.com/plus/v1/people/me', params={'access_token': accessToken}) if r.status != 200: raise web.HTTPUnauthorized() profile = yield from r.json() # normalize profile user = { 'provider': 'google', 'role' : 'user', 'name' : profile['displayName'], 'email' : profile['emails'][0]['value'], 'google' : { 'id': profile['id'], } } mongo = req.app['mongo'] yield from mongo.users.insert([user]) user = yield from mongo.users.find_one(user) jwt_token = auth_service.sign_token(user['_id'], user['role']) response = web.HTTPFound('/') response.set_cookie('token', util.Angular.serialize_cookie(jwt_token)) raise response
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_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 release(self, token): if token is None: raise RuntimeError('trying to release empty token') r = yield from client.request('get', self.revokeUrl, params={'token': token}) if r.status != 200: raise RuntimeError('attempt to release token failed')
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_POST_MULTIPART(self): with 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 go(url): connector = aiohttp.TCPConnector(loop=self.loop) r = yield from client.request("GET", url, connector=connector, loop=self.loop) yield from r.read() self.assertEqual(1, len(connector._conns)) connector.close()
def test_POST_STREAM_DATA(self): with 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_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_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_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_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_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_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_request_conn_closed(self): with run_server(self.loop, router=Functional) as httpd: httpd['close'] = True with self.assertRaises(aiohttp.ServerDisconnectedError): self.loop.run_until_complete( client.request('get', httpd.url('method', 'get'), 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_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_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_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 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()
def ws_connect(url, protocols=(), timeout=10.0, connector=None, response_class=None, autoclose=True, autoping=True, loop=None): """Initiate websocket connection.""" if loop is None: loop = asyncio.get_event_loop() sec_key = base64.b64encode(os.urandom(16)) headers = { hdrs.UPGRADE: hdrs.WEBSOCKET, hdrs.CONNECTION: hdrs.UPGRADE, hdrs.SEC_WEBSOCKET_VERSION: '13', hdrs.SEC_WEBSOCKET_KEY: sec_key.decode(), } if protocols: headers[hdrs.SEC_WEBSOCKET_PROTOCOL] = ','.join(protocols) # send request resp = yield from client.request( 'get', url, headers=headers, read_until_eof=False, connector=connector, loop=loop) # check handshake if resp.status != 101: raise WSServerHandshakeError('Invalid response status') if resp.headers.get(hdrs.UPGRADE, '').lower() != 'websocket': raise WSServerHandshakeError('Invalid upgrade header') if resp.headers.get(hdrs.CONNECTION, '').lower() != 'upgrade': raise WSServerHandshakeError('Invalid connection header') # key calculation key = resp.headers.get(hdrs.SEC_WEBSOCKET_ACCEPT, '') match = base64.b64encode(hashlib.sha1(sec_key + WS_KEY).digest()).decode() if key != match: raise WSServerHandshakeError('Invalid challenge response') # websocket protocol protocol = None if protocols and hdrs.SEC_WEBSOCKET_PROTOCOL in resp.headers: resp_protocols = [proto.strip() for proto in resp.headers[hdrs.SEC_WEBSOCKET_PROTOCOL].split(',')] for proto in resp_protocols: if proto in protocols: protocol = proto break reader = resp.connection.reader.set_parser(WebSocketParser) writer = WebSocketWriter(resp.connection.writer, use_mask=True) if response_class is None: response_class = ClientWebSocketResponse return response_class( reader, writer, protocol, resp, timeout, autoclose, autoping, loop)
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_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_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 go(url): r = yield from client.request('GET', url, loop=self.loop) connection = r.connection self.assertIsNotNone(connection) connector = connection._connector self.assertIsNotNone(connector) yield from r.read() self.assertEqual(0, len(connector._conns))
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 go(): _, srv, url = yield from self.create_server("GET", "/", handler) connector = aiohttp.TCPConnector(loop=self.loop) r = yield from client.request("GET", url, connector=connector, loop=self.loop) content = yield from r.read() self.assertEqual(r.status, 304) self.assertEqual(content, b"") r.release() connector.close()
async def _download_img(self, src, img_name): file_path = "%s%s.png" % (self.config.directory, img_name) content = None async with client.request("GET", src, connector=self.conn) as response: content = await response.read() if content: with open(file_path, 'wb') as fd: fd.write(content) print(" :=> downloaded wallpaper of date: %s" % img_name)
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_share_cookies(self): with test_utils.run_server(self.loop, router=Functional) as httpd: conn = aiohttp.TCPConnector(share_cookies=True, loop=self.loop) resp = self.loop.run_until_complete( client.request('get', httpd.url('cookies'), connector=conn, loop=self.loop)) self.assertIn('SET-COOKIE', resp.headers) self.assertEqual(resp.cookies['c1'].value, 'cookie1') self.assertEqual(resp.cookies['c2'].value, 'cookie2') self.assertEqual(conn.cookies, resp.cookies) resp2 = self.loop.run_until_complete( client.request('get', httpd.url('method', 'get'), connector=conn, loop=self.loop)) self.assertNotIn('SET-COOKIE', resp2.headers) data = self.loop.run_until_complete(resp2.json()) self.assertEqual(data['headers']['Cookie'], 'c1=cookie1; c2=cookie2')
def go(url): connector = aiohttp.TCPConnector(loop=self.loop) r = yield from client.request('GET', url, connector=connector, loop=self.loop) yield from r.read() self.assertEqual(1, len(connector._conns)) connector.close()
def go(): _, srv, url = yield from self.create_server('GET', '/', handler) connector = aiohttp.TCPConnector(loop=self.loop) r = yield from client.request('GET', url, connector=connector, loop=self.loop) yield from r.read() r.release() r2 = yield from client.request('GET', url, connector=connector, loop=self.loop) yield from r2.read() r2.release() self.assertEqual(1, len(connector._conns)) connector.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 go(): _, srv, url = yield from self.create_server('GET', '/', handler) connector = aiohttp.TCPConnector(loop=self.loop) r = yield from client.request('GET', url, connector=connector, loop=self.loop) content = yield from r.read() self.assertEqual(r.status, 304) self.assertEqual(content, b'') r.release() connector.close()
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_session_close(self): conn = aiohttp.SocketConnector(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.read(True)) 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.read(True)) self.assertEqual(content['content'], 'requests=1') r.close()
async def handle_web_request(request, target_url): async with client.request(request.method, target_url, headers=request.headers.copy(), allow_redirects=False, data=await request.read()) as res: body = await res.read() response = web.Response(headers=res.headers.copy(), status=res.status, body=body) return response
def test_keepalive(self): from aiohttp import connector c = connector.SocketConnector(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.read(True)) 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.read(True)) self.assertEqual(content['content'], 'requests=2') 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()
async def handler_proxy(self, req): headers = req.headers.copy() self.logger.debug("headers: %s", headers) subdomain = self.subdomain(headers) path = req.path self.logger.debug("subdomain: %r, path: %r", subdomain, path) upstream = self.get_upstream(subdomain, path) if not upstream: return web.Response(status=HTTPStatus.NOT_FOUND) if (headers.get("connection", "").lower() == "upgrade" and headers.get("upgrade", "").lower() == "websocket" and req.method == "GET"): # Handle websocket proxy try: async with aiohttp.ClientSession( cookies=req.cookies) as client_session: async with client_session.ws_connect( upstream) as ws_client: ws_server = web.WebSocketResponse() await ws_server.prepare(req) self.loop.create_task( asyncio.wait( [ self.wsforward(ws_server, ws_client), self.wsforward(ws_client, ws_server), ], return_when=asyncio.FIRST_COMPLETED, )) return ws_server except aiohttp.client_exceptions.WSServerHandshakeError: return web.Response(status=HTTPStatus.NOT_FOUND) else: # Handle regular HTTP request proxy self.logger.debug("upstream for (%r): %s", upstream, (subdomain, path)) async with client.request( req.method, upstream, headers=headers, allow_redirects=False, data=await req.read(), ) as res: self.logger.debug("upstream url(%s) status: %d", upstream, res.status) return web.Response( headers=res.headers, status=res.status, body=await res.read(), ) return ws_server
async def handler(req): proxyPath = req.match_info.get('proxyPath', 'no proxyPath placeholder defined') reqH = req.headers.copy() if reqH['connection'].lower() == 'upgrade' and reqH['upgrade'].lower( ) == 'websocket' and req.method == 'GET': ws_server = web.WebSocketResponse() await ws_server.prepare(req) logger.info('##### WS_SERVER %s', pprint.pformat(ws_server)) client_session = aiohttp.ClientSession(cookies=req.cookies) async with client_session.ws_connect(baseUrl + req.path_qs, ) as ws_client: logger.info('##### WS_CLIENT %s', pprint.pformat(ws_client)) async def ws_forward(ws_from, ws_to): async for msg in ws_from: logger.info('>>> msg: %s', pprint.pformat(msg)) mt = msg.type md = msg.data if mt == aiohttp.WSMsgType.TEXT: await ws_to.send_str(md) elif mt == aiohttp.WSMsgType.BINARY: await ws_to.send_bytes(md) elif mt == aiohttp.WSMsgType.PING: await ws_to.ping() elif mt == aiohttp.WSMsgType.PONG: await ws_to.pong() elif ws_to.closed: await ws_to.close(code=ws_to.close_code, message=msg.extra) else: raise ValueError('unexpected message type: %s' % pprint.pformat(msg)) await asyncio.wait([ ws_forward(ws_server, ws_client), ws_forward(ws_client, ws_server) ], return_when=asyncio.FIRST_COMPLETED) return ws_server else: async with client.request(req.method, baseUrl + mountPoint + proxyPath, headers=reqH, allow_redirects=False, data=await req.read()) as res: headers = res.headers.copy() body = await res.read() return web.Response(headers=headers, status=res.status, body=body) return ws_server
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_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_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()
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_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_POST_MultiDict(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=multidict.MultiDict( [('q', 'test1'), ('q', 'test2')]), loop=self.loop)) self.assertEqual(r.status, 200) content = self.loop.run_until_complete(r.json()) self.assertEqual({'q': ['test1', 'test2']}, content['form']) self.assertEqual(r.status, 200) r.close()