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)
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)
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)
def test_chunked_explicit(self, m_http): req = HttpRequest('get', 'http://python.org/', chunked=True) req.send(self.transport) self.assertEqual('chunked', req.headers['Transfer-encoding']) m_http.Request.return_value\ .add_chunking_filter.assert_called_with(8196)
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')
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') ])
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)
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'])
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'])
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)
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'])
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'))
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)
def test_chunked_explicit_size(self, m_http): req = HttpRequest('get', 'http://python.org/', chunked=1024, 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(1024)
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)
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)
def test_chunked(self): req = HttpRequest('get', 'http://python.org/', headers={'Transfer-encoding': 'gzip'}) req.send(self.transport) self.assertEqual('gzip', req.headers['Transfer-encoding']) req = HttpRequest('get', 'http://python.org/', headers={'Transfer-encoding': 'chunked'}) req.send(self.transport) self.assertEqual('chunked', req.headers['Transfer-encoding'])
def test_expect100(self): req = HttpRequest('get', 'http://python.org/', expect100=True, loop=self.loop) req.send(self.transport) self.assertEqual('100-continue', req.headers['expect']) self.assertIsNotNone(req._continue) req = HttpRequest('get', 'http://python.org/', headers={'expect': '100-continue'}, loop=self.loop) req.send(self.transport) self.assertEqual('100-continue', req.headers['expect']) self.assertIsNotNone(req._continue)
def test_chunked(self): req = HttpRequest( 'get', 'http://python.org/', headers={'Transfer-encoding': 'gzip'}) req.send(self.transport) self.assertEqual('gzip', req.headers['Transfer-encoding']) req = HttpRequest( 'get', 'http://python.org/', headers={'Transfer-encoding': 'chunked'}) req.send(self.transport) self.assertEqual('chunked', req.headers['Transfer-encoding'])
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)
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)
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)
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)
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')])
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') ])
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'))
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'))
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')])
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')])
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') ])