Пример #1
0
def test_cache_miss_slow_database_read(admin_client, international_root_page):
    class SlowSerializer(Serializer):
        def to_representation(self, instance):
            time.sleep(3)
            return {}

    service_name = cms.GREAT_INTERNATIONAL

    page = InternationalArticlePageFactory.create(
        parent=international_root_page, live=True)

    url = reverse('api:lookup-by-slug', kwargs={'slug': page.slug})

    # given the page  is very slow to read
    with mock.patch.dict(serializer_mapping.MODELS_SERIALIZERS_MAPPING,
                         {page.__class__: SlowSerializer}):
        response = admin_client.get(url, {'service_name': service_name})

    # then the response results in 501
    assert response.status_code == 501
Пример #2
0
def test_cache_etags_mismatch(admin_client, international_root_page):
    service_name = cms.GREAT_INTERNATIONAL
    # given there exists a page that is cached
    page = InternationalArticlePageFactory.create(
        parent=international_root_page, live=True)

    # when the page is retrieved
    url = reverse('api:lookup-by-slug', kwargs={'slug': page.slug})
    admin_client.get(url, {'service_name': service_name})

    # then exposing the same etag in subsequent responses results in 304
    response_two = admin_client.get(
        url,
        {'service_name': service_name},
        HTTP_IF_NONE_MATCH='something-123',
    )

    assert isinstance(response_two, CachedResponse)
    assert response_two.status_code == 200
    assert response_two.content
Пример #3
0
def test_cache_etags_match(admin_client, international_root_page):
    service_name = cms.GREAT_INTERNATIONAL

    # given there exists a page that is cached
    page = InternationalArticlePageFactory.create(
        parent=international_root_page, live=True)
    url = reverse('api:lookup-by-slug', kwargs={'slug': page.slug})
    admin_client.get(url, {'service_name': service_name})

    # and the cached page is retrieved
    response_two = admin_client.get(url, {'service_name': service_name})

    # then exposing the same etag in subsequent responses results in 304
    response_three = admin_client.get(
        url,
        {'service_name': service_name},
        HTTP_IF_NONE_MATCH=response_two['ETag'],
    )
    assert response_three.status_code == 304
    assert response_three.content == b''