import logging
import re
-import mimetools
import base64
import pycurl
from cStringIO import StringIO
return None
+def _FormatHeaders(headers):
+ """Formats HTTP headers.
+
+ @type headers: sequence of strings
+ @rtype: string
+
+ """
+ assert compat.all(": " in header for header in headers)
+ return "\n".join(headers)
+
+
class FakeCurl:
"""Fake cURL object.
writefn = self._opts[pycurl.WRITEFUNCTION]
if pycurl.HTTPHEADER in self._opts:
- baseheaders = "\n".join(self._opts[pycurl.HTTPHEADER])
+ baseheaders = _FormatHeaders(self._opts[pycurl.HTTPHEADER])
else:
baseheaders = ""
- headers = mimetools.Message(StringIO(baseheaders), 0)
+ headers = http.ParseHeaders(StringIO(baseheaders))
if request_body:
headers[http.HTTP_CONTENT_LENGTH] = str(len(request_body))
"%s %s" % (http.auth.HTTP_BASIC_AUTH, base64.b64encode(userpwd))
path = _GetPathFromUri(url)
- (code, resp_body) = \
+ (code, _, resp_body) = \
self._handler.FetchResponse(path, method, headers, request_body)
self._info[pycurl.RESPONSE_CODE] = code
"""Mocking out the RAPI server parts.
"""
- def __init__(self, user_fn, luxi_client):
+ def __init__(self, user_fn, luxi_client, reqauth=False):
"""Initialize this class.
@type user_fn: callable
"""
self.handler = \
- server.rapi.RemoteApiHandler(user_fn, _client_cls=luxi_client)
+ server.rapi.RemoteApiHandler(user_fn, reqauth, _client_cls=luxi_client)
def FetchResponse(self, path, method, headers, request_body):
"""This is a callback method used to fetch a response.
@param request_body: Request body
@type headers: mimetools.Message
@param headers: Request headers
- @return: Tuple containing status code and response body
+ @return: Tuple containing status code, response headers and response body
"""
req_msg = http.HttpMessage()
(_, _, _, resp_msg) = \
http.server.HttpResponder(self.handler)(lambda: (req_msg, None))
- return (resp_msg.start_line.code, resp_msg.body)
+ return (resp_msg.start_line.code, resp_msg.headers, resp_msg.body)
class _TestLuxiTransport:
"""
return self._called
- def __call__(self):
+ def __call__(self, address=None):
"""Creates an instrumented LUXI client.
The LUXI client will record all method calls (use L{CalledNames} to
"""
return luxi.Client(transport=compat.partial(_TestLuxiTransport,
- self.Record))
+ self.Record),
+ address=address)
def _TestWrapper(fn, *args, **kwargs):