def test_form(): app = TestApp(CGIApplication({}, script='form.cgi', path=[data_dir])) res = app.post('', params={'name': b'joe'}, upload_files=[('up', 'file.txt', b'x'*10000)]) assert 'file.txt' in res assert 'joe' in res assert 'x'*10000 in res
def create_mapserver_app(): cgi = CGIApplication({}, os.environ.get('MAPSERV_BIN', 'mapserv')) def app(environ, start_response): request = Request(environ) request.GET['map'] = VIEWER_HOME / 'money.map' request.GET['SRS'] = 'EPSG:3857' return cgi(environ, start_response) return app
def create_mapserver_app(): mapserv_cgi = CGIApplication({}, os.environ.get('MAPSERV_BIN', 'mapserv')) @wsgify def mapserv_wrapper(request): request.GET['map'] = VIEWER_HOME / 'money.map' request.GET['SRS'] = 'EPSG:3857' return request.get_response(mapserv_cgi) return mapserv_wrapper
def create_git_app(repo): git_http_backend = Path(__file__).parent.absolute() / 'git-http-backend' cgi = CGIApplication({}, str(git_http_backend)) @responder def git_app(environ, start_response): environ['GIT_PROJECT_ROOT'] = repo environ['REMOTE_USER'] = environ.get('HTTP_X_FORWARDED_USER') return cgi return git_app
def create_git_app(repo): git_http_backend = Path(__file__).parent.absolute() / 'git-http-backend' cgi = CGIApplication({}, str(git_http_backend)) @responder def git_app(environ, start_response): environ['GIT_PROJECT_ROOT'] = repo environ['GIT_HTTP_EXPORT_ALL'] = '' environ['REMOTE_USER'] = '******' return cgi return git_app
def create_git_app(repo): """ Sets up the Git App (setting git project root and remote user). Arguments: repo [str] -- the Git repo that the project is located in. Returns: the 'git app' """ git_http_backend = Path(__file__).parent.absolute() / 'git-http-backend' cgi = CGIApplication({}, str(git_http_backend)) @responder def git_app(environ, start_response): environ['GIT_PROJECT_ROOT'] = repo environ['REMOTE_USER'] = environ.get('HTTP_X_FORWARDED_USER') return cgi return git_app
def test_stderr(): app = TestApp(CGIApplication({}, script='stderr.cgi', path=[data_dir])) res = app.get('', expect_errors=True) assert res.status == 500 assert 'error' in res assert b'some data' in res.errors
def test_error(): app = TestApp(CGIApplication({}, script='error.cgi', path=[data_dir])) pytest.raises(CGIError, app.get, '', status=500)
def test_ok(): app = TestApp(CGIApplication({}, script='ok.cgi', path=[data_dir])) res = app.get('') assert res.header('content-type') == 'text/html; charset=UTF-8' assert res.full_status == '200 Okay' assert 'This is the body' in res
def application(req): if req.method == 'OPTIONS': resp = Response('', content_type='text/plain') resp.allow = 'GET,POST,OPTIONS' add_access_headers(resp) return resp script_path = None if req.path_info == '/graphs.html': # Must redirect raise exc.HTTPFound(location='/') py_name = None ## Rewrite rules: if re.match('/api/test/?$', req.path_info): req.path_info = '/server/api.cgi' req.GET['item'] = 'tests' elif re.match(r'/api/test/run/info/?$', req.path_info): req.path_info = '/server/api.cgi' req.GET['item'] = 'testrun' elif re.match(r'/api/test/runs/values/?$', req.path_info): req.path_info = '/server/api.cgi' req.GET['item'] = 'testrun' req.GET['attribute'] = 'values' elif re.match(r'/api/test/runs/revisions/?$', req.path_info): req.path_info = '/server/api.cgi' req.GET['item'] = 'testrun' req.GET['attribute'] = 'revisions' elif re.match(r'/api/test/runs/latest/?$', req.path_info): req.path_info = '/server/api.cgi' req.GET['item'] = 'testrun' req.GET['id'] = '' # The RewriteRule doesn't make sense req.GET['attribute'] = 'latest' elif re.match(r'/api/test/runs?$', req.path_info): req.path_info = '/server/api.cgi' req.GET['item'] = 'testruns' else: match = re.match('/api/test/([0-9]+)/?$', req.path_info) if match: req.path_info = '/server/api.cgi' req.GET['item'] = 'test' req.GET['id'] = match.group(1) if req.path_info_peek() == 'server': req.path_info_pop() script_path = os.path.join(cgi_scripts, req.path_info.lstrip('/')) script_path = os.path.abspath(script_path) assert script_path.startswith(cgi_scripts + '/') if script_path.endswith('.cgi'): py_name = script_path[:-4] py_name = os.path.join(os.path.dirname(py_name), os.path.basename(py_name).replace('-', '_')) py_name += '_cgi.py' if not os.path.exists(py_name): py_name = None if (not os.path.isfile(script_path) and py_name is None): raise exc.HTTPNotFound('Does not point to a file: %r' % script_path) if script_path is None and py_name is None: raise exc.HTTPNotFound() if py_name: mod_name = os.path.basename(py_name)[:-3].replace('/', '.') __import__(mod_name) mod = sys.modules[mod_name] app = mod.application else: app = CGIApplication({}, script_path) resp = req.get_response(app) add_access_headers(resp) return resp