From de4f53e6a407330ebf07e903daa8aedb33369ef1 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Tue, 11 May 2010 14:47:15 +0200
Subject: [PATCH] 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: Michael Hanselmann <hansmi@google.com>
Reviewed-by: David Knowles <dknowles@google.com>
---
 test/ganeti.rapi.client_unittest.py | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/test/ganeti.rapi.client_unittest.py b/test/ganeti.rapi.client_unittest.py
index 0c3e20c3e..abe3d3385 100755
--- a/test/ganeti.rapi.client_unittest.py
+++ b/test/ganeti.rapi.client_unittest.py
@@ -81,40 +81,32 @@ class OpenerDirectorMock:
 
 
 class RapiMock(object):
-
   def __init__(self):
     self._mapper = connector.Mapper()
     self._responses = []
     self._last_handler = None
 
-  def AddResponse(self, response):
-    self._responses.insert(0, response)
-
-  def PopResponse(self):
-    if len(self._responses) > 0:
-      return self._responses.pop()
-    else:
-      return None
+  def AddResponse(self, response, code=200):
+    self._responses.insert(0, (code, response))
 
   def GetLastHandler(self):
     return self._last_handler
 
   def FetchResponse(self, path, method):
-    code = 200
-    response = None
-
     try:
       HandlerClass, items, args = self._mapper.getController(path)
       self._last_handler = HandlerClass(items, args, None)
       if not hasattr(self._last_handler, method.upper()):
-        code = 501
-        response = "Method not implemented"
+        raise http.HttpNotImplemented(message="Method not implemented")
+
     except http.HttpException, ex:
       code = ex.code
       response = ex.message
+    else:
+      if not self._responses:
+        raise Exception("No responses")
 
-    if not response:
-      response = self.PopResponse()
+      (code, response) = self._responses.pop()
 
     return code, response
 
-- 
GitLab