Merge lp:~wallyworld/lazr.restful/namedpost-url-change-1056666 into lp:lazr.restful

Proposed by Ian Booth
Status: Merged
Approved by: Curtis Hovey
Approved revision: 202
Merged at revision: 201
Proposed branch: lp:~wallyworld/lazr.restful/namedpost-url-change-1056666
Merge into: lp:lazr.restful
Diff against target: 73 lines (+21/-3)
4 files modified
src/lazr/restful/NEWS.txt (+6/-0)
src/lazr/restful/_resource.py (+9/-0)
src/lazr/restful/example/base/tests/entry.txt (+5/-2)
src/lazr/restful/version.txt (+1/-1)
To merge this branch: bzr merge lp:~wallyworld/lazr.restful/namedpost-url-change-1056666
Reviewer Review Type Date Requested Status
Curtis Hovey (community) code Approve
Review via email: mp+126405@code.launchpad.net

Commit message

Redirect to a new location if the resource url changes after a named operation.

Description of the change

A named post operation may result in the resource on which the post is operating to have a different url at the end of the operation. Put and Patch operations do the right thing and issue a 301 with the new location, but named post operations don't. This branch corrects that.

I updated an existing doc test which performs a named post to account for the new behaviour.

To post a comment you must log in.
Revision history for this message
Curtis Hovey (sinzui) wrote :

Thank you.

review: Approve (code)

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 2012-03-15 03:07:15 +0000
3+++ src/lazr/restful/NEWS.txt 2012-09-26 09:35:24 +0000
4@@ -2,6 +2,12 @@
5 NEWS for lazr.restful
6 =====================
7
8+0.19.7 (2012-09-26)
9+===================
10+
11+Fixed bug 1056666: make named operations which result in a resource URL change
12+issue a 301 response containing the new location.
13+
14 0.19.6 (2012-03-15)
15 ===================
16
17
18=== modified file 'src/lazr/restful/_resource.py'
19--- src/lazr/restful/_resource.py 2011-10-11 13:33:15 +0000
20+++ src/lazr/restful/_resource.py 2012-09-26 09:35:24 +0000
21@@ -1393,6 +1393,7 @@
22
23 def handleCustomPOST(self, operation_name):
24 """See `CustomOperationResourceMixin`."""
25+ original_url = absoluteURL(self.entry.context, self.request)
26 value = super(EntryResource, self).handleCustomPOST(operation_name)
27 # We don't know what the custom operation might have done.
28 # Remove this object from the representation cache, just to be
29@@ -1400,6 +1401,14 @@
30 cache = self._representation_cache
31 if cache is not None:
32 cache.delete(self.context)
33+ new_url = absoluteURL(self.entry.context, self.request)
34+ if original_url != new_url:
35+ # The object moved. Serve a redirect to its new location.
36+ # This might not necessarily be the location of the entry!
37+ self.request.response.setStatus(301)
38+ self.request.response.setHeader(
39+ 'Location', absoluteURL(self.context, self.request))
40+ return ''
41 return value
42
43 def _getETagCores(self, unmarshalled_field_values=None):
44
45=== modified file 'src/lazr/restful/example/base/tests/entry.txt'
46--- src/lazr/restful/example/base/tests/entry.txt 2011-02-01 00:57:27 +0000
47+++ src/lazr/restful/example/base/tests/entry.txt 2012-09-26 09:35:24 +0000
48@@ -252,13 +252,16 @@
49
50 Some entries support custom operations through POST. You can invoke a
51 custom operation to modify a cookbook's name, making it seem more
52-interesting.
53+interesting. As a result of the operation, the cookbook's location will
54+change so we get a 301 response with the new URL.
55
56 >>> print webservice.get(joy_url).jsonBody()['cuisine']
57 General
58
59 >>> print webservice.named_post(joy_url, 'make_more_interesting', {})
60- HTTP/1.1 200 Ok
61+ HTTP/1.1 301 Moved Permanently
62+ ...
63+ Location: http://cookbooks.dev/devel/cookbooks/The%20New%20The%20Joy%20of%20Cooking
64 ...
65
66 >>> new_joy_url = quote("/cookbooks/The New The Joy of Cooking")
67
68=== modified file 'src/lazr/restful/version.txt'
69--- src/lazr/restful/version.txt 2012-03-15 03:07:15 +0000
70+++ src/lazr/restful/version.txt 2012-09-26 09:35:24 +0000
71@@ -1,1 +1,1 @@
72-0.19.6
73+0.19.7

Subscribers

People subscribed via source and target branches