atom and xml_detail working, with tests

This commit is contained in:
William Wolf
2011-07-25 18:28:43 -04:00
parent 71a103822b
commit 7be2b2482f
2 changed files with 183 additions and 66 deletions

View File

@@ -116,7 +116,7 @@ class VersionV10(object):
"version" : {
"id": "v1.0",
"status": "CURRENT",
"updated": "2011-01-21T11:33:21-06:00",
"updated": "2011-01-21T11:33:21Z",
"links": [
{
"rel": "self",
@@ -155,7 +155,7 @@ class VersionV11(object):
"version" : {
"id": "v1.1",
"status": "CURRENT",
"updated": "2011-01-21T11:33:21-06:00",
"updated": "2011-01-21T11:33:21Z",
"links": [
{
"rel": "self",
@@ -298,8 +298,33 @@ class VersionsAtomSerializer(wsgi.XMLDictSerializer):
link_href = link_href.rstrip('/')
return link_href.rsplit('/', 1)[0] + '/'
def _create_meta(self, root, versions):
title = self._create_text_elem('title', 'Available API Versions',
def _create_detail_meta(self, root, version):
title = self._create_text_elem('title', "About This Version",
type='text')
updated = self._create_text_elem('updated', version['updated'])
uri = version['links'][0]['href']
id = self._create_text_elem('id', uri)
link = self._xml_doc.createElement('link')
link.setAttribute('rel', 'self')
link.setAttribute('href', uri)
author = self._xml_doc.createElement('author')
author_name = self._create_text_elem('name', 'Rackspace')
author_uri = self._create_text_elem('uri', 'http://www.rackspace.com/')
author.appendChild(author_name)
author.appendChild(author_uri)
root.appendChild(title)
root.appendChild(updated)
root.appendChild(id)
root.appendChild(author)
root.appendChild(link)
def _create_list_meta(self, root, versions):
title = self._create_text_elem('title', "Available API Versions",
type='text')
# Set this updated to the most recently updated version
recent = self._get_most_recent_update(versions)
@@ -307,6 +332,7 @@ class VersionsAtomSerializer(wsgi.XMLDictSerializer):
base_url = self._get_base_url(versions[0]['links'][0]['href'])
id = self._create_text_elem('id', base_url)
link = self._xml_doc.createElement('link')
link.setAttribute('rel', 'self')
link.setAttribute('href', base_url)
@@ -341,7 +367,10 @@ class VersionsAtomSerializer(wsgi.XMLDictSerializer):
link_node = self._xml_doc.createElement('link')
link_node.setAttribute('rel', link['rel'])
link_node.setAttribute('href', link['href'])
entry.appendChild(link_node)
if 'type' in link:
link_node.setAttribute('type', link['type'])
entry.appendChild(link_node)
content = self._create_text_elem('content',
'Version %s %s (%s)' %
@@ -356,14 +385,18 @@ class VersionsAtomSerializer(wsgi.XMLDictSerializer):
def index(self, data):
self._xml_doc = minidom.Document()
node = self._xml_doc.createElementNS(self.xmlns, 'feed')
self._create_meta(node, data['versions'])
self._create_list_meta(node, data['versions'])
self._create_version_entries(node, data['versions'])
return self.to_xml_string(node)
def detail(self, data):
#TODO
pass
self._xml_doc = minidom.Document()
node = self._xml_doc.createElementNS(self.xmlns, 'feed')
self._create_detail_meta(node, data['version'])
self._create_version_entries(node, [data['version']])
return self.to_xml_string(node)
def create_resource(version='1.0'):
controller = {

View File

@@ -79,7 +79,7 @@ class VersionsTest(test.TestCase):
"version" : {
"id" : "v1.0",
"status" : "CURRENT",
"updated" : "2011-01-21T11:33:21-06:00",
"updated" : "2011-01-21T11:33:21Z",
"links": [
{
"rel" : "self",
@@ -125,7 +125,7 @@ class VersionsTest(test.TestCase):
"version" : {
"id" : "v1.1",
"status" : "CURRENT",
"updated" : "2011-01-21T11:33:21-06:00",
"updated" : "2011-01-21T11:33:21Z",
"links": [
{
"rel" : "self",
@@ -168,7 +168,7 @@ class VersionsTest(test.TestCase):
self.assertEqual(res.content_type, "application/xml")
expected = """
<version id="v1.0" status="CURRENT"
updated="2011-01-21T11:33:21-06:00"
updated="2011-01-21T11:33:21Z"
xmlns="http://docs.openstack.org/common/api/v1.0"
xmlns:atom="http://www.w3.org/2005/Atom">
@@ -204,7 +204,7 @@ class VersionsTest(test.TestCase):
self.assertEqual(res.content_type, "application/xml")
expected = """
<version id="v1.1" status="CURRENT"
updated="2011-01-21T11:33:21-06:00"
updated="2011-01-21T11:33:21Z"
xmlns="http://docs.openstack.org/common/api/v1.1"
xmlns:atom="http://www.w3.org/2005/Atom">
@@ -253,6 +253,80 @@ class VersionsTest(test.TestCase):
self.assertEqual(expected, actual)
def test_get_version_1_0_detail_atom(self):
#TODO
req = webob.Request.blank('/v1.0/')
req.accept = "application/xml"
res = req.get_response(fakes.wsgi_app())
self.assertEqual(res.status_int, 200)
self.assertEqual(res.content_type, "application/xml")
expected = """
<version id="v1.0" status="CURRENT"
updated="2011-01-21T11:33:21Z"
xmlns="http://docs.openstack.org/common/api/v1.0"
xmlns:atom="http://www.w3.org/2005/Atom">
<media-types>
<media-type base="application/xml"
type="application/vnd.openstack.compute-v1.0+xml"/>
<media-type base="application/json"
type="application/vnd.openstack.compute-v1.0+json"/>
</media-types>
<atom:link href="http://servers.api.openstack.org/v1.0/"
rel="self"/>
<atom:link href="http://docs.rackspacecloud.com/servers/
api/v1.0/cs-devguide-20110125.pdf"
rel="describedby"
type="application/pdf"/>
<atom:link href="http://docs.rackspacecloud.com/servers/
api/v1.0/application.wadl"
rel="describedby"
type="application/vnd.sun.wadl+xml"/>
</version>""".replace(" ", "").replace("\n", "")
actual = res.body.replace(" ", "").replace("\n", "")
self.assertEqual(expected, actual)
def test_get_version_1_1_detail_atom(self):
#TODO
req = webob.Request.blank('/v1.1/')
req.accept = "application/xml"
res = req.get_response(fakes.wsgi_app())
self.assertEqual(res.status_int, 200)
self.assertEqual(res.content_type, "application/xml")
expected = """
<version id="v1.1" status="CURRENT"
updated="2011-01-21T11:33:21Z"
xmlns="http://docs.openstack.org/common/api/v1.1"
xmlns:atom="http://www.w3.org/2005/Atom">
<media-types>
<media-type base="application/xml"
type="application/vnd.openstack.compute-v1.1+xml"/>
<media-type base="application/json"
type="application/vnd.openstack.compute-v1.1+json"/>
</media-types>
<atom:link href="http://servers.api.openstack.org/v1.1/"
rel="self"/>
<atom:link href="http://docs.rackspacecloud.com/servers/
api/v1.1/cs-devguide-20110125.pdf"
rel="describedby"
type="application/pdf"/>
<atom:link href="http://docs.rackspacecloud.com/servers/
api/v1.1/application.wadl"
rel="describedby"
type="application/vnd.sun.wadl+xml"/>
</version>""".replace(" ", "").replace("\n", "")
actual = res.body.replace(" ", "").replace("\n", "")
self.assertEqual(expected, actual)
def test_get_version_list_atom(self):
req = webob.Request.blank('/')
req.accept = "application/atom+xml"
@@ -361,12 +435,12 @@ class VersionsTest(test.TestCase):
response = response.replace(" ", "").replace("\n", "")
self.assertEqual(expected, response)
def test_versions_detail_xml_serializer(self):
versions_data = {
def test_version_detail_xml_serializer(self):
version_data = {
"version" : {
"id": "v1.0",
"status": "CURRENT",
"updated": "2011-01-21T11:33:21-06:00",
"updated": "2011-01-21T11:33:21Z",
"links": [
{
"rel": "self",
@@ -400,7 +474,7 @@ class VersionsTest(test.TestCase):
expected = """
<version id="v1.0" status="CURRENT"
updated="2011-01-21T11:33:21-06:00"
updated="2011-01-21T11:33:21Z"
xmlns="http://docs.openstack.org/common/api/v1.0"
xmlns:atom="http://www.w3.org/2005/Atom">
@@ -426,7 +500,7 @@ class VersionsTest(test.TestCase):
</version>""".replace(" ", "").replace("\n", "")
serializer = versions.VersionsXMLSerializer()
response = serializer.detail(versions_data)
response = serializer.detail(version_data)
response = response.replace(" ", "").replace("\n", "")
self.assertEqual(expected, response)
@@ -492,59 +566,69 @@ class VersionsTest(test.TestCase):
def test_version_detail_atom_serializer(self):
#TODO
versions_data = {
'versions': [
{
"id": "2.9.8",
"updated": "2011-07-20T11:40:00Z",
"status": "CURRENT",
"links": [
{
"rel": "self",
"href": "http://test/2.9.8",
}
],
},
]
"version" : {
"id": "v1.1",
"status": "CURRENT",
"updated": "2011-01-21T11:33:21Z",
"links": [
{
"rel": "self",
"href": "http://servers.api.openstack.org/v1.1/"
},
{
"rel": "describedby",
"type": "application/pdf",
"href": "http://docs.rackspacecloud.com/"
"servers/api/v1.1/cs-devguide-20110125.pdf"
},
{
"rel": "describedby",
"type": "application/vnd.sun.wadl+xml",
"href": "http://docs.rackspacecloud.com/"
"servers/api/v1.1/application.wadl"
},
],
"media-types": [
{
"base" : "application/xml",
"type" : "application/vnd.openstack.compute-v1.1+xml"
},
{
"base" : "application/json",
"type" : "application/vnd.openstack.compute-v1.1+json"
}
],
},
}
expected = """
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">
Available API Versions
</title>
<updated>
2011-07-20T11:40:00Z
</updated>
<id>
http://test/
</id>
<author>
<name>
Rackspace
</name>
<uri>
http://www.rackspace.com/
</uri>
</author>
<link href="http://test/" rel="self"/>
<entry>
<id>
http://test/2.9.8
</id>
<title type="text">
Version 2.9.8
</title>
<updated>
2011-07-20T11:40:00Z
</updated>
<link href="http://test/2.9.8" rel="self"/>
<content type="text">
Version 2.9.8 CURRENT (2011-07-20T11:40:00Z)
</content>
</entry>
</feed>""".replace(" ", "").replace("\n", "")
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">About This Version</title>
<updated>2011-01-21T11:33:21Z</updated>
<id>http://servers.api.openstack.org/v1.1/</id>
<author>
<name>Rackspace</name>
<uri>http://www.rackspace.com/</uri>
</author>
<link href="http://servers.api.openstack.org/v1.1/" rel="self"/>
<entry>
<id>http://servers.api.openstack.org/v1.1/</id>
<title type="text">Version v1.1</title>
<updated>2011-01-21T11:33:21Z</updated>
<link href="http://servers.api.openstack.org/v1.1/" rel="self"/>
<link href="http://docs.rackspacecloud.com/servers/
api/v1.1/cs-devguide-20110125.pdf"
rel="describedby" type="application/pdf"/>
<link href="http://docs.rackspacecloud.com/servers/
api/v1.1/application.wadl"
rel="describedby" type="application/vnd.sun.wadl+xml"/>
<content type="text">
Version v1.1 CURRENT (2011-01-21T11:33:21Z)
</content>
</entry>
</feed>""".replace(" ", "").replace("\n", "")
serializer = versions.VersionsAtomSerializer()
response = serializer.index(versions_data)
response = serializer.detail(versions_data)
response = response.replace(" ", "").replace("\n", "")
self.assertEqual(expected, response)