Example #1
0
 def from_dict(cls, dct):
     """Create a new cassette from *dct*, as deserialized from JSON
     or YAML format."""
     cassette = cls()
     for interaction in dct['http_interactions']:
         rq = interaction['request']
         # Overwrite the scheme and netloc, leaving just the part of
         # the URI that would be sent in a real request.
         relative_uri = urlunparse(('', '') + urlparse(rq['uri'])[2:])
         request = Request._construct(
             rq['method'], relative_uri, Headers(rq['headers']),
             SavedBodyProducer(body_from_dict(rq)),
             False, URI.fromBytes(rq['uri'].encode('utf-8')))
         rp = interaction['response']
         response = Response._construct(
             ('HTTP', 1, 1), rp['status']['code'], rp['status']['message'],
             Headers(rp['headers']), AbortableStringTransport(), request)
         content_length = response.headers.getRawHeaders('Content-Length')
         if content_length:
             try:
                 response.length = int(content_length[0])
             except ValueError:
                 pass
         cassette.responses.append(
             SavedResponse(response, body_from_dict(rp)))
     return cassette