xref: /OK3568_Linux_fs/buildroot/support/testing/tests/package/test_python_flask_expects_json.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1from tests.package.test_python import TestPythonPackageBase
2import os
3import time
4
5
6class TestPythonPy3FlaskExpectsJson(TestPythonPackageBase):
7    __test__ = True
8    config = TestPythonPackageBase.config + \
9        """
10        BR2_PACKAGE_LIBCURL=y
11        BR2_PACKAGE_LIBCURL_CURL=y
12        BR2_PACKAGE_PYTHON3=y
13        BR2_PACKAGE_PYTHON_FLASK=y
14        BR2_PACKAGE_PYTHON_FLASK_EXPECTS_JSON=y
15        """
16    sample_scripts = ["tests/package/sample_python_flask_expects_json.py"]
17    timeout = 60
18
19    def try_json(self, payload, expects):
20        cmd = """curl -s -o /dev/null -w "%{http_code}\\n" -X POST """
21        cmd += """-H "Content-Type: application/json" -d '%s' http://127.0.0.1:5000""" % payload
22        output, exit_code = self.emulator.run(cmd, timeout=self.timeout)
23        self.assertEqual(exit_code, 0)
24        self.assertEqual(output[0], str(expects))
25
26    def test_run(self):
27        self.login()
28        self.check_sample_scripts_exist()
29        cmd = "FLASK_APP=%s %s -m flask run > /dev/null 2>&1 &" % (os.path.basename(self.sample_scripts[0]),
30                                                                   self.interpreter)
31        _, exit_code = self.emulator.run(cmd, timeout=self.timeout)
32
33        # Give enough time for the flask server to start up
34        time.sleep(15)
35
36        self.try_json("""{"email": "test", "name": "test"}""", 200)
37        self.try_json("""{"email": "test", "name": 2}""", 400)
38        self.try_json("""{"email": "test"}""", 400)
39