Return relative URIs for href-template of homedoc

Based on current implement, some href-template of homedoc
are returning absolute path ('/queues'...). It may cause
a 404 error because client may join a host with the path
directly. This patch will make sure all the href-template
start with /v1 and add unit test case to make url joining
works well.

Fixes bug 1245656

Change-Id: I12059249135cdb82be7d1937cf65db5da860011b
This commit is contained in:
Fei Long Wang 2013-12-12 00:50:40 +08:00
parent 9c87eb244d
commit 9c5bc99b5c
2 changed files with 27 additions and 5 deletions

View File

@ -24,7 +24,7 @@ JSON_HOME = {
# Queues # Queues
#------------------------------------------------------------------ #------------------------------------------------------------------
'rel/queues': { 'rel/queues': {
'href-template': '/queues{?marker,limit,detailed}', 'href-template': '/v1/queues{?marker,limit,detailed}',
'href-vars': { 'href-vars': {
'marker': 'param/marker', 'marker': 'param/marker',
'limit': 'param/queue_limit', 'limit': 'param/queue_limit',
@ -38,7 +38,7 @@ JSON_HOME = {
}, },
}, },
'rel/queue': { 'rel/queue': {
'href-template': '/queues/{queue_name}', 'href-template': '/v1/queues/{queue_name}',
'href-vars': { 'href-vars': {
'queue_name': 'param/queue_name', 'queue_name': 'param/queue_name',
}, },
@ -50,7 +50,7 @@ JSON_HOME = {
}, },
}, },
'rel/queue-metadata': { 'rel/queue-metadata': {
'href-template': '/queues/{queue_name}/metadata', 'href-template': '/v1/queues/{queue_name}/metadata',
'href-vars': { 'href-vars': {
'queue_name': 'param/queue_name', 'queue_name': 'param/queue_name',
}, },
@ -62,7 +62,7 @@ JSON_HOME = {
}, },
}, },
'rel/queue-stats': { 'rel/queue-stats': {
'href-template': '/queues/{queue_name}/stats', 'href-template': '/v1/queues/{queue_name}/stats',
'href-vars': { 'href-vars': {
'queue_name': 'param/queue_name', 'queue_name': 'param/queue_name',
}, },
@ -78,7 +78,7 @@ JSON_HOME = {
# Messages # Messages
#------------------------------------------------------------------ #------------------------------------------------------------------
'rel/messages': { 'rel/messages': {
'href-template': ('/queues/{queue_name}/messages' 'href-template': ('/v1/queues/{queue_name}/messages'
'{?marker,limit,echo,include_claimed}'), '{?marker,limit,echo,include_claimed}'),
'href-vars': { 'href-vars': {
'queue_name': 'param/queue_name', 'queue_name': 'param/queue_name',

View File

@ -15,6 +15,7 @@
# limitations under the License. # limitations under the License.
import json import json
import urlparse
import falcon import falcon
@ -36,3 +37,24 @@ class TestHomeDocument(base.TestBase):
json.loads(body[0]) json.loads(body[0])
except ValueError: except ValueError:
self.fail('Home document is not valid JSON') self.fail('Home document is not valid JSON')
def test_href_template(self):
body = self.simulate_get('/v1')
self.assertEqual(self.srmock.status, falcon.HTTP_200)
resp = json.loads(body[0])
queue_href_template = resp['resources']['rel/queue']['href-template']
path_1 = 'https://marconi.example.com/v1'
path_2 = 'https://marconi.example.com/v1/'
# verify all the href template start with /v1
for resource in list(resp['resources']):
self.assertTrue(resp['resources'][resource]['href-template'].
startswith('/v1'))
url = urlparse.urljoin(path_1, queue_href_template)
self.assertEqual(url.format(queue_name='foo'),
'https://marconi.example.com/v1/queues/foo')
url = urlparse.urljoin(path_2, queue_href_template)
self.assertEqual(url.format(queue_name='foo'),
'https://marconi.example.com/v1/queues/foo')