Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

g fields persist across requests #156

Open
mephi42 opened this issue Dec 5, 2022 · 0 comments
Open

g fields persist across requests #156

mephi42 opened this issue Dec 5, 2022 · 0 comments

Comments

@mephi42
Copy link

mephi42 commented Dec 5, 2022

Consider the following app:

import unittest

from flask import Flask, g, request
from flask_testing.utils import TestCase



def create_app():
    app = Flask(__name__)

    @app.route('/', methods=['POST'])
    def index():
        try:
            result = g.dejavu
        except AttributeError:
            result = '?'
        g.dejavu = request.json['dejavu']
        return result

    return app


app = create_app()


class MyTestCase(TestCase):
    def create_app(self):
        return create_app()

    def test(self):
        with self.app.test_client() as client:
            response = client.post('/', json={'dejavu': '42'})
            self.assert200(response)
            self.assertEqual(b'?', response.data)
            response = client.post('/', json={'dejavu': '42'})
            self.assert200(response)
            self.assertEqual(b'?', response.data)


if __name__ == '__main__':
    unittest.main()

flask --app foo run works as expected:

$ curl http://127.0.0.1:5000 -H 'Content-Type: application/json' --data '{"dejavu": 42}'
?
$ curl http://127.0.0.1:5000 -H 'Content-Type: application/json' --data '{"dejavu": 42}'
?

However, the respective test fails:

$ python3 foo.py
# AssertionError: b'?' != b'42'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant