rapi.testutils.FakeCurl: Add header support
authorMichael Hanselmann <hansmi@google.com>
Fri, 30 Mar 2012 14:41:04 +0000 (16:41 +0200)
committerMichael Hanselmann <hansmi@google.com>
Thu, 26 Apr 2012 19:33:59 +0000 (21:33 +0200)
With this patch headers are constructed from the PycURL options
and passed to the mock implementation.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: RenĂ© Nussbaumer <rn@google.com>

lib/rapi/testutils.py
test/ganeti.rapi.client_unittest.py

index eb8c691..258f379 100644 (file)
 
 import logging
 import re
+import mimetools
+import base64
 import pycurl
+from cStringIO import StringIO
 
 from ganeti import errors
 from ganeti import opcodes
+from ganeti import http
 
 
 _URI_RE = re.compile(r"https://(?P<host>.*):(?P<port>\d+)(?P<path>/.*)")
@@ -154,8 +158,29 @@ class FakeCurl:
     request_body = self._opts[pycurl.POSTFIELDS]
     writefn = self._opts[pycurl.WRITEFUNCTION]
 
+    if pycurl.HTTPHEADER in self._opts:
+      baseheaders = "\n".join(self._opts[pycurl.HTTPHEADER])
+    else:
+      baseheaders = ""
+
+    headers = mimetools.Message(StringIO(baseheaders), 0)
+
+    if request_body:
+      headers[http.HTTP_CONTENT_LENGTH] = str(len(request_body))
+
+    if self._opts.get(pycurl.HTTPAUTH, 0) & pycurl.HTTPAUTH_BASIC:
+      try:
+        userpwd = self._opts[pycurl.USERPWD]
+      except KeyError:
+        raise errors.ProgrammerError("Basic authentication requires username"
+                                     " and password")
+
+      headers[http.HTTP_AUTHORIZATION] = \
+        "%s %s" % (http.auth.HTTP_BASIC_AUTH, base64.b64encode(userpwd))
+
     path = _GetPathFromUri(url)
-    (code, resp_body) = self._handler.FetchResponse(path, method, request_body)
+    (code, resp_body) = \
+      self._handler.FetchResponse(path, method, headers, request_body)
 
     self._info[pycurl.RESPONSE_CODE] = code
     if resp_body is not None:
index 5949744..a09d4a2 100755 (executable)
@@ -74,7 +74,7 @@ class RapiMock(object):
   def GetLastRequestData(self):
     return self._last_req_data
 
-  def FetchResponse(self, path, method, request_body):
+  def FetchResponse(self, path, method, headers, request_body):
     self._last_req_data = request_body
 
     try:
@@ -139,11 +139,11 @@ class RapiMockTest(unittest.TestCase):
   def test(self):
     rapi = RapiMock()
     path = "/version"
-    self.assertEqual((404, None), rapi.FetchResponse("/foo", "GET", None))
+    self.assertEqual((404, None), rapi.FetchResponse("/foo", "GET", None, None))
     self.assertEqual((501, "Method not implemented"),
-                     rapi.FetchResponse("/version", "POST", None))
+                     rapi.FetchResponse("/version", "POST", None, None))
     rapi.AddResponse("2")
-    code, response = rapi.FetchResponse("/version", "GET", None)
+    code, response = rapi.FetchResponse("/version", "GET", None, None)
     self.assertEqual(200, code)
     self.assertEqual("2", response)
     self.failUnless(isinstance(rapi.GetLastHandler(), rlib2.R_version))