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

View File

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