Esempio n. 1
0
async def test_get_content_length():
    raw_headers = [("Content-Length", "10")]
    headers = CIMultiDict(raw_headers)
    req = make_mocked_request("GET", "/", headers=headers)
    assert req.content_length == 10

    with pytest.raises(ValueError):
        headers = CIMultiDict([("Content-Length", "foobar")])
        req = make_mocked_request("GET", "/", headers=headers)
        assert req.content_length
Esempio n. 2
0
def test_vhm_url(dummy_guillotina):
    request = make_mocked_request('GET', '/', {
        'X-VirtualHost-Monster': 'https://foobar.com/foo/bar'
    })

    assert get_url(
        request, '/c/d') == 'https://foobar.com/foo/bar/c/d'
Esempio n. 3
0
def test_forwarded_proto_url(dummy_guillotina):
    request = make_mocked_request('GET', '/', {
        'X-Forwarded-Proto': 'https'
    })
    url = get_url(request, '/c/d')
    assert url.startswith('https://')
    assert url.endswith('/c/d')
Esempio n. 4
0
    async def initialize(self):
        """
        create root object if necessary
        """
        request = make_mocked_request('POST', '/')
        request._db_write_enabled = True
        tm = request._tm = self.get_transaction_manager()
        txn = await tm.begin(request=request)
        # for get_current_request magic
        self.request = request

        commit = False
        try:
            assert tm.get(request=request) == txn
            root = await txn.get(ROOT_ID)
            if root.__db_id__ is None:
                root.__db_id__ = self._database_name
                txn.register(root)
                commit = True
        except KeyError:
            root = Root(self._database_name)
            txn.register(root, new_oid=ROOT_ID)
            commit = True

        if commit:
            await tm.commit(txn=txn)
        else:
            await tm.abort(txn=txn)
Esempio n. 5
0
    async def initialize(self):
        """
        create root object if necessary
        """
        request = make_mocked_request('POST', '/')
        request._db_write_enabled = True
        tm = request._tm = self.get_transaction_manager()
        txn = await tm.begin()

        try:
            await txn._strategy.retrieve_tid()
            root = await tm._storage.load(txn, ROOT_ID)
            if root is not None:
                root = reader(root)
                root._p_jar = txn
                if root.__db_id__ is None:
                    root.__db_id__ = self._database_name
                    await tm._storage.store(ROOT_ID, 0, IWriter(root), root,
                                            txn)
        except KeyError:
            from guillotina.db.db import Root
            root = Root(self._database_name)
            await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn)
        finally:
            await tm.commit(txn=txn)
Esempio n. 6
0
async def test_execute_futures():
    req = make_mocked_request('GET', '/')
    req.add_future('sleep', asyncio.sleep(0.01))
    assert req.get_future('sleep') is not None
    assert req.get_future('sleep2') is None
    task = req.execute_futures()
    await asyncio.wait_for(task, 1)
    assert task.done()
Esempio n. 7
0
async def test_get_headers():
    raw_headers = [("a", "1"), ("a", "2"), ("b", "3")]
    headers = CIMultiDict(raw_headers)
    req = make_mocked_request("GET", "/", headers=headers)
    assert req.headers == headers

    for k, v in raw_headers:
        assert (bytes(k, "utf-8"), bytes(v, "utf-8")) in req.raw_headers
Esempio n. 8
0
async def test_execute_futures():
    req = make_mocked_request("GET", "/")
    req.add_future("sleep", asyncio.sleep(0.01))
    assert req.get_future("sleep") is not None
    assert req.get_future("sleep2") is None
    task = req.execute_futures()
    await asyncio.wait_for(task, 1)
    assert task.done()
async def test_serialize_request():
    request = make_mocked_request("POST", "http://foobar.com",
                                  {"X-Header": "1"}, b"param1=yes,param2=no")
    request.annotations = {"foo": "bar"}
    serialized = serialize_request(request)
    assert serialized["url"] == request.url
    assert serialized["method"] == request.method
    assert serialized["headers"] == dict(request.headers)
    assert serialized["annotations"] == request.annotations
Esempio n. 10
0
    def __init__(self, base_request, data, channel, envelope):
        if base_request is None:
            from guillotina.tests.utils import make_mocked_request
            base_request = make_mocked_request('POST', '/db')
        self.base_request = base_request
        self.data = data
        self.channel = channel
        self.envelope = envelope

        self.task = None
        self._state_manager = None
        self._started = time.time()
Esempio n. 11
0
async def test_make_request():
    serialized = {
        "annotations": {
            "_corr_id": "foo"
        },
        "headers": {
            "Host": "localhost",
            "X-Header": "1"
        },
        "method": "POST",
        "url": "http://localhost/http://foobar.com?param1=yes,param2=no",
    }
    base_request = make_mocked_request("GET", "http://bar.ba", {"A": "2"},
                                       b"caca=tua")
    request = make_request(base_request, serialized)
    assert serialized["url"] == request.url
    assert serialized["method"] == request.method
    assert serialized["headers"] == dict(request.headers)
    assert serialized["annotations"] == request.annotations
Esempio n. 12
0
    async def initialize(self):
        """
        create root object if necessary
        """
        request = make_mocked_request('POST', '/')
        request._db_write_enabled = True
        tm = request._tm = self.get_transaction_manager()
        txn = await tm.begin()

        try:
            await txn._strategy.retrieve_tid()
            root = await tm._storage.load(txn, ROOT_ID)
            if root is not None:
                root = reader(root)
                root._p_jar = txn
                if root.__db_id__ is None:
                    root.__db_id__ = self._database_name
                    await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn)
        except KeyError:
            from guillotina.db.db import Root
            root = Root(self._database_name)
            await tm._storage.store(ROOT_ID, 0, IWriter(root), root, txn)
        finally:
            await tm.commit(txn=txn)
Esempio n. 13
0
async def test_get_content():
    req = make_mocked_request("GET", "/", payload=b"X" * 2048)
    body = bytes(await req.content.read())
    assert body == b"X" * 2048
Esempio n. 14
0
async def test_get_path_qs():
    req = make_mocked_request("GET", "/some/path", query_string=b"q=blabla")
    assert req.path_qs == "/some/path?q=blabla"
Esempio n. 15
0
async def test_get_query():
    req = make_mocked_request("GET", "/", query_string=b"q=blabla")
    assert req.query.get("q") == "blabla"

    req = make_mocked_request("GET", "/", query_string=b"flag")
    assert "flag" in req.query
Esempio n. 16
0
def test_vhm_url(dummy_guillotina):
    request = make_mocked_request(
        "GET", "/", {"X-VirtualHost-Monster": "https://foobar.com/foo/bar"})

    assert get_url(request, "/c/d") == "https://foobar.com/foo/bar/c/d"
Esempio n. 17
0
def test_forwarded_proto_url(dummy_guillotina):
    request = make_mocked_request("GET", "/", {"X-Forwarded-Proto": "https"})
    url = get_url(request, "/c/d")
    assert url.startswith("https://")
    assert url.endswith("/c/d")
Esempio n. 18
0
async def test_get_uid():
    req = make_mocked_request("GET", "/")
    assert req.uid is not None
Esempio n. 19
0
def test_vhm_url(dummy_guillotina):
    request = make_mocked_request(
        'GET', '/', {'X-VirtualHost-Monster': 'https://foobar.com/foo/bar'})

    assert get_url(request, '/c/d') == 'https://foobar.com/foo/bar/c/d'
Esempio n. 20
0
def test_vh_path_url(dummy_guillotina):
    request = make_mocked_request("GET", "/",
                                  {"X-VirtualHost-Path": "/foo/bar"})

    assert get_url(request, "/c/d").endswith("/foo/bar/c/d")
Esempio n. 21
0
async def test_get_uid_from_forwarded():
    req = make_mocked_request('GET',
                              '/',
                              headers={'X-FORWARDED-REQUEST-UID': 'foobar'})
    assert req.uid == 'foobar'
Esempio n. 22
0
async def test_get_uid():
    req = make_mocked_request('GET', '/')
    assert req.uid is not None
Esempio n. 23
0
async def test_body_exist():
    req = make_mocked_request("GET", "/", payload=b"X" * 2048)
    assert await req.body_exists is True
Esempio n. 24
0
async def test_get_uid_from_forwarded():
    req = make_mocked_request("GET",
                              "/",
                              headers={"X-FORWARDED-REQUEST-UID": "foobar"})
    assert req.uid == "foobar"
Esempio n. 25
0
async def test_not_body_exist():
    req = make_mocked_request("GET", "/")
    assert await req.body_exists is False
Esempio n. 26
0
async def test_get_content_type():
    raw_headers = [("Content-Type", "application/json")]
    headers = CIMultiDict(raw_headers)
    req = make_mocked_request("GET", "/", headers=headers)
    assert req.content_type == "application/json"
Esempio n. 27
0
def test_forwarded_proto_url(dummy_guillotina):
    request = make_mocked_request('GET', '/', {'X-Forwarded-Proto': 'https'})
    url = get_url(request, '/c/d')
    assert url.startswith('https://')
    assert url.endswith('/c/d')
Esempio n. 28
0
def test_url(dummy_guillotina):
    request = make_mocked_request("GET", "/a/b", query_string=b"include=title")

    assert get_url(request, "/c/d") == "http://localhost/c/d"