示例#1
0
 def build_stub_environ(self, app):
     kw = {'base_url': 'http://localhost'}
     builder = EnvironBuilder(self.app, **kw)
     try:
         environ = builder.get_environ()
     finally:
         builder.close()
     return environ
示例#2
0
    def send(self, request, **kwargs):
        kw = {
            'environ_base': self.environ_base,
            'method': request.method,
            'data': request.body,
            'headers': request.headers.items()
        }
        builder = EnvironBuilder(app=self.app, path=request.path_url, **kw)

        try:
            environ = builder.get_environ()
        finally:
            builder.close()

        rv = run_wsgi_app(self.app, environ, buffered=True)
        return self.build_response(request, rv)
示例#3
0
    def _create_wsgi_request(self, request_args: List,
                             request_info: dict) -> Request:
        # Same logic as super.open, which we use to build the
        # werkzeug Request.
        # https://github.com/pallets/flask/blob/master/src/flask/testing.py#L164
        request = None

        def copy_environ(other):
            return {
                **self.environ_base,
                **other,
                "flask._preserve_context": self.preserve_context,
            }

        if not request_info and len(request_args) == 1:
            arg = request_args[0]

            if isinstance(arg, werkzeug.test.EnvironBuilder):
                builder = copy(arg)
                builder.environ_base = copy_environ(builder.environ_base or {})
                request = builder.get_request()
            elif isinstance(arg, dict):
                request = EnvironBuilder.from_environ(
                    arg, app=self.application,
                    environ_base=copy_environ({})).get_request()
            elif isinstance(arg, Request):
                request = copy(arg)
                request.environ = copy_environ(request.environ)

        if request is None:
            request_info["environ_base"] = copy_environ(
                request_info.get("environ_base", {}))
            builder = EnvironBuilder(self.application, *request_args,
                                     **request_info)

            try:
                request = builder.get_request()
            finally:
                builder.close()

        return request