koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 1 | # ============LICENSE_START======================================================= |
| 2 | # INTEGRATION CSIT |
| 3 | # ================================================================================ |
| 4 | # Copyright (C) 2018 Nokia Intellectual Property. All rights reserved. |
| 5 | # ================================================================================ |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # ============LICENSE_END========================================================= |
| 18 | |
| 19 | import json |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 20 | import logging |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 21 | from functools import partial |
| 22 | from sys import argv |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 23 | from http.server import BaseHTTPRequestHandler, HTTPServer |
| 24 | |
| 25 | DEFAULT_PORT = 8443 |
| 26 | |
| 27 | |
| 28 | class SOHandler(BaseHTTPRequestHandler): |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 29 | def __init__(self, expected_requests, expected_responses, *args, **kwargs): |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 30 | |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 31 | self._expected_requests = expected_requests |
| 32 | self._expected_responses = expected_responses |
| 33 | super().__init__(*args, **kwargs) |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 34 | |
| 35 | def do_POST(self): |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 36 | logging.info( |
| 37 | 'POST called. Expected POST REQUEST: ' + json.dumps( |
| 38 | self._expected_requests["post"]) + '\nExpected POST response: ' + |
| 39 | json.dumps(self._expected_responses["post"])) |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 40 | self.send_response(200) |
| 41 | self._set_headers() |
| 42 | |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 43 | self.wfile.write(json.dumps(self._expected_responses["post"]).encode("utf-8")) |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 44 | return |
| 45 | |
| 46 | def do_GET(self): |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 47 | logging.info( |
| 48 | 'GET called. Expected GET REQUEST: ' + json.dumps( |
| 49 | self._expected_requests["get"]) + '\nExpected GET response: ' + |
| 50 | json.dumps(self._expected_responses["get"])) |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 51 | self.send_response(200) |
| 52 | self._set_headers() |
| 53 | |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 54 | self.wfile.write(json.dumps(self._expected_responses["get"]).encode("utf-8")) |
| 55 | return self._expected_responses["get"] |
| 56 | |
| 57 | def do_PUT(self): |
| 58 | request_body_json = self._get_request_body() |
| 59 | if request_body_json is not None: |
| 60 | self._apply_expected_data(request_body_json) |
| 61 | logging.info("EXPECTED RESPONSES: " + str(self._expected_responses)) |
| 62 | logging.info("EXPECTED REQUESTS: " + str(self._expected_requests)) |
| 63 | response_status = 200 |
| 64 | else: |
| 65 | response_status = 400 |
| 66 | self.send_response(response_status) |
| 67 | self._set_headers() |
| 68 | |
| 69 | def _get_request_body(self): |
| 70 | content_len = int(self.headers['Content-Length'], 0) |
| 71 | parsed_req_body = None |
| 72 | if content_len > 0: |
| 73 | body = self.rfile.read(content_len) |
| 74 | body_decoded = body.decode('utf8') |
| 75 | logging.info("BODY: %s type: %s body decoded: %s type: %s", str(body), type(body), str(body_decoded), |
| 76 | type(body_decoded)) |
| 77 | parsed_req_body = json.loads(body_decoded) |
| 78 | return parsed_req_body |
| 79 | |
| 80 | def _apply_expected_data(self, request_body_json): |
| 81 | if self.path == '/setResponse': |
| 82 | logging.info("IN PUT /setResponse: " + str(request_body_json)) |
| 83 | print("TYPE: %s and text: %s", type(request_body_json), str(request_body_json)) |
| 84 | self._expected_responses.update(request_body_json) |
| 85 | print("TYPE: %s", type(request_body_json)) |
| 86 | elif self.path == '/setRequest': |
| 87 | logging.info("IN PUT /setRequest: " + str(request_body_json)) |
| 88 | self._expected_requests.update(request_body_json) |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 89 | |
| 90 | def _set_headers(self): |
| 91 | self.send_header('Content-Type', 'application/json') |
| 92 | self.end_headers() |
| 93 | |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 94 | |
| 95 | class JsonFileToDictReader(object): |
| 96 | |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 97 | @staticmethod |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 98 | def read_expected_test_data(expected_responses_filename): |
| 99 | with open(expected_responses_filename, 'r') as file: |
| 100 | return json.load(file) |
| 101 | |
| 102 | |
| 103 | def init_so_simulator(): |
| 104 | expected_so_requests = JsonFileToDictReader.read_expected_test_data(argv[1]) |
| 105 | expected_so_responses = JsonFileToDictReader.read_expected_test_data(argv[2]) |
Ittay Stern | f8af5bf | 2019-03-27 18:21:29 +0200 | [diff] [blame] | 106 | logging.basicConfig(level=logging.INFO) |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 107 | handler = partial(SOHandler, expected_so_requests, expected_so_responses) |
| 108 | handler.protocol_version = "HTTP/1.0" |
| 109 | httpd = HTTPServer(('', DEFAULT_PORT), handler) |
| 110 | logging.info("serving on: " + str(httpd.socket.getsockname())) |
| 111 | httpd.serve_forever() |
adam | 6142bef | 2018-10-01 10:34:19 +0200 | [diff] [blame] | 112 | |
| 113 | |
| 114 | if __name__ == '__main__': |
koblosz | 2ac270a | 2018-10-23 07:32:30 +0200 | [diff] [blame] | 115 | init_so_simulator() |