def test_request(self): dir_path = testing.create_test_pod_dir() pod = pods.Pod(dir_path) pod.router.add_all(use_cache=False) # When serving a pod, should 200. app = main.create_wsgi_app(pod, 'localhost', 8080) request = webob.Request.blank('/') response = request.get_response(app) self.assertEqual(200, response.status_int) # Verify 404 is sent for page not found. request = webob.Request.blank('/dummy/page/') response = request.get_response(app) self.assertEqual(404, response.status_int) # Verify 206 for partial content. headers = {'Range': 'bytes=0-4'} request = webob.Request.blank('/public/file.txt', headers=headers) response = request.get_response(app) self.assertEqual(206, response.status_int) self.assertEqual('bytes 0-4/13', response.headers['Content-Range']) self.assertEqual(b'Hello', response.body) headers = {'Range': 'bytes=5-13'} request = webob.Request.blank('/public/file.txt', headers=headers) response = request.get_response(app) self.assertEqual('bytes 5-12/13', response.headers['Content-Range']) self.assertEqual(b' World!\n', response.body) # Verify full response when headers omitted. request = webob.Request.blank('/public/file.txt') response = request.get_response(app) self.assertEqual(b'Hello World!\n', response.body) # Verify 304. url_path = '/public/file.txt' matched = pod.router.routes.match(url_path) controller = pod.router.get_render_controller(url_path, matched.value, params=matched.params) response_headers = controller.get_http_headers() headers = {'If-None-Match': response_headers['Last-Modified']} request = webob.Request.blank(url_path, headers=headers) response = request.get_response(app) self.assertEqual(304, response.status_int) self.assertEqual(b'', response.body) response = request.get_response(app) self.assertEqual(304, response.status_int) self.assertEqual(b'', response.body) # Verify sitemap on server. path = '/root/sitemap.xml' request = webob.Request.blank(path) response = request.get_response(app) self.assertEqual(200, response.status_int) self.assertEqual('application/xml', response.headers['Content-Type'])
def test_request(self): dir_path = testing.create_test_pod_dir() pod = pods.Pod(dir_path) pod.router.add_all() # When serving a pod, should 200. app = main.create_wsgi_app(pod, 'localhost', 8080) request = webapp2.Request.blank('/') response = request.get_response(app) self.assertEqual(200, response.status_int) # Verify 404 is sent for page not found. request = webapp2.Request.blank('/dummy/page/') response = request.get_response(app) self.assertEqual(404, response.status_int) # Verify 206 for partial content. headers = {'Range': 'bytes=0-4'} request = webapp2.Request.blank('/public/file.txt', headers=headers) response = request.get_response(app) self.assertEqual(206, response.status_int) self.assertEqual('bytes 0-4/13', response.headers['Content-Range']) self.assertEqual('Hello', response.body) headers = {'Range': 'bytes=5-13'} request = webapp2.Request.blank('/public/file.txt', headers=headers) response = request.get_response(app) self.assertEqual('bytes 5-12/13', response.headers['Content-Range']) self.assertEqual(' World!\n', response.body) # Verify full response when headers omitted. request = webapp2.Request.blank('/public/file.txt') response = request.get_response(app) self.assertEqual('Hello World!\n', response.body) # Verify 304. url_path = '/public/file.txt' matched = pod.router.routes.match(url_path) controller = pod.router.get_render_controller( url_path, matched.value, params=matched.params) response_headers = controller.get_http_headers() headers = {'If-None-Match': response_headers['Last-Modified']} request = webapp2.Request.blank(url_path, headers=headers) response = request.get_response(app) self.assertEqual(304, response.status_int) self.assertEqual('', response.body) response = request.get_response(app) self.assertEqual(304, response.status_int) self.assertEqual('', response.body) # Verify sitemap on server. path = '/root/sitemap.xml' request = webapp2.Request.blank(path) response = request.get_response(app) self.assertEqual(200, response.status_int) self.assertEqual('application/xml', response.headers['Content-Type'])
def test_request(self): """Test that api requests can be completed correctly.""" dir_path = testing.create_test_pod_dir() pod = pods.Pod(dir_path) pod.router.add_all() # When serving a pod, should 200. app = main.create_wsgi_app(pod, 'localhost', 8080) request = webapp2.Request.blank( '/_grow/api/editor/content?pod_path=/content/pages/home.yaml') response = request.get_response(app) self.assertEqual(200, response.status_int)
def start(pod, host=None, port=None, open_browser=False, debug=False, preprocess=True, update_check=False): main_observer, podspec_observer = file_watchers.create_dev_server_observers(pod) if preprocess: thread = threading.Thread(target=pod.preprocess, kwargs={'build': False}) thread.setDaemon(True) thread.start() port = 8080 if port is None else int(port) host = 'localhost' if host is None else host patch_broken_pipe_error() # Not safe for multi-pod serving env. CallbackHTTPServer.pod = pod CallbackHTTPServer.open_browser = open_browser CallbackHTTPServer.update_check = update_check serving.ThreadedWSGIServer = CallbackHTTPServer app = main_lib.create_wsgi_app(pod, host, port, debug=debug) serving._log = lambda *args, **kwargs: '' handler = main_lib.RequestHandler num_tries = 0 done = False while num_tries < NUM_TRIES and not done: try: app.app.port = port serving.run_simple(host, port, app, request_handler=handler, threaded=True) done = True except socket.error as e: # if any(x in str(e) for x in ('Errno 48', 'Errno 98')): if 'Errno 48' in str(e): num_tries += 1 port += 1 else: # Clean up the file watchers. main_observer.stop() podspec_observer.stop() raise e finally: if done: if pod.podcache.is_dirty: pod.podcache.write() # Clean up the file watchers. main_observer.stop() podspec_observer.stop() return text = 'Unable to find a port for the server (tried {}).' pod.logger.error(text.format(port)) sys.exit(-1)
def start(pod, host=None, port=None, open_browser=False, debug=False, preprocess=True, update_check=False): main_observer, podspec_observer = file_watchers.create_dev_server_observers(pod) if preprocess: thread = threading.Thread(target=pod.preprocess, kwargs={'build': False}) thread.setDaemon(True) thread.start() port = 8080 if port is None else int(port) host = 'localhost' if host is None else host patch_broken_pipe_error() # Not safe for multi-pod serving env. CallbackHTTPServer.pod = pod CallbackHTTPServer.open_browser = open_browser CallbackHTTPServer.update_check = update_check serving.ThreadedWSGIServer = CallbackHTTPServer app = main_lib.create_wsgi_app(pod, host, port, debug=debug) serving._log = lambda *args, **kwargs: '' handler = main_lib.RequestHandler num_tries = 0 done = False while num_tries < NUM_TRIES and not done: try: app.app.port = port serving.run_simple(host, port, app, request_handler=handler, threaded=True) done = True except socket.error as e: # if any(x in str(e) for x in ('Errno 48', 'Errno 98')): if 'Errno 48' in str(e): num_tries += 1 port += 1 else: # Clean up the file watchers. main_observer.stop() podspec_observer.stop() raise e finally: if done: # Clean up the file watchers. main_observer.stop() podspec_observer.stop() # Ensure ctrl+c works no matter what. # https://github.com/grow/grow/issues/149 os._exit(0) text = 'Unable to find a port for the server (tried {}).' pod.logger.error(text.format(port)) sys.exit(-1)
def test_request(self): self.dir_path = testing.create_test_pod_dir() pod = pods.Pod(self.dir_path) # When serving a pod, should 200. app = main.create_wsgi_app(pod) request = webapp2.Request.blank('/') response = request.get_response(app) self.assertEqual(response.status_int, 200) # Verify 404 is sent for blank page. request = webapp2.Request.blank('/dummy/page/') response = request.get_response(app) self.assertEqual(response.status_int, 404)
def start(pod, host=None, port=None, open_browser=False, debug=False, preprocess=True, update_check=False): observer, podspec_observer = file_watchers.create_dev_server_observers(pod) if preprocess: thread = threading.Thread(target=pod.preprocess, kwargs={'build': False}) thread.setDaemon(True) thread.start() port = 8080 if port is None else int(port) host = 'localhost' if host is None else host patch_broken_pipe_error() # Not safe for multi-pod serving env. CallbackHTTPServer.pod = pod CallbackHTTPServer.open_browser = open_browser CallbackHTTPServer.update_check = update_check serving.ThreadedWSGIServer = CallbackHTTPServer app = main_lib.create_wsgi_app(pod, debug=debug) serving._log = lambda *args, **kwargs: '' handler = main_lib.RequestHandler num_tries = 0 done = False while num_tries < NUM_TRIES and not done: try: serving.run_simple(host, port, app, request_handler=handler, threaded=True) done = True except socket.error as e: if 'Errno 48' in str(e): num_tries += 1 port += 1 else: raise e finally: # Ensure ctrl+c works no matter what. # https://github.com/grow/grow/issues/149 if done: os._exit(0) text = 'Unable to find a port for the server (tried {}).' pod.logger.error(text.format(port)) sys.exit(-1)
def test_editor(self): dir_path = testing.create_test_pod_dir() pod = pods.Pod(dir_path) pod.router.add_all() app = main.create_wsgi_app(pod, 'localhost', 8080) # Verify routes are served. request = webapp2.Request.blank('/_grow/editor') response = request.get_response(app) self.assertEqual(200, response.status_int) js_sentinel = 'front_matter' self.assertIn(js_sentinel, response.body) request = webapp2.Request.blank('/_grow/ui/css/editor.min.css') response = request.get_response(app) self.assertEqual(200, response.status_int)
def test_editor(self): dir_path = testing.create_test_pod_dir() pod = pods.Pod(dir_path, use_reroute=True) pod.router.add_all() app = main.create_wsgi_app(pod, 'localhost', 8080) # Verify routes are served. request = webapp2.Request.blank('/_grow/editor') response = request.get_response(app) self.assertEqual(200, response.status_int) js_sentinel = 'front_matter' self.assertIn(js_sentinel, response.body) request = webapp2.Request.blank('/_grow/ui/css/editor.min.css') response = request.get_response(app) self.assertEqual(200, response.status_int)
def test_admin(self): dir_path = testing.create_test_pod_dir() pod = pods.Pod(dir_path) pod.router.add_all(use_cache=False) app = main.create_wsgi_app(pod, 'localhost', 8080) # Verify routes are served. request = webob.Request.blank('/_grow/routes') response = request.get_response(app) self.assertEqual(200, response.status_int) js_sentinel = b'<h2>Routes</h2>' self.assertIn(js_sentinel, response.body) request = webob.Request.blank('/_grow/ui/css/admin.min.css') response = request.get_response(app) self.assertEqual(200, response.status_int)
def test_request(self): self.dir_path = testing.create_test_pod_dir() pod = pods.Pod(self.dir_path) # When serving a pod, should 200. app = main.create_wsgi_app(pod) request = webapp2.Request.blank('/') response = request.get_response(app) self.assertEqual(200, response.status_int) # Verify 404 is sent for page not found. request = webapp2.Request.blank('/dummy/page/') response = request.get_response(app) self.assertEqual(404, response.status_int) # Verify 206 for partial content. headers = {'Range': 'bytes=0-4'} request = webapp2.Request.blank('/public/file.txt', headers=headers) response = request.get_response(app) self.assertEqual(206, response.status_int) self.assertEqual('bytes 0-4/13', response.headers['Content-Range']) self.assertEqual('Hello', response.body) headers = {'Range': 'bytes=5-13'} request = webapp2.Request.blank('/public/file.txt', headers=headers) response = request.get_response(app) self.assertEqual('bytes 5-12/13', response.headers['Content-Range']) self.assertEqual(' World!\n', response.body) # Verify full response when headers omitted. request = webapp2.Request.blank('/public/file.txt') response = request.get_response(app) self.assertEqual('Hello World!\n', response.body) # Verify 304. url_path = '/public/file.txt' controller, params = pod.match(url_path) response_headers = controller.get_http_headers(params) headers = {'If-None-Match': response_headers['Last-Modified']} request = webapp2.Request.blank(url_path, headers=headers) response = request.get_response(app) self.assertEqual(304, response.status_int) self.assertEqual('', response.body) response = request.get_response(app) self.assertEqual(304, response.status_int) self.assertEqual('', response.body)
def test_ui(self): dir_path = testing.create_test_pod_dir() pod = pods.Pod(dir_path) app = main.create_wsgi_app(pod) # Verify JS and CSS are served. request = webapp2.Request.blank('/_grow/ui/js/ui.min.js') response = request.get_response(app) self.assertEqual(200, response.status_int) js_sentinel = '!function' self.assertIn(js_sentinel, response.body) request = webapp2.Request.blank('/_grow/ui/css/ui.min.css') response = request.get_response(app) self.assertEqual(200, response.status_int) css_sentinel = '#grow' self.assertIn(css_sentinel, response.body)
def test_ui(self): dir_path = testing.create_test_pod_dir() pod = pods.Pod(dir_path) pod.router.add_all(use_cache=False) app = main.create_wsgi_app(pod, 'localhost', 8080) # Verify JS and CSS are served. request = webob.Request.blank('/_grow/ui/js/ui.min.js') response = request.get_response(app) self.assertEqual(200, response.status_int) js_sentinel = b'setAttribute("target","_blank")' self.assertIn(js_sentinel, response.body) request = webob.Request.blank('/_grow/ui/css/ui.min.css') response = request.get_response(app) self.assertEqual(200, response.status_int) css_sentinel = b'#grow' self.assertIn(css_sentinel, response.body)
def test_ui(self): dir_path = testing.create_test_pod_dir() pod = pods.Pod(dir_path, use_reroute=True) pod.router.add_all() app = main.create_wsgi_app(pod, 'localhost', 8080) # Verify JS and CSS are served. request = webapp2.Request.blank('/_grow/ui/js/ui.min.js') response = request.get_response(app) self.assertEqual(200, response.status_int) js_sentinel = 'function(modules)' self.assertIn(js_sentinel, response.body) request = webapp2.Request.blank('/_grow/ui/css/ui.min.css') response = request.get_response(app) self.assertEqual(200, response.status_int) css_sentinel = '#grow' self.assertIn(css_sentinel, response.body)
def find_port_and_start_server(pod, host, port, debug): app = main_lib.create_wsgi_app(pod, debug=debug) num_tries = 0 while num_tries < 10: try: thread_pool = reactor.getThreadPool() wsgi_resource = wsgi.WSGIResource(reactor, thread_pool, app) site = server.Site(wsgi_resource) version = sdk_utils.get_this_version() server.version = 'Grow/{}'.format(version) reactor.listenTCP(port, site, interface=host) return port except twisted.internet.error.CannotListenError as e: if 'Errno 48' in str(e): num_tries += 1 port += 1 else: raise e pod.logger.error('Unable to bind to {}:{}'.format(host, port)) sys.exit(-1)
from google.appengine.ext import vendor vendor.add('lib') from grow.server import main as grow_main import grow pod = grow.Pod('.') app = grow_main.create_wsgi_app(pod)
def __call__(self, environ, start_response): email = os.getenv('USER_EMAIL', '') is_https = os.getenv('wsgi.url_scheme', '') == 'https' if not is_https and not IS_DEV_SERVER: status = '302 Found' url = os.getenv('HTTP_ORIGIN', '').replace('http://', 'https://') response_headers = [('Location', url)] start_response(status, response_headers) return [] if not email: status = '302 Found' url = users.create_login_url(environ['PATH_INFO']) response_headers = [('Location', url)] start_response(status, response_headers) return [] if not self.has_access(email): status = '403 Forbidden' response_headers = [('Content-Type', 'text/html')] start_response(status, response_headers) url = users.create_logout_url(environ['PATH_INFO']) message = '{} is forbidden. <a href="{}">Sign out</a>.' return [message.format(email, url)] return self.app(environ, start_response) pod = grow.Pod('.') pod.env.name = 'dev' pod_app = grow_main.create_wsgi_app(pod) app = AuthMiddleware(pod_app)