def test_clear_down_only_happens_if_no_request_for_timeout_period(): # Test that stale mock servers are not cleared if they # recently made a request. # TODO: Once the timeout specification can be dictated by the client # the sleep in this test can be reduced. http_mock = HTTPMock('localhost', 8000, pretender_timeout=5) pretender = http_mock.get_pretender() timeout_server = pretender.timeout_in_secs assert_equal(pretender.last_call, pretender.start) for i in range(3): # Sleep for a while, check that the server is still running and then # make a call to the mock server. time.sleep(timeout_server / 2) # Check that we are still running pretender = http_mock.get_pretender() # Make a call to the mock server. pretender_client = APIHelper( HTTPConnection(http_mock.pretend_access_point), '') pretender_client.http( method="GET", url="/some_url" )
def __init__(self, host, port, pretender_timeout=TIMEOUT_PRETENDER): self.host = host self.port = port self.pretender_timeout = pretender_timeout self.full_host = "{0}:{1}".format(self.host, self.port) self.connection = HTTPConnection(self.full_host) self.boss_access = APIHelper(self.connection, '') (self.pretend_access_point, self.pretend_access_point_id) = self._request_mock_access() if self.pretend_access_point: self.pretend_port = int(self.pretend_access_point.split(':')[1]) self.history = APIHelper(self.connection, '/history/{0}'.format( self.pretend_access_point_id)) self.preset = PresetHelper(self.connection, '/preset/{0}'.format( self.pretend_access_point_id))
def __init__(self, host, port, pretender_timeout=TIMEOUT_PRETENDER): self.host = host self.port = port self.pretender_timeout = pretender_timeout self.full_host = "{0}:{1}".format(self.host, self.port) self.connection = HTTPConnection(self.full_host) self.boss_access = APIHelper(self.connection, '') (self.pretend_access_point, self.pretend_access_point_id) = self._request_mock_access() if self.pretend_access_point: self.pretend_port = int(self.pretend_access_point.split(':')[1]) self.history = APIHelper( self.connection, '/history/{0}'.format(self.pretend_access_point_id)) self.preset = PresetHelper( self.connection, '/preset/{0}'.format(self.pretend_access_point_id))
def __init__(self, host, port, timeout=None): self.host = host self.port = port self.timeout = timeout self.full_host = "{0}:{1}".format(self.host, self.port) self.connection = HTTPConnection(self.full_host) self.boss_access = APIHelper(self.connection, '') LOGGER.info('Requesting {0} pretender. Port:{1} Timeout:{2}' .format(self.boss_mock_type, self.port, self.timeout)) if self.boss_mock_type: self.pretender_details = self._request_mock_access() else: self.pretender_details = {} self.history = APIHelper(self.connection, '/history/{0}'.format( self.pretend_access_point_id)) self.preset = PresetHelper(self.connection, '/preset/{0}'.format( self.pretend_access_point_id))
def test_clear_down_only_happens_if_no_request_for_timeout_period(): # Test that stale mock servers are not cleared if they # recently made a request. # TODO: Once the timeout specification can be dictated by the client # the sleep in this test can be reduced. http_mock = HTTPMock('localhost', 8000, pretender_timeout=5) pretender = http_mock.get_pretender() timeout_server = pretender.timeout_in_secs assert_equal(pretender.last_call, pretender.start) for i in range(3): # Sleep for a while, check that the server is still running and then # make a call to the mock server. time.sleep(timeout_server / 2) # Check that we are still running pretender = http_mock.get_pretender() # Make a call to the mock server. pretender_client = APIHelper( HTTPConnection(http_mock.pretend_access_point), '') pretender_client.http(method="GET", url="/some_url")
class BossClient(object): boss_mock_type = None def __init__(self, host, port, timeout=None, name=None): self.host = host self.port = port self.timeout = timeout self.name = name self.full_host = "{0}:{1}".format(self.host, self.port) self.connection = HTTPConnection(self.full_host) self.boss_access = APIHelper(self.connection, '') LOGGER.info('Requesting {0} pretender. Port:{1} Timeout:{2} ({3})' .format(self.boss_mock_type, self.port, self.timeout, self.name)) if self.boss_mock_type: self.pretender_details = self._request_mock_access() else: self.pretender_details = {} self.history = APIHelper(self.connection, '/history/{0}'.format( self.pretend_access_point_id)) self.preset = PresetHelper(self.connection, '/preset/{0}'.format( self.pretend_access_point_id)) def reset(self): """ Delete all history. """ self.history.reset() self.preset.reset() return self @property def create_mock_url(self): return "/{0}".format(self.boss_mock_type) @property def pretend_access_point_id(self): return self.pretender_details.get('id', "") @property def pretend_access_point(self): return self.full_host def _request_mock_access(self): """ Ask the boss to create a mock server by POSTing to ``create_mock_url`` :returns: A tuple containing: position 0: hostname[:port] of the mock server position 1: unique id of the pretender (for teardown purposes) """ post_body = {'name': self.name} if self.timeout: post_body['pretender_timeout'] = self.timeout post_body = json.dumps(post_body) response = self.boss_access.http('POST', url=self.create_mock_url, body=post_body) pretender_json = response.read().decode('ascii') pretender_details = json.loads(pretender_json) return pretender_details @property def delete_mock_url(self): return "{0}/{1}".format(self.create_mock_url, self.pretend_access_point_id) def delete_mock(self): "Delete the mock server that this points to." response = self.boss_access.http( method="DELETE", url=self.delete_mock_url) if not response.status == 200: raise Exception("Delete failed") def get_pretender(self): "Get pretenders from the server in dict format" response = self.boss_access.http( method='GET', url='/{0}/{1}'.format(self.boss_mock_type, self.pretend_access_point_id), ) if response.status == 200: return PretenderModel.from_json_response(response) elif response.status == 404: raise ResourceNotFound( 'The mock server for this client was shutdown.') else: raise UnexpectedResponseStatus(response.status)
class BossClient(object): boss_mock_type = '' def __init__(self, host, port, pretender_timeout=TIMEOUT_PRETENDER): self.host = host self.port = port self.pretender_timeout = pretender_timeout self.full_host = "{0}:{1}".format(self.host, self.port) self.connection = HTTPConnection(self.full_host) self.boss_access = APIHelper(self.connection, '') (self.pretend_access_point, self.pretend_access_point_id) = self._request_mock_access() if self.pretend_access_point: self.pretend_port = int(self.pretend_access_point.split(':')[1]) self.history = APIHelper(self.connection, '/history/{0}'.format( self.pretend_access_point_id)) self.preset = PresetHelper(self.connection, '/preset/{0}'.format( self.pretend_access_point_id)) def reset(self): """ Delete all history. """ self.history.reset() self.preset.reset() return self @property def create_mock_url(self): return "/pretender/{0}".format(self.boss_mock_type) def _request_mock_access(self): """ Ask the boss to create a mock server by POSTing to ``create_mock_url`` :returns: A tuple containing: position 0: url to the mock server position 1: unique id of the pretender (for teardown purposes) """ if self.boss_mock_type: post_body = json.dumps( {'pretender_timeout': self.pretender_timeout}) response = self.boss_access.http('POST', url=self.create_mock_url, body=post_body) pretender_json = response.read().decode('ascii') pretender_details = json.loads(pretender_json) return pretender_details["url"], pretender_details["id"] return "", "" @property def delete_mock_url(self): return "{0}/{1}".format(self.create_mock_url, self.pretend_access_point_id) def get_pretender(self): "Get pretenders from the server in dict format" response = self.boss_access.http( method='GET', url='/pretender/{0}'.format(self.pretend_access_point_id), ) if response.status == 200: return PretenderModel.from_json_response(response) elif response.status == 404: raise ResourceNotFound( 'The mock server for this client was shutdown.') else: raise UnexpectedResponseStatus(response.status)
class BossClient(object): boss_mock_type = '' def __init__(self, host, port, pretender_timeout=TIMEOUT_PRETENDER): self.host = host self.port = port self.pretender_timeout = pretender_timeout self.full_host = "{0}:{1}".format(self.host, self.port) self.connection = HTTPConnection(self.full_host) self.boss_access = APIHelper(self.connection, '') (self.pretend_access_point, self.pretend_access_point_id) = self._request_mock_access() if self.pretend_access_point: self.pretend_port = int(self.pretend_access_point.split(':')[1]) self.history = APIHelper( self.connection, '/history/{0}'.format(self.pretend_access_point_id)) self.preset = PresetHelper( self.connection, '/preset/{0}'.format(self.pretend_access_point_id)) def reset(self): """ Delete all history. """ self.history.reset() self.preset.reset() return self @property def create_mock_url(self): return "/pretender/{0}".format(self.boss_mock_type) def _request_mock_access(self): """ Ask the boss to create a mock server by POSTing to ``create_mock_url`` :returns: A tuple containing: position 0: url to the mock server position 1: unique id of the pretender (for teardown purposes) """ if self.boss_mock_type: post_body = json.dumps( {'pretender_timeout': self.pretender_timeout}) response = self.boss_access.http('POST', url=self.create_mock_url, body=post_body) pretender_json = response.read().decode('ascii') pretender_details = json.loads(pretender_json) return pretender_details["url"], pretender_details["id"] return "", "" @property def delete_mock_url(self): return "{0}/{1}".format(self.create_mock_url, self.pretend_access_point_id) def get_pretender(self): "Get pretenders from the server in dict format" response = self.boss_access.http( method='GET', url='/pretender/{0}'.format(self.pretend_access_point_id), ) if response.status == 200: return PretenderModel.from_json_response(response) elif response.status == 404: raise ResourceNotFound( 'The mock server for this client was shutdown.') else: raise UnexpectedResponseStatus(response.status)