Merge lp:~leonardr/lazr.restful/bug-619180 into lp:lazr.restful

Proposed by Leonard Richardson
Status: Merged
Approved by: Данило Шеган
Approved revision: 173
Merged at revision: 171
Proposed branch: lp:~leonardr/lazr.restful/bug-619180
Merge into: lp:lazr.restful
Diff against target: 96 lines (+29/-8)
5 files modified
src/lazr/restful/NEWS.txt (+3/-0)
src/lazr/restful/_operation.py (+3/-3)
src/lazr/restful/example/base/tests/collection.txt (+10/-0)
src/lazr/restful/example/base/tests/field.txt (+1/-5)
src/lazr/restful/testing/helpers.py (+12/-0)
To merge this branch: bzr merge lp:~leonardr/lazr.restful/bug-619180
Reviewer Review Type Date Requested Status
Данило Шеган (community) Approve
Review via email: mp+48355@code.launchpad.net

Description of the change

This branch fixes bug 619180. Field-specific validation errors that contained Unicode characters were treated properly if the validation was done as part of a PUT or PATCH request, but not if it was done as part of a named operation call. I fixed the place where the problem was and two other places as well (though I don't think the problem can really happen in those other two places).

I refactored a test helper method to avoid duplicating code.

To post a comment you must log in.
Revision history for this message
Данило Шеган (danilo) wrote :

> leonardr, hi, is .encode(utf-8) safe to do for URLs?
<lifeless> if it is a url, then it must be a subset of ascii
 by definition
<leonardr> danilos: what specific case are you thinking of?
<danilos> ok, let me rephrase that... leonardr, is .encode('utf-8') safe to do before passing to webservice.get() [i.e. does it do escaping as appropriate, and is it documented that our webservice works with UTF-8]
 leonardr, line 48
<leonardr> danilos: well, the test works... that's a good question, though
<danilos> leonardr, heh, ok, fair enough
 leonardr, how do we answer that question?
<leonardr> danilos: i'll take a look at the incoming request
<jam> jtv: are you still around? I got stuck shoveling snow for a couple of hours this morning, but I'm around now
<danilos> leonardr, cool, thanks... even if it might be a problem already, it doesn't have much to do with this branch I suppose, so r=me
 leonardr, please do file a bug if you find it is, though
<leonardr> danilos: actually, i know it works in reality, because ursula's test request went through properly (and once this fix is applied, it works everywhere)
<leonardr> so i think httplib2 percent-encodes the utf-8, and that's how it works
<danilos> leonardr, right, makes sense, thanks

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/lazr/restful/NEWS.txt'
2--- src/lazr/restful/NEWS.txt 2011-01-26 16:50:13 +0000
3+++ src/lazr/restful/NEWS.txt 2011-02-02 18:05:04 +0000
4@@ -13,6 +13,9 @@
5 You can suppress the website link for a particular entry class by
6 passing publish_web_link=False into export_as_webservice_entry().
7
8+Validation errors for named operations will be properly sent to the
9+client even if they contain Unicode characters. (Launchpad bug 619180.)
10+
11 0.15.4 (2010-01-26)
12 ===================
13
14
15=== modified file 'src/lazr/restful/_operation.py'
16--- src/lazr/restful/_operation.py 2010-08-13 14:29:52 +0000
17+++ src/lazr/restful/_operation.py 2011-02-02 18:05:04 +0000
18@@ -191,14 +191,14 @@
19 value = marshaller.marshall_from_request(
20 self.request.form.get(name))
21 except ValueError, e:
22- errors.append("%s: %s" % (name, e))
23+ errors.append(u"%s: %s" % (name, e))
24 continue
25 try:
26 field.validate(value)
27 except RequiredMissing:
28- errors.append("%s: Required input is missing." % name)
29+ errors.append(u"%s: Required input is missing." % name)
30 except ValidationError, e:
31- errors.append("%s: %s" % (name, e))
32+ errors.append(u"%s: %s" % (name, e))
33 else:
34 validated_values[name] = value
35 return (validated_values, errors)
36
37=== modified file 'src/lazr/restful/example/base/tests/collection.txt'
38--- src/lazr/restful/example/base/tests/collection.txt 2011-02-02 13:24:08 +0000
39+++ src/lazr/restful/example/base/tests/collection.txt 2011-02-02 18:05:04 +0000
40@@ -244,6 +244,16 @@
41 ...
42 search: Required input is missing.
43
44+The error message may contain Unicode characters:
45+
46+ >>> from lazr.restful.testing.helpers import encode_response
47+ >>> url = u"/cookbooks?ws.op=find_for_cuisine&cuisine=\N{SNOWMAN}"
48+ >>> response = webservice.get(url.encode("utf-8"))
49+ >>> print encode_response(response)
50+ HTTP/1.1 400 Bad Request
51+ ...
52+ cuisine: Invalid value "\u2603". Acceptable values are:...
53+
54 If a named operation takes an argument that's a value for a vocabulary
55 (such as Cuisine in the example web service), the client can specify
56 the name of the value, just as they would when changing the value with
57
58=== modified file 'src/lazr/restful/example/base/tests/field.txt'
59--- src/lazr/restful/example/base/tests/field.txt 2011-01-26 16:14:11 +0000
60+++ src/lazr/restful/example/base/tests/field.txt 2011-02-02 18:05:04 +0000
61@@ -297,11 +297,7 @@
62 ...and the XHTML representation of an ICookbook's description will be the
63 result of calling a dummy_renderer object.
64
65- >>> def encode_response(response):
66- ... """Encode a response that contains weird Unicode characters."""
67- ... response_unicode = str(response).decode("utf-8")
68- ... return response_unicode.encode("ascii", "backslashreplace")
69-
70+ >>> from lazr.restful.testing.helpers import encode_response
71 >>> response = webservice.get(field_url, 'application/xhtml+xml')
72 >>> print encode_response(response)
73 HTTP/1.1 200 Ok
74
75=== modified file 'src/lazr/restful/testing/helpers.py'
76--- src/lazr/restful/testing/helpers.py 2010-09-29 16:18:47 +0000
77+++ src/lazr/restful/testing/helpers.py 2011-02-02 18:05:04 +0000
78@@ -39,6 +39,18 @@
79 return new_module
80
81
82+def encode_response(response):
83+ """Encode a response that may contain weird Unicode characters.
84+
85+ The resulting string will (eg.) contain the six characters
86+ "\u2603" instead of the single Unicode character SNOWMAN.
87+
88+ :param response: an httplib HTTPResponse object.
89+ """
90+ response_unicode = str(response).decode("utf-8")
91+ return response_unicode.encode("ascii", "backslashreplace")
92+
93+
94 class TestWebServiceConfiguration:
95 implements(IWebServiceConfiguration)
96 view_permission = "lazr.View"

Subscribers

People subscribed via source and target branches