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
Example #2
0
    def fetch(self, path, list_as_set=False, **kwargs):
        if type(kwargs.get('postdata', None)) == dict:
            kwargs['postdata'] = urlencode(kwargs['postdata'], doseq=True)

        if self.use_networking:
            dResponse = cyclone.httpclient.fetch(
                url=self.getUrl(path=path), **kwargs
            )
        else:
            from cyclone.httpclient import Receiver
            from twisted.web._newclient import Request, Response
            from twisted.web.http_headers import Headers
            from twisted.test.proto_helpers import AccumulatingProtocol

            server_proto = self.app.buildProtocol( ('127.0.0.1', 0) )
            server_transport = proto_helpers.StringTransport()
            server_proto.makeConnection(server_transport)

            rawHeaders = kwargs.get('headers', {})
            rawHeaders['Host'] = ['localhost']
            req = Request(
                method=kwargs['method'],
                uri=path,
                headers=Headers( rawHeaders=rawHeaders ),
                bodyProducer=None,
            )
            req.writeTo(server_transport)
            server_proto.dataReceived(server_transport.value())
            print server_transport.value()

            # Strip out the original request.
            parts = server_transport.value().split("\r\n\r\n")
            actual_response = "\r\n\r\n".join(parts[1:])

            print actual_response

            from twisted.web._newclient import HTTPClientParser
            client_parser = HTTPClientParser(req, lambda x:x)
            client_parser.makeConnection(proto_helpers.StringTransport())
            client_parser.dataReceived(actual_response)
            response = client_parser.response

            # This was done in cyclone.httpclient
            if response.code in (204, 304):
                response.body = ''
                dResponse = defer.succeed(response)
            else:
                dResponse = defer.Deferred()
                response.deliverBody(Receiver(dResponse))
                def set_response(value):
                    response.body = value
                    return response
                dResponse.addCallback(set_response)

            # This triggers response.deliverBody. It needs a reason of some kind
            client_parser.connectionLost('Finished')

        if list_as_set:
            def decode_body(res):
                res.body = json.loads(res.body, object_hook=decode_list_as_set)
                return res
            dResponse.addCallback(decode_body)
        else:
            def decode_body(res):
                try:
                    res.body = json.loads(res.body)
                except:
                    print "counld not parse body: %s" % res.body
                return res
            dResponse.addCallback(decode_body)

        return dResponse