def test_optional_value(self): input_val = OptionalValue(StringValue('val1')) actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '"val1"' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val) input_val = OptionalValue() actual_val = DataValueConverter.convert_to_json(input_val) expected_val = 'null' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_default(self): input_val = 'hello' actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '"hello"' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_double_value(self): input_val = DoubleValue(10.10) actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '1.01E1' self.assertEqual(expected_val, actual_val) # Verify json is valid output = json.loads(actual_val, parse_float=decimal.Decimal) self.assertEqual(output, decimal.Decimal('10.10')) input_val = DoubleValue(decimal.Decimal('10.10')) actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '1.01E1' self.assertEqual(expected_val, actual_val) # Verify json is valid output = json.loads(actual_val, parse_float=decimal.Decimal) self.assertEqual(output, decimal.Decimal('10.10'))
def test_void_value(self): input_val = VoidValue() actual_val = DataValueConverter.convert_to_json(input_val) expected_val = 'null' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_secret_value(self): input_val = SecretValue('password') actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '"password"' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_boolean_value(self): input_val = BooleanValue(True) actual_val = DataValueConverter.convert_to_json(input_val) expected_val = 'true' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_integer_value(self): input_val = IntegerValue(10) actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '10' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_string_value(self): input_val = StringValue('val1') actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '"val1"' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_binary_value(self): input_val = BlobValue(b'asdlkfu') actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '"%s"' % base64.b64encode(b'asdlkfu').decode('utf-8') self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_list_value(self): input_val = ListValue( values=[StringValue('val1'), StringValue('val2')]) actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '["val1","val2"]' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def _serialize_output(self, request, service_id, operation_id, method_result, use_cookies): """ Serialize the MethodResult object :type request: :class:`werkzeug.wrappers.Request` :param request: Request object :type service_id: :class:`str` :param service_id: Identifier of the service to be invoked. :type operation_id: :class:`str` :param operation_id: Identifier of the operation to be invoked. :type method_result: :class:`vmware.vapi.core.MethodResult` :param method_result: MethodResult object to be serialized :type use_cookies: :class:`bool` :param use_cookies: Whether cookies are to be sent in response :rtype: :class:`tuple` of :class:`int`, :class:`str`, :class:`dict` :return: HTTP status code, serialized json output of the operation and a dictionary of response cookies. """ if method_result.success(): if self.response_cookie_builder is None or not use_cookies: cookies = None else: cookies = self.response_cookie_builder.build( service_id, operation_id, method_result) output = method_result.output json_output = None if not isinstance(output, VoidValue): if not isinstance(output, StructValue): # If the output is not a struct or error value, # box it with {'value':..} to make it a # good json output output = StructValue(name='output', values={'value': output}) json_output = DataValueConverter.convert_to_json(output) status = 204 if request.method == 'DELETE' else 200 else: cookies = None error = method_result.error json_output = DataValueConverter.convert_to_json(error) status = vapi_to_http_error_map.get(error.name, 500) return (status, json_output, cookies)
def test_error_value(self): input_val = ErrorValue(name='name', values={ 'val1': StringValue('val1'), 'val2': StringValue('val2') }) actual_val = DataValueConverter.convert_to_json(input_val) self.assertTrue('"val1":"val1"' in actual_val) self.assertTrue('"val2":"val2"' in actual_val) # Verify json is valid json.loads(actual_val)
def test_struct_value_with_optional_unset(self): input_val = StructValue(name='name', values={ 'val1': StringValue('val1'), 'val2': OptionalValue() }) actual_val = DataValueConverter.convert_to_json(input_val) expected_val = '{"val1":"val1"}' self.assertEqual(expected_val, actual_val) # Verify json is valid json.loads(actual_val)
def test_struct_value_with_optional_set(self): input_val = StructValue(name='name', values={ 'val1': StringValue('val1'), 'val2': OptionalValue(StringValue('val2')) }) actual_val = DataValueConverter.convert_to_json(input_val) self.assertTrue('"val1":"val1"' in actual_val) self.assertTrue('"val2":"val2"' in actual_val) # Verify json is valid json.loads(actual_val)
def __call__(self, request): """ The implementation of WsgiApplication :type request: :class:`werkzeug.wrappers.Request` :param request: Request object :rtype: :class:`werkzeug.wrappers.Response` :return: Response object """ try: urls = self.rule_map.bind_to_environ(request.environ) endpoint, args = urls.match() if provider_wire_logger.isEnabledFor(logging.DEBUG): provider_wire_logger.debug('HTTP %s %s ', request.method, request.url) provider_wire_logger.debug('REQUEST HEADERS: %s', request.headers) provider_wire_logger.debug('REQUEST BODY: %s', request.get_data()) if endpoint == JSONRPC: result, headers = self._handle_jsonrpc_call(request) response = Response(result) if headers: response.headers = headers else: status, result, cookies, headers = self._handle_rest_call( request, endpoint, args) response = Response(result) response.status_code = status if headers: response.headers = headers if cookies: path = self._rest_prefix for k, v in six.iteritems(cookies): response.set_cookie(k, v, path=path) response.content_type = JSON_CONTENT_TYPE if provider_wire_logger.isEnabledFor(logging.DEBUG): provider_wire_logger.debug('RESPONSE STATUS: %s', response.status_code) provider_wire_logger.debug('RESPONSE HEADERS: %s', response.headers) provider_wire_logger.debug('RESPONSE BODY: %s', response.response) return response except HTTPException as e: try: internal_server_error_def = make_std_error_def( 'com.vmware.vapi.std.errors.invalid_request') msg = message_factory.get_message( 'vapi.method.invoke.exception', # pylint: disable=line-too-long e.description) error_value = make_error_value_from_msgs( internal_server_error_def, msg) json_output = DataValueConverter.convert_to_json(error_value) response = e.get_response() response.set_data(json_output) response.headers['Content-Type'] = 'application/json' return response except Exception: # if there is error in serialize the error, # just return the original raw exception return e