Exemple #1
0
 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)
Exemple #2
0
 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')
Exemple #3
0
    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)
Exemple #4
0
 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')
Exemple #5
0
 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)
Exemple #6
0
    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)
Exemple #7
0
    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)
Exemple #8
0
    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')
        ])
Exemple #9
0
 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'])
Exemple #10
0
 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'])
Exemple #11
0
 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'])
Exemple #12
0
 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)
Exemple #13
0
    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)
Exemple #14
0
 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'])
Exemple #15
0
    def test_host_header(self):
        req = HttpRequest('get', 'http://python.org/')
        self.assertEqual(req.headers['host'], 'python.org')

        req = HttpRequest('get',
                          'http://python.org/',
                          headers={'host': 'example.com'})
        self.assertEqual(req.headers['host'], 'example.com')
Exemple #16
0
    def test_method(self):
        req = HttpRequest('get', 'http://python.org/')
        self.assertEqual(req.method, 'GET')

        req = HttpRequest('head', 'http://python.org/')
        self.assertEqual(req.method, 'HEAD')

        req = HttpRequest('HEAD', 'http://python.org/')
        self.assertEqual(req.method, 'HEAD')
Exemple #17
0
 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)
Exemple #18
0
    def test_path_is_not_double_encoded(self):
        req = HttpRequest('get', "http://0.0.0.0/get/test case")
        self.assertEqual(req.path, "/get/test%20case")

        req = HttpRequest('get', "http://0.0.0.0/get/test%2fcase")
        self.assertEqual(req.path, "/get/test%2fcase")

        req = HttpRequest('get', "http://0.0.0.0/get/test%20case")
        self.assertEqual(req.path, "/get/test%20case")
Exemple #19
0
    def test_params_are_added_before_fragment(self):
        req = HttpRequest('GET',
                          "http://example.com/path#fragment",
                          params={"a": "b"})
        self.assertEqual(req.path, "/path?a=b#fragment")

        req = HttpRequest('GET',
                          "http://example.com/path?key=value#fragment",
                          params={"a": "b"})
        self.assertEqual(req.path, "/path?key=value&a=b#fragment")
Exemple #20
0
    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)
Exemple #21
0
    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)
Exemple #22
0
    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)
Exemple #23
0
    def test_cookies(self):
        req = HttpRequest('get',
                          'http://test.com/path',
                          cookies={'cookie1': 'val1'})
        self.assertIn('Cookie', req.headers)
        self.assertEqual('cookie1=val1', req.headers['cookie'])

        req = HttpRequest('get',
                          'http://test.com/path',
                          headers={'cookie': 'cookie1=val1'},
                          cookies={'cookie2': 'val2'})
        self.assertEqual('cookie1=val1; cookie2=val2', req.headers['cookie'])
Exemple #24
0
    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)
Exemple #25
0
    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)
Exemple #26
0
    def test_unicode_get(self):
        def join(*suffix):
            return urllib.parse.urljoin('http://python.org/', '/'.join(suffix))

        url = 'http://python.org'
        req = HttpRequest('get', url, params={'foo': 'f\xf8\xf8'})
        self.assertEqual('/?foo=f%C3%B8%C3%B8', req.path)
        req = HttpRequest('', url, params={'f\xf8\xf8': 'f\xf8\xf8'})
        self.assertEqual('/?f%C3%B8%C3%B8=f%C3%B8%C3%B8', req.path)
        req = HttpRequest('', url, params={'foo': 'foo'})
        self.assertEqual('/?foo=foo', req.path)
        req = HttpRequest('', join('\xf8'), params={'foo': 'foo'})
        self.assertEqual('/%C3%B8?foo=foo', req.path)
Exemple #27
0
    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)
Exemple #28
0
    def test_basic_auth_from_url(self):
        req = HttpRequest('get', 'http://*****:*****@python.org')
        self.assertIn('Authorization', req.headers)
        self.assertEqual('Basic bmtpbToxMjM0', req.headers['Authorization'])

        req = HttpRequest('get', 'http://[email protected]')
        self.assertIn('Authorization', req.headers)
        self.assertEqual('Basic bmtpbTo=', req.headers['Authorization'])

        req = HttpRequest('get',
                          'http://[email protected]',
                          auth=('nkim', '1234'))
        self.assertIn('Authorization', req.headers)
        self.assertEqual('Basic bmtpbToxMjM0', req.headers['Authorization'])
Exemple #29
0
    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'))
Exemple #30
0
    def test_host_port(self):
        req = HttpRequest('get', 'http://python.org/')
        self.assertEqual(req.host, 'python.org')
        self.assertEqual(req.port, 80)
        self.assertFalse(req.ssl)

        req = HttpRequest('get', 'https://python.org/')
        self.assertEqual(req.host, 'python.org')
        self.assertEqual(req.port, 443)
        self.assertTrue(req.ssl)

        req = HttpRequest('get', 'https://python.org:960/')
        self.assertEqual(req.host, 'python.org')
        self.assertEqual(req.port, 960)
        self.assertTrue(req.ssl)
Exemple #31
0
    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')])
Exemple #32
0
 def test_headers(self):
     req = HttpRequest('get',
                       'http://python.org/',
                       headers={'Content-Type': 'text/plain'})
     self.assertIn('Content-Type', req.headers)
     self.assertEqual(req.headers['Content-Type'], 'text/plain')
     self.assertEqual(req.headers['Accept-Encoding'], 'gzip, deflate')
Exemple #33
0
    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')
        ])
Exemple #34
0
    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'))
Exemple #35
0
    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'])
Exemple #36
0
    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'))
Exemple #37
0
    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')])
Exemple #38
0
    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')
        ])
Exemple #39
0
    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'))
Exemple #40
0
    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)
Exemple #41
0
    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')])
Exemple #42
0
    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'])
Exemple #43
0
    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)
Exemple #44
0
 def test_no_path(self):
     req = HttpRequest('get', 'http://python.org')
     self.assertEqual('/', req.path)
Exemple #45
0
 def test_version(self):
     req = HttpRequest('get', 'http://python.org/', version='1.0')
     self.assertEqual(req.version, (1, 0))
Exemple #46
0
 def test_query_multivalued_param(self):
     for meth in HttpRequest.ALL_METHODS:
         req = HttpRequest(meth,
                           'http://python.org',
                           params=(('test', 'foo'), ('test', 'baz')))
         self.assertEqual(req.path, '/?test=foo&test=baz')
Exemple #47
0
 def test_get_with_data(self):
     for meth in HttpRequest.GET_METHODS:
         req = HttpRequest(meth, 'http://python.org/', data={'life': '42'})
         self.assertEqual('/?life=42', req.path)