Mercurial > hg
annotate tests/test-lrucachedict.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 | 79add5a4e857 |
children | 067f7d2c7d60 |
rev | line source |
---|---|
28931
ba0e4789bd2e
tests: make test-lrucachedict use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28930
diff
changeset
|
1 from __future__ import absolute_import, print_function |
28930
e3f01188d439
tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27576
diff
changeset
|
2 |
e3f01188d439
tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27576
diff
changeset
|
3 from mercurial import ( |
e3f01188d439
tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27576
diff
changeset
|
4 util, |
e3f01188d439
tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27576
diff
changeset
|
5 ) |
18603 | 6 |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
7 def printifpresent(d, xs, name='d'): |
18603 | 8 for x in xs: |
9 present = x in d | |
28931
ba0e4789bd2e
tests: make test-lrucachedict use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28930
diff
changeset
|
10 print("'%s' in %s: %s" % (x, name, present)) |
18603 | 11 if present: |
28931
ba0e4789bd2e
tests: make test-lrucachedict use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28930
diff
changeset
|
12 print("%s['%s']: %s" % (name, x, d[x])) |
18603 | 13 |
14 def test_lrucachedict(): | |
15 d = util.lrucachedict(4) | |
16 d['a'] = 'va' | |
17 d['b'] = 'vb' | |
18 d['c'] = 'vc' | |
19 d['d'] = 'vd' | |
20 | |
21 # all of these should be present | |
22 printifpresent(d, ['a', 'b', 'c', 'd']) | |
23 | |
24 # 'a' should be dropped because it was least recently used | |
25 d['e'] = 've' | |
26 printifpresent(d, ['a', 'b', 'c', 'd', 'e']) | |
27 | |
29828
79add5a4e857
util: properly implement lrucachedict.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28931
diff
changeset
|
28 assert d.get('a') is None |
79add5a4e857
util: properly implement lrucachedict.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28931
diff
changeset
|
29 assert d.get('e') == 've' |
79add5a4e857
util: properly implement lrucachedict.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28931
diff
changeset
|
30 |
18603 | 31 # touch entries in some order (get or set). |
32 d['e'] | |
33 d['c'] = 'vc2' | |
34 d['d'] | |
35 d['b'] = 'vb2' | |
36 | |
37 # 'e' should be dropped now | |
38 d['f'] = 'vf' | |
39 printifpresent(d, ['b', 'c', 'd', 'e', 'f']) | |
40 | |
19710
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
18603
diff
changeset
|
41 d.clear() |
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
18603
diff
changeset
|
42 printifpresent(d, ['b', 'c', 'd', 'e', 'f']) |
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
18603
diff
changeset
|
43 |
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
44 # Now test dicts that aren't full. |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
45 d = util.lrucachedict(4) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
46 d['a'] = 1 |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
47 d['b'] = 2 |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
48 d['a'] |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
49 d['b'] |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
50 printifpresent(d, ['a', 'b']) |
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
51 |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
52 # test copy method |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
53 d = util.lrucachedict(4) |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
54 d['a'] = 'va3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
55 d['b'] = 'vb3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
56 d['c'] = 'vc3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
57 d['d'] = 'vd3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
58 |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
59 dc = d.copy() |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
60 |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
61 # all of these should be present |
28931
ba0e4789bd2e
tests: make test-lrucachedict use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28930
diff
changeset
|
62 print("\nAll of these should be present:") |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
63 printifpresent(dc, ['a', 'b', 'c', 'd'], 'dc') |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
64 |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
65 # 'a' should be dropped because it was least recently used |
28931
ba0e4789bd2e
tests: make test-lrucachedict use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28930
diff
changeset
|
66 print("\nAll of these except 'a' should be present:") |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
67 dc['e'] = 've3' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
68 printifpresent(dc, ['a', 'b', 'c', 'd', 'e'], 'dc') |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
69 |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
70 # contents and order of original dict should remain unchanged |
28931
ba0e4789bd2e
tests: make test-lrucachedict use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28930
diff
changeset
|
71 print("\nThese should be in reverse alphabetical order and read 'v?3':") |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
72 dc['b'] = 'vb3_new' |
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
73 for k in list(iter(d)): |
28931
ba0e4789bd2e
tests: make test-lrucachedict use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28930
diff
changeset
|
74 print("d['%s']: %s" % (k, d[k])) |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
75 |
18603 | 76 if __name__ == '__main__': |
77 test_lrucachedict() |