Merge lp:~benji/lazr.restful/error-on-multiple-ws.op-fields into lp:lazr.restful

Proposed by Benji York
Status: Merged
Merged at revision: 195
Proposed branch: lp:~benji/lazr.restful/error-on-multiple-ws.op-fields
Merge into: lp:lazr.restful
Diff against target: 62 lines (+37/-0)
2 files modified
src/lazr/restful/_resource.py (+8/-0)
src/lazr/restful/tests/test_webservice.py (+29/-0)
To merge this branch: bzr merge lp:~benji/lazr.restful/error-on-multiple-ws.op-fields
Reviewer Review Type Date Requested Status
j.c.sackett (community) Approve
Review via email: mp+74682@code.launchpad.net

Description of the change

Bug 842917 describes an unhandled TypeError raised when more than one
value for ws.op is provided in web service requests. This branch fixes
that by simply ensuring there is only one ws.op value (which is a
string) and generating a 400 with an informative message if not.

To post a comment you must log in.
Revision history for this message
j.c.sackett (jcsackett) wrote :

This looks good to land (based on http://paste.ubuntu.com/685561/).

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/_resource.py'
2--- src/lazr/restful/_resource.py 2011-09-07 20:46:50 +0000
3+++ src/lazr/restful/_resource.py 2011-09-08 20:43:24 +0000
4@@ -700,6 +700,10 @@
5 :return: The result of the operation: either a string or an
6 object that needs to be serialized to JSON.
7 """
8+ if not isinstance(operation_name, basestring):
9+ self.request.response.setStatus(400)
10+ return "Expected a single operation: %r" % (operation_name,)
11+
12 try:
13 operation = getMultiAdapter((self.context, self.request),
14 IResourceGETOperation,
15@@ -722,6 +726,10 @@
16 :return: The result of the operation: either a string or an
17 object that needs to be serialized to JSON.
18 """
19+ if not isinstance(operation_name, basestring):
20+ self.request.response.setStatus(400)
21+ return "Expected a single operation: %r" % (operation_name,)
22+
23 try:
24 operation = getMultiAdapter((self.context, self.request),
25 IResourcePOSTOperation,
26
27=== modified file 'src/lazr/restful/tests/test_webservice.py'
28--- src/lazr/restful/tests/test_webservice.py 2011-03-31 01:13:59 +0000
29+++ src/lazr/restful/tests/test_webservice.py 2011-09-08 20:43:24 +0000
30@@ -1005,3 +1005,32 @@
31 IHasOneField, HasOneField, "") as resource:
32 resource.applyChanges({})
33 self.assertEquals(len(eventtesting.getEvents()), 0)
34+
35+
36+class MalformedRequest(EntryTestCase):
37+
38+ testmodule_objects = [HasOneField, IHasOneField]
39+ default_media_type = "application/xhtml+xml"
40+
41+ def setUp(self):
42+ super(MalformedRequest, self).setUp()
43+ self._register_url_adapter(IHasOneField)
44+ self.unicode_message = u"Hello from a \N{SNOWMAN}"
45+
46+ def test_multiple_named_operations_generate_error_on_GET(self):
47+ with self.entry_resource(
48+ IHasOneField, HasOneField, self.unicode_message) as resource:
49+ resource.request.form['ws.op'] = ['foo', 'bar']
50+ result = resource.do_GET()
51+ self.assertEquals(resource.request.response.getStatus(), 400)
52+ self.assertEquals(
53+ result, "Expected a single operation: ['foo', 'bar']")
54+
55+ def test_multiple_named_operations_generate_error_on_POST(self):
56+ with self.entry_resource(
57+ IHasOneField, HasOneField, self.unicode_message) as resource:
58+ resource.request.form['ws.op'] = ['foo', 'bar']
59+ result = resource.do_POST()
60+ self.assertEquals(resource.request.response.getStatus(), 400)
61+ self.assertEquals(
62+ result, "Expected a single operation: ['foo', 'bar']")

Subscribers

People subscribed via source and target branches