Commit de4f53e6 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

Support different HTTP error codes in RAPI client unittest mock

This allows testing error conditions (e.g. if a job wasn't found).
Signed-off-by: default avatarMichael Hanselmann <>
Reviewed-by: default avatarDavid Knowles <>
parent 09127904
...@@ -81,40 +81,32 @@ class OpenerDirectorMock: ...@@ -81,40 +81,32 @@ class OpenerDirectorMock:
class RapiMock(object): class RapiMock(object):
def __init__(self): def __init__(self):
self._mapper = connector.Mapper() self._mapper = connector.Mapper()
self._responses = [] self._responses = []
self._last_handler = None self._last_handler = None
def AddResponse(self, response): def AddResponse(self, response, code=200):
self._responses.insert(0, response) self._responses.insert(0, (code, response))
def PopResponse(self):
if len(self._responses) > 0:
return self._responses.pop()
return None
def GetLastHandler(self): def GetLastHandler(self):
return self._last_handler return self._last_handler
def FetchResponse(self, path, method): def FetchResponse(self, path, method):
code = 200
response = None
try: try:
HandlerClass, items, args = self._mapper.getController(path) HandlerClass, items, args = self._mapper.getController(path)
self._last_handler = HandlerClass(items, args, None) self._last_handler = HandlerClass(items, args, None)
if not hasattr(self._last_handler, method.upper()): if not hasattr(self._last_handler, method.upper()):
code = 501 raise http.HttpNotImplemented(message="Method not implemented")
response = "Method not implemented"
except http.HttpException, ex: except http.HttpException, ex:
code = ex.code code = ex.code
response = ex.message response = ex.message
if not self._responses:
raise Exception("No responses")
if not response: (code, response) = self._responses.pop()
response = self.PopResponse()
return code, response return code, response
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment