|
| 1 | +from mock_decorators import setup, teardown |
| 2 | +from flask import Flask, request |
| 3 | +from threading import Thread |
| 4 | +import urllib2 |
| 5 | +import os |
| 6 | +import ssl |
| 7 | + |
| 8 | +@setup('HTTP GET request') |
| 9 | +def setup_http_get(e): |
| 10 | + app = Flask(__name__) |
| 11 | + def shutdown_server(): |
| 12 | + func = request.environ.get('werkzeug.server.shutdown') |
| 13 | + if func is None: |
| 14 | + raise RuntimeError('Not running with the Werkzeug Server') |
| 15 | + func() |
| 16 | + @app.route('/shutdown') |
| 17 | + def shutdown(): |
| 18 | + shutdown_server() |
| 19 | + return 'Server shutting down...' |
| 20 | + @app.route("/") |
| 21 | + def root(): |
| 22 | + return 'hello!!!' |
| 23 | + @app.route("/data") |
| 24 | + def get_data(): |
| 25 | + size = int(request.args['size']) |
| 26 | + return 'a'*size |
| 27 | + def flaskThread(): |
| 28 | + app.run(host='0.0.0.0', port=8088) |
| 29 | + th = Thread(target=flaskThread) |
| 30 | + th.start() |
| 31 | + |
| 32 | +@teardown('HTTP GET request') |
| 33 | +def teardown_http_get(e): |
| 34 | + response = urllib2.urlopen('http://localhost:8088/shutdown') |
| 35 | + html = response.read() |
| 36 | + |
| 37 | + |
| 38 | +@setup('HTTPS GET request') |
| 39 | +def setup_http_get(e): |
| 40 | + app = Flask(__name__) |
| 41 | + def shutdown_server(): |
| 42 | + func = request.environ.get('werkzeug.server.shutdown') |
| 43 | + if func is None: |
| 44 | + raise RuntimeError('Not running with the Werkzeug Server') |
| 45 | + func() |
| 46 | + @app.route('/shutdown') |
| 47 | + def shutdown(): |
| 48 | + shutdown_server() |
| 49 | + return 'Server shutting down...' |
| 50 | + @app.route("/") |
| 51 | + def root(): |
| 52 | + return 'hello!!!' |
| 53 | + @app.route("/data") |
| 54 | + def get_data(): |
| 55 | + size = int(request.args['size']) |
| 56 | + return 'a'*size |
| 57 | + def flaskThread(): |
| 58 | + p = os.path.dirname(os.path.abspath(__file__)) |
| 59 | + context = (p + '/server.crt', p + '/server.key') |
| 60 | + print context |
| 61 | + app.run(host='0.0.0.0', port=8088, ssl_context=context) |
| 62 | + th = Thread(target=flaskThread) |
| 63 | + th.start() |
| 64 | + |
| 65 | +@teardown('HTTPS GET request') |
| 66 | +def teardown_http_get(e): |
| 67 | + ctx = ssl.create_default_context() |
| 68 | + ctx.check_hostname = False |
| 69 | + ctx.verify_mode = ssl.CERT_NONE |
| 70 | + p = os.path.dirname(os.path.abspath(__file__)) |
| 71 | + response = urllib2.urlopen('https://localhost:8088/shutdown', context=ctx) |
| 72 | + html = response.read() |
| 73 | + |
0 commit comments