Esempio n. 1
0
    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/")
Esempio n. 2
0
    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/")
Esempio n. 3
0
    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/")
Esempio n. 4
0
    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"
        )
Esempio n. 5
0
    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/")
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")