1from email import parser, message 2import sys 3 4 5class MockHTTPResponse(object): 6 def __init__(self, headers): 7 from betamax.util import coerce_content 8 9 h = ["%s: %s" % (k, v) for k in headers for v in headers.getlist(k)] 10 h = map(coerce_content, h) 11 h = '\r\n'.join(h) 12 if sys.version_info < (2, 7): 13 h = h.encode() 14 p = parser.Parser(EmailMessage) 15 # Thanks to Python 3, we have to use the slightly more awful API below 16 # mimetools was deprecated so we have to use email.message.Message 17 # which takes no arguments in its initializer. 18 self.msg = p.parsestr(h) 19 self.msg.set_payload(h) 20 21 def isclosed(self): 22 return False 23 24 25class EmailMessage(message.Message): 26 def getheaders(self, value, *args): 27 return self.get_all(value, []) 28