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 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 |
|
2 | |||
3 |
"""This does HTTP |
|
3 | """This does HTTP requests (GET by default) given a host:port and path and | |
4 | a subset of the headers plus the body of the result.""" |
|
4 | returns a subset of the headers plus the body of the result.""" | |
5 |
|
5 | |||
6 |
|
6 | |||
7 | import argparse |
|
7 | import argparse | |
8 | import json |
|
8 | import json | |
9 | import os |
|
9 | import os | |
10 | import sys |
|
10 | import sys | |
11 |
|
11 | |||
12 | from mercurial import ( |
|
12 | from mercurial import ( | |
13 | pycompat, |
|
13 | pycompat, | |
14 | util, |
|
14 | util, | |
15 | ) |
|
15 | ) | |
16 |
|
16 | |||
17 | httplib = util.httplib |
|
17 | httplib = util.httplib | |
18 |
|
18 | |||
19 | try: |
|
19 | try: | |
20 | import msvcrt |
|
20 | import msvcrt | |
21 |
|
21 | |||
22 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) |
|
22 | msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) | |
23 | msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) |
|
23 | msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY) | |
24 | except ImportError: |
|
24 | except ImportError: | |
25 | pass |
|
25 | pass | |
26 |
|
26 | |||
27 | stdout = getattr(sys.stdout, 'buffer', sys.stdout) |
|
27 | stdout = getattr(sys.stdout, 'buffer', sys.stdout) | |
28 |
|
28 | |||
29 | parser = argparse.ArgumentParser() |
|
29 | parser = argparse.ArgumentParser() | |
30 | parser.add_argument('--twice', action='store_true') |
|
30 | parser.add_argument('--twice', action='store_true') | |
31 | parser.add_argument('--headeronly', action='store_true') |
|
31 | parser.add_argument('--headeronly', action='store_true') | |
32 | parser.add_argument('--json', action='store_true') |
|
32 | parser.add_argument('--json', action='store_true') | |
33 | parser.add_argument('--hgproto') |
|
33 | parser.add_argument('--hgproto') | |
34 | parser.add_argument( |
|
34 | parser.add_argument( | |
35 | '--requestheader', |
|
35 | '--requestheader', | |
36 | nargs='*', |
|
36 | nargs='*', | |
37 | default=[], |
|
37 | default=[], | |
38 | help='Send an additional HTTP request header. Argument ' |
|
38 | help='Send an additional HTTP request header. Argument ' | |
39 | 'value is <header>=<value>', |
|
39 | 'value is <header>=<value>', | |
40 | ) |
|
40 | ) | |
41 | parser.add_argument('--bodyfile', help='Write HTTP response body to a file') |
|
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 | parser.add_argument('host') |
|
43 | parser.add_argument('host') | |
43 | parser.add_argument('path') |
|
44 | parser.add_argument('path') | |
44 | parser.add_argument('show', nargs='*') |
|
45 | parser.add_argument('show', nargs='*') | |
45 |
|
46 | |||
46 | args = parser.parse_args() |
|
47 | args = parser.parse_args() | |
47 |
|
48 | |||
48 | twice = args.twice |
|
49 | twice = args.twice | |
49 | headeronly = args.headeronly |
|
50 | headeronly = args.headeronly | |
50 | formatjson = args.json |
|
51 | formatjson = args.json | |
51 | hgproto = args.hgproto |
|
52 | hgproto = args.hgproto | |
52 | requestheaders = args.requestheader |
|
53 | requestheaders = args.requestheader | |
53 |
|
54 | |||
54 | tag = None |
|
55 | tag = None | |
55 |
|
56 | |||
56 |
|
57 | |||
57 | def request(host, path, show): |
|
58 | def request(method, host, path, show): | |
58 | assert not path.startswith('/'), path |
|
59 | assert not path.startswith('/'), path | |
59 | global tag |
|
60 | global tag | |
60 | headers = {} |
|
61 | headers = {} | |
61 | if tag: |
|
62 | if tag: | |
62 | headers['If-None-Match'] = tag |
|
63 | headers['If-None-Match'] = tag | |
63 | if hgproto: |
|
64 | if hgproto: | |
64 | headers['X-HgProto-1'] = hgproto |
|
65 | headers['X-HgProto-1'] = hgproto | |
65 |
|
66 | |||
66 | for header in requestheaders: |
|
67 | for header in requestheaders: | |
67 | key, value = header.split('=', 1) |
|
68 | key, value = header.split('=', 1) | |
68 | headers[key] = value |
|
69 | headers[key] = value | |
69 |
|
70 | |||
70 | conn = httplib.HTTPConnection(host) |
|
71 | conn = httplib.HTTPConnection(host) | |
71 |
conn.request( |
|
72 | conn.request(method, '/' + path, None, headers) | |
72 | response = conn.getresponse() |
|
73 | response = conn.getresponse() | |
73 | stdout.write( |
|
74 | stdout.write( | |
74 | b'%d %s\n' % (response.status, response.reason.encode('ascii')) |
|
75 | b'%d %s\n' % (response.status, response.reason.encode('ascii')) | |
75 | ) |
|
76 | ) | |
76 | if show[:1] == ['-']: |
|
77 | if show[:1] == ['-']: | |
77 | show = sorted( |
|
78 | show = sorted( | |
78 | h for h, v in response.getheaders() if h.lower() not in show |
|
79 | h for h, v in response.getheaders() if h.lower() not in show | |
79 | ) |
|
80 | ) | |
80 | for h in [h.lower() for h in show]: |
|
81 | for h in [h.lower() for h in show]: | |
81 | if response.getheader(h, None) is not None: |
|
82 | if response.getheader(h, None) is not None: | |
82 | stdout.write( |
|
83 | stdout.write( | |
83 | b"%s: %s\n" |
|
84 | b"%s: %s\n" | |
84 | % (h.encode('ascii'), response.getheader(h).encode('ascii')) |
|
85 | % (h.encode('ascii'), response.getheader(h).encode('ascii')) | |
85 | ) |
|
86 | ) | |
86 | if headeronly: |
|
87 | if headeronly: | |
87 | # still read the body to prevent windows to be unhappy about that |
|
88 | # still read the body to prevent windows to be unhappy about that | |
88 | # (this might some flakyness in test-hgweb-filelog.t on Windows) |
|
89 | # (this might some flakyness in test-hgweb-filelog.t on Windows) | |
89 | data = response.read() |
|
90 | data = response.read() | |
90 | else: |
|
91 | else: | |
91 | stdout.write(b'\n') |
|
92 | stdout.write(b'\n') | |
92 | data = response.read() |
|
93 | data = response.read() | |
93 |
|
94 | |||
94 | if args.bodyfile: |
|
95 | if args.bodyfile: | |
95 | bodyfh = open(args.bodyfile, 'wb') |
|
96 | bodyfh = open(args.bodyfile, 'wb') | |
96 | else: |
|
97 | else: | |
97 | bodyfh = stdout |
|
98 | bodyfh = stdout | |
98 |
|
99 | |||
99 | # Pretty print JSON. This also has the beneficial side-effect |
|
100 | # Pretty print JSON. This also has the beneficial side-effect | |
100 | # of verifying emitted JSON is well-formed. |
|
101 | # of verifying emitted JSON is well-formed. | |
101 | if formatjson: |
|
102 | if formatjson: | |
102 | # json.dumps() will print trailing newlines. Eliminate them |
|
103 | # json.dumps() will print trailing newlines. Eliminate them | |
103 | # to make tests easier to write. |
|
104 | # to make tests easier to write. | |
104 | data = pycompat.json_loads(data) |
|
105 | data = pycompat.json_loads(data) | |
105 | lines = json.dumps(data, sort_keys=True, indent=2).splitlines() |
|
106 | lines = json.dumps(data, sort_keys=True, indent=2).splitlines() | |
106 | for line in lines: |
|
107 | for line in lines: | |
107 | bodyfh.write(pycompat.sysbytes(line.rstrip())) |
|
108 | bodyfh.write(pycompat.sysbytes(line.rstrip())) | |
108 | bodyfh.write(b'\n') |
|
109 | bodyfh.write(b'\n') | |
109 | else: |
|
110 | else: | |
110 | bodyfh.write(data) |
|
111 | bodyfh.write(data) | |
111 |
|
112 | |||
112 | if args.bodyfile: |
|
113 | if args.bodyfile: | |
113 | bodyfh.close() |
|
114 | bodyfh.close() | |
114 |
|
115 | |||
115 | if twice and response.getheader('ETag', None): |
|
116 | if twice and response.getheader('ETag', None): | |
116 | tag = response.getheader('ETag') |
|
117 | tag = response.getheader('ETag') | |
117 |
|
118 | |||
118 | # further try to please the windows-flakyness deity |
|
119 | # further try to please the windows-flakyness deity | |
119 | conn.close() |
|
120 | conn.close() | |
120 |
|
121 | |||
121 | return response.status |
|
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 | if twice: |
|
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 | if 200 <= status <= 305: |
|
129 | if 200 <= status <= 305: | |
129 | sys.exit(0) |
|
130 | sys.exit(0) | |
130 | sys.exit(1) |
|
131 | sys.exit(1) |
General Comments 0
You need to be logged in to leave comments.
Login now