tests/get-with-headers.py
author Dirkjan Ochtman <dirkjan@ochtman.nl>
Wed, 08 Sep 2010 15:11:35 +0200
changeset 12182 1121af239761
parent 10905 13a1b2fb7ef2
child 12250 bd98796c0b6f
permissions -rwxr-xr-x
tests: extend get-with-headers to support cache testing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2532
84655f721f39 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     1
#!/usr/bin/env python
84655f721f39 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     2
8447
d5ebcf8f6855 tests: fix doc string in get-with-headers.py
Martin Geisler <mg@lazybytes.net>
parents: 7544
diff changeset
     3
"""This does HTTP GET requests given a host:port and path and returns
2532
84655f721f39 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."""
84655f721f39 Add a test for getting raw files via the web UI.
Eric Hopper <hopper@omnifarious.org>
parents:
diff changeset
     5
10905
13a1b2fb7ef2 pylint, pyflakes: remove unused or duplicate imports
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 9722
diff changeset
     6
import httplib, sys
7054
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
     7
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
     8
try:
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
     9
    import msvcrt, os
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    10
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    11
    msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    12
except ImportError:
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    13
    pass
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    14
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    15
twice = False
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    16
if '--twice' in sys.argv:
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    17
    sys.argv.remove('--twice')
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    18
    twice = True
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    19
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    20
tag = None
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    21
def request(host, path, show):
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    22
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    23
    global tag
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    24
    headers = {}
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    25
    if tag:
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    26
        headers['If-None-Match'] = tag
5561
22713dce19f6 hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 2532
diff changeset
    27
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    28
    conn = httplib.HTTPConnection(host)
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    29
    conn.request("GET", path, None, headers)
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    30
    response = conn.getresponse()
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    31
    print response.status, response.reason
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    32
    for h in [h.lower() for h in show]:
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    33
        if response.getheader(h, None) is not None:
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    34
            print "%s: %s" % (h, response.getheader(h))
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    35
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    36
    print
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    37
    data = response.read()
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    38
    sys.stdout.write(data)
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    39
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    40
    if twice and response.getheader('ETag', None):
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    41
        tag = response.getheader('ETag')
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    42
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    43
    return response.status
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    44
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    45
status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    46
if twice:
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    47
    status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    48
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    49
if 200 <= status <= 305:
5561
22713dce19f6 hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 2532
diff changeset
    50
    sys.exit(0)
22713dce19f6 hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 2532
diff changeset
    51
sys.exit(1)