tests/get-with-headers.py
author Mads Kiilerich <madski@unity3d.com>
Tue, 15 Jan 2013 20:54:57 +0100
branchstable
changeset 1199 b9f44c378e62
parent 1106 a02b1025536c
child 1200 1a67e2797f33
permissions -rwxr-xr-x
serve: don't send any content headers with 304 responses Fixes HTTP protocol violation introduced in cf5c76017e11. 'hg serve' would show a stacktrace when loading pages that not had been modified. There was test coverage for this, but the wrong response headers wasn't shown and thus not detected. [ original upstream message ]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
513
90b51a0fe1f0 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     1
#!/usr/bin/env python
90b51a0fe1f0 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     2
567
fd52c78e1aa7 tests: fix doc string in get-with-headers.py
Martin Geisler <mg@lazybytes.net>
parents: 566
diff changeset
     3
"""This does HTTP GET requests given a host:port and path and returns
513
90b51a0fe1f0 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     4
a subset of the headers plus the body of the result."""
90b51a0fe1f0 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     5
1051
5b75af0ca224 pylint, pyflakes: remove unused or duplicate imports
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 717
diff changeset
     6
import httplib, sys
515
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
     7
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
     8
try:
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
     9
    import msvcrt, os
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    10
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    11
    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    12
except ImportError:
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    13
    pass
9ea5ac3258b6 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 514
diff changeset
    14
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    15
twice = False
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    16
if '--twice' in sys.argv:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    17
    sys.argv.remove('--twice')
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    18
    twice = True
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    19
1053
ede95d3a1f27 tests: fix incompatibility with python-2.4 in test-hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1052
diff changeset
    20
reasons = {'Not modified': 'Not Modified'} # python 2.4
ede95d3a1f27 tests: fix incompatibility with python-2.4 in test-hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1052
diff changeset
    21
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    22
tag = None
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    23
def request(host, path, show):
1106
a02b1025536c tests: prepare get-with-headers.py for MSYS
Mads Kiilerich <mads@kiilerich.com>
parents: 1053
diff changeset
    24
    assert not path.startswith('/'), path
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    25
    global tag
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    26
    headers = {}
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    27
    if tag:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    28
        headers['If-None-Match'] = tag
514
06b69a53ab0f hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 513
diff changeset
    29
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    30
    conn = httplib.HTTPConnection(host)
1106
a02b1025536c tests: prepare get-with-headers.py for MSYS
Mads Kiilerich <mads@kiilerich.com>
parents: 1053
diff changeset
    31
    conn.request("GET", '/' + path, None, headers)
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    32
    response = conn.getresponse()
1053
ede95d3a1f27 tests: fix incompatibility with python-2.4 in test-hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1052
diff changeset
    33
    print response.status, reasons.get(response.reason, response.reason)
1199
b9f44c378e62 serve: don't send any content headers with 304 responses
Mads Kiilerich <madski@unity3d.com>
parents: 1106
diff changeset
    34
    if show[:1] == ['-']:
b9f44c378e62 serve: don't send any content headers with 304 responses
Mads Kiilerich <madski@unity3d.com>
parents: 1106
diff changeset
    35
        show = [h for h, v in response.getheaders() if h.lower() not in show]
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    36
    for h in [h.lower() for h in show]:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    37
        if response.getheader(h, None) is not None:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    38
            print "%s: %s" % (h, response.getheader(h))
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    39
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    40
    print
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    41
    data = response.read()
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    42
    sys.stdout.write(data)
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    43
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    44
    if twice and response.getheader('ETag', None):
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    45
        tag = response.getheader('ETag')
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    46
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    47
    return response.status
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    48
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    49
status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    50
if twice:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    51
    status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    52
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    53
if 200 <= status <= 305:
514
06b69a53ab0f hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 513
diff changeset
    54
    sys.exit(0)
06b69a53ab0f hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 513
diff changeset
    55
sys.exit(1)