def test_running_same_server_multiple_times(self): app = HttpServerMock("test-http-server-mock") @app.route("/", methods=["GET"]) def index_route(): return "random text" with app.run("localhost", 5002): r = requests.get("http://localhost:5002/") self.assertEqual(r.status_code, 200) self.assertEqual(r.text, "random text") if not isWindows: with self.assertRaises(requests.exceptions.ConnectionError): requests.get("http://localhost:5002/") with app.run("localhost", 5003): r = requests.get("http://localhost:5003/") self.assertEqual(r.status_code, 200) self.assertEqual(r.text, "random text") if not isWindows: with self.assertRaises(requests.exceptions.ConnectionError): requests.get("http://localhost:5003/") with app.run("localhost", 3000): r = requests.get("http://localhost:3000/") self.assertEqual(r.status_code, 200) self.assertEqual(r.text, "random text") if not isWindows: with self.assertRaises(requests.exceptions.ConnectionError): requests.get("http://localhost:3000/")
def repo_app(): app = HttpServerMock(__name__, is_alive_route='/') app.config['RESOURCES'] = set() @app.route('/') def root(): return 'Mock fcrepo server', 200 @app.route('/<path:repo_path>', methods=['GET']) def get_resource(repo_path): if repo_path in app.config['RESOURCES']: return repo_path, 200 else: return 'Not Found', 404 @app.route('/<path:repo_path>', methods=['PUT']) def put_resource(repo_path): uri = url_for('get_resource', _external=True, repo_path=repo_path) app.config['RESOURCES'].add(repo_path) return uri, 201, {'Location': uri} @app.route('/<path:repo_path>', methods=['POST']) def post_resource(repo_path): uuid = str(uuid4()) pairtree = [uuid[0:2], uuid[2:4], uuid[4:6], uuid[6:8]] path = str(Path(repo_path, *pairtree, uuid)) app.config['RESOURCES'].add(path) uri = url_for('get_resource', _external=True, repo_path=path) return uri, 201, {'Location': uri} return app
def test_multiple_servers(self): app = HttpServerMock("test-http-server-mock") app._testing_error = True with self.assertRaises(Exception): with app.run("localhost", 3001): r = requests.get("http://localhost:3001/")
def test_http_server_mock(self): print( "------------------ Test Case : HTTP Server Mock ------------------" ) app = HttpServerMock(__name__, is_alive_route="/metrics") @app.route("/", methods=["GET"]) def index(): return "Prometheus Metrics" with app.run("localhost", 8000): r = requests.get("http://localhost:8000/") print( "------------------------ Test Case : Completed ------------------\n" )
def test_simple_server_custom_is_alive_route(self): app = HttpServerMock("test-http-server-mock", is_alive_route="/is-alive") @app.route("/", methods=["GET"]) def index_route(): return "random text" with app.run("localhost", 5001): r = requests.get("http://localhost:5001/") self.assertEqual(r.status_code, 200) self.assertEqual(r.text, "random text") if not isWindows: with self.assertRaises(Exception): requests.get("http://localhost:5001/")
def test_multiple_servers(self): app = HttpServerMock("test-http-server-mock") @app.route("/", methods=["GET"]) def index_route(): return "random text" another_app = HttpServerMock("another-test-http-server-mock") @another_app.route("/", methods=["GET"]) def another_index_route(): return "another random text" with app.run("localhost", 8000), another_app.run("localhost", 5004): r = requests.get("http://localhost:8000/") self.assertEqual(r.status_code, 200) self.assertEqual(r.text, "random text") r = requests.get("http://localhost:5004/") self.assertEqual(r.status_code, 200) self.assertEqual(r.text, "another random text") if not isWindows: with self.assertRaises(requests.exceptions.ConnectionError): requests.get("http://localhost:8000/") if not isWindows: with self.assertRaises(requests.exceptions.ConnectionError): requests.get("http://localhost:5004/")
from http_server_mock import HttpServerMock import requests import pytest app = HttpServerMock(__name__) @pytest.mark.skip(reason="no way of currently testing this") @app.route("/test/", methods=["GET"]) def index(): ''' Main index page endpoint ''' print("INSIDE /test route") return [{'id': 1}]
from http_server_mock import HttpServerMock from flask import Flask import requests app = HttpServerMock(__name__) @app.route('/api') def hello(): return {'skip': 0, 'limit': 150} @app.route('/api/details') def hell(): return {'skip': 0} @app.route('/user', methods=['POST']) def user(): return {'user': '******'} with app.run("localhost", 8081): r = requests.get("http://localhost:8081/") print(r.status_code == 200) print(r.text == "Hello world")