Python flask.after_this_request() Examples

The following are code examples for showing how to use flask.after_this_request(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: basic.py    Apache License 2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 2
Project: Flask-Python-GAE-Login-Registration   Author: orymeyer   File: basic.py    Apache License 2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 3
Project: flasky   Author: RoseOu   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 4
Project: Flask_Blog   Author: sugarguo   File: basic.py    GNU General Public License v3.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 5
Project: platzi-hello-gae   Author: xertica-cloud   File: basic.py    GNU General Public License v2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 6
Project: tesismometro   Author: joapaspe   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 7
Project: neo4j-social-network   Author: bestvibes   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 8
Project: neo4j-social-network   Author: bestvibes   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 9
Project: AneMo   Author: jspargo   File: basic.py    GNU General Public License v2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 10
Project: oa_qian   Author: sunqb   File: basic.py    Apache License 2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 11
Project: Where2Eat   Author: thetimothyp   File: basic.py    Creative Commons Zero v1.0 Universal 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 12
Project: PennApps2015-Heartmates   Author: natanlailari   File: basic.py    Apache License 2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 13
Project: noobotkit   Author: nazroll   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 14
Project: xuemc   Author: skycucumber   File: basic.py    GNU General Public License v2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 15
Project: chihu   Author: yelongyu   File: basic.py    GNU General Public License v3.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 16
Project: url_shortener   Author: martydill   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 17
Project: musapaedia   Author: rossgoodwin   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 18
Project: api   Author: SMISC   File: util.py    MIT License 5 votes vote down vote up
def make_json_response(f):
    @wraps(f)
    def decorator(*args, **kwargs):

        @flask.after_this_request
        def add_header(response):
            if (len(response.response) == 0 or response.response[0] == '[]') and response.status_code == 200:
                response.status_code = 204

            response.headers['Content-Type'] = 'application/json'
            return response

        return f(*args, **kwargs)
    return decorator 
Example 19
Project: api   Author: SMISC   File: util.py    MIT License 5 votes vote down vote up
def nearest_scan(scan_type):
    def deco(f):
        @wraps(f)
        def decorator(*args, **kwargs):
            vtime = kwargs['vtime']

            nearest_scan_result = Scan.query.filter(
                Scan.end <= vtime,
                Scan.type == scan_type
            ).order_by(Scan.id.desc()).first()

            if nearest_scan_result is not None:
                if nearest_scan_result.ref_start is not None:
                    kwargs['min_scan_id'] = int(nearest_scan_result.ref_start)
                else:
                    kwargs['min_scan_id'] = None

                if nearest_scan_result.ref_end is not None:
                    kwargs['max_scan_id'] = int(nearest_scan_result.ref_end)
                else:
                    kwargs['max_scan_id'] = None

                @flask.after_this_request
                def add_header(response):
                    response.headers['X-Observed-Min'] = int(nearest_scan_result.start)
                    response.headers['X-Observed-Max'] = int(nearest_scan_result.end)
                    return response
            else:
                logging.info('did not find scan around %d', vtime)
                kwargs['min_scan_id'] = 0
                kwargs['max_scan_id'] = 0

            return f(*args, **kwargs)

        return decorator
    return deco 
Example 20
Project: cl-auto-reply   Author: KanagiMiss   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 21
Project: Backend   Author: LockScreen   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 22
Project: islam-buddy   Author: hamir   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 23
Project: DevFest-MaxBond   Author: aiyyoi   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 24
Project: that-startpage-rocks   Author: the-duck   File: basic.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 25
Project: Tellal   Author: mehtapgundogan   File: basic.py    GNU General Public License v2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 26
Project: Flask-P2P   Author: chalasr   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 27
Project: taskqueue   Author: matthappens   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 28
Project: WRGameVideos-API   Author: thundernet8   File: basic.py    GNU General Public License v2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 29
Project: pipa-pay-server   Author: davidvon   File: basic.py    Apache License 2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 30
Project: cas-eval   Author: varepsilon   File: main.py    Apache License 2.0 5 votes vote down vote up
def save_page():
    @flask.after_this_request
    def add_headers(response):
        response.headers['Access-Control-Allow-Origin'] = '*'
        return response
    values = flask.request.values
    if values.get('type', '') == 'Serp':
        try:
            user_id = Session.get_user_id(values['url'])
        except Exception as e:
            app.logger.error(e)
            return 'Incorrect user_id used', 400
        try:
            query = Session.get_query(values['url'])
        except Exception as e:
            app.logger.error(e)
            return 'No query set?', 400
        for k in ['data', 'tab_id', 'time']:
            if k not in values:
                return 'Missing param: %s' % k, 400
        data = values['data']
        try:
            ts = Session.convert_time(values['time'])
        except Exception as e:
            app.logger.error(e)
            return 'Incorrect timestamp', 400
        session = Session(id=values['tab_id'], user_id=user_id, q=query,
                serp_html=data, start_ts=ts)
        n = len(data)
        while n > 1:
            session.serp_html = data[:n]
            try:
                session.put()
                break
            except apiproxy_errors.RequestTooLargeError as e:
                app.logger.error(e)
                n /= 2
        return 'Saved', 201
    return 'Only support saving SERPs using POST requests, sorry.', 403 
Example 31
Project: cas-eval   Author: varepsilon   File: main.py    Apache License 2.0 5 votes vote down vote up
def save_settings():
    @flask.after_this_request
    def add_headers(response):
        response.headers['Access-Control-Allow-Origin'] = '*'
        return response
    values = flask.request.values
    try:
        user_id = Session.get_user_id(values['url'])
    except Exception as e:
        app.logger.error(e)
        return 'Incorrect user_id used', 400
    for k in ['data', 'tab_id', 'time']:
        if k not in values:
            return 'Missing param: %s' % k, 400
    try:
        ts = Session.convert_time(values['time'])
    except Exception as e:
        app.logger.error(e)
        return 'Incorrect timestamp', 400
    mute_period_m = 0
    for data in values['data'].split(','):
        try:
            mute_period_m = max(mute_period_m, UserSettings.convert_mute_period_m(data))
        except Exception as e:
            app.logger.error(e)
            return 'Incorrect mute period settings: %s' % data, 400
    mute_deadline = UserSettings.get_mute_deadline(ts, mute_period_m)
    settings = ndb.Key(UserSettings, user_id).get()
    if settings is None:
        # Create settings for the current user
        settings = UserSettings(id=user_id, mute_deadline=mute_deadline, ts=ts)
        settings.put()
    elif settings.mute_deadline is None or settings.mute_deadline < mute_deadline:
        settings.mute_deadline = mute_deadline
        settings.ts = ts
        settings.put()
    return 'Saved', 201 
Example 32
Project: cas-eval   Author: varepsilon   File: main.py    Apache License 2.0 5 votes vote down vote up
def ask_feedback():
    @flask.after_this_request
    def add_headers(response):
        response.headers['Access-Control-Allow-Origin'] = '*'
        return response
    return '10', 200
    values = flask.request.values
    now = datetime.now()
    try:
        user_id = Session.get_user_id(values['url'])
    except:
        return 'Incorrect user_id used', 400
    settings = ndb.Key(UserSettings, user_id).get()
    if settings is None:
        # Create settings for the current user
        settings = UserSettings(id=user_id, ts=now)
    if settings.mute_deadline is not None and settings.mute_deadline > now:
        return '0', 200
    questionnaire_left = 10
    for prev_shown_ts in reversed(settings.questionnaire_shown_ts):
        if prev_shown_ts < now - timedelta(hours=24):
            break
        questionnaire_left -= 1
    if random.random() < 0.5:
        # Suppress the popup for 50% of all SERPs.
        questionnaire_left = 0
    if questionnaire_left > 0:
        settings.questionnaire_shown_ts.append(now)
        settings.put()
    return str(questionnaire_left), 200 
Example 33
Project: cas-eval   Author: varepsilon   File: main.py    Apache License 2.0 5 votes vote down vote up
def log():
    @flask.after_this_request
    def add_headers(response):
        response.headers['Access-Control-Allow-Origin'] = '*'
        return response
    values = flask.request.values
    tab_id = values.get('tab_id', '')
    session = ndb.Key(Session, tab_id).get()
    if session is None:
        return 'No sessions with tab_id = %s' % tab_id, 404
    elif session.shared:
        return 'Cannot update previously shared session with tab_id = %s' % tab_id, 403
    try:
        user_id = Session.get_user_id(values['url'])
    except:
        return 'Incorrect user_id used', 400
    if session.user_id != user_id:
        return 'Session does not belong to %s' % user_id, 403
    try:
        if 'buffer' in values:
            buffer = json.loads(values['buffer'])
        else:
            buffer = [flask.request.url.split('?', 1)[-1]]
        actions = []
        for log_str in buffer:
            log_item = urlparse.parse_qs(log_str)
            ts = Session.convert_time(log_item['time'][0])
            event_type = log_item.get('ev', ['UNKNOWN'])[0]
            fields = {k: v[0] for (k, v) in log_item.iteritems() if k not in ['ev', 'time']}
            actions.append(Action(ts=ts, event_type=event_type, fields=fields))
        session.actions += actions
        session.put()
        return 'Updated', 200

    except Exception as e:
        app.logger.error(e)
        app.logger.error('Buffer: %s' % values.get('buffer', ''))
        return 'Incorrect buffer contents', 400 
Example 34
Project: trace-examples   Author: DataDog   File: app.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def joke():
    res = requests.get('https://icanhazdadjoke.com/', headers=dict(Accept='text/plain'))
    res.raise_for_status()

    @after_this_request
    def after_joke(response):
        print('Hook: after_this_request')
        return response

    return res.content 
Example 35
Project: Ridr_app   Author: RydrDojo   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 36
Project: duelistDB   Author: YesIndeed   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 37
Project: sentimizer   Author: sarthfrey   File: basic.py    Apache License 2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 38
Project: capybara   Author: AkihikoITOH   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 39
Project: pywebapps   Author: gumblex   File: main.py    GNU General Public License v3.0 5 votes vote down vote up
def gzipped(f):
    @functools.wraps(f)
    def view_func(*args, **kwargs):
        @flask.after_this_request
        def zipper(response):
            accept_encoding = flask.request.headers.get('Accept-Encoding', '')

            if 'gzip' not in accept_encoding.lower():
                return response

            response.direct_passthrough = False

            if (response.status_code < 200 or
                    response.status_code >= 300 or
                    'Content-Encoding' in response.headers):
                return response
            response.data = gzip.compress(response.data)
            response.headers['Content-Encoding'] = 'gzip'
            response.headers['Vary'] = 'Accept-Encoding'
            response.headers['Content-Length'] = len(response.data)

            return response

        return f(*args, **kwargs)

    return view_func

# From django.utils.translation.trans_real.parse_accept_lang_header 
Example 40
Project: magik   Author: mrinalabrol   File: basic.py    GNU General Public License v3.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 41
Project: syntheticmass   Author: synthetichealth   File: basic.py    Apache License 2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 42
Project: plask   Author: theDarkForce   File: basic.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 43
Project: optimizers   Author: crowdhackathon-transport   File: basic.py    MIT License 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 44
Project: WRGameVideos-Server   Author: thundernet8   File: basic.py    GNU General Public License v2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 45
Project: cron   Author: kartikluke   File: basic.py    Apache License 2.0 5 votes vote down vote up
def test_after_request_processing(self):
        app = flask.Flask(__name__)
        app.testing = True
        @app.route('/')
        def index():
            @flask.after_this_request
            def foo(response):
                response.headers['X-Foo'] = 'a header'
                return response
            return 'Test'
        c = app.test_client()
        resp = c.get('/')
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['X-Foo'], 'a header') 
Example 46
Project: api   Author: SMISC   File: util.py    MIT License 4 votes vote down vote up
def cursor(default_cursor_size = None):
    if None is default_cursor_size:
        default_cursor_size = DEFAULT_CURSOR_SIZE

    def deco(f):
        @wraps(f)
        def decorator(*args, **kwargs):
            cursor = None

            if 'X-Cursor' in flask.request.headers:
                cursor = flask.request.headers['X-Cursor']
                (offset, cursor_size) = cursor.split('-')
                offset = int(offset)
                cursor_size = int(cursor_size)

            elif 'X-Cursor-Size' in flask.request.headers:
                cursor_size = min(GENEROUS_CURSOR_UPPER_BOUND, int(flask.request.headers['X-Cursor-Size']))
                offset = 0
            else:
                cursor_size = default_cursor_size
                offset = 0

            next_cursor = str(offset + cursor_size) + '-' + str(cursor_size)

            kwargs['cursor_size'] = cursor_size
            kwargs['offset'] = offset

            @flask.after_this_request
            def add_header(response):
                if offset > 0:
                    prev_cursor = str(offset - cursor_size) + '-' + str(cursor_size)
                    response.headers['X-Cursor-Previous'] = prev_cursor

                response.headers['X-Cursor-Next'] = next_cursor

                if cursor is not None:
                    response.headers['X-Cursor-Current'] = cursor

                return response

            return f(*args, **kwargs)

        return decorator
    return deco