コード例 #1
0
def test_parse_cache_control_header():
    # check happy path
    headers = {
        "cache-control": "no-cache,max-age=3600",
        "content-type": "application/json",
    }
    expected = {"no-cache": True, "max-age": 3600}
    assert AsyncCache.parse_cache_control_header(headers) == expected

    # check spaces are being handled correctly
    headers = {
        "cache-control": "public, max-age=3600",
        "content-type": "application/json",
    }
    expected = {"max-age": 3600}
    assert AsyncCache.parse_cache_control_header(headers) == expected

    # check if max-age is not a number
    headers = {
        "cache-control": "max-age=age",
        "content-type": "application/json",
    }
    expected = {}
    assert AsyncCache.parse_cache_control_header(headers) == expected

    # check if no cache-control header is given
    headers = {"content-type": "application/json"}
    expected = {}
    assert AsyncCache.parse_cache_control_header(headers) == expected
コード例 #2
0
def test_non_cacheable_method():
    acache = AsyncCache()
    acache.add(
        key=("test_method", "test_url"),
        value="test_response",
        headers={
            "Cache-Control": "max-age=604800",
            "Content-Type": "text/html; charset=UTF-8",
        },
    )
    assert len(acache.cache) == 0
コード例 #3
0
def test_cache_capacity(monkeypatch):
    cache_capacity = 2
    acache = AsyncCache(config={"capacity": cache_capacity})

    def add_cache_entry(url):
        current_timestamp = time.time()
        monkeypatch.setattr(time, "time", lambda: current_timestamp)
        acache.add(
            key=("GET", url),
            value="test_response",
            headers={
                "Cache-Control": "max-age=604800",
                "Content-Type": "text/html; charset=UTF-8",
            },
        )

    add_cache_entry("test_url_1")
    assert ("GET", "test_url_1") in acache.cache
    add_cache_entry("test_url_2")
    add_cache_entry("test_url_3")

    assert len(acache.cache) == cache_capacity
    assert ("GET", "test_url_1") not in acache.cache
    assert ("GET", "test_url_2") in acache.cache
    assert ("GET", "test_url_3") in acache.cache
コード例 #4
0
def test_add_happy_path(monkeypatch):
    current_timestamp = time.time()
    monkeypatch.setattr(time, "time", lambda: current_timestamp)
    acache = AsyncCache()
    acache.add(
        key=("GET", "test_url"),
        value="test_response",
        headers={
            "Cache-Control": "max-age=604800",
            "Content-Type": "text/html; charset=UTF-8",
        },
    )
    cache_key = ("GET", "test_url")
    assert cache_key in acache.cache
    assert acache.cache[cache_key]["created_at"] == current_timestamp
    assert acache.cache[cache_key]["max-age"] == 604800
    assert acache.cache[cache_key]["value"] == "test_response"
コード例 #5
0
 def __init__(self,
              max_retries=MAX_RETRIES,
              max_requests=MAX_REQUESTS,
              request_timeout=REQUEST_TIMEOUT,
              cache_controller=AsyncCache()):
     self.cache = cache_controller
     self.max_retries = max_retries
     self.max_requests = max_requests
     self.request_timeout = request_timeout
     self.requests_count = 0
コード例 #6
0
def test_init(mocker):
    method = "GET"
    url = "http://example.com"
    timeout = 10
    cache = AsyncCache()
    rcm = RequestContextManager(mocker.Mock(),
                                cache,
                                method,
                                url,
                                timeout=timeout,
                                allow_redirects=True)

    assert rcm.method == method
    assert rcm.url == url
    assert rcm.key == (method, url)
    assert rcm.params == {"timeout": timeout, "allow_redirects": True}
    assert rcm.cache == cache
    assert rcm.timeout == timeout
コード例 #7
0
def test_add_no_cache():
    acache = AsyncCache()
    acache.add(
        key=("GET", "test_url"),
        value="test_response",
        headers={
            "Cache-Control": "max-age=604800,no-cache",
            "Content-Type": "text/html; charset=UTF-8",
        },
    )
    assert len(acache.cache) == 0

    acache.add(
        key=("GET", "test_url"),
        value="test_response",
        headers={
            "Cache-Control": "max-age=604800,no-store",
            "Content-Type": "text/html; charset=UTF-8",
        },
    )
    assert len(acache.cache) == 0