tests/get-with-headers.py
author Bryan O'Sullivan <bryano@fb.com>
Tue, 27 Nov 2012 11:18:31 -0800
branchstable
changeset 1164 8ed9a9f075f9
parent 1106 a02b1025536c
child 1199 b9f44c378e62
permissions -rwxr-xr-x
run-tests: make build command line less intimidating Use a dict for parameters to the format string, instead of a ridiculous number of positional parameters. [ 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)
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    34
    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
    35
        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
    36
            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
    37
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    38
    print
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    39
    data = response.read()
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    40
    sys.stdout.write(data)
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    41
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    42
    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
    43
        tag = response.getheader('ETag')
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    44
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    45
    return response.status
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
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
    48
if twice:
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
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    51
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
    52
    sys.exit(0)
06b69a53ab0f hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 513
diff changeset
    53
sys.exit(1)