mock_flask_server.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import os
  2. from flask import Flask, request, make_response
  3. def create_app():
  4. app = Flask(__name__)
  5. @app.route('/', defaults={"path": ""})
  6. @app.route('/<path:path>')
  7. def echo(path):
  8. status = int(request.args.get('status', 200))
  9. content = request.args.get('content', '')
  10. if 'content_long' in request.args:
  11. content = '*' * 1000001
  12. response = make_response(content, status)
  13. headers = [
  14. item
  15. for item in list(request.args.items())
  16. if item[0] not in ('content', 'status')
  17. ]
  18. if 'length' in request.args:
  19. cl = request.args.get('length')
  20. headers += [('Content-Length', cl)]
  21. elif content and 'no-content-length' not in request.args:
  22. headers += [('Content-Length', bytes(len(content)))]
  23. for k, v in headers:
  24. response.headers[k] = v
  25. return response
  26. @app.route('/WMS_1_3/', defaults={"path": ""})
  27. @app.route('/WMS_1_3/<path:path>')
  28. def WMS_1_3(path):
  29. status = int(request.args.get('status', 200))
  30. content = request.args.get('content', '')
  31. if request.args.get('service') == 'WMS':
  32. if request.args.get('request') == 'GetCapabilities':
  33. if request.args.get('version') == "1.3":
  34. content = get_file_content('wms_getcap_1.3.xml')
  35. response = make_response(content, status)
  36. headers = [
  37. item
  38. for item in list(request.args.items())
  39. if item[0] not in ('content', 'status')
  40. ]
  41. for k, v in headers:
  42. response.headers[k] = v
  43. return response
  44. @app.route('/WMS_1_1_1/', defaults={"path": ""})
  45. @app.route('/WMS_1_1_1/<path:path>')
  46. def WMS_1_1_1(path):
  47. status = int(request.args.get('status', 200))
  48. content = request.args.get('content', '')
  49. if request.args.get('service') == 'WMS':
  50. if request.args.get('request') == 'GetCapabilities':
  51. if request.args.get('version') == "1.1.1":
  52. content = get_file_content('wms_getcap_1.1.1.xml')
  53. response = make_response(content, status)
  54. headers = [
  55. item
  56. for item in list(request.args.items())
  57. if item[0] not in ('content', 'status')
  58. ]
  59. for k, v in headers:
  60. response.headers[k] = v
  61. return response
  62. @app.route('/WFS/', defaults={"path": ""})
  63. @app.route('/WFS/<path:path>')
  64. def WFS(path):
  65. status = int(request.args.get('status', 200))
  66. content = request.args.get('content', '')
  67. if request.args.get('service') == 'WFS':
  68. if request.args.get('request') == 'GetCapabilities':
  69. content = get_file_content('wfs_getcap.xml')
  70. response = make_response(content, status)
  71. headers = [
  72. item
  73. for item in list(request.args.items())
  74. if item[0] not in ('content', 'status')
  75. ]
  76. for k, v in headers:
  77. response.headers[k] = v
  78. return response
  79. return app
  80. def get_file_content(data_filename):
  81. filepath = os.path.join(os.path.dirname(__file__), 'data', data_filename)
  82. assert os.path.exists(filepath), filepath
  83. with open(filepath, 'rb') as f:
  84. return f.read()