def do_POST(self): # pylint: disable=C0103 """Handles a POST request to the server. Sends 400 error if there is an issue, otherwise sends a success message. Raises: ValidationError: Returns when data given is not valid against schema HTTPError: Returns when there is an error sending message to output url """ content_length = int(self.headers['Content-Length']) data = self.rfile.read(content_length) data = data.decode("utf-8") try: datahelper.validate_schema(data, SCHEMA_FILEPATH) except BaseException: self.send_error( 400, 'Incorrect data format. Please check JSON schema.') logging.error('Incorrect data format. Please check JSON schema.') raise try: transformed_data = datahelper.transform(data) output_message(transformed_data) self._set_headers() self.wfile.write(bytes("Data successfully consumed", 'utf8')) except BaseException: self.send_error(400, 'Error when sending output message') logging.error('Error when sending output message') raise
def test_transform(data): """ Test the simple transformation on the input data """ encoded_original_json = json.dumps(data) transformed_object = datahelper.transform(encoded_original_json) data['intValue'] = data['intValue'] + 100 assert transformed_object == data