##// END OF EJS Templates
tests: fix get-with-headers.py on python3 when writing to stdout...
Augie Fackler -
r36290:c95c8ab2 default
parent child Browse files
Show More
@@ -1,108 +1,108
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2
2
3 """This does HTTP GET requests given a host:port and path and returns
3 """This does HTTP GET requests given a host:port and path and returns
4 a subset of the headers plus the body of the result."""
4 a subset of the headers plus the body of the result."""
5
5
6 from __future__ import absolute_import, print_function
6 from __future__ import absolute_import, print_function
7
7
8 import argparse
8 import argparse
9 import json
9 import json
10 import os
10 import os
11 import sys
11 import sys
12
12
13 from mercurial import (
13 from mercurial import (
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 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
21 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
22 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
22 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
23 except ImportError:
23 except ImportError:
24 pass
24 pass
25
25
26 parser = argparse.ArgumentParser()
26 parser = argparse.ArgumentParser()
27 parser.add_argument('--twice', action='store_true')
27 parser.add_argument('--twice', action='store_true')
28 parser.add_argument('--headeronly', action='store_true')
28 parser.add_argument('--headeronly', action='store_true')
29 parser.add_argument('--json', action='store_true')
29 parser.add_argument('--json', action='store_true')
30 parser.add_argument('--hgproto')
30 parser.add_argument('--hgproto')
31 parser.add_argument('--requestheader', nargs='*', default=[],
31 parser.add_argument('--requestheader', nargs='*', default=[],
32 help='Send an additional HTTP request header. Argument '
32 help='Send an additional HTTP request header. Argument '
33 'value is <header>=<value>')
33 'value is <header>=<value>')
34 parser.add_argument('--bodyfile',
34 parser.add_argument('--bodyfile',
35 help='Write HTTP response body to a file')
35 help='Write HTTP response body to a file')
36 parser.add_argument('host')
36 parser.add_argument('host')
37 parser.add_argument('path')
37 parser.add_argument('path')
38 parser.add_argument('show', nargs='*')
38 parser.add_argument('show', nargs='*')
39
39
40 args = parser.parse_args()
40 args = parser.parse_args()
41
41
42 twice = args.twice
42 twice = args.twice
43 headeronly = args.headeronly
43 headeronly = args.headeronly
44 formatjson = args.json
44 formatjson = args.json
45 hgproto = args.hgproto
45 hgproto = args.hgproto
46 requestheaders = args.requestheader
46 requestheaders = args.requestheader
47
47
48 tag = None
48 tag = None
49 def request(host, path, show):
49 def request(host, path, show):
50 assert not path.startswith('/'), path
50 assert not path.startswith('/'), path
51 global tag
51 global tag
52 headers = {}
52 headers = {}
53 if tag:
53 if tag:
54 headers['If-None-Match'] = tag
54 headers['If-None-Match'] = tag
55 if hgproto:
55 if hgproto:
56 headers['X-HgProto-1'] = hgproto
56 headers['X-HgProto-1'] = hgproto
57
57
58 for header in requestheaders:
58 for header in requestheaders:
59 key, value = header.split('=', 1)
59 key, value = header.split('=', 1)
60 headers[key] = value
60 headers[key] = value
61
61
62 conn = httplib.HTTPConnection(host)
62 conn = httplib.HTTPConnection(host)
63 conn.request("GET", '/' + path, None, headers)
63 conn.request("GET", '/' + path, None, headers)
64 response = conn.getresponse()
64 response = conn.getresponse()
65 print(response.status, response.reason)
65 print(response.status, response.reason)
66 if show[:1] == ['-']:
66 if show[:1] == ['-']:
67 show = sorted(h for h, v in response.getheaders()
67 show = sorted(h for h, v in response.getheaders()
68 if h.lower() not in show)
68 if h.lower() not in show)
69 for h in [h.lower() for h in show]:
69 for h in [h.lower() for h in show]:
70 if response.getheader(h, None) is not None:
70 if response.getheader(h, None) is not None:
71 print("%s: %s" % (h, response.getheader(h)))
71 print("%s: %s" % (h, response.getheader(h)))
72 if not headeronly:
72 if not headeronly:
73 print()
73 print()
74 data = response.read()
74 data = response.read()
75
75
76 if args.bodyfile:
76 if args.bodyfile:
77 bodyfh = open(args.bodyfile, 'wb')
77 bodyfh = open(args.bodyfile, 'wb')
78 else:
78 else:
79 bodyfh = sys.stdout
79 bodyfh = getattr(sys.stdout, 'buffer', sys.stdout)
80
80
81 # Pretty print JSON. This also has the beneficial side-effect
81 # Pretty print JSON. This also has the beneficial side-effect
82 # of verifying emitted JSON is well-formed.
82 # of verifying emitted JSON is well-formed.
83 if formatjson:
83 if formatjson:
84 # json.dumps() will print trailing newlines. Eliminate them
84 # json.dumps() will print trailing newlines. Eliminate them
85 # to make tests easier to write.
85 # to make tests easier to write.
86 data = json.loads(data)
86 data = json.loads(data)
87 lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
87 lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
88 for line in lines:
88 for line in lines:
89 bodyfh.write(line.rstrip())
89 bodyfh.write(line.rstrip())
90 bodyfh.write(b'\n')
90 bodyfh.write(b'\n')
91 else:
91 else:
92 bodyfh.write(data)
92 bodyfh.write(data)
93
93
94 if args.bodyfile:
94 if args.bodyfile:
95 bodyfh.close()
95 bodyfh.close()
96
96
97 if twice and response.getheader('ETag', None):
97 if twice and response.getheader('ETag', None):
98 tag = response.getheader('ETag')
98 tag = response.getheader('ETag')
99
99
100 return response.status
100 return response.status
101
101
102 status = request(args.host, args.path, args.show)
102 status = request(args.host, args.path, args.show)
103 if twice:
103 if twice:
104 status = request(args.host, args.path, args.show)
104 status = request(args.host, args.path, args.show)
105
105
106 if 200 <= status <= 305:
106 if 200 <= status <= 305:
107 sys.exit(0)
107 sys.exit(0)
108 sys.exit(1)
108 sys.exit(1)
General Comments 0
You need to be logged in to leave comments. Login now