How to use multidict - 10 common examples

To help you get started, we’ve selected a few multidict examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github aio-libs / aiohttp / aiohttp / test_utils.py View on Github external
"""

    task = mock.Mock()
    if loop is ...:
        loop = mock.Mock()
        loop.create_future.return_value = ()

    if version < HttpVersion(1, 1):
        closing = True

    if headers:
        headers = CIMultiDictProxy(CIMultiDict(headers))
        raw_hdrs = tuple(
            (k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items())
    else:
        headers = CIMultiDictProxy(CIMultiDict())
        raw_hdrs = ()

    chunked = 'chunked' in headers.get(hdrs.TRANSFER_ENCODING, '').lower()

    message = RawRequestMessage(
        method, path, version, headers,
        raw_hdrs, closing, False, False, chunked, URL(path))
    if app is None:
        app = _create_app_mock()

    if transport is sentinel:
        transport = _create_transport(sslcontext)

    if protocol is sentinel:
        protocol = mock.Mock()
        protocol.transport = transport
github aio-libs / aiohttp / tests / test_client_response.py View on Github external
def test_response_links_multiple_headers(loop, session) -> None:
    url = URL('http://def-cl-resp.org/')
    response = ClientResponse('get', url,
                              request_info=mock.Mock(),
                              writer=mock.Mock(),
                              continue100=None,
                              timer=TimerNoop(),
                              traces=[],
                              loop=loop,
                              session=session)
    response._headers = CIMultiDict([
        (
            "Link",
            '; rel=next'
        ),
        (
            "Link",
            '; rel=home'
        )
    ])
    assert (
        response.links ==
        {'next':
         {'url': URL('http://example.com/page/1.html'),
          'rel': 'next'},
         'home':
         {'url': URL('http://example.com/'),
github foglamp / FogLAMP / tests / unit / python / foglamp / plugins / south / http_south / test_http_south.py View on Github external
def mock_request(data, loop):
    payload = StreamReader(loop=loop)
    payload.feed_data(data.encode())
    payload.feed_eof()

    protocol = mock.Mock()
    app = mock.Mock()
    headers = CIMultiDict([('CONTENT-TYPE', 'application/json')])
    req = make_mocked_request('POST', '/sensor-reading', headers=headers,
                              protocol=protocol, payload=payload, app=app)
    return req
github Tygs / tygs / tests / test_components.py View on Github external
def factory(method="GET", url="/toto", headers=None):
        headers = CIMultiDict(headers or {})
        if "HOST" not in headers:  # noqa
            headers['HOST'] = "test.local"
        return RawRequestMessage(method, url, HttpVersion(1, 1),
                                 headers, [], False, False)
    return factory
github aio-libs / aiohttp / tests / test_web_request.py View on Github external
def test_single_forwarded_header_injection1() -> None:
    # We might receive a header like this if we're sitting behind a reverse
    # proxy that blindly appends a forwarded-element without checking
    # the syntax of existing field-values. We should be able to recover
    # the appended element anyway.
    header = 'for=_injected;by=", for=_real'
    req = make_mocked_request('GET', '/',
                              headers=CIMultiDict({'Forwarded': header}))
    assert len(req.forwarded) == 2
    assert 'by' not in req.forwarded[0]
    assert req.forwarded[1]['for'] == '_real'
github dianbaer / jupiter / firstaio-require-win / aiohttp / test_utils.py View on Github external
"""

    task = mock.Mock()
    loop = mock.Mock()
    loop.create_future.return_value = ()

    if version < HttpVersion(1, 1):
        closing = True

    if headers:
        headers = CIMultiDict(headers)
        raw_hdrs = tuple(
            (k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items())
    else:
        headers = CIMultiDict()
        raw_hdrs = ()

    chunked = 'chunked' in headers.get(hdrs.TRANSFER_ENCODING, '').lower()

    message = RawRequestMessage(
        method, path, version, headers,
        raw_hdrs, closing, False, False, chunked, URL(path))
    if app is None:
        app = _create_app_mock()

    if protocol is sentinel:
        protocol = mock.Mock()

    if transport is sentinel:
        transport = _create_transport(sslcontext)
github aio-libs / aiohttp / tests / test_client_functional.py View on Github external
async def test_multidict_headers(aiohttp_client) -> None:
    async def handler(request):
        assert await request.read() == data
        return web.Response()

    app = web.Application()
    app.router.add_post('/', handler)

    client = await aiohttp_client(app)

    data = b'sample data'

    r = await client.post('/', data=data,
                          headers=MultiDict(
                              {'Content-Length': str(len(data))}))
    assert r.status == 200
github aio-libs / aiohttp / tests / test_client_request.py View on Github external
async def start(self, connection, read_until_eof=False):
            nonlocal conn
            conn = connection
            self.status = 123
            self.reason = 'Test OK'
            self._headers = CIMultiDictProxy(CIMultiDict())
            self.cookies = SimpleCookie()
            return
github aio-libs / aiohttp / tests / test_web_functional.py View on Github external
async def handler(request):
        assert isinstance(request.headers, CIMultiDictProxy)
        return web.Response()
github aio-libs / aiohttp / tests / test_web_request.py View on Github external
def test_base_ctor() -> None:
    message = RawRequestMessage(
        'GET', '/path/to?a=1&b=2', HttpVersion(1, 1),
        CIMultiDictProxy(CIMultiDict()), (),
        False, False, False, False, URL('/path/to?a=1&b=2'))

    req = web.BaseRequest(message,
                          mock.Mock(),
                          mock.Mock(),
                          mock.Mock(),
                          mock.Mock(),
                          mock.Mock())

    assert 'GET' == req.method
    assert HttpVersion(1, 1) == req.version
    assert req.host == socket.getfqdn()
    assert '/path/to?a=1&b=2' == req.path_qs
    assert '/path/to' == req.path
    assert 'a=1&b=2' == req.query_string
    assert CIMultiDict() == req.headers