comparison tests/get-with-headers.py @ 49905:cd125eef4388

tests: check how hgweb handles HEAD requests This test file is loosely based on test-hgweb.t. HEAD support originally implemented in fda5a4b853ab.
author Anton Shestakov <av6@dwimlabs.net>
date Wed, 11 Jan 2023 17:51:04 +0400
parents 6000f5b25c9b
children f8f14e6d032b
comparison
equal deleted inserted replaced
49904:3cbd0e919165 49905:cd125eef4388
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 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
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()
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
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("GET", '/' + path, None, headers) 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] == ['-']:
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)