##// END OF EJS Templates
tests: just use 'response.reason'...
Pierre-Yves David -
r25208:5a6820f8 default
parent child Browse files
Show More
@@ -1,88 +1,86
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 import httplib, sys
6 import httplib, sys
7
7
8 try:
8 try:
9 import json
9 import json
10 except ImportError:
10 except ImportError:
11 try:
11 try:
12 import simplejson as json
12 import simplejson as json
13 except ImportError:
13 except ImportError:
14 json = None
14 json = None
15
15
16 try:
16 try:
17 import msvcrt, os
17 import msvcrt, os
18 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
18 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
19 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
19 msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
20 except ImportError:
20 except ImportError:
21 pass
21 pass
22
22
23 twice = False
23 twice = False
24 if '--twice' in sys.argv:
24 if '--twice' in sys.argv:
25 sys.argv.remove('--twice')
25 sys.argv.remove('--twice')
26 twice = True
26 twice = True
27 headeronly = False
27 headeronly = False
28 if '--headeronly' in sys.argv:
28 if '--headeronly' in sys.argv:
29 sys.argv.remove('--headeronly')
29 sys.argv.remove('--headeronly')
30 headeronly = True
30 headeronly = True
31 formatjson = False
31 formatjson = False
32 if '--json' in sys.argv:
32 if '--json' in sys.argv:
33 sys.argv.remove('--json')
33 sys.argv.remove('--json')
34 formatjson = True
34 formatjson = True
35
35
36 reasons = {'Not modified': 'Not Modified'} # python 2.4
37
38 tag = None
36 tag = None
39 def request(host, path, show):
37 def request(host, path, show):
40 assert not path.startswith('/'), path
38 assert not path.startswith('/'), path
41 global tag
39 global tag
42 headers = {}
40 headers = {}
43 if tag:
41 if tag:
44 headers['If-None-Match'] = tag
42 headers['If-None-Match'] = tag
45
43
46 conn = httplib.HTTPConnection(host)
44 conn = httplib.HTTPConnection(host)
47 conn.request("GET", '/' + path, None, headers)
45 conn.request("GET", '/' + path, None, headers)
48 response = conn.getresponse()
46 response = conn.getresponse()
49 print response.status, reasons.get(response.reason, response.reason)
47 print response.status, response.reason
50 if show[:1] == ['-']:
48 if show[:1] == ['-']:
51 show = sorted(h for h, v in response.getheaders()
49 show = sorted(h for h, v in response.getheaders()
52 if h.lower() not in show)
50 if h.lower() not in show)
53 for h in [h.lower() for h in show]:
51 for h in [h.lower() for h in show]:
54 if response.getheader(h, None) is not None:
52 if response.getheader(h, None) is not None:
55 print "%s: %s" % (h, response.getheader(h))
53 print "%s: %s" % (h, response.getheader(h))
56 if not headeronly:
54 if not headeronly:
57 print
55 print
58 data = response.read()
56 data = response.read()
59
57
60 # Pretty print JSON. This also has the beneficial side-effect
58 # Pretty print JSON. This also has the beneficial side-effect
61 # of verifying emitted JSON is well-formed.
59 # of verifying emitted JSON is well-formed.
62 if formatjson:
60 if formatjson:
63 if not json:
61 if not json:
64 print 'no json module not available'
62 print 'no json module not available'
65 print 'did you forget a #require json?'
63 print 'did you forget a #require json?'
66 sys.exit(1)
64 sys.exit(1)
67
65
68 # json.dumps() will print trailing newlines. Eliminate them
66 # json.dumps() will print trailing newlines. Eliminate them
69 # to make tests easier to write.
67 # to make tests easier to write.
70 data = json.loads(data)
68 data = json.loads(data)
71 lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
69 lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
72 for line in lines:
70 for line in lines:
73 print line.rstrip()
71 print line.rstrip()
74 else:
72 else:
75 sys.stdout.write(data)
73 sys.stdout.write(data)
76
74
77 if twice and response.getheader('ETag', None):
75 if twice and response.getheader('ETag', None):
78 tag = response.getheader('ETag')
76 tag = response.getheader('ETag')
79
77
80 return response.status
78 return response.status
81
79
82 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
80 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
83 if twice:
81 if twice:
84 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
82 status = request(sys.argv[1], sys.argv[2], sys.argv[3:])
85
83
86 if 200 <= status <= 305:
84 if 200 <= status <= 305:
87 sys.exit(0)
85 sys.exit(0)
88 sys.exit(1)
86 sys.exit(1)
General Comments 0
You need to be logged in to leave comments. Login now