def test_alias_equal():
    """Paths exactly matching an alias can be resolved."""

    req = OriginRequest(CONF_PATH)

    aliases = [{"src": "/foo/bar", "dest": "/quux"}]

    assert req.uri_alias("/foo/bar", aliases) == "/quux"
def test_alias_boundary():
    """Aliases are only resolved at path boundaries."""

    req = OriginRequest(CONF_PATH)

    aliases = [{"src": "/foo/bar", "dest": "/"}]

    # /foo/bar should not be resolved since it's not followed by /.
    assert req.uri_alias("/foo/bar-somefile", aliases) == "/foo/bar-somefile"
Ejemplo n.º 3
0
def test_alias_single():
    """Each alias is only applied a single time."""

    req = OriginRequest(CONF_PATH)

    aliases = [Alias("/foo/bar", ""), Alias("/baz", "/quux")]

    # Only the first layer of /foo/bar ends up being resolved.
    assert (req.uri_alias("/foo/bar/foo/bar/baz/somefile",
                          aliases) == "/foo/bar/baz/somefile")
Ejemplo n.º 4
0
def test_origin_request_invalid_item(mocked_datetime, mocked_boto3_client,
                                     caplog):
    mocked_datetime.now().isoformat.return_value = MOCKED_DT
    mocked_boto3_client().query.return_value = {
        "Items": [{
            "web_uri": {
                "S": TEST_PATH
            },
            "from_date": {
                "S": "2020-02-17T00:00:00.000+00:00"
            },
        }]
    }

    event = {"Records": [{"cf": {"request": {"uri": TEST_PATH}}}]}

    with pytest.raises(KeyError):
        request = OriginRequest(conf_file=TEST_CONF).handler(event,
                                                             context=None)

    assert ("Exception occurred while processing %s" % json.dumps({
        "web_uri": {
            "S": TEST_PATH
        },
        "from_date": {
            "S": "2020-02-17T00:00:00.000+00:00"
        },
    }) in caplog.text)
Ejemplo n.º 5
0
def test_origin_request(
    mocked_datetime, mocked_boto3_client, req_uri, real_uri, caplog
):
    mocked_datetime.now().isoformat.return_value = MOCKED_DT
    mocked_boto3_client().query.return_value = {
        "Items": [
            {
                "web_uri": {"S": real_uri},
                "from_date": {"S": "2020-02-17T00:00:00.000+00:00"},
                "object_key": {"S": "e4a3f2sum"},
            }
        ]
    }

    event = {"Records": [{"cf": {"request": {"uri": req_uri, "headers": {}}}}]}

    with caplog.at_level(logging.DEBUG):
        request = OriginRequest(conf_file=TEST_CONF).handler(
            event, context=None
        )

    assert "Item found for '%s'" % real_uri in caplog.text

    assert request == {
        "uri": "/e4a3f2sum",
        "headers": {
            "exodus-original-uri": [
                {"key": "exodus-original-uri", "value": req_uri}
            ]
        },
    }
Ejemplo n.º 6
0
def test_origin_request_no_item(mocked_datetime, mocked_boto3_client, caplog):
    mocked_datetime.now().isoformat.return_value = MOCKED_DT
    mocked_boto3_client().query.return_value = {"Items": []}

    event = {"Records": [{"cf": {"request": {"uri": TEST_PATH}}}]}

    with caplog.at_level(logging.DEBUG):
        request = OriginRequest(conf_file=TEST_CONF).handler(event,
                                                             context=None)

    assert request == {"status": "404", "statusDescription": "Not Found"}
    assert "No item found for '%s'" % TEST_PATH in caplog.text
Ejemplo n.º 7
0
def test_origin_request(
    mocked_datetime,
    mocked_boto3_client,
    req_uri,
    real_uri,
    content_type,
    caplog,
):
    mocked_datetime.now().isoformat.return_value = MOCKED_DT
    mocked_boto3_client().query.return_value = {
        "Items": [{
            "web_uri": {
                "S": real_uri
            },
            "from_date": {
                "S": "2020-02-17T00:00:00.000+00:00"
            },
            "object_key": {
                "S": "e4a3f2sum"
            },
            "content_type": {
                "S": content_type
            },
        }]
    }

    event = {"Records": [{"cf": {"request": {"uri": req_uri, "headers": {}}}}]}

    with caplog.at_level(logging.DEBUG):
        request = OriginRequest(conf_file=TEST_CONF,
                                definitions_source=mock_definitions()).handler(
                                    event, context=None)

    assert "Item found for '%s'" % real_uri in caplog.text

    assert request == {
        "uri":
        "/e4a3f2sum",
        "querystring":
        urllib.parse.urlencode({"response-content-type": content_type}),
        "headers": {
            "exodus-original-uri": [{
                "key": "exodus-original-uri",
                "value": req_uri
            }]
        },
    }