1 # ============LICENSE_START=======================================================
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 from functools import partial
23 from http.server import BaseHTTPRequestHandler, HTTPServer
28 class SOHandler(BaseHTTPRequestHandler):
29 def __init__(self, expected_requests, expected_responses, *args, **kwargs):
31 self._expected_requests = expected_requests
32 self._expected_responses = expected_responses
33 super().__init__(*args, **kwargs)
37 'POST called. Expected POST REQUEST: ' + json.dumps(
38 self._expected_requests["post"]) + '\nExpected POST response: ' +
39 json.dumps(self._expected_responses["post"]))
40 self.send_response(200)
43 self.wfile.write(json.dumps(self._expected_responses["post"]).encode("utf-8"))
48 'GET called. Expected GET REQUEST: ' + json.dumps(
49 self._expected_requests["get"]) + '\nExpected GET response: ' +
50 json.dumps(self._expected_responses["get"]))
51 self.send_response(200)
54 self.wfile.write(json.dumps(self._expected_responses["get"]).encode("utf-8"))
55 return self._expected_responses["get"]
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))
66 self.send_response(response_status)
69 def _get_request_body(self):
70 content_len = int(self.headers['Content-Length'], 0)
71 parsed_req_body = None
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),
77 parsed_req_body = json.loads(body_decoded)
78 return parsed_req_body
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)
90 def _set_headers(self):
91 self.send_header('Content-Type', 'application/json')
95 class JsonFileToDictReader(object):
98 def read_expected_test_data(expected_responses_filename):
99 with open(expected_responses_filename, 'r') as file:
100 return json.load(file)
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])
106 logging.basicConfig(filename='output.log', level=logging.INFO)
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()
114 if __name__ == '__main__':