blob: fa481b3863dbd213ebce2ac5540ebc76900fe820 [file] [log] [blame]
koblosz2ac270a2018-10-23 07:32:30 +02001# ============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
19import json
adam6142bef2018-10-01 10:34:19 +020020import logging
koblosz2ac270a2018-10-23 07:32:30 +020021from functools import partial
22from sys import argv
adam6142bef2018-10-01 10:34:19 +020023from http.server import BaseHTTPRequestHandler, HTTPServer
24
25DEFAULT_PORT = 8443
26
27
28class SOHandler(BaseHTTPRequestHandler):
koblosz2ac270a2018-10-23 07:32:30 +020029 def __init__(self, expected_requests, expected_responses, *args, **kwargs):
adam6142bef2018-10-01 10:34:19 +020030
koblosz2ac270a2018-10-23 07:32:30 +020031 self._expected_requests = expected_requests
32 self._expected_responses = expected_responses
33 super().__init__(*args, **kwargs)
adam6142bef2018-10-01 10:34:19 +020034
35 def do_POST(self):
koblosz2ac270a2018-10-23 07:32:30 +020036 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"]))
adam6142bef2018-10-01 10:34:19 +020040 self.send_response(200)
41 self._set_headers()
42
koblosz2ac270a2018-10-23 07:32:30 +020043 self.wfile.write(json.dumps(self._expected_responses["post"]).encode("utf-8"))
adam6142bef2018-10-01 10:34:19 +020044 return
45
46 def do_GET(self):
koblosz2ac270a2018-10-23 07:32:30 +020047 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"]))
adam6142bef2018-10-01 10:34:19 +020051 self.send_response(200)
52 self._set_headers()
53
koblosz2ac270a2018-10-23 07:32:30 +020054 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)
adam6142bef2018-10-01 10:34:19 +020089
90 def _set_headers(self):
91 self.send_header('Content-Type', 'application/json')
92 self.end_headers()
93
koblosz2ac270a2018-10-23 07:32:30 +020094
95class JsonFileToDictReader(object):
96
adam6142bef2018-10-01 10:34:19 +020097 @staticmethod
koblosz2ac270a2018-10-23 07:32:30 +020098 def read_expected_test_data(expected_responses_filename):
99 with open(expected_responses_filename, 'r') as file:
100 return json.load(file)
101
102
103def 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 Sternf8af5bf2019-03-27 18:21:29 +0200106 logging.basicConfig(level=logging.INFO)
koblosz2ac270a2018-10-23 07:32:30 +0200107 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()
adam6142bef2018-10-01 10:34:19 +0200112
113
114if __name__ == '__main__':
koblosz2ac270a2018-10-23 07:32:30 +0200115 init_so_simulator()