Mercurial > hg
annotate tests/test-check-interfaces.py @ 37048:fc5e261915b9
wireproto: require POST for all HTTPv2 requests
Wire protocol version 1 transfers argument data via request
headers by default. This has historically caused problems because
servers institute limits on the length of individual HTTP headers
as well as the total size of all request headers. Mercurial servers
can advertise the maximum length of an individual header. But
there's no guarantee any intermediate HTTP agents will accept
headers up to that length.
In the existing wire protocol, server operators typically also
key off the HTTP request method to implement authentication.
For example, GET requests translate to read-only requests and
can be allowed. But read-write commands must use POST and require
authentication. This has typically worked because the only wire
protocol commands that use POST modify the repo (e.g. the
"unbundle" command).
There is an experimental feature to enable clients to transmit
argument data via POST request bodies. This is technically a
better and more robust solution. But we can't enable it by default
because of servers assuming POST means write access.
In version 2 of the wire protocol, the permissions of a request
are encoded in the URL. And with it being a new protocol in a new
URL space, we're not constrained by backwards compatibility
requirements.
This commit adopts the technically superior mechanism of using
HTTP request bodies to send argument data by requiring POST for
all commands. Strictly speaking, it may be possible to send
request bodies on GET requests. But my experience is that not all
HTTP stacks support this. POST pretty much always works. Using POST
for read-only operations does sacrifice some RESTful design
purity. But this API cares about practicality, not about being
in Roy T. Fielding's REST ivory tower.
There's a chance we may relax this restriction in the future. But
for now, I want to see how far we can get with a POST only API.
Differential Revision: https://phab.mercurial-scm.org/D2837
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Tue, 13 Mar 2018 11:57:43 -0700 |
parents | 8e89c2bec1f7 |
children | 0dfb5672f015 |
rev | line source |
---|---|
33807
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
1 # Test that certain objects conform to well-defined interfaces. |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
2 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
3 from __future__ import absolute_import, print_function |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
4 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
5 from mercurial import ( |
34307
afcbc6f64d27
tests: add interface checks for bundle, statichttp, and union peers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33807
diff
changeset
|
6 bundlerepo, |
33807
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
7 httppeer, |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
8 localrepo, |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
9 sshpeer, |
34307
afcbc6f64d27
tests: add interface checks for bundle, statichttp, and union peers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33807
diff
changeset
|
10 statichttprepo, |
33807
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
11 ui as uimod, |
34307
afcbc6f64d27
tests: add interface checks for bundle, statichttp, and union peers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33807
diff
changeset
|
12 unionrepo, |
33807
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
13 ) |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
14 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
15 def checkobject(o): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
16 """Verify a constructed object conforms to interface rules. |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
17 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
18 An object must have __abstractmethods__ defined. |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
19 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
20 All "public" attributes of the object (attributes not prefixed with |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
21 an underscore) must be in __abstractmethods__ or appear on a base class |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
22 with __abstractmethods__. |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
23 """ |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
24 name = o.__class__.__name__ |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
25 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
26 allowed = set() |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
27 for cls in o.__class__.__mro__: |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
28 if not getattr(cls, '__abstractmethods__', set()): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
29 continue |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
30 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
31 allowed |= cls.__abstractmethods__ |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
32 allowed |= {a for a in dir(cls) if not a.startswith('_')} |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
33 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
34 if not allowed: |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
35 print('%s does not have abstract methods' % name) |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
36 return |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
37 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
38 public = {a for a in dir(o) if not a.startswith('_')} |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
39 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
40 for attr in sorted(public - allowed): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
41 print('public attributes not in abstract interface: %s.%s' % ( |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
42 name, attr)) |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
43 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
44 # Facilitates testing localpeer. |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
45 class dummyrepo(object): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
46 def __init__(self): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
47 self.ui = uimod.ui() |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
48 def filtered(self, name): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
49 pass |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
50 def _restrictcapabilities(self, caps): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
51 pass |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
52 |
37006
8e89c2bec1f7
httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36370
diff
changeset
|
53 class dummyopener(object): |
8e89c2bec1f7
httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36370
diff
changeset
|
54 handlers = [] |
8e89c2bec1f7
httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36370
diff
changeset
|
55 |
33807
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
56 # Facilitates testing sshpeer without requiring an SSH server. |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
57 class badpeer(httppeer.httppeer): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
58 def __init__(self): |
37006
8e89c2bec1f7
httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36370
diff
changeset
|
59 super(badpeer, self).__init__(None, None, None, dummyopener()) |
33807
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
60 self.badattribute = True |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
61 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
62 def badmethod(self): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
63 pass |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
64 |
36370
11ba1a96f946
sshpeer: defer pipe buffering and stderr sidechannel binding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35978
diff
changeset
|
65 class dummypipe(object): |
11ba1a96f946
sshpeer: defer pipe buffering and stderr sidechannel binding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35978
diff
changeset
|
66 def close(self): |
11ba1a96f946
sshpeer: defer pipe buffering and stderr sidechannel binding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35978
diff
changeset
|
67 pass |
11ba1a96f946
sshpeer: defer pipe buffering and stderr sidechannel binding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35978
diff
changeset
|
68 |
33807
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
69 def main(): |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
70 ui = uimod.ui() |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
71 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
72 checkobject(badpeer()) |
37006
8e89c2bec1f7
httppeer: refactor how httppeer is created (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents:
36370
diff
changeset
|
73 checkobject(httppeer.httppeer(None, None, None, dummyopener())) |
33807
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
74 checkobject(localrepo.localpeer(dummyrepo())) |
36370
11ba1a96f946
sshpeer: defer pipe buffering and stderr sidechannel binding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35978
diff
changeset
|
75 checkobject(sshpeer.sshv1peer(ui, 'ssh://localhost/foo', None, dummypipe(), |
11ba1a96f946
sshpeer: defer pipe buffering and stderr sidechannel binding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35978
diff
changeset
|
76 dummypipe(), None, None)) |
11ba1a96f946
sshpeer: defer pipe buffering and stderr sidechannel binding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35978
diff
changeset
|
77 checkobject(sshpeer.sshv2peer(ui, 'ssh://localhost/foo', None, dummypipe(), |
11ba1a96f946
sshpeer: defer pipe buffering and stderr sidechannel binding
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35978
diff
changeset
|
78 dummypipe(), None, None)) |
34307
afcbc6f64d27
tests: add interface checks for bundle, statichttp, and union peers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33807
diff
changeset
|
79 checkobject(bundlerepo.bundlepeer(dummyrepo())) |
afcbc6f64d27
tests: add interface checks for bundle, statichttp, and union peers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33807
diff
changeset
|
80 checkobject(statichttprepo.statichttppeer(dummyrepo())) |
afcbc6f64d27
tests: add interface checks for bundle, statichttp, and union peers
Gregory Szorc <gregory.szorc@gmail.com>
parents:
33807
diff
changeset
|
81 checkobject(unionrepo.unionpeer(dummyrepo())) |
33807
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
82 |
b70029f355a3
tests: verify that peer instances only expose interface members
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff
changeset
|
83 main() |