Skip to content

Commit

Permalink
Adding User-Agent and updating model definitions
Browse files Browse the repository at this point in the history
Updated ask-sdk-model-runtime test cases to use UnitTest framework
  • Loading branch information
Govinda Raju committed Dec 19, 2019
1 parent eb55886 commit 74ea73b
Show file tree
Hide file tree
Showing 20 changed files with 563 additions and 252 deletions.
6 changes: 6 additions & 0 deletions ask-sdk-model-runtime/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ CHANGELOG
---

* Initial release of alexa sdk model runtime.

1.0.1
-----

* Adding User-Agent functionality support to set custom user agent values.
* Updated all Unit tests to Python UnitTest framework.
3 changes: 2 additions & 1 deletion ask-sdk-model-runtime/ask_sdk_model_runtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
from .serializer import DefaultSerializer, Serializer
from .exceptions import (
ServiceException, SerializationException, ApiClientException)
from .service_client_response import ServiceClientResponse
from .service_client_response import ServiceClientResponse
from .utils import user_agent_info
2 changes: 1 addition & 1 deletion ask-sdk-model-runtime/ask_sdk_model_runtime/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
'Authentication Configuration etc that is used by the SDK '
'models.')
__url__ = 'https://github.com/alexa/alexa-apis-for-python'
__version__ = '1.0.0'
__version__ = '1.0.1'
__author__ = 'Alexa Skills Kit'
__author_email__ = 'ask-sdk-dynamic@amazon.com'
__license__ = 'Apache 2.0'
Expand Down
39 changes: 39 additions & 0 deletions ask-sdk-model-runtime/ask_sdk_model_runtime/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
#
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights
# Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the
# License.
#
import sys

def user_agent_info(sdk_version, custom_user_agent):
# type: (str, str) -> str
"""Return the user agent info along with the SDK and Python
Version information.
:param sdk_version: Version of the SDK being used.
:type sdk_version: str
:param custom_user_agent: Custom User Agent string provided by
the developer.
:type custom_user_agent: str
:return: User Agent Info string
:rtype: str
"""
python_version = ".".join(str(x) for x in sys.version_info[0:3])
user_agent = "ask-python-model/{} Python/{}".format(
sdk_version, python_version)
if custom_user_agent is None:
return user_agent
else:
return user_agent + " " + custom_user_agent
46 changes: 23 additions & 23 deletions ask-sdk-model-runtime/tests/unit/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def test_convert_null_header_tuples_to_dict(self):
test_headers_list = None
expected_headers_dict = {}

assert self.test_api_client._convert_list_tuples_to_dict(
test_headers_list) == expected_headers_dict, (
self.assertEqual(self.test_api_client._convert_list_tuples_to_dict(
test_headers_list), expected_headers_dict, (
"DefaultApiClient failed to convert null headers list to empty "
"dict object")
"dict object"))

def test_convert_header_tuples_to_dict(self):
test_headers_list = [
Expand All @@ -53,20 +53,20 @@ def test_convert_header_tuples_to_dict(self):
expected_headers_dict = {
"header_1": "test_1, test_3", "header_2": "test_2"}

assert self.test_api_client._convert_list_tuples_to_dict(
test_headers_list) == expected_headers_dict, (
self.assertEqual(self.test_api_client._convert_list_tuples_to_dict(
test_headers_list), expected_headers_dict, (
"DefaultApiClient failed to convert header list of tuples to "
"dictionary format needed for http "
"request call")
"request call"))

def test_convert_null_header_dict_to_tuples(self):
test_headers_dict = None
expected_headers_list = []

assert self.test_api_client._convert_dict_to_list_tuples(
test_headers_dict) == expected_headers_list, (
self.assertEqual(self.test_api_client._convert_dict_to_list_tuples(
test_headers_dict), expected_headers_list, (
"DefaultApiClient failed to convert null headers dict to empty "
"list object")
"list object"))

def test_convert_header_dict_to_tuples(self):
test_headers_dict = {
Expand All @@ -76,11 +76,11 @@ def test_convert_header_dict_to_tuples(self):
("header_1", "test_1"), ("header_1", "test_3"),
("header_2", "test_2"), ("header_3", "test_4")]

assert set(self.test_api_client._convert_dict_to_list_tuples(
test_headers_dict)) == set(
self.assertEqual(set(self.test_api_client._convert_dict_to_list_tuples(
test_headers_dict)), set(
expected_headers_list), (
"DefaultApiClient failed to convert headers dict to list of "
"tuples format for ApiClientResponse")
"tuples format for ApiClientResponse"))

def test_resolve_valid_http_method(self):
with mock.patch("requests.get",
Expand All @@ -104,15 +104,15 @@ def test_resolve_invalid_http_method_throw_exception(self):
with self.assertRaises(ApiClientException) as exc:
self.test_api_client.invoke(test_invalid_method_request)

assert "Invalid request method: GET_TEST" in str(exc.exception)
self.assertIn("Invalid request method: GET_TEST", str(exc.exception))

def test_invoke_http_method_throw_exception(self):
with mock.patch("requests.get",
side_effect=Exception("test exception")):
with self.assertRaises(ApiClientException) as exc:
self.test_api_client.invoke(self.valid_request)

assert "Error executing the request: test exception" in str(exc.exception)
self.assertIn("Error executing the request: test exception", str(exc.exception))

def test_api_client_invoke_with_method_headers_processed(self):
self.valid_request.headers = [
Expand All @@ -131,21 +131,21 @@ def test_api_client_invoke_with_method_headers_processed(self):
side_effect=lambda *args, **kwargs: test_response):
actual_response = self.test_api_client.invoke(self.valid_request)

assert set(actual_response.headers) == set([
self.assertEqual(set(actual_response.headers), set([
("response_header_1", "test_1"),
("response_header_1", "test_3"),
("response_header_2", "test_2"),
("response_header_3", "test_4")]), (
"Response headers from client doesn't match with the "
"expected headers")
"expected headers"))

assert actual_response.status_code == 400, (
self.assertEqual(actual_response.status_code, 400, (
"Status code from client response doesn't match with the "
"expected response status code")
"expected response status code"))

assert actual_response.body == "test response body", (
self.assertEqual(actual_response.body, "test response body", (
"Body from client response doesn't match with the expected "
"response body")
"response body"))

def test_api_client_invoke_with_http_url_throw_error(self):
test_invalid_url_scheme_request = ApiClientRequest(
Expand All @@ -157,7 +157,7 @@ def test_api_client_invoke_with_http_url_throw_error(self):
with self.assertRaises(ApiClientException) as exc:
self.test_api_client.invoke(test_invalid_url_scheme_request)

assert "Requests against non-HTTPS endpoints are not allowed." in str(exc.exception)
self.assertIn("Requests against non-HTTPS endpoints are not allowed.", str(exc.exception))

def test_api_client_invoke_with_http_case_sensitive_url_throw_error(self):
test_invalid_url_scheme_request = ApiClientRequest(
Expand All @@ -169,7 +169,7 @@ def test_api_client_invoke_with_http_case_sensitive_url_throw_error(self):
with self.assertRaises(ApiClientException) as exc:
self.test_api_client.invoke(test_invalid_url_scheme_request)

assert "Requests against non-HTTPS endpoints are not allowed." in str(exc.exception)
self.assertIn("Requests against non-HTTPS endpoints are not allowed.", str(exc.exception))

def test_api_client_invoke_with_no_url_schema_throw_error(self):
test_invalid_url_scheme_request = ApiClientRequest(
Expand All @@ -181,7 +181,7 @@ def test_api_client_invoke_with_no_url_schema_throw_error(self):
with self.assertRaises(ApiClientException) as exc:
self.test_api_client.invoke(test_invalid_url_scheme_request)

assert "Requests against non-HTTPS endpoints are not allowed." in str(exc.exception)
self.assertIn("Requests against non-HTTPS endpoints are not allowed.", str(exc.exception))

def test_api_client_send_request_with_raw_data_serialized_for_json_content(
self):
Expand Down
Loading

0 comments on commit 74ea73b

Please sign in to comment.