def test_vary_header_is_sent():
    # Verifies RFC 2616 13.6.
    # See https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html.
    http = httplib2.Http(cache=tests.get_cache_path())
    response = tests.http_response_bytes(headers={
        "vary": "Accept",
        "cache-control": "max-age=300"
    },
                                         add_date=True)
    with tests.server_const_bytes(response, request_count=3) as uri:
        response, content = http.request(uri,
                                         "GET",
                                         headers={"accept": "text/plain"})
        assert response.status == 200
        assert "vary" in response

        # get the resource again, from the cache since accept header in this
        # request is the same as the request
        response, content = http.request(uri,
                                         "GET",
                                         headers={"Accept": "text/plain"})
        assert response.status == 200
        assert response.fromcache, "Should be from cache"

        # get the resource again, not from cache since Accept headers does not match
        response, content = http.request(uri,
                                         "GET",
                                         headers={"Accept": "text/html"})
        assert response.status == 200
        assert not response.fromcache, "Should not be from cache"

        # get the resource again, without any Accept header, so again no match
        response, content = http.request(uri, "GET")
        assert response.status == 200
        assert not response.fromcache, "Should not be from cache"
Exemple #2
0
def test_vary_header_double():
    http = httplib2.Http(cache=tests.get_cache_path())
    response = tests.http_response_bytes(
        headers={'vary': 'Accept, Accept-Language', 'cache-control': 'max-age=300'},
        add_date=True,
    )
    with tests.server_const_bytes(response, request_count=3) as uri:
        response, content = http.request(uri, 'GET', headers={
            'Accept': 'text/plain',
            'Accept-Language': 'da, en-gb;q=0.8, en;q=0.7',
        })
        assert response.status == 200
        assert 'vary' in response

        # we are from cache
        response, content = http.request(uri, 'GET', headers={
            'Accept': 'text/plain', 'Accept-Language': 'da, en-gb;q=0.8, en;q=0.7'})
        assert response.fromcache, "Should be from cache"

        response, content = http.request(uri, 'GET', headers={'Accept': 'text/plain'})
        assert response.status == 200
        assert not response.fromcache

        # get the resource again, not from cache, varied headers don't match exact
        response, content = http.request(uri, 'GET', headers={'Accept-Language': 'da'})
        assert response.status == 200
        assert not response.fromcache, "Should not be from cache"
Exemple #3
0
def test_head_read():
    # Test that we don't try to read the response of a HEAD request
    # since httplib blocks response.read() for HEAD requests.
    http = httplib2.Http()
    respond_with = b"HTTP/1.0 200 OK\r\ncontent-length: " b"14\r\n\r\nnon-empty-body"
    with tests.server_const_bytes(respond_with) as uri:
        response, content = http.request(uri, "HEAD")
    assert response.status == 200
    assert content == b""
Exemple #4
0
def test_head_read():
    # Test that we don't try to read the response of a HEAD request
    # since httplib blocks response.read() for HEAD requests.
    http = httplib2.Http()
    respond_with = b'HTTP/1.0 200 OK\r\ncontent-length: 14\r\n\r\nnon-empty-body'
    with tests.server_const_bytes(respond_with) as uri:
        response, content = http.request(uri, 'HEAD')
    assert response.status == 200
    assert content == b""
def test_get_300_with_location_noredirect():
    # Test the we automatically follow 300 redirects if a Location: header is provided
    http = httplib2.Http()
    http.follow_redirects = False
    response = tests.http_response_bytes(status='300 Multiple Choices',
                                         headers={'location': '/final'},
                                         body=b'redirect body')
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, 'GET')
    assert response.status == 300
Exemple #6
0
def test_gzip_head():
    # Test that we don't try to decompress a HEAD response
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={"content-encoding": "gzip", "content-length": 42}
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, "HEAD")
        assert response.status == 200
        assert int(response["content-length"]) != 0
        assert content == b""
Exemple #7
0
def test_gzip_post_response():
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={"content-encoding": "gzip"},
        body=tests.gzip_compress(b"properly compressed"),
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, "POST", body=b"")
        assert response.status == 200
        assert "content-encoding" not in response
        assert "-content-encoding" in response
Exemple #8
0
def test_gzip_post_response():
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={'content-encoding': 'gzip'},
        body=tests.gzip_compress(b'properly compressed'),
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, 'POST', body=b'')
        assert response.status == 200
        assert 'content-encoding' not in response
        assert '-content-encoding' in response
Exemple #9
0
def test_get_300_with_location_noredirect():
    # Test the we automatically follow 300 redirects if a Location: header is provided
    http = httplib2.Http()
    http.follow_redirects = False
    response = tests.http_response_bytes(
        status='300 Multiple Choices',
        headers={'location': '/final'},
        body=b'redirect body')
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, 'GET')
    assert response.status == 300
Exemple #10
0
def test_gzip_post_response():
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={'content-encoding': 'gzip'},
        body=tests.gzip_compress(b'properly compressed'),
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, 'POST', body=b'')
        assert response.status == 200
        assert 'content-encoding' not in response
        assert '-content-encoding' in response
Exemple #11
0
def test_gzip_head():
    # Test that we don't try to decompress a HEAD response
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={'content-encoding': 'gzip', 'content-length': 42},
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, 'HEAD')
        assert response.status == 200
        assert int(response['content-length']) != 0
        assert content == b''
Exemple #12
0
def test_gzip_post_response():
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={"content-encoding": "gzip"},
        body=tests.gzip_compress(b"properly compressed"),
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, "POST", body=b"")
        assert response.status == 200
        assert "content-encoding" not in response
        assert "-content-encoding" in response
Exemple #13
0
def test_gzip_head():
    # Test that we don't try to decompress a HEAD response
    http = httplib2.Http()
    response = tests.http_response_bytes(headers={
        'content-encoding': 'gzip',
        'content-length': 42
    }, )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, 'HEAD')
        assert response.status == 200
        assert int(response['content-length']) != 0
        assert content == b''
Exemple #14
0
def test_gzip_head():
    # Test that we don't try to decompress a HEAD response
    http = httplib2.Http()
    response = tests.http_response_bytes(headers={
        "content-encoding": "gzip",
        "content-length": 42
    })
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, "HEAD")
        assert response.status == 200
        assert int(response["content-length"]) != 0
        assert content == b""
Exemple #15
0
def test_deflate_get():
    # Test that we support deflate compression
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={'content-encoding': 'deflate'},
        body=tests.deflate_compress(b'properly compressed'),
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, 'GET')
        assert response.status == 200
        assert 'content-encoding' not in response
        assert int(response['content-length']) == len(b'properly compressed')
        assert content == b'properly compressed'
Exemple #16
0
def test_get_duplicate_headers():
    # Test that duplicate headers get concatenated via ','
    http = httplib2.Http()
    response = b'''HTTP/1.0 200 OK\r\n\
Link: link1\r\n\
Content-Length: 7\r\n\
Link: link2\r\n\r\n\
content'''
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, 'GET')
        assert response.status == 200
        assert content == b"content"
        assert response['link'], 'link1, link2'
Exemple #17
0
def test_deflate_get():
    # Test that we support deflate compression
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={"content-encoding": "deflate"},
        body=tests.deflate_compress(b"properly compressed"),
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, "GET")
        assert response.status == 200
        assert "content-encoding" not in response
        assert int(response["content-length"]) == len(b"properly compressed")
        assert content == b"properly compressed"
Exemple #18
0
def test_get_duplicate_headers():
    # Test that duplicate headers get concatenated via ','
    http = httplib2.Http()
    response = b"""HTTP/1.0 200 OK\r\n\
Link: link1\r\n\
Content-Length: 7\r\n\
Link: link2\r\n\r\n\
content"""
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, "GET")
        assert response.status == 200
        assert content == b"content"
        assert response["link"], "link1, link2"
Exemple #19
0
def test_deflate_get():
    # Test that we support deflate compression
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={'content-encoding': 'deflate'},
        body=tests.deflate_compress(b'properly compressed'),
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, 'GET')
        assert response.status == 200
        assert 'content-encoding' not in response
        assert int(response['content-length']) == len(b'properly compressed')
        assert content == b'properly compressed'
Exemple #20
0
def test_deflate_get():
    # Test that we support deflate compression
    http = httplib2.Http()
    response = tests.http_response_bytes(
        headers={"content-encoding": "deflate"},
        body=tests.deflate_compress(b"properly compressed"),
    )
    with tests.server_const_bytes(response) as uri:
        response, content = http.request(uri, "GET")
        assert response.status == 200
        assert "content-encoding" not in response
        assert int(response["content-length"]) == len(b"properly compressed")
        assert content == b"properly compressed"
Exemple #21
0
def test_vary_unused_header():
    http = httplib2.Http(cache=tests.get_cache_path())
    response = tests.http_response_bytes(
        headers={'vary': 'X-No-Such-Header', 'cache-control': 'max-age=300'},
        add_date=True,
    )
    with tests.server_const_bytes(response, request_count=1) as uri:
        # A header's value is not considered to vary if it's not used at all.
        response, content = http.request(uri, 'GET', headers={'Accept': 'text/plain'})
        assert response.status == 200
        assert 'vary' in response

        # we are from cache
        response, content = http.request(uri, 'GET', headers={'Accept': 'text/plain'})
        assert response.fromcache, "Should be from cache"
Exemple #22
0
def test_gzip_malformed_response():
    http = httplib2.Http()
    # Test that we raise a good exception when the gzip fails
    http.force_exception_to_status_code = False
    response = tests.http_response_bytes(headers={"content-encoding": "gzip"},
                                         body=b"obviously not compressed")
    with tests.server_const_bytes(response, request_count=2) as uri:
        with tests.assert_raises(httplib2.FailedToDecompressContent):
            http.request(uri, "GET")

        # Re-run the test with out the exceptions
        http.force_exception_to_status_code = True

        response, content = http.request(uri, "GET")
        assert response.status == 500
        assert response.reason.startswith("Content purported")
Exemple #23
0
def test_get_301_no_redirect():
    # Test that we cache the 301 response
    http = httplib2.Http(cache=tests.get_cache_path(), timeout=0.5)
    http.follow_redirects = False
    response = tests.http_response_bytes(
        status="301 Now where did I leave that URL",
        headers={"location": "/final", "cache-control": "max-age=300"},
        body=b"redirect body",
        add_date=True,
    )
    with tests.server_const_bytes(response) as uri:
        response, _ = http.request(uri, "GET")
        assert response.status == 301
        assert not response.fromcache
        response, _ = http.request(uri, "GET")
        assert response.status == 301
        assert response.fromcache
Exemple #24
0
def test_deflate_malformed_response():
    # Test that we raise a good exception when the deflate fails
    http = httplib2.Http()
    http.force_exception_to_status_code = False
    response = tests.http_response_bytes(
        headers={"content-encoding": "deflate"}, body=b"obviously not compressed"
    )
    with tests.server_const_bytes(response, request_count=2) as uri:
        with tests.assert_raises(httplib2.FailedToDecompressContent):
            http.request(uri, "GET")

        # Re-run the test with out the exceptions
        http.force_exception_to_status_code = True

        response, content = http.request(uri, "GET")
        assert response.status == 500
        assert response.reason.startswith("Content purported")
Exemple #25
0
def test_get_301_no_redirect():
    # Test that we cache the 301 response
    http = httplib2.Http(cache=tests.get_cache_path(), timeout=0.5)
    http.follow_redirects = False
    response = tests.http_response_bytes(
        status='301 Now where did I leave that URL',
        headers={'location': '/final', 'cache-control': 'max-age=300'},
        body=b'redirect body',
        add_date=True,
    )
    with tests.server_const_bytes(response) as uri:
        response, _ = http.request(uri, 'GET')
        assert response.status == 301
        assert not response.fromcache
        response, _ = http.request(uri, 'GET')
        assert response.status == 301
        assert response.fromcache
def test_get_301_no_redirect():
    # Test that we cache the 301 response
    http = httplib2.Http(cache=tests.get_cache_path(), timeout=0.5)
    http.follow_redirects = False
    response = tests.http_response_bytes(
        status="301 Now where did I leave that URL",
        headers={"location": "/final", "cache-control": "max-age=300"},
        body=b"redirect body",
        add_date=True,
    )
    with tests.server_const_bytes(response) as uri:
        response, _ = http.request(uri, "GET")
        assert response.status == 301
        assert not response.fromcache
        response, _ = http.request(uri, "GET")
        assert response.status == 301
        assert response.fromcache
def test_vary_header_simple():
    """
    RFC 2616 13.6
    When the cache receives a subsequent request whose Request-URI
    specifies one or more cache entries including a Vary header field,
    the cache MUST NOT use such a cache entry to construct a response
    to the new request unless all of the selecting request-headers
    present in the new request match the corresponding stored
    request-headers in the original request.
    """
    # test that the vary header is sent
    http = httplib2.Http(cache=tests.get_cache_path())
    response = tests.http_response_bytes(
        headers={
            'vary': 'Accept',
            'cache-control': 'max-age=300'
        },
        add_date=True,
    )
    with tests.server_const_bytes(response, request_count=3) as uri:
        response, content = http.request(uri,
                                         'GET',
                                         headers={'accept': 'text/plain'})
        assert response.status == 200
        assert 'vary' in response

        # get the resource again, from the cache since accept header in this
        # request is the same as the request
        response, content = http.request(uri,
                                         'GET',
                                         headers={'Accept': 'text/plain'})
        assert response.status == 200
        assert response.fromcache, "Should be from cache"

        # get the resource again, not from cache since Accept headers does not match
        response, content = http.request(uri,
                                         'GET',
                                         headers={'Accept': 'text/html'})
        assert response.status == 200
        assert not response.fromcache, "Should not be from cache"

        # get the resource again, without any Accept header, so again no match
        response, content = http.request(uri, 'GET')
        assert response.status == 200
        assert not response.fromcache, "Should not be from cache"
Exemple #28
0
def test_deflate_malformed_response():
    # Test that we raise a good exception when the deflate fails
    http = httplib2.Http()
    http.force_exception_to_status_code = False
    response = tests.http_response_bytes(
        headers={'content-encoding': 'deflate'},
        body=b'obviously not compressed',
    )
    with tests.server_const_bytes(response, request_count=2) as uri:
        with tests.assert_raises(httplib2.FailedToDecompressContent):
            http.request(uri, 'GET')

        # Re-run the test with out the exceptions
        http.force_exception_to_status_code = True

        response, content = http.request(uri, 'GET')
        assert response.status == 500
        assert response.reason.startswith('Content purported')
Exemple #29
0
def test_gzip_malformed_response():
    http = httplib2.Http()
    # Test that we raise a good exception when the gzip fails
    http.force_exception_to_status_code = False
    response = tests.http_response_bytes(
        headers={'content-encoding': 'gzip'},
        body=b'obviously not compressed',
    )
    with tests.server_const_bytes(response, request_count=2) as uri:
        with tests.assert_raises(httplib2.FailedToDecompressContent):
            http.request(uri, 'GET')

        # Re-run the test with out the exceptions
        http.force_exception_to_status_code = True

        response, content = http.request(uri, 'GET')
        assert response.status == 500
        assert response.reason.startswith('Content purported')
def test_vary_header_double():
    http = httplib2.Http(cache=tests.get_cache_path())
    response = tests.http_response_bytes(
        headers={
            'vary': 'Accept, Accept-Language',
            'cache-control': 'max-age=300'
        },
        add_date=True,
    )
    with tests.server_const_bytes(response, request_count=3) as uri:
        response, content = http.request(uri,
                                         'GET',
                                         headers={
                                             'Accept':
                                             'text/plain',
                                             'Accept-Language':
                                             'da, en-gb;q=0.8, en;q=0.7',
                                         })
        assert response.status == 200
        assert 'vary' in response

        # we are from cache
        response, content = http.request(uri,
                                         'GET',
                                         headers={
                                             'Accept':
                                             'text/plain',
                                             'Accept-Language':
                                             'da, en-gb;q=0.8, en;q=0.7'
                                         })
        assert response.fromcache, "Should be from cache"

        response, content = http.request(uri,
                                         'GET',
                                         headers={'Accept': 'text/plain'})
        assert response.status == 200
        assert not response.fromcache

        # get the resource again, not from cache, varied headers don't match exact
        response, content = http.request(uri,
                                         'GET',
                                         headers={'Accept-Language': 'da'})
        assert response.status == 200
        assert not response.fromcache, "Should not be from cache"
def test_vary_header_double():
    http = httplib2.Http(cache=tests.get_cache_path())
    response = tests.http_response_bytes(
        headers={
            "vary": "Accept, Accept-Language",
            "cache-control": "max-age=300"
        },
        add_date=True,
    )
    with tests.server_const_bytes(response, request_count=3) as uri:
        response, content = http.request(
            uri,
            "GET",
            headers={
                "Accept": "text/plain",
                "Accept-Language": "da, en-gb;q=0.8, en;q=0.7",
            },
        )
        assert response.status == 200
        assert "vary" in response

        # we are from cache
        response, content = http.request(
            uri,
            "GET",
            headers={
                "Accept": "text/plain",
                "Accept-Language": "da, en-gb;q=0.8, en;q=0.7",
            },
        )
        assert response.fromcache, "Should be from cache"

        response, content = http.request(uri,
                                         "GET",
                                         headers={"Accept": "text/plain"})
        assert response.status == 200
        assert not response.fromcache

        # get the resource again, not from cache, varied headers don't match exact
        response, content = http.request(uri,
                                         "GET",
                                         headers={"Accept-Language": "da"})
        assert response.status == 200
        assert not response.fromcache, "Should not be from cache"
def test_get_301_no_redirect():
    # Test that we cache the 301 response
    http = httplib2.Http(cache=tests.get_cache_path(), timeout=0.5)
    http.follow_redirects = False
    response = tests.http_response_bytes(
        status='301 Now where did I leave that URL',
        headers={
            'location': '/final',
            'cache-control': 'max-age=300'
        },
        body=b'redirect body',
        add_date=True,
    )
    with tests.server_const_bytes(response) as uri:
        response, _ = http.request(uri, 'GET')
        assert response.status == 301
        assert not response.fromcache
        response, _ = http.request(uri, 'GET')
        assert response.status == 301
        assert response.fromcache
def test_vary_unused_header():
    http = httplib2.Http(cache=tests.get_cache_path())
    response = tests.http_response_bytes(
        headers={
            'vary': 'X-No-Such-Header',
            'cache-control': 'max-age=300'
        },
        add_date=True,
    )
    with tests.server_const_bytes(response, request_count=1) as uri:
        # A header's value is not considered to vary if it's not used at all.
        response, content = http.request(uri,
                                         'GET',
                                         headers={'Accept': 'text/plain'})
        assert response.status == 200
        assert 'vary' in response

        # we are from cache
        response, content = http.request(uri,
                                         'GET',
                                         headers={'Accept': 'text/plain'})
        assert response.fromcache, "Should be from cache"
def test_vary_unused_header():
    http = httplib2.Http(cache=tests.get_cache_path())
    response = tests.http_response_bytes(
        headers={
            "vary": "X-No-Such-Header",
            "cache-control": "max-age=300"
        },
        add_date=True,
    )
    with tests.server_const_bytes(response, request_count=1) as uri:
        # A header's value is not considered to vary if it's not used at all.
        response, content = http.request(uri,
                                         "GET",
                                         headers={"Accept": "text/plain"})
        assert response.status == 200
        assert "vary" in response

        # we are from cache
        response, content = http.request(uri,
                                         "GET",
                                         headers={"Accept": "text/plain"})
        assert response.fromcache, "Should be from cache"
Exemple #35
0
def test_vary_header_simple():
    """
    RFC 2616 13.6
    When the cache receives a subsequent request whose Request-URI
    specifies one or more cache entries including a Vary header field,
    the cache MUST NOT use such a cache entry to construct a response
    to the new request unless all of the selecting request-headers
    present in the new request match the corresponding stored
    request-headers in the original request.
    """
    # test that the vary header is sent
    http = httplib2.Http(cache=tests.get_cache_path())
    response = tests.http_response_bytes(
        headers={'vary': 'Accept', 'cache-control': 'max-age=300'},
        add_date=True,
    )
    with tests.server_const_bytes(response, request_count=3) as uri:
        response, content = http.request(uri, 'GET', headers={'accept': 'text/plain'})
        assert response.status == 200
        assert 'vary' in response

        # get the resource again, from the cache since accept header in this
        # request is the same as the request
        response, content = http.request(uri, 'GET', headers={'Accept': 'text/plain'})
        assert response.status == 200
        assert response.fromcache, "Should be from cache"

        # get the resource again, not from cache since Accept headers does not match
        response, content = http.request(uri, 'GET', headers={'Accept': 'text/html'})
        assert response.status == 200
        assert not response.fromcache, "Should not be from cache"

        # get the resource again, without any Accept header, so again no match
        response, content = http.request(uri, 'GET')
        assert response.status == 200
        assert not response.fromcache, "Should not be from cache"
Exemple #36
0
def TestOneInput(input_bytes):
    http = httplib2.Http()
    ib = b"HTTP/1.0 200 OK\r\n" + input_bytes
    with tests.server_const_bytes(ib) as uri:
        http.request(uri)