diff --git a/lib/rapi/testutils.py b/lib/rapi/testutils.py index eb8c69134b18d3de8111ebda9ff64d3a1bc33900..258f3798f989d01361baabf33dc565511a2a4476 100644 --- a/lib/rapi/testutils.py +++ b/lib/rapi/testutils.py @@ -25,10 +25,14 @@ 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: diff --git a/test/ganeti.rapi.client_unittest.py b/test/ganeti.rapi.client_unittest.py index 5949744407b1b02168b63fb633e969ad0a5b87fc..a09d4a2e2795e533d590d551f6b63d0e7c702148 100755 --- a/test/ganeti.rapi.client_unittest.py +++ b/test/ganeti.rapi.client_unittest.py @@ -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))