##// END OF EJS Templates
tests: teach get-with-headers.py some new tricks...
Gregory Szorc -
r35799:c6ef8e84 default
parent child Browse files
Show More
@@ -28,6 +28,11 b" parser.add_argument('--twice', action='s"
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=[],
32 help='Send an additional HTTP request header. Argument '
33 'value is <header>=<value>')
34 parser.add_argument('--bodyfile',
35 help='Write HTTP response body to a file')
31 parser.add_argument('host')
36 parser.add_argument('host')
32 parser.add_argument('path')
37 parser.add_argument('path')
33 parser.add_argument('show', nargs='*')
38 parser.add_argument('show', nargs='*')
@@ -38,6 +43,7 b' twice = args.twice'
38 headeronly = args.headeronly
43 headeronly = args.headeronly
39 formatjson = args.json
44 formatjson = args.json
40 hgproto = args.hgproto
45 hgproto = args.hgproto
46 requestheaders = args.requestheader
41
47
42 tag = None
48 tag = None
43 def request(host, path, show):
49 def request(host, path, show):
@@ -49,6 +55,10 b' def request(host, path, show):'
49 if hgproto:
55 if hgproto:
50 headers['X-HgProto-1'] = hgproto
56 headers['X-HgProto-1'] = hgproto
51
57
58 for header in requestheaders:
59 key, value = header.split('=', 1)
60 headers[key] = value
61
52 conn = httplib.HTTPConnection(host)
62 conn = httplib.HTTPConnection(host)
53 conn.request("GET", '/' + path, None, headers)
63 conn.request("GET", '/' + path, None, headers)
54 response = conn.getresponse()
64 response = conn.getresponse()
@@ -63,6 +73,11 b' def request(host, path, show):'
63 print()
73 print()
64 data = response.read()
74 data = response.read()
65
75
76 if args.bodyfile:
77 bodyfh = open(args.bodyfile, 'wb')
78 else:
79 bodyfh = sys.stdout
80
66 # Pretty print JSON. This also has the beneficial side-effect
81 # Pretty print JSON. This also has the beneficial side-effect
67 # of verifying emitted JSON is well-formed.
82 # of verifying emitted JSON is well-formed.
68 if formatjson:
83 if formatjson:
@@ -71,9 +86,13 b' def request(host, path, show):'
71 data = json.loads(data)
86 data = json.loads(data)
72 lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
87 lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
73 for line in lines:
88 for line in lines:
74 print(line.rstrip())
89 bodyfh.write(line.rstrip())
90 bodyfh.write(b'\n')
75 else:
91 else:
76 sys.stdout.write(data)
92 bodyfh.write(data)
93
94 if args.bodyfile:
95 bodyfh.close()
77
96
78 if twice and response.getheader('ETag', None):
97 if twice and response.getheader('ETag', None):
79 tag = response.getheader('ETag')
98 tag = response.getheader('ETag')
General Comments 0
You need to be logged in to leave comments. Login now