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

rapi.testutils.FakeCurl: Add header support

With this patch headers are constructed from the PycURL options
and passed to the mock implementation.
Signed-off-by: default avatarMichael Hanselmann <>
Reviewed-by: default avatarRené Nussbaumer <>
parent f90a1ab5
......@@ -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])
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:
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:
......@@ -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
......@@ -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))
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))
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