Skip to content

Commit 93bd437

Browse files
committed
Improve the mock server for testing to work with additional query string values
1 parent 3a3e349 commit 93bd437

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

tests/mock_web_api_server.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from http.server import HTTPServer, SimpleHTTPRequestHandler
99
from typing import Type
1010
from unittest import TestCase
11-
from urllib.parse import urlparse, parse_qs
11+
from urllib.parse import urlparse, parse_qs, ParseResult
1212

1313
from multiprocessing import Process
1414
from urllib.request import urlopen, Request
@@ -90,38 +90,38 @@ def set_common_headers(self):
9090
"""
9191

9292
def _handle(self):
93-
self.received_requests[self.path] = self.received_requests.get(self.path, 0) + 1
93+
parsed_path: ParseResult = urlparse(self.path)
94+
path = parsed_path.path
95+
self.received_requests[path] = self.received_requests.get(path, 0) + 1
9496
try:
95-
if self.path == "/webhook":
97+
if path == "/webhook":
9698
self.send_response(200)
9799
self.set_common_headers()
98100
self.wfile.write("OK".encode("utf-8"))
99101
return
100102

101-
if self.path == "/received_requests.json":
103+
if path == "/received_requests.json":
102104
self.send_response(200)
103105
self.set_common_headers()
104106
self.wfile.write(json.dumps(self.received_requests).encode("utf-8"))
105107
return
106108

107109
body = {"ok": True}
108-
if self.path == "/oauth.v2.access":
110+
if path == "/oauth.v2.access":
109111
self.send_response(200)
110112
self.set_common_headers()
111113
self.wfile.write(self.oauth_v2_access_response.encode("utf-8"))
112114
return
113115

114116
if self.is_valid_user_token():
115-
if self.path == "/auth.test":
117+
if path == "/auth.test":
116118
self.send_response(200)
117119
self.set_common_headers()
118120
self.wfile.write(self.user_auth_test_response.encode("utf-8"))
119121
return
120122

121123
if self.is_valid_token():
122-
parsed_path = urlparse(self.path)
123-
124-
if self.path == "/auth.test":
124+
if path == "/auth.test":
125125
self.send_response(200)
126126
self.set_common_headers()
127127
self.wfile.write(self.bot_auth_test_response.encode("utf-8"))
@@ -148,7 +148,7 @@ def _handle(self):
148148
k: v[0] for k, v in parse_qs(parsed_path.query).items()
149149
}
150150

151-
self.logger.info(f"request body: {request_body}")
151+
self.logger.info(f"request: {path} {request_body}")
152152

153153
header = self.headers["authorization"]
154154
pattern = str(header).split("xoxb-", 1)[1]

0 commit comments

Comments
 (0)