##// END OF EJS Templates
revlog: subclass the new `repository.iverifyproblem` Protocol class...
revlog: subclass the new `repository.iverifyproblem` Protocol class This is the same transformation as 3a90a6fd710d did for dirstate, but the CamelCase naming was already cleaned up here. We shouldn't have to explicitly subclass, but I'm doing so to test the interplay of regular attributes and the `attrs` class. Also, PyCharm has a nifty feature that puts a jump point in the gutter to navigate back and forth between the base class and subclasses (and override functions and base class functions) when there's an explicit subclassing. Additionally, PyCharm will immediately flag signature mismatches without a 40m pytype run.

File last commit:

r50800:cd125eef default
r53365:4ef6dbc2 default
Show More
get-with-headers.py
131 lines | 3.5 KiB | text/x-python | PythonLexer
/ tests / get-with-headers.py
run-tests: stop writing a `python3` symlink pointing to python2...
r48294 #!/usr/bin/env python
Eric Hopper
Add a test for getting raw files via the web UI.
r2532
av6
tests: check how hgweb handles HEAD requests...
r50800 """This does HTTP requests (GET by default) given a host:port and path and
returns a subset of the headers plus the body of the result."""
Eric Hopper
Add a test for getting raw files via the web UI.
r2532
Gregory Szorc
tests: use absolute_import in /get-with-headers.py...
r27296
Gregory Szorc
tests: use argparse in get-with-headers.py...
r35798 import argparse
Gregory Szorc
tests: use absolute_import in /get-with-headers.py...
r27296 import json
import os
import sys
Patrick Mezard
get-with-headers: fix stream modes under Windows
r7054
Pulkit Goyal
py3: conditionalize httplib import...
r29455 from mercurial import (
Gregory Szorc
py3: encode JSON str to bytes...
r40190 pycompat,
Pulkit Goyal
py3: conditionalize httplib import...
r29455 util,
)
httplib = util.httplib
Patrick Mezard
get-with-headers: fix stream modes under Windows
r7054 try:
Gregory Szorc
tests: use absolute_import in /get-with-headers.py...
r27296 import msvcrt
Augie Fackler
formatting: blacken the codebase...
r43346
Patrick Mezard
get-with-headers: fix stream modes under Windows
r7054 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stderr.fileno(), os.O_BINARY)
except ImportError:
pass
Yuya Nishihara
get-with-headers: use bytes stdout thoroughly...
r36594 stdout = getattr(sys.stdout, 'buffer', sys.stdout)
Gregory Szorc
tests: use argparse in get-with-headers.py...
r35798 parser = argparse.ArgumentParser()
parser.add_argument('--twice', action='store_true')
parser.add_argument('--headeronly', action='store_true')
parser.add_argument('--json', action='store_true')
parser.add_argument('--hgproto')
Augie Fackler
formatting: blacken the codebase...
r43346 parser.add_argument(
'--requestheader',
nargs='*',
default=[],
help='Send an additional HTTP request header. Argument '
'value is <header>=<value>',
)
parser.add_argument('--bodyfile', help='Write HTTP response body to a file')
av6
tests: check how hgweb handles HEAD requests...
r50800 parser.add_argument('--method', default='GET', help='HTTP method to use')
Gregory Szorc
tests: use argparse in get-with-headers.py...
r35798 parser.add_argument('host')
parser.add_argument('path')
parser.add_argument('show', nargs='*')
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182
Gregory Szorc
tests: use argparse in get-with-headers.py...
r35798 args = parser.parse_args()
twice = args.twice
headeronly = args.headeronly
formatjson = args.json
hgproto = args.hgproto
Gregory Szorc
tests: teach get-with-headers.py some new tricks...
r35799 requestheaders = args.requestheader
Gregory Szorc
protocol: send application/mercurial-0.2 responses to capable clients...
r30764
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182 tag = None
Augie Fackler
formatting: blacken the codebase...
r43346
av6
tests: check how hgweb handles HEAD requests...
r50800 def request(method, host, path, show):
Mads Kiilerich
tests: prepare get-with-headers.py for MSYS...
r17017 assert not path.startswith('/'), path
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182 global tag
headers = {}
if tag:
headers['If-None-Match'] = tag
Gregory Szorc
protocol: send application/mercurial-0.2 responses to capable clients...
r30764 if hgproto:
headers['X-HgProto-1'] = hgproto
Bryan O'Sullivan
hgweb: return meaningful HTTP status codes instead of nonsense
r5561
Gregory Szorc
tests: teach get-with-headers.py some new tricks...
r35799 for header in requestheaders:
key, value = header.split('=', 1)
headers[key] = value
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182 conn = httplib.HTTPConnection(host)
av6
tests: check how hgweb handles HEAD requests...
r50800 conn.request(method, '/' + path, None, headers)
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182 response = conn.getresponse()
Augie Fackler
formatting: blacken the codebase...
r43346 stdout.write(
b'%d %s\n' % (response.status, response.reason.encode('ascii'))
)
Mads Kiilerich
serve: don't send any content headers with 304 responses...
r18380 if show[:1] == ['-']:
Augie Fackler
formatting: blacken the codebase...
r43346 show = sorted(
h for h, v in response.getheaders() if h.lower() not in show
)
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182 for h in [h.lower() for h in show]:
if response.getheader(h, None) is not None:
Augie Fackler
formatting: blacken the codebase...
r43346 stdout.write(
b"%s: %s\n"
% (h.encode('ascii'), response.getheader(h).encode('ascii'))
)
windows: make sure we fully read and cleanly close the connection...
r48382 if headeronly:
# still read the body to prevent windows to be unhappy about that
# (this might some flakyness in test-hgweb-filelog.t on Windows)
data = response.read()
else:
Yuya Nishihara
get-with-headers: use bytes stdout thoroughly...
r36594 stdout.write(b'\n')
Gregory Szorc
hgweb: send proper HTTP response after uncaught exception...
r23409 data = response.read()
Gregory Szorc
get-with-headers: support parsing and pretty printing JSON...
r24543
Gregory Szorc
tests: teach get-with-headers.py some new tricks...
r35799 if args.bodyfile:
bodyfh = open(args.bodyfile, 'wb')
else:
Yuya Nishihara
get-with-headers: use bytes stdout thoroughly...
r36594 bodyfh = stdout
Gregory Szorc
tests: teach get-with-headers.py some new tricks...
r35799
Gregory Szorc
get-with-headers: support parsing and pretty printing JSON...
r24543 # Pretty print JSON. This also has the beneficial side-effect
# of verifying emitted JSON is well-formed.
if formatjson:
# json.dumps() will print trailing newlines. Eliminate them
# to make tests easier to write.
Gregory Szorc
py3: define and use json.loads polyfill...
r43697 data = pycompat.json_loads(data)
Gregory Szorc
get-with-headers: support parsing and pretty printing JSON...
r24543 lines = json.dumps(data, sort_keys=True, indent=2).splitlines()
for line in lines:
Gregory Szorc
py3: encode JSON str to bytes...
r40190 bodyfh.write(pycompat.sysbytes(line.rstrip()))
Gregory Szorc
tests: teach get-with-headers.py some new tricks...
r35799 bodyfh.write(b'\n')
Gregory Szorc
get-with-headers: support parsing and pretty printing JSON...
r24543 else:
Gregory Szorc
tests: teach get-with-headers.py some new tricks...
r35799 bodyfh.write(data)
if args.bodyfile:
bodyfh.close()
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182
Gregory Szorc
tests: store ETag when using --headeronly...
r31791 if twice and response.getheader('ETag', None):
tag = response.getheader('ETag')
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182
windows: make sure we fully read and cleanly close the connection...
r48382 # further try to please the windows-flakyness deity
conn.close()
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182 return response.status
Augie Fackler
formatting: blacken the codebase...
r43346
av6
tests: check how hgweb handles HEAD requests...
r50800 status = request(args.method, args.host, args.path, args.show)
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182 if twice:
av6
tests: check how hgweb handles HEAD requests...
r50800 status = request(args.method, args.host, args.path, args.show)
Dirkjan Ochtman
tests: extend get-with-headers to support cache testing
r12182
if 200 <= status <= 305:
Bryan O'Sullivan
hgweb: return meaningful HTTP status codes instead of nonsense
r5561 sys.exit(0)
sys.exit(1)