Refactored port configuration strategy to allow a single test case to address both the admin and service API's

This commit is contained in:
Dolph Mathews
2011-07-14 10:55:10 -05:00
parent 0d8f222173
commit 936670fd65
2 changed files with 28 additions and 36 deletions

View File

@@ -4,13 +4,8 @@ import httplib
class RestfulTestCase(unittest.TestCase):
"""Performs generic HTTP request testing"""
def setUp(self):
"""Sets default connection settings"""
self.host = '127.0.0.1'
self.port = 80
def request(self, method='GET', path='/', headers={}, body=None,
expect_exception=False):
def request(self, host='127.0.0.1', port=80, method='GET', path='/',
headers={}, body=None, expect_exception=False,):
"""Perform request and fetch httplib.HTTPResponse from the server
Dynamically includes 'json' and 'xml' attributes based on the detected
@@ -21,7 +16,7 @@ class RestfulTestCase(unittest.TestCase):
"""
# Initialize a connection
connection = httplib.HTTPConnection(self.host, self.port, timeout=3)
connection = httplib.HTTPConnection(host, port, timeout=3)
# Perform the request
connection.request(method, path, body, headers)
@@ -42,7 +37,7 @@ class RestfulTestCase(unittest.TestCase):
# Attempt to parse JSON and XML automatically, if detected
response = self._parseResponseBody(response)
# This contains the response headers, body, parsed json/xml, etc
# Contains the response headers, body, parsed json/xml, etc
return response
def assertSuccessfulResponse(self, status_code):
@@ -77,22 +72,19 @@ class RestfulTestCase(unittest.TestCase):
except Exception as e:
self.fail(e)
class ServiceTestCase(RestfulTestCase):
"""Perform generic HTTP request testing against Service API"""
class KeystoneTestCase(RestfulTestCase):
"""Perform generic HTTP request against Keystone APIs"""
def setUp(self):
"""Sets custom connection settings"""
super(ServiceTestCase, self).setUp()
def service_request(self, **kwargs):
"""Returns a request to the service API"""
# Override request with expected service port
kwargs['port'] = 5000
# Override parent's connection settings
self.port = 5000 # The port the service API is expected to run on
class AdminTestCase(RestfulTestCase):
"""Perform generic HTTP request testing against Admin API"""
return self.request(**kwargs)
def setUp(self):
"""Sets custom connection settings"""
super(AdminTestCase, self).setUp()
def admin_request(self, **kwargs):
"""Returns a request to the admin API"""
# Override request with expected admin port
kwargs['port'] = 5001
# Override parent's connection settings
self.port = 5001 # The port the admin API is expected to run on
return self.request(**kwargs)

View File

@@ -1,55 +1,55 @@
import unittest
from common import AdminTestCase
from common import KeystoneTestCase
class TestUrlHandling(AdminTestCase):
class TestUrlHandling(KeystoneTestCase):
"""Tests API's global URL handling behaviors"""
def test_optional_trailing_slash(self):
"""Same response returned regardless of a trailing slash in the url."""
r1 = self.request(path='/v2.0/')
r2 = self.request(path='/v2.0')
r1 = self.admin_request(path='/v2.0/')
r2 = self.admin_request(path='/v2.0')
self.assertEqual(r1.read(), r2.read())
class TestContentTypes(AdminTestCase):
class TestContentTypes(KeystoneTestCase):
"""Tests API's Content-Type handling"""
def test_default_content_type(self):
"""Service returns JSON without being asked to"""
r = self.request(path='/v2.0')
r = self.admin_request(path='/v2.0')
self.assertTrue('application/json' in r.getheader('Content-Type'))
def test_xml_extension(self):
"""Service responds to .xml URL extension"""
r = self.request(path='/v2.0.xml')
r = self.admin_request(path='/v2.0.xml')
self.assertTrue('application/xml' in r.getheader('Content-Type'))
def test_json_extension(self):
"""Service responds to .json URL extension"""
r = self.request(path='/v2.0.json')
r = self.admin_request(path='/v2.0.json')
self.assertTrue('application/json' in r.getheader('Content-Type'))
def test_xml_accept_header(self):
"""Service responds to xml Accept header"""
r = self.request(path='/v2.0',
r = self.admin_request(path='/v2.0',
headers={'Accept': 'application/xml'})
self.assertTrue('application/xml' in r.getheader('Content-Type'))
def test_json_accept_header(self):
"""Service responds to json Accept header"""
r = self.request(path='/v2.0',
r = self.admin_request(path='/v2.0',
headers={'Accept': 'application/json'})
self.assertTrue('application/json' in r.getheader('Content-Type'))
def test_xml_extension_overrides_conflicting_header(self):
"""Service returns XML when Accept header conflicts with extension"""
r = self.request(path='/v2.0.xml',
r = self.admin_request(path='/v2.0.xml',
headers={'Accept': 'application/json'})
self.assertTrue('application/xml' in r.getheader('Content-Type'))
def test_json_extension_overrides_conflicting_header(self):
"""Service returns JSON when Accept header conflicts with extension"""
r = self.request(path='/v2.0.json',
r = self.admin_request(path='/v2.0.json',
headers={'Accept': 'application/xml'})
self.assertTrue('application/json' in r.getheader('Content-Type'))