Exemple #1
0
def test_get_307():
    # Test that we do follow 307 redirects but
    # do not cache the 307
    http = httplib2.Http(cache=tests.get_cache_path(), timeout=1)
    r307 = tests.http_response_bytes(status=307,
                                     headers={"location": "/final"})
    r200 = tests.http_response_bytes(
        status=200,
        add_date=True,
        body=b"final content\n",
        headers={"cache-control": "max-age=300"},
    )

    with tests.server_list_http([r307, r200, r307]) as uri:
        response, content = http.request(uri, "GET")
        assert response.previous.status == 307
        assert not response.previous.fromcache
        assert response.status == 200
        assert not response.fromcache
        assert content == b"final content\n"

        response, content = http.request(uri, "GET")
        assert response.previous.status == 307
        assert not response.previous.fromcache
        assert response.status == 200
        assert response.fromcache
        assert content == b"final content\n"
 def handler(request):
     if request.method == "CONNECT":
         return tests.http_response_bytes(
             status="400 Expected direct",
             headers={"connection": "close"},
         )
     return tests.http_response_bytes()
 def handler(request):
     if request.method in ('PUT', 'PATCH', 'DELETE'):
         return tests.http_response_bytes(status=405)
     return tests.http_response_bytes(
         add_date=True,
         add_etag=True,
         headers={'cache-control': 'max-age=300'})
Exemple #4
0
def test_get_302_redirection_limit():
    # Test that we can set a lower redirection limit
    # and that we raise an exception when we exceed
    # that limit.
    http = httplib2.Http()
    http.force_exception_to_status_code = False
    routes = {
        "/second":
        tests.http_response_bytes(status="302 Found",
                                  headers={"location": "/final"},
                                  body=b"second redirect"),
        "":
        tests.http_response_bytes(status="302 Found",
                                  headers={"location": "/second"},
                                  body=b"redirect body"),
    }
    with tests.server_route(routes, request_count=4) as uri:
        try:
            http.request(uri, "GET", redirections=1)
            assert False, "This should not happen"
        except httplib2.RedirectLimit:
            pass
        except Exception:
            assert False, "Threw wrong kind of exception "

        # Re-run the test with out the exceptions
        http.force_exception_to_status_code = True
        response, content = http.request(uri, "GET", redirections=1)

    assert response.status == 500
    assert response.reason.startswith("Redirected more")
    assert response["status"] == "302"
    assert content == b"second redirect"
    assert response.previous is not None
def test_get_302_redirection_limit():
    # Test that we can set a lower redirection limit
    # and that we raise an exception when we exceed
    # that limit.
    http = httplib2.Http()
    http.force_exception_to_status_code = False
    routes = {
        '/second':
        tests.http_response_bytes(status='302 Found',
                                  headers={'location': '/final'},
                                  body=b'second redirect'),
        '':
        tests.http_response_bytes(status='302 Found',
                                  headers={'location': '/second'},
                                  body=b'redirect body'),
    }
    with tests.server_route(routes, request_count=4) as uri:
        try:
            http.request(uri, 'GET', redirections=1)
            assert False, 'This should not happen'
        except httplib2.RedirectLimit:
            pass
        except Exception:
            assert False, 'Threw wrong kind of exception '

        # Re-run the test with out the exceptions
        http.force_exception_to_status_code = True
        response, content = http.request(uri, 'GET', redirections=1)

    assert response.status == 500
    assert response.reason.startswith('Redirected more')
    assert response['status'] == '302'
    assert content == b'second redirect'
    assert response.previous is not None
Exemple #6
0
def test_get_302_redirection_limit():
    # Test that we can set a lower redirection limit
    # and that we raise an exception when we exceed
    # that limit.
    http = httplib2.Http()
    http.force_exception_to_status_code = False
    routes = {
        '/second': tests.http_response_bytes(
            status='302 Found', headers={'location': '/final'}, body=b'second redirect'),
        '': tests.http_response_bytes(
            status='302 Found', headers={'location': '/second'}, body=b'redirect body'),
    }
    with tests.server_route(routes, request_count=4) as uri:
        try:
            http.request(uri, 'GET', redirections=1)
            assert False, 'This should not happen'
        except httplib2.RedirectLimit:
            pass
        except Exception:
            assert False, 'Threw wrong kind of exception '

        # Re-run the test with out the exceptions
        http.force_exception_to_status_code = True
        response, content = http.request(uri, 'GET', redirections=1)

    assert response.status == 500
    assert response.reason.startswith('Redirected more')
    assert response['status'] == '302'
    assert content == b'second redirect'
    assert response.previous is not None
Exemple #7
0
def test_get_301():
    # Test that we automatically follow 301 redirects
    # and that we cache the 301 response
    http = httplib2.Http(cache=tests.get_cache_path())
    destination = ''
    routes = {
        '/final': tests.http_response_bytes(body=b'This is the final destination.\n'),
        '': tests.http_response_bytes(
            status='301 Now where did I leave that URL', headers={'location': '/final'}, body=b'redirect body'),
    }
    with tests.server_route(routes, request_count=3) as uri:
        destination = urllib.parse.urljoin(uri, '/final')
        response1, content1 = http.request(uri, 'GET')
        response2, content2 = http.request(uri, 'GET')
    assert response1.status == 200
    assert 'content-location' in response2
    assert response1['content-location'] == destination
    assert content1 == b'This is the final destination.\n'
    assert response1.previous.status == 301
    assert not response1.previous.fromcache

    assert response2.status == 200
    assert response2['content-location'] == destination
    assert content2 == b'This is the final destination.\n'
    assert response2.previous.status == 301
    assert response2.previous.fromcache
Exemple #8
0
def test_get_301():
    # Test that we automatically follow 301 redirects
    # and that we cache the 301 response
    http = httplib2.Http(cache=tests.get_cache_path())
    destination = ""
    routes = {
        "/final": tests.http_response_bytes(body=b"This is the final destination.\n"),
        "": tests.http_response_bytes(
            status="301 Now where did I leave that URL",
            headers={"location": "/final"},
            body=b"redirect body",
        ),
    }
    with tests.server_route(routes, request_count=3) as uri:
        destination = urllib.parse.urljoin(uri, "/final")
        response1, content1 = http.request(uri, "GET")
        response2, content2 = http.request(uri, "GET")
    assert response1.status == 200
    assert "content-location" in response2
    assert response1["content-location"] == destination
    assert content1 == b"This is the final destination.\n"
    assert response1.previous.status == 301
    assert not response1.previous.fromcache

    assert response2.status == 200
    assert response2["content-location"] == destination
    assert content2 == b"This is the final destination.\n"
    assert response2.previous.status == 301
    assert response2.previous.fromcache
Exemple #9
0
def test_get_307():
    # Test that we do follow 307 redirects but
    # do not cache the 307
    http = httplib2.Http(cache=tests.get_cache_path(), timeout=1)
    r307 = tests.http_response_bytes(
        status=307,
        headers={'location': '/final'},
    )
    r200 = tests.http_response_bytes(
        status=200,
        add_date=True,
        body=b'final content\n',
        headers={'cache-control': 'max-age=300'},
    )

    with tests.server_list_http([r307, r200, r307]) as uri:
        response, content = http.request(uri, 'GET')
        assert response.previous.status == 307
        assert not response.previous.fromcache
        assert response.status == 200
        assert not response.fromcache
        assert content == b'final content\n'

        response, content = http.request(uri, 'GET')
        assert response.previous.status == 307
        assert not response.previous.fromcache
        assert response.status == 200
        assert response.fromcache
        assert content == b'final content\n'
 def handler(request):
     if request.method in ("PUT", "PATCH", "DELETE"):
         return tests.http_response_bytes(status=405)
     return tests.http_response_bytes(
         add_date=True,
         add_etag=True,
         headers={"cache-control": "max-age=300"})
Exemple #11
0
def test_get_302_redirection_limit():
    # Test that we can set a lower redirection limit
    # and that we raise an exception when we exceed
    # that limit.
    http = httplib2.Http()
    http.force_exception_to_status_code = False
    routes = {
        "/second": tests.http_response_bytes(
            status="302 Found", headers={"location": "/final"}, body=b"second redirect"
        ),
        "": tests.http_response_bytes(
            status="302 Found", headers={"location": "/second"}, body=b"redirect body"
        ),
    }
    with tests.server_route(routes, request_count=4) as uri:
        try:
            http.request(uri, "GET", redirections=1)
            assert False, "This should not happen"
        except httplib2.RedirectLimit:
            pass
        except Exception:
            assert False, "Threw wrong kind of exception "

        # Re-run the test with out the exceptions
        http.force_exception_to_status_code = True
        response, content = http.request(uri, "GET", redirections=1)

    assert response.status == 500
    assert response.reason.startswith("Redirected more")
    assert response["status"] == "302"
    assert content == b"second redirect"
    assert response.previous is not None
Exemple #12
0
def test_get_300_with_location():
    # Test the we automatically follow 300 redirects if a Location: header is provided
    http = httplib2.Http()
    final_content = b"This is the final destination.\n"
    routes = {
        "/final":
        tests.http_response_bytes(body=final_content),
        "":
        tests.http_response_bytes(status="300 Multiple Choices",
                                  headers={"location": "/final"}),
    }
    with tests.server_route(routes, request_count=2) as uri:
        response, content = http.request(uri, "GET")
    assert response.status == 200
    assert content == final_content
    assert response.previous.status == 300
    assert not response.previous.fromcache

    # Confirm that the intermediate 300 is not cached
    with tests.server_route(routes, request_count=2) as uri:
        response, content = http.request(uri, "GET")
    assert response.status == 200
    assert content == final_content
    assert response.previous.status == 300
    assert not response.previous.fromcache
Exemple #13
0
def test_get_301():
    # Test that we automatically follow 301 redirects
    # and that we cache the 301 response
    http = httplib2.Http(cache=tests.get_cache_path())
    destination = ""
    routes = {
        "/final":
        tests.http_response_bytes(body=b"This is the final destination.\n"),
        "":
        tests.http_response_bytes(
            status="301 Now where did I leave that URL",
            headers={"location": "/final"},
            body=b"redirect body",
        ),
    }
    with tests.server_route(routes, request_count=3) as uri:
        destination = urllib.parse.urljoin(uri, "/final")
        response1, content1 = http.request(uri, "GET")
        response2, content2 = http.request(uri, "GET")
    assert response1.status == 200
    assert "content-location" in response2
    assert response1["content-location"] == destination
    assert content1 == b"This is the final destination.\n"
    assert response1.previous.status == 301
    assert not response1.previous.fromcache

    assert response2.status == 200
    assert response2["content-location"] == destination
    assert content2 == b"This is the final destination.\n"
    assert response2.previous.status == 301
    assert response2.previous.fromcache
def test_get_301():
    # Test that we automatically follow 301 redirects
    # and that we cache the 301 response
    http = httplib2.Http(cache=tests.get_cache_path())
    destination = ''
    routes = {
        '/final':
        tests.http_response_bytes(body=b'This is the final destination.\n'),
        '':
        tests.http_response_bytes(status='301 Now where did I leave that URL',
                                  headers={'location': '/final'},
                                  body=b'redirect body'),
    }
    with tests.server_route(routes, request_count=3) as uri:
        destination = urllib.parse.urljoin(uri, '/final')
        response1, content1 = http.request(uri, 'GET')
        response2, content2 = http.request(uri, 'GET')
    assert response1.status == 200
    assert 'content-location' in response2
    assert response1['content-location'] == destination
    assert content1 == b'This is the final destination.\n'
    assert response1.previous.status == 301
    assert not response1.previous.fromcache

    assert response2.status == 200
    assert response2['content-location'] == destination
    assert content2 == b'This is the final destination.\n'
    assert response2.previous.status == 301
    assert response2.previous.fromcache
Exemple #15
0
 def handle(cls, request):
     # request.number is always 1 because of
     # the new socket connection each time
     cls.number += 1
     if cls.number % 2 != 0:
         time.sleep(0.6)
         return tests.http_response_bytes(status=500)
     return tests.http_response_bytes(status=200)
    def handler(read):
        read()
        yield tests.http_response_bytes(
            status=200, body=b"something", headers={"date": date, "last-modified": date}
        )

        request2 = read()
        assert request2.headers["if-modified-since"] == date
        yield tests.http_response_bytes(status=304)
Exemple #17
0
    def handler(read):
        read()
        yield tests.http_response_bytes(
            status=200, body=b"something", headers={"date": date, "last-modified": date}
        )

        request2 = read()
        assert request2.headers["if-modified-since"] == date
        yield tests.http_response_bytes(status=304)
    def handler(read):
        read()
        yield tests.http_response_bytes(
            status=200,
            body=b'something',
            headers={
                'date': date,
                'last-modified': date,
            },
        )

        request2 = read()
        assert request2.headers['if-modified-since'] == date
        yield tests.http_response_bytes(status=304)
Exemple #19
0
    def handler(read):
        read()
        yield tests.http_response_bytes(
            status=200,
            body=b'something',
            headers={
                'date': date,
                'last-modified': date,
            },
        )

        request2 = read()
        assert request2.headers['if-modified-since'] == date
        yield tests.http_response_bytes(status=304)
Exemple #20
0
def test_change_308():
    # 308: follow with same method, cache redirect
    http = httplib2.Http(cache=tests.get_cache_path(), timeout=1)
    routes = {
        "/final":
        tests.make_http_reflect(),
        "":
        tests.http_response_bytes(
            status="308 Permanent Redirect",
            add_date=True,
            headers={
                "cache-control": "max-age=300",
                "location": "/final"
            },
        ),
    }

    with tests.server_route(routes, request_count=3) as uri:
        response, content = http.request(uri, "CHANGE", body=b"hello308")
        assert response.previous.status == 308
        assert not response.previous.fromcache
        assert response.status == 200
        assert not response.fromcache
        assert content.startswith(b"CHANGE /final HTTP")

        response, content = http.request(uri, "CHANGE")
        assert response.previous.status == 308
        assert response.previous.fromcache
        assert response.status == 200
        assert not response.fromcache
        assert content.startswith(b"CHANGE /final HTTP")
Exemple #21
0
def test_head_301():
    # Test that we automatically follow 301 redirects
    http = httplib2.Http()
    destination = ''
    routes = {
        '/final': tests.http_response_bytes(body=b'This is the final destination.\n'),
        '': tests.http_response_bytes(
            status='301 Now where did I leave that URL', headers={'location': '/final'}, body=b'redirect body'),
    }
    with tests.server_route(routes, request_count=2) as uri:
        destination = urllib.parse.urljoin(uri, '/final')
        response, content = http.request(uri, 'HEAD')
    assert response.status == 200
    assert response['content-location'] == destination
    assert response.previous.status == 301
    assert not response.previous.fromcache
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 #23
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 #24
0
def test_get_302():
    # Test that we automatically follow 302 redirects
    # and that we DO NOT cache the 302 response
    http = httplib2.Http(cache=tests.get_cache_path())
    second_url, final_url = "", ""
    routes = {
        "/final":
        tests.http_response_bytes(body=b"This is the final destination.\n"),
        "/second":
        tests.http_response_bytes(status="302 Found",
                                  headers={"location": "/final"},
                                  body=b"second redirect"),
        "":
        tests.http_response_bytes(status="302 Found",
                                  headers={"location": "/second"},
                                  body=b"redirect body"),
    }
    with tests.server_route(routes, request_count=7) as uri:
        second_url = urllib.parse.urljoin(uri, "/second")
        final_url = urllib.parse.urljoin(uri, "/final")
        response1, content1 = http.request(second_url, "GET")
        response2, content2 = http.request(second_url, "GET")
        response3, content3 = http.request(uri, "GET")
    assert response1.status == 200
    assert response1["content-location"] == final_url
    assert content1 == b"This is the final destination.\n"
    assert response1.previous.status == 302
    assert not response1.previous.fromcache

    assert response2.status == 200
    # FIXME:
    # assert response2.fromcache
    assert response2["content-location"] == final_url
    assert content2 == b"This is the final destination.\n"
    assert response2.previous.status == 302
    assert not response2.previous.fromcache
    assert response2.previous["content-location"] == second_url

    assert response3.status == 200
    # FIXME:
    # assert response3.fromcache
    assert content3 == b"This is the final destination.\n"
    assert response3.previous.status == 302
    assert not response3.previous.fromcache
def test_get_302():
    # Test that we automatically follow 302 redirects
    # and that we DO NOT cache the 302 response
    http = httplib2.Http(cache=tests.get_cache_path())
    second_url, final_url = '', ''
    routes = {
        '/final':
        tests.http_response_bytes(body=b'This is the final destination.\n'),
        '/second':
        tests.http_response_bytes(status='302 Found',
                                  headers={'location': '/final'},
                                  body=b'second redirect'),
        '':
        tests.http_response_bytes(status='302 Found',
                                  headers={'location': '/second'},
                                  body=b'redirect body'),
    }
    with tests.server_route(routes, request_count=7) as uri:
        second_url = urllib.parse.urljoin(uri, '/second')
        final_url = urllib.parse.urljoin(uri, '/final')
        response1, content1 = http.request(second_url, 'GET')
        response2, content2 = http.request(second_url, 'GET')
        response3, content3 = http.request(uri, 'GET')
    assert response1.status == 200
    assert response1['content-location'] == final_url
    assert content1 == b'This is the final destination.\n'
    assert response1.previous.status == 302
    assert not response1.previous.fromcache

    assert response2.status == 200
    # FIXME:
    # assert response2.fromcache
    assert response2['content-location'] == final_url
    assert content2 == b'This is the final destination.\n'
    assert response2.previous.status == 302
    assert not response2.previous.fromcache
    assert response2.previous['content-location'] == second_url

    assert response3.status == 200
    # FIXME:
    # assert response3.fromcache
    assert content3 == b'This is the final destination.\n'
    assert response3.previous.status == 302
    assert not response3.previous.fromcache
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
def test_head_301():
    # Test that we automatically follow 301 redirects
    http = httplib2.Http()
    destination = ''
    routes = {
        '/final':
        tests.http_response_bytes(body=b'This is the final destination.\n'),
        '':
        tests.http_response_bytes(status='301 Now where did I leave that URL',
                                  headers={'location': '/final'},
                                  body=b'redirect body'),
    }
    with tests.server_route(routes, request_count=2) as uri:
        destination = urllib.parse.urljoin(uri, '/final')
        response, content = http.request(uri, 'HEAD')
    assert response.status == 200
    assert response['content-location'] == destination
    assert response.previous.status == 301
    assert not response.previous.fromcache
Exemple #28
0
def test_head_301():
    # Test that we automatically follow 301 redirects
    http = httplib2.Http()
    destination = ""
    routes = {
        "/final": tests.http_response_bytes(body=b"This is the final destination.\n"),
        "": tests.http_response_bytes(
            status="301 Now where did I leave that URL",
            headers={"location": "/final"},
            body=b"redirect body",
        ),
    }
    with tests.server_route(routes, request_count=2) as uri:
        destination = urllib.parse.urljoin(uri, "/final")
        response, content = http.request(uri, "HEAD")
    assert response.status == 200
    assert response["content-location"] == destination
    assert response.previous.status == 301
    assert not response.previous.fromcache
def test_head_301():
    # Test that we automatically follow 301 redirects
    http = httplib2.Http()
    destination = ""
    routes = {
        "/final": tests.http_response_bytes(body=b"This is the final destination.\n"),
        "": tests.http_response_bytes(
            status="301 Now where did I leave that URL",
            headers={"location": "/final"},
            body=b"redirect body",
        ),
    }
    with tests.server_route(routes, request_count=2) as uri:
        destination = urllib.parse.urljoin(uri, "/final")
        response, content = http.request(uri, "HEAD")
    assert response.status == 200
    assert response["content-location"] == destination
    assert response.previous.status == 301
    assert not response.previous.fromcache
Exemple #30
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 #31
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 #32
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 #33
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 #34
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 #35
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 #36
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 #37
0
def test_get_302():
    # Test that we automatically follow 302 redirects
    # and that we DO NOT cache the 302 response
    http = httplib2.Http(cache=tests.get_cache_path())
    second_url, final_url = "", ""
    routes = {
        "/final": tests.http_response_bytes(body=b"This is the final destination.\n"),
        "/second": tests.http_response_bytes(
            status="302 Found", headers={"location": "/final"}, body=b"second redirect"
        ),
        "": tests.http_response_bytes(
            status="302 Found", headers={"location": "/second"}, body=b"redirect body"
        ),
    }
    with tests.server_route(routes, request_count=7) as uri:
        second_url = urllib.parse.urljoin(uri, "/second")
        final_url = urllib.parse.urljoin(uri, "/final")
        response1, content1 = http.request(second_url, "GET")
        response2, content2 = http.request(second_url, "GET")
        response3, content3 = http.request(uri, "GET")
    assert response1.status == 200
    assert response1["content-location"] == final_url
    assert content1 == b"This is the final destination.\n"
    assert response1.previous.status == 302
    assert not response1.previous.fromcache

    assert response2.status == 200
    # FIXME:
    # assert response2.fromcache
    assert response2["content-location"] == final_url
    assert content2 == b"This is the final destination.\n"
    assert response2.previous.status == 302
    assert not response2.previous.fromcache
    assert response2.previous["content-location"] == second_url

    assert response3.status == 200
    # FIXME:
    # assert response3.fromcache
    assert content3 == b"This is the final destination.\n"
    assert response3.previous.status == 302
    assert not response3.previous.fromcache
Exemple #38
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""
def test_post_307():
    # 307: follow with same method
    http = httplib2.Http(cache=tests.get_cache_path(), timeout=1)
    http.follow_all_redirects = True
    r307 = tests.http_response_bytes(status=307, headers={"location": "/final"})
    r200 = tests.http_response_bytes(status=200, body=b"final content\n")

    with tests.server_list_http([r307, r200, r307, r200]) as uri:
        response, content = http.request(uri, "POST")
        assert response.previous.status == 307
        assert not response.previous.fromcache
        assert response.status == 200
        assert not response.fromcache
        assert content == b"final content\n"

        response, content = http.request(uri, "POST")
        assert response.previous.status == 307
        assert not response.previous.fromcache
        assert response.status == 200
        assert not response.fromcache
        assert content == b"final content\n"
Exemple #40
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 #41
0
def test_get_302():
    # Test that we automatically follow 302 redirects
    # and that we DO NOT cache the 302 response
    http = httplib2.Http(cache=tests.get_cache_path())
    second_url, final_url = '', ''
    routes = {
        '/final': tests.http_response_bytes(body=b'This is the final destination.\n'),
        '/second': tests.http_response_bytes(
            status='302 Found', headers={'location': '/final'}, body=b'second redirect'),
        '': tests.http_response_bytes(
            status='302 Found', headers={'location': '/second'}, body=b'redirect body'),
    }
    with tests.server_route(routes, request_count=7) as uri:
        second_url = urllib.parse.urljoin(uri, '/second')
        final_url = urllib.parse.urljoin(uri, '/final')
        response1, content1 = http.request(second_url, 'GET')
        response2, content2 = http.request(second_url, 'GET')
        response3, content3 = http.request(uri, 'GET')
    assert response1.status == 200
    assert response1['content-location'] == final_url
    assert content1 == b'This is the final destination.\n'
    assert response1.previous.status == 302
    assert not response1.previous.fromcache

    assert response2.status == 200
    # FIXME:
    # assert response2.fromcache
    assert response2['content-location'] == final_url
    assert content2 == b'This is the final destination.\n'
    assert response2.previous.status == 302
    assert not response2.previous.fromcache
    assert response2.previous['content-location'] == second_url

    assert response3.status == 200
    # FIXME:
    # assert response3.fromcache
    assert content3 == b'This is the final destination.\n'
    assert response3.previous.status == 302
    assert not response3.previous.fromcache
Exemple #42
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 #43
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 #44
0
def test_get_300_with_location():
    # Test the we automatically follow 300 redirects if a Location: header is provided
    http = httplib2.Http()
    final_content = b'This is the final destination.\n'
    routes = {
        '/final': tests.http_response_bytes(body=final_content),
        '': tests.http_response_bytes(status='300 Multiple Choices', headers={'location': '/final'}),
    }
    with tests.server_route(routes, request_count=2) as uri:
        response, content = http.request(uri, 'GET')
    assert response.status == 200
    assert content == final_content
    assert response.previous.status == 300
    assert not response.previous.fromcache

    # Confirm that the intermediate 300 is not cached
    with tests.server_route(routes, request_count=2) as uri:
        response, content = http.request(uri, 'GET')
    assert response.status == 200
    assert content == final_content
    assert response.previous.status == 300
    assert not response.previous.fromcache
Exemple #45
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 #46
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 #47
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 #48
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 #49
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 #50
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 #51
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 #54
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 #55
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 #56
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 #57
0
 def handler(request):
     if request.method in ('PUT', 'PATCH', 'DELETE'):
         return tests.http_response_bytes(status=405)
     return tests.http_response_bytes(
         add_date=True, add_etag=True, headers={'cache-control': 'max-age=300'})
Exemple #58
0
 def handler(request):
     if request.headers.get('range'):
         return tests.http_response_bytes(status=206, **response_kwargs)
     return tests.http_response_bytes(**response_kwargs)