Mercurial > p > roundup > code
annotate test/test_misc.py @ 5710:0b79bfcb3312
Add support for making an idempotent POST. This allows retrying a POST
that was interrupted. It involves creating a post once only (poe) url
/rest/data/<class>/@poe/<random_token>. This url acts the same as a
post to /rest/data/<class>. However once the @poe url is used, it
can't be used for a second POST.
To make these changes:
1) Take the body of post_collection into a new post_collection_inner
function. Have post_collection call post_collection_inner.
2) Add a handler for POST to rest/data/class/@poe. This will return a
unique POE url. By default the url expires after 30 minutes. The
POE random token is only good for a specific user and is stored in
the session db.
3) Add a handler for POST to rest/data/<class>/@poe/<random token>.
The random token generated in 2 is validated for proper class (if
token is not generic) and proper user and must not have expired.
If everything is valid, call post_collection_inner to process the
input and generate the new entry.
To make recognition of 2 stable (so it's not confused with
rest/data/<:class_name>/<:item_id>), removed @ from
Routing::url_to_regex.
The current Routing.execute method stops on the first regular
expression to match the URL. Since item_id doesn't accept a POST, I
was getting 405 bad method sometimes. My guess is the order of the
regular expressions is not stable, so sometime I would get the right
regexp for /data/<class>/@poe and sometime I would get the one for
/data/<class>/<item_id>. By removing the @ from the url_to_regexp,
there was no way for the item_id case to match @poe.
There are alternate fixes we may need to look at. If a regexp matches
but the method does not, return to the regexp matching loop in
execute() looking for another match. Only once every possible match
has failed should the code return a 405 method failure.
Another fix is to implement a more sophisticated mechanism so that
@Routing.route("/data/<:class_name>/<:item_id>/<:attr_name>", 'PATCH')
has different regexps for matching <:class_name> <:item_id> and
<:attr_name>. Currently the regexp specified by url_to_regex is used
for every component.
Other fixes:
Made failure to find any props in props_from_args return an empty
dict rather than throwing an unhandled error.
Make __init__ for SimulateFieldStorageFromJson handle an empty json
doc. Useful for POSTing to rest/data/class/@poe with an empty
document.
Testing:
added testPostPOE to test/rest_common.py that I think covers
all the code that was added.
Documentation:
Add doc to rest.txt in the "Client API" section titled: Safely
Re-sending POST". Move existing section "Adding new rest endpoints" in
"Client API" to a new second level section called "Programming the
REST API". Also a minor change to the simple rest client moving the
header setting to continuation lines rather than showing one long
line.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 14 Apr 2019 21:07:11 -0400 |
| parents | 9a09719b0d8e |
| children | 3b945aee0919 |
| rev | line source |
|---|---|
|
5155
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
1 # misc tests |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
2 |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
3 import unittest |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5155
diff
changeset
|
4 import roundup.anypy.cmp_ |
|
5155
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
5 from roundup.cgi.accept_language import parse |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
6 |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
7 class AcceptLanguageTest(unittest.TestCase): |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
8 def testParse(self): |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
9 self.assertEqual(parse("da, en-gb;q=0.8, en;q=0.7"), |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
10 ['da', 'en_gb', 'en']) |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
11 self.assertEqual(parse("en;q=0.2, fr;q=1"), ['fr', 'en']) |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
12 self.assertEqual(parse("zn; q = 0.2 ,pt-br;q =1"), ['pt_br', 'zn']) |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
13 self.assertEqual(parse("es-AR"), ['es_AR']) |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
14 self.assertEqual(parse("es-es-cat"), ['es_es_cat']) |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
15 self.assertEqual(parse(""), []) |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
16 self.assertEqual(parse(None),[]) |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
17 self.assertEqual(parse(" "), []) |
|
e1e3531b4d9b
add tests for roundup/cgi/accept_language.py copied from embedded doctests already in file.
John Rouillard <rouilj@ieee.org>
parents:
diff
changeset
|
18 self.assertEqual(parse("en,"), ['en']) |
|
5481
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5155
diff
changeset
|
19 |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5155
diff
changeset
|
20 class CmpTest(unittest.TestCase): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5155
diff
changeset
|
21 def testCmp(self): |
|
9a09719b0d8e
helper to allow comparing dicts and None values in Python 3
Christof Meerwald <cmeerw@cmeerw.org>
parents:
5155
diff
changeset
|
22 roundup.anypy.cmp_._test() |
