tests/get-with-headers.py
author Dirkjan Ochtman <dirkjan@ochtman.nl>
Wed, 08 Sep 2010 15:11:35 +0200
branchstable
changeset 1052 5a3bcda2de29
parent 1051 5b75af0ca224
child 1053 ede95d3a1f27
permissions -rwxr-xr-x
tests: extend get-with-headers to support cache testing [ 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
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    20
tag = None
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    21
def request(host, path, show):
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    22
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    23
    global tag
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    24
    headers = {}
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    25
    if tag:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    26
        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
    27
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    28
    conn = httplib.HTTPConnection(host)
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    29
    conn.request("GET", path, None, headers)
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    30
    response = conn.getresponse()
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    31
    print response.status, response.reason
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    32
    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
    33
        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
    34
            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
    35
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    36
    print
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    37
    data = response.read()
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    38
    sys.stdout.write(data)
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
    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
    41
        tag = response.getheader('ETag')
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    42
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    43
    return response.status
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
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
    46
if twice:
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
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    49
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
    50
    sys.exit(0)
06b69a53ab0f hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 513
diff changeset
    51
sys.exit(1)