forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttptest.py
68 lines (58 loc) · 2.24 KB
/
httptest.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
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import unittest
from http import HTTPStatus
from http.server import BaseHTTPRequestHandler, HTTPServer
from threading import Thread
class HttpTestBase(unittest.TestCase):
DEFAULT_RESPONSE = b"Hello!"
class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1" # Support keep-alive.
# timeout = 3 # No timeout -- if shutdown hangs, make sure to close your connection
STATUS_RE = re.compile(r"/status/(\d+)")
def do_GET(self): # pylint:disable=invalid-name
status_match = self.STATUS_RE.fullmatch(self.path)
status = 200
if status_match:
status = int(status_match.group(1))
if status == 200:
body = HttpTestBase.DEFAULT_RESPONSE
self.send_response(HTTPStatus.OK)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
else:
self.send_error(status)
@classmethod
def create_server(cls):
server_address = ("127.0.0.1", 0) # Only bind to localhost.
return HTTPServer(server_address, cls.Handler)
@classmethod
def run_server(cls):
httpd = cls.create_server()
worker = Thread(
target=httpd.serve_forever, daemon=True, name="Test server worker"
)
worker.start()
return worker, httpd
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.server_thread, cls.server = cls.run_server()
@classmethod
def tearDownClass(cls):
cls.server.shutdown()
cls.server_thread.join()
super().tearDownClass()