tests/get-with-headers.py
author Augie Fackler <raf@durin42.com>
Thu, 19 Sep 2013 15:29:04 -0400
branchstable
changeset 1292 aa3c5b7a290e
parent 1201 6f2f7246e4be
child 1297 60e578eae19d
permissions -rwxr-xr-x
run-tests: if run by python3, execute setup.py with --c2to3 [ 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
1201
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    19
headeronly = False
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    20
if '--headeronly' in sys.argv:
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    21
    sys.argv.remove('--headeronly')
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    22
    headeronly = True
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    23
1053
ede95d3a1f27 tests: fix incompatibility with python-2.4 in test-hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1052
diff changeset
    24
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
    25
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    26
tag = None
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    27
def request(host, path, show):
1106
a02b1025536c tests: prepare get-with-headers.py for MSYS
Mads Kiilerich <mads@kiilerich.com>
parents: 1053
diff changeset
    28
    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
    29
    global tag
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    30
    headers = {}
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    31
    if tag:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    32
        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
    33
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    34
    conn = httplib.HTTPConnection(host)
1106
a02b1025536c tests: prepare get-with-headers.py for MSYS
Mads Kiilerich <mads@kiilerich.com>
parents: 1053
diff changeset
    35
    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
    36
    response = conn.getresponse()
1053
ede95d3a1f27 tests: fix incompatibility with python-2.4 in test-hgweb
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1052
diff changeset
    37
    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
    38
    if show[:1] == ['-']:
1200
1a67e2797f33 tests: make test-hgweb.t output stable
Mads Kiilerich <madski@unity3d.com>
parents: 1199
diff changeset
    39
        show = sorted(h for h, v in response.getheaders()
1a67e2797f33 tests: make test-hgweb.t output stable
Mads Kiilerich <madski@unity3d.com>
parents: 1199
diff changeset
    40
                      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
    41
    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
    42
        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
    43
            print "%s: %s" % (h, response.getheader(h))
1201
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    44
    if not headeronly:
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    45
        print
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    46
        data = response.read()
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    47
        sys.stdout.write(data)
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    48
1201
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    49
        if twice and response.getheader('ETag', None):
6f2f7246e4be get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 1200
diff changeset
    50
            tag = response.getheader('ETag')
1052
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    51
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    52
    return response.status
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    53
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    54
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
    55
if twice:
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    56
    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
    57
5a3bcda2de29 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 1051
diff changeset
    58
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
    59
    sys.exit(0)
06b69a53ab0f hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 513
diff changeset
    60
sys.exit(1)