test-cloud-meta-mock: add and use helper to send response code

This commit is contained in:
Thomas Haller
2023-05-17 17:09:51 +02:00
parent 77fd4d5f0e
commit 4691f45bde

View File

@@ -33,39 +33,37 @@ class MockCloudMDRequestHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args): def log_message(self, format, *args):
pass pass
def _response_and_end(self, code):
self.send_response(code)
self.end_headers()
def do_GET(self): def do_GET(self):
path = self.path.encode("ascii") path = self.path.encode("ascii")
if path in self.server._resources: if path in self.server._resources:
self.send_response(200) self._response_and_end(200)
self.end_headers()
self.wfile.write(self.server._resources[path]) self.wfile.write(self.server._resources[path])
else: else:
self.send_response(404) self._response_and_end(404)
self.end_headers()
def do_PUT(self): def do_PUT(self):
path = self.path.encode("ascii") path = self.path.encode("ascii")
if path == b"/latest/api/token": if path == b"/latest/api/token":
self.send_response(200) self._response_and_end(200)
self.end_headers()
self.wfile.write( self.wfile.write(
b"AQAAALH-k7i18JMkK-ORLZQfAa7nkNjQbKwpQPExNHqzk1oL_7eh-A==" b"AQAAALH-k7i18JMkK-ORLZQfAa7nkNjQbKwpQPExNHqzk1oL_7eh-A=="
) )
else: else:
length = int(self.headers["content-length"]) length = int(self.headers["content-length"])
self.server._resources[path] = self.rfile.read(length) self.server._resources[path] = self.rfile.read(length)
self.send_response(201) self._response_and_end(201)
self.end_headers()
def do_DELETE(self): def do_DELETE(self):
path = self.path.encode("ascii") path = self.path.encode("ascii")
if path in self.server._resources: if path in self.server._resources:
del self.server._resources[path] del self.server._resources[path]
self.send_response(204) self._response_and_end(204)
self.end_headers()
else: else:
self.send_response(404) self._response_and_end(404)
self.end_headers()
class SocketHTTPServer(HTTPServer): class SocketHTTPServer(HTTPServer):