本文整理汇总了Python中aiohttp.client.HttpRequest类的典型用法代码示例。如果您正苦于以下问题:Python HttpRequest类的具体用法?Python HttpRequest怎么用?Python HttpRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HttpRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_data_stream_exc
def test_data_stream_exc(self):
fut = asyncio.Future(loop=self.loop)
def gen():
yield b'binary data'
yield from fut
return b' result'
req = HttpRequest(
'POST', 'http://python.org/', data=gen(), loop=self.loop)
self.assertTrue(req.chunked)
self.assertTrue(inspect.isgenerator(req.body))
self.assertEqual(req.headers['transfer-encoding'], 'chunked')
@asyncio.coroutine
def exc():
yield from asyncio.sleep(0.01, loop=self.loop)
fut.set_exception(ValueError)
asyncio.async(exc(), loop=self.loop)
req.send(self.transport)
self.assertRaises(
ValueError, self.loop.run_until_complete, req._writer)
self.assertRaises(self.transport.close.called)
示例2: test_chunked_length
def test_chunked_length(self):
req = HttpRequest(
'get', 'http://python.org/',
headers={'Content-Length': '1000'}, chunked=1024)
req.send(self.transport)
self.assertEqual(req.headers['Transfer-Encoding'], 'chunked')
self.assertNotIn('Content-Length', req.headers)
示例3: test_content_encoding
def test_content_encoding(self, m_http):
req = HttpRequest('get', 'http://python.org/', compress='deflate')
req.send(self.transport)
self.assertEqual(req.headers['Transfer-encoding'], 'chunked')
self.assertEqual(req.headers['Content-encoding'], 'deflate')
m_http.Request.return_value\
.add_compression_filter.assert_called_with('deflate')
示例4: test_chunked_explicit_size
def test_chunked_explicit_size(self, m_http):
req = HttpRequest(
'get', 'http://python.org/', chunked=1024)
req.send(self.transport)
self.assertEqual('chunked', req.headers['Transfer-encoding'])
m_http.Request.return_value\
.add_chunking_filter.assert_called_with(1024)
示例5: test_chunked_explicit
def test_chunked_explicit(self, m_http):
req = HttpRequest(
'get', 'http://python.org/', chunked=True, loop=self.loop)
req.send(self.transport, self.protocol)
self.assertEqual('chunked', req.headers['Transfer-encoding'])
m_http.Request.return_value\
.add_chunking_filter.assert_called_with(8196)
示例6: test_bytes_data
def test_bytes_data(self):
for meth in HttpRequest.POST_METHODS:
req = HttpRequest(meth, 'http://python.org/', data=b'binary data')
req.send(self.transport)
self.assertEqual('/', req.path)
self.assertEqual((b'binary data',), req.body)
self.assertEqual('application/octet-stream',
req.headers['content-type'])
示例7: test_post_data
def test_post_data(self):
for meth in HttpRequest.POST_METHODS:
req = HttpRequest(meth, 'http://python.org/', data={'life': '42'})
req.send(self.transport)
self.assertEqual('/', req.path)
self.assertEqual(b'life=42', req.body[0])
self.assertEqual('application/x-www-form-urlencoded',
req.headers['content-type'])
示例8: test_data_stream_not_bytes
def test_data_stream_not_bytes(self):
@asyncio.coroutine
def gen():
yield object()
return b' result'
req = HttpRequest(
'POST', 'http://python.org/', data=gen(), loop=self.loop)
req.send(self.transport)
self.assertRaises(
ValueError, self.loop.run_until_complete, req._writer)
示例9: test_data_stream_not_bytes
def test_data_stream_not_bytes(self):
@asyncio.coroutine
def gen():
yield object()
return b' result'
req = HttpRequest(
'POST', 'http://python.org/', data=gen(), loop=self.loop)
req.send(self.transport, self.protocol)
self.loop.run_until_complete(req._writer)
self.assertTrue(self.protocol.set_exception.called)
示例10: test_content_encoding_header
def test_content_encoding_header(self, m_http):
req = HttpRequest(
'get', 'http://python.org/',
headers={'Content-Encoding': 'deflate'}, loop=self.loop)
req.send(self.transport, self.protocol)
self.assertEqual(req.headers['Transfer-encoding'], 'chunked')
self.assertEqual(req.headers['Content-encoding'], 'deflate')
m_http.Request.return_value\
.add_compression_filter.assert_called_with('deflate')
m_http.Request.return_value\
.add_chunking_filter.assert_called_with(8196)
示例11: test_close
def test_close(self):
@asyncio.coroutine
def gen():
yield from asyncio.sleep(0.00001, loop=self.loop)
return b'result'
req = HttpRequest(
'POST', 'http://python.org/', data=gen(), loop=self.loop)
req.send(self.transport)
self.loop.run_until_complete(req.close())
self.assertEqual(
self.transport.write.mock_calls[-3:],
[unittest.mock.call(b'result'),
unittest.mock.call(b'\r\n'),
unittest.mock.call(b'0\r\n\r\n')])
示例12: test_data_continue
def test_data_continue(self):
req = HttpRequest(
'POST', 'http://python.org/', data=b'data',
expect100=True, loop=self.loop)
def coro():
yield from asyncio.sleep(0.0001, loop=self.loop)
req._continue.set_result(1)
asyncio.async(coro(), loop=self.loop)
req.send(self.transport)
self.assertEqual(1, len(self.transport.write.mock_calls))
self.loop.run_until_complete(req._writer)
self.assertEqual(
self.transport.write.mock_calls[-1],
unittest.mock.call(b'data'))
示例13: test_data_stream
def test_data_stream(self):
def gen():
yield b'binary data'
return b' result'
req = HttpRequest(
'POST', 'http://python.org/', data=gen(), loop=self.loop)
self.assertTrue(req.chunked)
self.assertTrue(inspect.isgenerator(req.body))
self.assertEqual(req.headers['transfer-encoding'], 'chunked')
resp = req.send(self.transport)
self.assertIsInstance(req._writer, asyncio.Future)
self.loop.run_until_complete(resp.wait_for_close())
self.assertIsNone(req._writer)
self.assertEqual(
self.transport.write.mock_calls[-3:],
[unittest.mock.call(b'binary data result'),
unittest.mock.call(b'\r\n'),
unittest.mock.call(b'0\r\n\r\n')])
示例14: test_no_content_length
def test_no_content_length(self):
req = HttpRequest('get', 'http://python.org')
req.send(self.transport)
self.assertEqual('0', req.headers.get('Content-Length'))
req = HttpRequest('head', 'http://python.org')
req.send(self.transport)
self.assertEqual('0', req.headers.get('Content-Length'))
示例15: test_data_stream_continue
def test_data_stream_continue(self):
def gen():
yield b'binary data'
return b' result'
req = HttpRequest(
'POST', 'http://python.org/', data=gen(),
expect100=True, loop=self.loop)
self.assertTrue(req.chunked)
self.assertTrue(inspect.isgenerator(req.body))
def coro():
yield from asyncio.sleep(0.0001, loop=self.loop)
req._continue.set_result(1)
asyncio.async(coro(), loop=self.loop)
req.send(self.transport)
self.loop.run_until_complete(req._writer)
self.assertEqual(
self.transport.write.mock_calls[-3:],
[unittest.mock.call(b'binary data result'),
unittest.mock.call(b'\r\n'),
unittest.mock.call(b'0\r\n\r\n')])