-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_http_server_mock.py
121 lines (87 loc) · 3.85 KB
/
test_http_server_mock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from http_server_mock import HttpServerMock
import requests
import time
import unittest
import os
isWindows = False
if os.name == "nt":
isWindows = True
class TestHttpServerMock(unittest.TestCase):
def test_simple_server(self):
app = HttpServerMock("test-http-server-mock")
@app.route("/", methods=["GET"])
def index_route():
return "random text"
with app.run("localhost", 5000):
r = requests.get("http://localhost:5000/")
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, "random text")
if not isWindows:
with self.assertRaises(Exception):
requests.get("http://localhost:5000/")
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_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 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/")
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/")