def test_shared_data_middleware(): """Shared data middleware""" def null_application(environ, start_response): start_response('404 NOT FOUND', [('Content-Type', 'text/plain')]) yield 'NOT FOUND' app = SharedDataMiddleware( null_application, { '/': path.join(path.dirname(__file__), 'res'), '/sources': path.join(path.dirname(__file__), 'res'), '/pkg': ('werkzeug.debug', 'shared') }) for p in '/test.txt', '/sources/test.txt': app_iter, status, headers = run_wsgi_app(app, create_environ(p)) assert status == '200 OK' assert ''.join(app_iter).strip() == 'FOUND' app_iter, status, headers = run_wsgi_app(app, create_environ('/pkg/body.tmpl')) contents = ''.join(app_iter) assert 'Werkzeug Debugger' in contents app_iter, status, headers = run_wsgi_app(app, create_environ('/missing')) assert status == '404 NOT FOUND' assert ''.join(app_iter).strip() == 'NOT FOUND'
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 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 _open(self, url, method='GET', data=None, refer=True, content_type=None): before_browser_activity.send(self) open_started = time() environ = self._create_environ(url, method, data, refer, content_type) # keep a copy, the app may mutate the environ request_environ = dict(environ) logger.info('%s(%s) == %s', method, url, request_uri(environ)) request_started = time() rv = run_wsgi_app(self._wsgi_app, environ) response = BaseResponse(*rv) # TODO: # response.make_sequence() # werkzeug 0.6+ # For now, must: response.response = list(response.response) if hasattr(rv[0], 'close'): rv[0].close() # end TODO # request is complete after the app_iter (rv[0]) has been fully read + # closed down. request_ended = time() self._request_environ = request_environ self._cookie_jar.extract_from_werkzeug(response, environ) self.status_code = response.status_code # Automatically follow redirects if 301 <= self.status_code <= 302: logger.debug("Redirect to %s", response.headers['Location']) after_browser_activity.send(self) self._open(response.headers['Location']) return # redirects report the original referrer self._referrer = request_uri(environ) self.status = response.status self.headers = response.headers # TODO: unicodify self.response = response.data self._sync_document() # TODO: what does a http-equiv redirect report for referrer? if 'meta[http-equiv=refresh]' in self.document: refresh = self.document['meta[http-equiv=refresh]'][0] if 'content' in refresh.attrib: parts = refresh.get('content').split(';url=', 1) if len(parts) == 2: logger.debug("HTTP-EQUIV Redirect to %s", parts[1]) after_browser_activity.send(self) self._open(parts[1]) return open_ended = time() request_time = request_ended - request_started logger.info("Fetched %s in %0.3fsec + %0.3fsec browser overhead", url, request_time, open_ended - open_started - request_time) after_browser_activity.send(self)
def test_shared_data_middleware(): """Shared data middleware""" def null_application(environ, start_response): start_response('404 NOT FOUND', [('Content-Type', 'text/plain')]) yield 'NOT FOUND' app = SharedDataMiddleware(null_application, { '/': path.join(path.dirname(__file__), 'res'), '/sources': path.join(path.dirname(__file__), 'res'), '/pkg': ('werkzeug.debug', 'shared') }) for p in '/test.txt', '/sources/test.txt': app_iter, status, headers = run_wsgi_app(app, create_environ(p)) assert status == '200 OK' assert ''.join(app_iter).strip() == 'FOUND' app_iter, status, headers = run_wsgi_app(app, create_environ('/pkg/body.tmpl')) contents = ''.join(app_iter) assert 'Werkzeug Debugger' in contents app_iter, status, headers = run_wsgi_app(app, create_environ('/missing')) assert status == '404 NOT FOUND' assert ''.join(app_iter).strip() == 'NOT FOUND'
def test_wsgi_app_return(): class TestView(View): def default(self): def hello_world(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return ['wsgi hw'] return hello_world v = TestView({}) r = v.process() appiter, status, headers = run_wsgi_app(r, rg.environ) eq_(''.join(appiter), 'wsgi hw')
def open(self, path='/', base_url=None, query_string=None, method='GET', data=None, input_stream=None, content_type=None, content_length=0, errors_stream=None, multithread=False, multiprocess=False, run_once=False, environ_overrides=None, buffered=True): parsed = urlparse(path) if parsed.scheme: if base_url is None: base_url = parsed.scheme + '://' + parsed.netloc if query_string is None: query_string = parsed.query path = parsed.path if (input_stream is None and data is not None and method in ('PUT', 'POST')): input_stream, content_length, content_type = \ self._prep_input(input_stream, data, content_type) if base_url is None: base_url = self.base_url or self.state.base_url environ = create_environ(path, base_url, query_string, method, input_stream, content_type, content_length, errors_stream, multithread, multiprocess, run_once) current_state = self.state current_state.prepare_environ(environ) if environ_overrides: environ.update(environ_overrides) logger.info("%s %s" % (method, request_uri(environ))) rv = run_wsgi_app(self.application, environ, buffered=buffered) response = _APIClientResponse(*rv) response.state = new_state = current_state.copy() new_state.process_response(response, environ) return response
def run_app(app, path='/'): env = create_environ(path, SECRET_KEY) return run_wsgi_app(app, env)
def run_app(app, path='/'): config = config_generate() env = create_environ(path, config["secret_key"]) return run_wsgi_app(app, env)