def process_response(self, response): """Inject mimetype into response before it's sent to the WSGI server. This is only intended for stashable view functions, created by :meth:`Tango.build_view`. """ Flask.process_response(self, response) ctx = _request_ctx_stack.top if hasattr(ctx, 'mimetype'): mimetype, charset = (ctx.mimetype, response.charset) response.content_type = get_content_type(mimetype, charset) response.headers['Content-Type'] = response.content_type return response
def global_response_handler(response): response.headers['Access-Control-Allow-Origin'] = request.headers.get( 'Origin', '*') response.headers['Access-Control-Allow-Credentials'] = 'true' response.headers['X-HOSTNAME'] = app.config['X-HOSTNAME'] response.headers['X-APP-VERSION'] = app.config['VERSION'] return response def global_request_handler(): # this needs to be safe for use by the app g.user_token = secure_filename( request.headers.get(app.config['USER_HEADER_NAME'], 'DEFAULT')) app.process_response = global_response_handler app.preprocess_request = global_request_handler ################################################################################ # views @app.route("/", methods=["GET"]) @app.route("/diag", methods=["GET"]) @app.route("/diagnostic", methods=["GET"]) def pyserver_core_diagnostic_view(): """ Used to return the status of the application, including the version of the running application. :statuscode 200: returned as long as all checks return healthy
return ( response_string, status_code, {"Content-Type": content_type, "Cache-Control": "no-cache", "Pragma": "no-cache"} ) def global_response_handler(response): response.headers['X-HOSTNAME'] = app.config['X-HOSTNAME'] response.headers['X-APP-VERSION'] = app.config['VERSION'] return response def global_request_handler(): # this needs to be safe for use by the app g.user_token = secure_filename(request.headers.get(app.config['USER_HEADER_NAME'], 'DEFAULT')) app.process_response = global_response_handler app.preprocess_request = global_request_handler ################################################################################ # views @app.route("/", methods=["GET"]) @app.route("/diagnostic", methods=["GET"]) @make_my_response_json def pyserver_core_diagnostic_view(): """ Used to return the status of the application, including the version of the running application. :statuscode 200: returned as long as all checks return healthy :statuscode 500: returned in the case of any diagnostic tests failing
class TestCompressibleAnnotator(object): """Test the @compressible annotator.""" def setup_class(self): self.app = Flask(__name__) def test_compressible(self): # Test the @compressible annotator. # Prepare a value and a gzipped version of the value. value = b"Compress me! (Or not.)" buffer = BytesIO() gzipped = gzip.GzipFile(mode="wb", fileobj=buffer) gzipped.write(value) gzipped.close() compressed = buffer.getvalue() # Spot-check the compressed value assert b"-(J-.V" in compressed # This compressible controller function always returns the # same value. @compressible def function(): return value def ask_for_compression(compression, header="Accept-Encoding"): """This context manager simulates the entire Flask request-response cycle, including a call to process_response(), which triggers the @after_this_request hooks. :return: The Response object. """ headers = {} if compression: headers[header] = compression with self.app.test_request_context(headers=headers): response = flask.Response(function()) self.app.process_response(response) return response # If the client asks for gzip through Accept-Encoding, the # representation is compressed. response = ask_for_compression("gzip") assert compressed == response.data assert "gzip" == response.headers["Content-Encoding"] # If the client doesn't ask for compression, the value is # passed through unchanged. response = ask_for_compression(None) assert value == response.data assert "Content-Encoding" not in response.headers # Similarly if the client asks for an unsupported compression # mechanism. response = ask_for_compression("compress") assert value == response.data assert "Content-Encoding" not in response.headers # Or if the client asks for a compression mechanism through # Accept-Transfer-Encoding, which is currently unsupported. response = ask_for_compression("gzip", "Accept-Transfer-Encoding") assert value == response.data assert "Content-Encoding" not in response.headers
def process_response(self, response): response = _request_ctx_stack.top.request.process_response(response) return _Flask.process_response(self, response)
def process_response(self, response): response.headers["server"] = "Awesome Server!" return Flask.process_response(app, response)
class EpisodeRoutesTestCase(unittest.TestCase): """ Tests Show-related page content. """ def setUp(self): bg_tracking.app.testing = True self.app = Flask(__name__) self.atc = bg_tracking.app.test_client() def test_edit_new_with_show_id(self): show_id = 2 route = '/episode/add/?show_id={}'.format(show_id) with self.app.test_request_context(route): assert int(request.args.get('show_id')) == show_id def test_edit_new_with_missing_show_id(self): route = '/episode/add' with self.app.test_request_context(route): self.assertIsNone(request.args.get('show_id')) def test_edit_existing(self): episode_id = 4 route = '/episode/{}/edit'.format(episode_id) response = self.atc.get(route, follow_redirects=True) self.assertEqual(response.status_code, 200) def test_default_details_view(self): episode_id = 1 route = '/episode/{}'.format(episode_id) response = self.atc.get(route, follow_redirects=True) self.assertEqual(response.status_code, 200) # assert b'<title>Error</title>' in response.data def tet_submit_new_record(self): data = {'title': 'unit test {:%Y%d%m}'.format(datetime.now()), 'number': 1, 'show': 3, 'id': None } response = self.atc.post('/episode/add/', data=data) self.assertEqual(response.status_code, 200) def _out_test_submit_new_record(self): data = {'title': 'unit test {:%Y%d%m}'.format(datetime.now()), 'number': 1, 'show': 3, 'id': None } with self.app.test_request_context('/episode/add/', method='POST', data=data ): rv = self.app.preprocess_request() if rv is not None: response = self.app.make_response(rv) else: rv = self.app.dispatch_request() response = self.app.make_response(rv) response = self.app.process_response(response) self.assertEqual(response.status_code, 200)