diff options
author | michal.banka <michal.banka@nokia.com> | 2019-04-09 13:29:37 +0200 |
---|---|---|
committer | Wojciech Sliwka <wojciech.sliwka@nokia.com> | 2019-04-11 18:25:55 +0200 |
commit | 5d7b066e556612e30d8e6130c47008bd9ca12a9b (patch) | |
tree | 746bc4c272daaa87f06159870a2ba6634ab395ec /tests/vid/resources/simulators/SO.py | |
parent | 823e2407491160b49501dee1c240aa0780a0ff8b (diff) |
Prepare new endpoints for csit/vid internal api
Change-Id: Ib69287eb1c21d30eb39472270c160f4b318c5c12
Issue-ID: VID-399
Signed-off-by: michal.banka <michal.banka@nokia.com>
Diffstat (limited to 'tests/vid/resources/simulators/SO.py')
-rw-r--r-- | tests/vid/resources/simulators/SO.py | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/tests/vid/resources/simulators/SO.py b/tests/vid/resources/simulators/SO.py index fa481b38..12fd7763 100644 --- a/tests/vid/resources/simulators/SO.py +++ b/tests/vid/resources/simulators/SO.py @@ -26,10 +26,13 @@ DEFAULT_PORT = 8443 class SOHandler(BaseHTTPRequestHandler): - def __init__(self, expected_requests, expected_responses, *args, **kwargs): + def __init__(self, expected_requests, expected_responses, requests, responses, *args, **kwargs): self._expected_requests = expected_requests self._expected_responses = expected_responses + self._requests = requests["requests"] + self._responses = responses["responses"] + self._known_endpoints = self._get_known_endpoints() super().__init__(*args, **kwargs) def do_POST(self): @@ -44,15 +47,26 @@ class SOHandler(BaseHTTPRequestHandler): return def do_GET(self): - logging.info( - 'GET called. Expected GET REQUEST: ' + json.dumps( - self._expected_requests["get"]) + '\nExpected GET response: ' + - json.dumps(self._expected_responses["get"])) - self.send_response(200) - self._set_headers() - - self.wfile.write(json.dumps(self._expected_responses["get"]).encode("utf-8")) - return self._expected_responses["get"] + print(self._known_endpoints) + if self._does_path_exist_as_endpoint(): + expected_response = self._get_response_by_path() + if expected_response: + self.send_response(expected_response["responseCode"]) + self._set_headers() + self.wfile.write(json.dumps(expected_response["body"]).encode("utf-8")) + else: + response_body = "{\"message:\" \"no response for endpoint\"}" + self.send_response(400) + self._set_headers() + self.wfile.write(json.dumps(response_body).encode("utf-8")) + else: + logging.info('GET called. Expected GET REQUEST: ' + + json.dumps(self._expected_requests["get"]) + + '\nExpected GET response: ' + + json.dumps(self._expected_responses["get"])) + self.send_response(200) + self._set_headers() + self.wfile.write(json.dumps(self._expected_responses["get"]).encode("utf-8")) def do_PUT(self): request_body_json = self._get_request_body() @@ -80,9 +94,7 @@ class SOHandler(BaseHTTPRequestHandler): def _apply_expected_data(self, request_body_json): if self.path == '/setResponse': logging.info("IN PUT /setResponse: " + str(request_body_json)) - print("TYPE: %s and text: %s", type(request_body_json), str(request_body_json)) self._expected_responses.update(request_body_json) - print("TYPE: %s", type(request_body_json)) elif self.path == '/setRequest': logging.info("IN PUT /setRequest: " + str(request_body_json)) self._expected_requests.update(request_body_json) @@ -91,6 +103,37 @@ class SOHandler(BaseHTTPRequestHandler): self.send_header('Content-Type', 'application/json') self.end_headers() + def _get_response_by_path(self): + for response in self._responses: + if response["path"] == self.path: + return response + + def _does_path_exist_as_endpoint(self): + does_exist = False + for ep in self._known_endpoints: + if ep == self.path: + does_exist = True + break + return does_exist + + def _create_path_from_request(self, request): + base_uri = request["path"] + query_params = request["queryParams"] + return base_uri + self._get_params_uri(query_params) + + def _get_known_endpoints(self): + endpoints = [] + for request in self._requests: + endpoints.append(self._create_path_from_request(request)) + return endpoints + + @staticmethod + def _get_params_uri(query_params): + params_uri = "?" + for parameter in query_params: + params_uri += parameter + '=' + query_params.get(parameter, '') + '&' + return params_uri[:-1] + class JsonFileToDictReader(object): @@ -101,10 +144,13 @@ class JsonFileToDictReader(object): def init_so_simulator(): + requests = JsonFileToDictReader.read_expected_test_data("test_data_assets/requests.json") + responses = JsonFileToDictReader.read_expected_test_data("test_data_assets/responses.json") + expected_so_requests = JsonFileToDictReader.read_expected_test_data(argv[1]) expected_so_responses = JsonFileToDictReader.read_expected_test_data(argv[2]) logging.basicConfig(level=logging.INFO) - handler = partial(SOHandler, expected_so_requests, expected_so_responses) + handler = partial(SOHandler, expected_so_requests, expected_so_responses, requests, responses) handler.protocol_version = "HTTP/1.0" httpd = HTTPServer(('', DEFAULT_PORT), handler) logging.info("serving on: " + str(httpd.socket.getsockname())) |