def open(self, *args, **kwargs): if self.context_preserved: _request_ctx_stack.pop() self.context_preserved = False kwargs.setdefault('environ_overrides', {}) \ ['flask._preserve_context'] = self.preserve_context as_tuple = kwargs.pop('as_tuple', False) buffered = kwargs.pop('buffered', False) follow_redirects = kwargs.pop('follow_redirects', False) builder = EnvironBuilder(*args, **kwargs) if self.application.config.get('SERVER_NAME'): server_name = self.application.config.get('SERVER_NAME') if ':' not in server_name: http_host, http_port = server_name, None else: http_host, http_port = server_name.split(':', 1) if builder.base_url == 'http://localhost/': # Default Generated Base URL if http_port != None: builder.host = http_host + ':' + http_port else: builder.host = http_host old = _request_ctx_stack.top try: return Client.open(self, builder, as_tuple=as_tuple, buffered=buffered, follow_redirects=follow_redirects) finally: self.context_preserved = _request_ctx_stack.top is not old
def run(self, args): from IPython.Shell import IPShellEmbed from werkzeug import Client, EnvironBuilder from dextrose.context import Context from dextrose.http import Response app = load_application(args.package, args.environment) environ_builder = EnvironBuilder() environ = environ_builder.get_environ() request = environ_builder.get_request() client = Client(app, Response) with Context(app, environ, request, {}) as context: banner = "Dextrose IPython shell\n%s" % args.package shell = IPShellEmbed( banner=banner, argv=[ '-prompt_in1', '%s> ' % args.package, '-prompt_in2', '%s... ' % (' ' * (len(args.package) - 3)), '-prompt_out', '=> ' ]) shell(global_ns={}, local_ns={ 'app': app, 'environ': environ, 'client': client, 'context': context })
def test_upload(self): # prepare form data _data = {'file': (StringIO('This is a test'), 'dummy.txt')} # build environ _builder = EnvironBuilder('/upload', method='POST', data=_data) _environ = _builder.get_environ() # call _app_iter, _status, _headers = run_wsgi_app(upload.app, _environ) # parse response for _i in _app_iter: # load data _data = json.loads(_i) # get filename _content_type = _data['contentType'] _file_name = _data['fileName'] # set upload path main var for removal self.upload_file_path = os.path.join(c.path.uploads, _file_name) # check content type assert _content_type == 'text/plain',\ 'Found: %s' % _content_type # check was uploaded assert os.path.exists(self.upload_file_path),\ 'Found: %s' % self.upload_file_path # open file _f = open(self.upload_file_path) # check content assert _f.read() == 'This is a test' _f.close()
def main(verbose=False): if (verbose): print "%s: start testing" % __name__ builder = EnvironBuilder(path='/', method='GET', query_string=None) (iter, status, headers) = werkzeug.run_wsgi_app(gdispatch.get_application(), builder.get_environ()) assert status == '200 OK', status assert ('Content-Length', '4') in headers assert "".join(iter) == "root" builder = EnvironBuilder(path='/sub', method='GET', query_string="foo=bar") (iter, status, headers) = werkzeug.run_wsgi_app(gdispatch.get_application(), builder.get_environ()) assert status == '200 OK', status assert "".join(iter) == "get sub(foo=bar)" builder = EnvironBuilder(path='/sub', method='POST', query_string=None) (iter, status, headers) = werkzeug.run_wsgi_app(gdispatch.get_application(), builder.get_environ()) assert status == '200 OK', status assert "".join(iter) == "post sub" builder = EnvironBuilder(path='/api/f1', method='GET', query_string=None) (iter, status, headers) = werkzeug.run_wsgi_app(gdispatch.get_application(), builder.get_environ()) assert status == '200 OK', status assert "".join(iter) == "api(/api/f1)" builder = EnvironBuilder(path='/error', method='GET', query_string=None) (iter, status, headers) = werkzeug.run_wsgi_app(gdispatch.get_application(), builder.get_environ()) assert status == '404 Not Found', status if (verbose): print "%s: testing complete" % __name__
def open(self, *args, **kw): # Copy the kwargs here since we will # modify them for passing to EnvironBuilder(). kwargs = kw.copy() # Pop off extra options that open() supports but that # will cause EnvironBuilder() to choke. as_tuple = kwargs.pop('as_tuple', False) buffered = kwargs.pop('buffered', False) follow_redirects = kwargs.pop('follow_redirects', False) environ = None if not kwargs and len(args) == 1: if isinstance(args[0], EnvironBuilder): environ = args[0].get_environ() elif isinstance(args[0], dict): environ = args[0] if environ is None: builder = EnvironBuilder(*args, **kwargs) try: environ = builder.get_environ() finally: builder.close() old_env = os.environ.copy() old_cwd = os.getcwd() stripped_environ = {} for key in environ.keys(): if isinstance(environ[key], basestring): stripped_environ[key] = environ[key] try: # Add the variables for the current environment to # the OS environment to mimic the appengine CGI environment. # Don't clear the os.environ since we have set up a number # of variables for the environment already. We only want to # add variables pertaining to the current request. os.environ.update(stripped_environ) # Add a dummy value for the appengine specific CGI variable. os.environ["PATH_TRANSLATED"] = "main.py" # Add additional environment variables like the app id and # version number. #setup_env() # Add the AUTH_DOMAIN #os.environ["AUTH_DOMAIN"] return super(Client, self).open(*args, **kw) finally: # Make sure we reset the os environment to the old value. os.environ.clear() os.environ.update(old_env)
def __init__(self, application, response_wrapper=None, use_cookies=True): super(Client, self).__init__(application, response_wrapper=response_wrapper, use_cookies=True) builder = EnvironBuilder() try: env = builder.get_environ() finally: builder.close() self.application.app._prepare(env) for key, submount_app in self.application.mounts.iteritems(): if not hasattr(submount_app, 'app_settings') or key == "/_kay": continue submount_app._prepare(env)
def run(self, args): from IPython.Shell import IPShellEmbed from werkzeug import Client, EnvironBuilder from dextrose.context import Context from dextrose.http import Response app = load_application(args.package, args.environment) environ_builder = EnvironBuilder() environ = environ_builder.get_environ() request = environ_builder.get_request() client = Client(app, Response) with Context(app, environ, request, {}) as context: banner="Dextrose IPython shell\n%s" % args.package shell = IPShellEmbed(banner=banner, argv=[ '-prompt_in1', '%s> ' % args.package, '-prompt_in2', '%s... ' % (' ' * (len(args.package) - 3)), '-prompt_out', '=> ' ]) shell(global_ns={}, local_ns={ 'app': app, 'environ': environ, 'client': client, 'context': context })