Show More
@@ -0,0 +1,102 b'' | |||
|
1 | #require serve | |
|
2 | ||
|
3 | Some tests for hgweb responding to HEAD requests | |
|
4 | ||
|
5 | $ hg init test | |
|
6 | $ cd test | |
|
7 | $ mkdir da | |
|
8 | $ echo foo > da/foo | |
|
9 | $ echo foo > foo | |
|
10 | $ hg ci -Ambase | |
|
11 | adding da/foo | |
|
12 | adding foo | |
|
13 | $ hg bookmark -r0 '@' | |
|
14 | $ hg bookmark -r0 'a b c' | |
|
15 | $ hg bookmark -r0 'd/e/f' | |
|
16 | $ hg serve -n test -p $HGPORT -d --pid-file=hg.pid -A access.log -E errors.log | |
|
17 | $ cat hg.pid >> $DAEMON_PIDS | |
|
18 | ||
|
19 | manifest | |
|
20 | ||
|
21 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'file/tip/?style=raw' - date etag server | |
|
22 | 200 Script output follows | |
|
23 | content-type: text/plain; charset=ascii | |
|
24 | ||
|
25 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'file/tip/da?style=raw' - date etag server | |
|
26 | 200 Script output follows | |
|
27 | content-type: text/plain; charset=ascii | |
|
28 | ||
|
29 | ||
|
30 | plain file | |
|
31 | ||
|
32 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'file/tip/foo?style=raw' - date etag server | |
|
33 | 200 Script output follows | |
|
34 | content-disposition: inline; filename="foo" | |
|
35 | content-length: 4 | |
|
36 | content-type: application/binary | |
|
37 | ||
|
38 | ||
|
39 | should give a 404 - static file that does not exist | |
|
40 | ||
|
41 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'static/bogus' - date etag server | |
|
42 | 404 Not Found | |
|
43 | content-type: text/html; charset=ascii | |
|
44 | ||
|
45 | [1] | |
|
46 | ||
|
47 | should give a 404 - bad revision | |
|
48 | ||
|
49 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'file/spam/foo?style=raw' - date etag server | |
|
50 | 404 Not Found | |
|
51 | content-type: text/plain; charset=ascii | |
|
52 | ||
|
53 | [1] | |
|
54 | ||
|
55 | should give a 400 - bad command | |
|
56 | ||
|
57 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'file/tip/foo?cmd=spam&style=raw' - date etag server | |
|
58 | 400* (glob) | |
|
59 | content-type: text/plain; charset=ascii | |
|
60 | ||
|
61 | [1] | |
|
62 | ||
|
63 | should give a 404 - file does not exist | |
|
64 | ||
|
65 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'file/tip/bork?style=raw' - date etag server | |
|
66 | 404 Not Found | |
|
67 | content-type: text/plain; charset=ascii | |
|
68 | ||
|
69 | [1] | |
|
70 | ||
|
71 | try bad style | |
|
72 | ||
|
73 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'file/tip/?style=foobar' - date etag server | |
|
74 | 200 Script output follows | |
|
75 | content-type: text/html; charset=ascii | |
|
76 | ||
|
77 | ||
|
78 | log | |
|
79 | ||
|
80 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'log?style=raw' - date etag server | |
|
81 | 200 Script output follows | |
|
82 | content-type: text/plain; charset=ascii | |
|
83 | ||
|
84 | ||
|
85 | access bookmarks | |
|
86 | ||
|
87 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'rev/@?style=paper' - date etag server | |
|
88 | 200 Script output follows | |
|
89 | content-type: text/html; charset=ascii | |
|
90 | ||
|
91 | ||
|
92 | static file | |
|
93 | ||
|
94 | $ get-with-headers.py localhost:$HGPORT --method=HEAD 'static/style-gitweb.css' - date etag server | |
|
95 | 200 Script output follows | |
|
96 | content-length: 9074 | |
|
97 | content-type: text/css | |
|
98 | ||
|
99 | ||
|
100 | $ killdaemons.py | |
|
101 | ||
|
102 | $ cd .. |
@@ -1,130 +1,131 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | |
|
3 |
"""This does HTTP |
|
|
4 | a subset of the headers plus the body of the result.""" | |
|
3 | """This does HTTP requests (GET by default) given a host:port and path and | |
|
4 | returns a subset of the headers plus the body of the result.""" | |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | import argparse |
|
8 | 8 | import json |
|
9 | 9 | import os |
|
10 | 10 | import sys |
|
11 | 11 | |
|
12 | 12 | from mercurial import ( |
|
13 | 13 | pycompat, |
|
14 | 14 | util, |
|
15 | 15 | ) |
|
16 | 16 | |
|
17 | 17 | httplib = util.httplib |
|
18 | 18 | |
|
19 | 19 | try: |
|
20 | 20 | import msvcrt |
|
21 | 21 | |
|
22 | 22 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
|
23 | 23 | msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
|
24 | 24 | except ImportError: |
|
25 | 25 | pass |
|
26 | 26 | |
|
27 | 27 | stdout = getattr(sys.stdout, 'buffer', sys.stdout) |
|
28 | 28 | |
|
29 | 29 | parser = argparse.ArgumentParser() |
|
30 | 30 | parser.add_argument('--twice', action='store_true') |
|
31 | 31 | parser.add_argument('--headeronly', action='store_true') |
|
32 | 32 | parser.add_argument('--json', action='store_true') |
|
33 | 33 | parser.add_argument('--hgproto') |
|
34 | 34 | parser.add_argument( |
|
35 | 35 | '--requestheader', |
|
36 | 36 | nargs='*', |
|
37 | 37 | default=[], |
|
38 | 38 | help='Send an additional HTTP request header. Argument ' |
|
39 | 39 | 'value is <header>=<value>', |
|
40 | 40 | ) |
|
41 | 41 | parser.add_argument('--bodyfile', help='Write HTTP response body to a file') |
|
42 | parser.add_argument('--method', default='GET', help='HTTP method to use') | |
|
42 | 43 | parser.add_argument('host') |
|
43 | 44 | parser.add_argument('path') |
|
44 | 45 | parser.add_argument('show', nargs='*') |
|
45 | 46 | |
|
46 | 47 | args = parser.parse_args() |
|
47 | 48 | |
|
48 | 49 | twice = args.twice |
|
49 | 50 | headeronly = args.headeronly |
|
50 | 51 | formatjson = args.json |
|
51 | 52 | hgproto = args.hgproto |
|
52 | 53 | requestheaders = args.requestheader |
|
53 | 54 | |
|
54 | 55 | tag = None |
|
55 | 56 | |
|
56 | 57 | |
|
57 | def request(host, path, show): | |
|
58 | def request(method, host, path, show): | |
|
58 | 59 | assert not path.startswith('/'), path |
|
59 | 60 | global tag |
|
60 | 61 | headers = {} |
|
61 | 62 | if tag: |
|
62 | 63 | headers['If-None-Match'] = tag |
|
63 | 64 | if hgproto: |
|
64 | 65 | headers['X-HgProto-1'] = hgproto |
|
65 | 66 | |
|
66 | 67 | for header in requestheaders: |
|
67 | 68 | key, value = header.split('=', 1) |
|
68 | 69 | headers[key] = value |
|
69 | 70 | |
|
70 | 71 | conn = httplib.HTTPConnection(host) |
|
71 |
conn.request( |
|
|
72 | conn.request(method, '/' + path, None, headers) | |
|
72 | 73 | response = conn.getresponse() |
|
73 | 74 | stdout.write( |
|
74 | 75 | b'%d %s\n' % (response.status, response.reason.encode('ascii')) |
|
75 | 76 | ) |
|
76 | 77 | if show[:1] == ['-']: |
|
77 | 78 | show = sorted( |
|
78 | 79 | h for h, v in response.getheaders() if h.lower() not in show |
|
79 | 80 | ) |
|
80 | 81 | for h in [h.lower() for h in show]: |
|
81 | 82 | if response.getheader(h, None) is not None: |
|
82 | 83 | stdout.write( |
|
83 | 84 | b"%s: %s\n" |
|
84 | 85 | % (h.encode('ascii'), response.getheader(h).encode('ascii')) |
|
85 | 86 | ) |
|
86 | 87 | if headeronly: |
|
87 | 88 | # still read the body to prevent windows to be unhappy about that |
|
88 | 89 | # (this might some flakyness in test-hgweb-filelog.t on Windows) |
|
89 | 90 | data = response.read() |
|
90 | 91 | else: |
|
91 | 92 | stdout.write(b'\n') |
|
92 | 93 | data = response.read() |
|
93 | 94 | |
|
94 | 95 | if args.bodyfile: |
|
95 | 96 | bodyfh = open(args.bodyfile, 'wb') |
|
96 | 97 | else: |
|
97 | 98 | bodyfh = stdout |
|
98 | 99 | |
|
99 | 100 | # Pretty print JSON. This also has the beneficial side-effect |
|
100 | 101 | # of verifying emitted JSON is well-formed. |
|
101 | 102 | if formatjson: |
|
102 | 103 | # json.dumps() will print trailing newlines. Eliminate them |
|
103 | 104 | # to make tests easier to write. |
|
104 | 105 | data = pycompat.json_loads(data) |
|
105 | 106 | lines = json.dumps(data, sort_keys=True, indent=2).splitlines() |
|
106 | 107 | for line in lines: |
|
107 | 108 | bodyfh.write(pycompat.sysbytes(line.rstrip())) |
|
108 | 109 | bodyfh.write(b'\n') |
|
109 | 110 | else: |
|
110 | 111 | bodyfh.write(data) |
|
111 | 112 | |
|
112 | 113 | if args.bodyfile: |
|
113 | 114 | bodyfh.close() |
|
114 | 115 | |
|
115 | 116 | if twice and response.getheader('ETag', None): |
|
116 | 117 | tag = response.getheader('ETag') |
|
117 | 118 | |
|
118 | 119 | # further try to please the windows-flakyness deity |
|
119 | 120 | conn.close() |
|
120 | 121 | |
|
121 | 122 | return response.status |
|
122 | 123 | |
|
123 | 124 | |
|
124 | status = request(args.host, args.path, args.show) | |
|
125 | status = request(args.method, args.host, args.path, args.show) | |
|
125 | 126 | if twice: |
|
126 | status = request(args.host, args.path, args.show) | |
|
127 | status = request(args.method, args.host, args.path, args.show) | |
|
127 | 128 | |
|
128 | 129 | if 200 <= status <= 305: |
|
129 | 130 | sys.exit(0) |
|
130 | 131 | sys.exit(1) |
General Comments 0
You need to be logged in to leave comments.
Login now