test_python_flask_expects_json.py 1.5 KB

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