2
0

server.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from os import getcwd
  2. from pathlib import Path
  3. from bottle import route, run, static_file, response, redirect
  4. @route("/")
  5. def index():
  6. return "Hello"
  7. @route("/static/<filename>")
  8. def static_path(filename):
  9. template_path = Path.cwd().resolve() / "tests/mock_server/templates"
  10. response = static_file(filename, root=template_path)
  11. return response
  12. @route("/static_no_content_type/<filename>")
  13. def static_no_content_type(filename):
  14. template_path = Path.cwd().resolve() / "tests/mock_server/templates"
  15. response = static_file(filename, root=template_path)
  16. response.set_header("Content-Type", "")
  17. return response
  18. @route("/static/headers/<filename>")
  19. def static_path_with_headers(filename):
  20. template_path = Path.cwd().resolve() / "tests/mock_server/templates"
  21. response = static_file(filename, root=template_path)
  22. response.add_header("Content-Language", "en")
  23. response.add_header("Content-Script-Type", "text/javascript")
  24. response.add_header("Content-Style-Type", "text/css")
  25. return response
  26. @route("/static/400/<filename>", method="HEAD")
  27. def static_400(filename):
  28. template_path = Path.cwd().resolve() / "tests/mock_server/templates"
  29. response = static_file(filename, root=template_path)
  30. response.status = 400
  31. response.add_header("Status-Code", "400")
  32. return response
  33. @route("/static/400/<filename>", method="GET")
  34. def static_200(filename):
  35. template_path = Path.cwd().resolve() / "tests/mock_server/templates"
  36. response = static_file(filename, root=template_path)
  37. response.add_header("Status-Code", "200")
  38. return response
  39. @route("/redirect/headers/<filename>")
  40. def redirect_to_static(filename):
  41. redirect(f"/static/headers/$filename")
  42. def start():
  43. run(host='localhost', port=8080)