tests/get-with-headers.py
author Jun Wu <quark@fb.com>
Wed, 30 Nov 2016 19:25:18 +0000
changeset 30556 c059286a0f9c
parent 29455 0c741fd6158a
child 30764 e75463e3179f
permissions -rwxr-xr-x
tests: replace "cp -r" with "cp -R" The POSIX documentation about "cp" [1] says: .... RATIONALE .... Earlier versions of this standard included support for the -r option to copy file hierarchies. The -r option is historical practice on BSD and BSD-derived systems. This option is no longer specified by POSIX.1-2008 but may be present in some implementations. The -R option was added as a close synonym to the -r option, selected for consistency with all other options in this volume of POSIX.1-2008 that do recursive directory descent. The difference between -R and the removed -r option is in the treatment by cp of file types other than regular and directory. It was implementation-defined how the - option treated special files to allow both historical implementations and those that chose to support -r with the same abilities as -R defined by this volume of POSIX.1-2008. The original -r flag, for historic reasons, did not handle special files any differently from regular files, but always read the file and copied its contents. This had obvious problems in the presence of special file types; for example, character devices, FIFOs, and sockets. .... .... Issue 6 The -r option is marked obsolescent. .... Issue 7 .... The obsolescent -r option is removed. .... (No "Issue 8" yet) Therefore it's clear that "cp -R" is strictly better than "cp -r". The issue was discovered when running tests on OS X after 0d87b1caed92. [1]: pubs.opengroup.org/onlinepubs/9699919799/utilities/cp.html
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
28726
f4b31fcd5e72 py3: use print_function in get-with-headers.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 27296
diff changeset
     6
from __future__ import absolute_import, print_function
27296
8e86679d8acd tests: use absolute_import in /get-with-headers.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25208
diff changeset
     7
8e86679d8acd tests: use absolute_import in /get-with-headers.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25208
diff changeset
     8
import json
8e86679d8acd tests: use absolute_import in /get-with-headers.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25208
diff changeset
     9
import os
8e86679d8acd tests: use absolute_import in /get-with-headers.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25208
diff changeset
    10
import sys
7054
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    11
29455
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28726
diff changeset
    12
from mercurial import (
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28726
diff changeset
    13
    util,
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28726
diff changeset
    14
)
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28726
diff changeset
    15
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28726
diff changeset
    16
httplib = util.httplib
0c741fd6158a py3: conditionalize httplib import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28726
diff changeset
    17
7054
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    18
try:
27296
8e86679d8acd tests: use absolute_import in /get-with-headers.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25208
diff changeset
    19
    import msvcrt
7054
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    20
    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
    21
    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
    22
except ImportError:
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    23
    pass
e837f2294643 get-with-headers: fix stream modes under Windows
Patrick Mezard <pmezard@gmail.com>
parents: 5561
diff changeset
    24
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    25
twice = False
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    26
if '--twice' in sys.argv:
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    27
    sys.argv.remove('--twice')
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    28
    twice = True
18400
f1118507174b get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18393
diff changeset
    29
headeronly = False
f1118507174b get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18393
diff changeset
    30
if '--headeronly' in sys.argv:
f1118507174b get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18393
diff changeset
    31
    sys.argv.remove('--headeronly')
f1118507174b get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18393
diff changeset
    32
    headeronly = True
24543
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    33
formatjson = False
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    34
if '--json' in sys.argv:
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    35
    sys.argv.remove('--json')
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    36
    formatjson = True
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    37
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    38
tag = None
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    39
def request(host, path, show):
17017
953faba28e91 tests: prepare get-with-headers.py for MSYS
Mads Kiilerich <mads@kiilerich.com>
parents: 12250
diff changeset
    40
    assert not path.startswith('/'), path
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    41
    global tag
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    42
    headers = {}
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    43
    if tag:
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    44
        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
    45
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    46
    conn = httplib.HTTPConnection(host)
17017
953faba28e91 tests: prepare get-with-headers.py for MSYS
Mads Kiilerich <mads@kiilerich.com>
parents: 12250
diff changeset
    47
    conn.request("GET", '/' + path, None, headers)
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    48
    response = conn.getresponse()
28726
f4b31fcd5e72 py3: use print_function in get-with-headers.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 27296
diff changeset
    49
    print(response.status, response.reason)
18380
a4d7fd7ad1f7 serve: don't send any content headers with 304 responses
Mads Kiilerich <madski@unity3d.com>
parents: 17017
diff changeset
    50
    if show[:1] == ['-']:
18393
a38039ef7312 tests: make test-hgweb.t output stable
Mads Kiilerich <madski@unity3d.com>
parents: 18380
diff changeset
    51
        show = sorted(h for h, v in response.getheaders()
a38039ef7312 tests: make test-hgweb.t output stable
Mads Kiilerich <madski@unity3d.com>
parents: 18380
diff changeset
    52
                      if h.lower() not in show)
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    53
    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
    54
        if response.getheader(h, None) is not None:
28726
f4b31fcd5e72 py3: use print_function in get-with-headers.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 27296
diff changeset
    55
            print("%s: %s" % (h, response.getheader(h)))
18400
f1118507174b get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18393
diff changeset
    56
    if not headeronly:
28726
f4b31fcd5e72 py3: use print_function in get-with-headers.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 27296
diff changeset
    57
        print()
23409
dc4d2cd3aa3e hgweb: send proper HTTP response after uncaught exception
Gregory Szorc <gregory.szorc@gmail.com>
parents: 19865
diff changeset
    58
        data = response.read()
24543
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    59
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    60
        # Pretty print JSON. This also has the beneficial side-effect
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    61
        # of verifying emitted JSON is well-formed.
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    62
        if formatjson:
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    63
            # json.dumps() will print trailing newlines. Eliminate them
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    64
            # to make tests easier to write.
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    65
            data = json.loads(data)
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    66
            lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    67
            for line in lines:
28726
f4b31fcd5e72 py3: use print_function in get-with-headers.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 27296
diff changeset
    68
                print(line.rstrip())
24543
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    69
        else:
747401086a38 get-with-headers: support parsing and pretty printing JSON
Gregory Szorc <gregory.szorc@gmail.com>
parents: 23409
diff changeset
    70
            sys.stdout.write(data)
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    71
18400
f1118507174b get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18393
diff changeset
    72
        if twice and response.getheader('ETag', None):
f1118507174b get-with-headers: add a --headeronly switch
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 18393
diff changeset
    73
            tag = response.getheader('ETag')
12182
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    74
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    75
    return response.status
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    76
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    77
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
    78
if twice:
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    79
    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
    80
1121af239761 tests: extend get-with-headers to support cache testing
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 10905
diff changeset
    81
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
    82
    sys.exit(0)
22713dce19f6 hgweb: return meaningful HTTP status codes instead of nonsense
Bryan O'Sullivan <bos@serpentine.com>
parents: 2532
diff changeset
    83
sys.exit(1)