<commit_before>from mock import Mock
import sys
import unittest
from zappa.handler import LambdaHandler


def no_args():
    return


def one_arg(first):
    return first


def two_args(first, second):
    return first, second


def var_args(*args):
    return args


def var_args_with_one(first, *args):
    return first, args[0]


def unsupported(first, second, third):
    return first, second, third


def raises_exception(*args, **kwargs):
    raise Exception('app exception')


def handle_bot_intent(event, context):
    return "Success"


mocked_exception_handler = Mock()


class TestZappa(unittest.TestCase):

    def setUp(self):
        mocked_exception_handler.reset_mock()

    def tearDown(self):
        LambdaHandler._LambdaHandler__instance = None
        LambdaHandler.settings = None
        LambdaHandler.settings_name = None

    def test_run_function(self):
        self.assertIsNone(LambdaHandler.run_function(no_args, 'e', 'c'))
        self.assertEqual(LambdaHandler.run_function(one_arg, 'e', 'c'), 'e')
        self.assertEqual(LambdaHandler.run_function(two_args, 'e', 'c'), ('e', 'c'))
        self.assertEqual(LambdaHandler.run_function(var_args, 'e', 'c'), ('e', 'c'))
        self.assertEqual(LambdaHandler.run_function(var_args_with_one, 'e', 'c'), ('e', 'c'))

        try:
            LambdaHandler.run_function(unsupported, 'e', 'c')
            self.fail('Exception expected')
        except RuntimeError as e:
            pass

    def test_run_fuction_with_type_hint(self):
        python_version = sys.version_info[0]
        # type hints are python 3 only
        if python_version == 3:
            scope = {}
            exec('def f_with_type_hint() -> None: return', scope)
            f_with_type_hint = scope['f_with_type_hint']
            self.assertIsNone(LambdaHandler.run_function(f_with_type_hint, 'e', 'c'))

    def test_merge_headers_no_multi_value(self):
        lh = LambdaHandler('tests.test_wsgi_script_name_settings')
        event = {
            'headers': {
                'a': 'b'
            }
        }

        merged = lh._merge_headers(event)
        self.assertEqual(merged['a'], 'b')

    def test_merge_headers_combine_values(self):
        lh = LambdaHandler('tests.test_wsgi_script_name_settings')
        event = {
            'headers': {
                'a': 'b'
            },
            'multiValueHeaders': {
                'a': ['c'],
                'x': ['y']
            }
        }

        merged = lh._merge_headers(event)
        self.assertEqual(merged['a'], 'c, b')
        self.assertEqual(merged['x'], 'y')

    def test_merge_headers_no_single_value(self):
        lh = LambdaHandler('tests.test_wsgi_script_name_settings')
        event = {
            'multiValueHeaders': {
                'a': ['c', 'd'],
                'x': ['y', 'z', 'f']
            }
        }
        merged = lh._merge_headers(event)
        self.assertEqual(merged['a'], 'c, d')
        self.assertEqual(merged['x'], 'y, z, f')

    def test_wsgi_script_name_on_aws_url(self):
        """
        Ensure that requests to the amazonaws.com host for an API with a
        domain have the correct request.url
        """
        lh = LambdaHandler('tests.test_wsgi_script_name_settings')

        event = {
            'body': '',
            'resource': '/{proxy+}',
            'requestContext': {},
            'queryStringParameters': {},
            'headers': {
                'Host': '1234567890.execute-api.us-east-1.amazonaws.com',
            },
            'pathParameters': {<commit_msg>add extra assert to test<commit_after>from mock import Mock
import sys
import unittest
from zappa.handler import LambdaHandler


def no_args():
    return


def one_arg(first):
    return first


def two_args(first, second):
    return first, second


def var_args(*args):
    return args


def var_args_with_one(first, *args):
    return first, args[0]


def unsupported(first, second, third):
    return first, second, third


def raises_exception(*args, **kwargs):
    raise Exception('app exception')


def handle_bot_intent(event, context):
    return "Success"


mocked_exception_handler = Mock()


class TestZappa(unittest.TestCase):

    def setUp(self):
        mocked_exception_handler.reset_mock()

    def tearDown(self):
        LambdaHandler._LambdaHandler__instance = None
        LambdaHandler.settings = None
        LambdaHandler.settings_name = None

    def test_run_function(self):
        self.assertIsNone(LambdaHandler.run_function(no_args, 'e', 'c'))
        self.assertEqual(LambdaHandler.run_function(one_arg, 'e', 'c'), 'e')
        self.assertEqual(LambdaHandler.run_function(two_args, 'e', 'c'), ('e', 'c'))
        self.assertEqual(LambdaHandler.run_function(var_args, 'e', 'c'), ('e', 'c'))
        self.assertEqual(LambdaHandler.run_function(var_args_with_one, 'e', 'c'), ('e', 'c'))

        try:
            LambdaHandler.run_function(unsupported, 'e', 'c')
            self.fail('Exception expected')
        except RuntimeError as e:
            pass

    def test_run_fuction_with_type_hint(self):
        python_version = sys.version_info[0]
        # type hints are python 3 only
        if python_version == 3:
            scope = {}
            exec('def f_with_type_hint() -> None: return', scope)
            f_with_type_hint = scope['f_with_type_hint']
            self.assertIsNone(LambdaHandler.run_function(f_with_type_hint, 'e', 'c'))

    def test_merge_headers_no_multi_value(self):
        lh = LambdaHandler('tests.test_wsgi_script_name_settings')
        event = {
            'headers': {
                'a': 'b'
            }
        }

        merged = lh._merge_headers(event)
        self.assertEqual(merged['a'], 'b')

    def test_merge_headers_combine_values(self):
        lh = LambdaHandler('tests.test_wsgi_script_name_settings')
        event = {
            'headers': {
                'a': 'b',
                'z': 'q'
            },
            'multiValueHeaders': {
                'a': ['c'],
                'x': ['y']
            }
        }

        merged = lh._merge_headers(event)
        self.assertEqual(merged['a'], 'c, b')
        self.assertEqual(merged['x'], 'y')
        self.assertEqual(merged['z'], 'q')

    def test_merge_headers_no_single_value(self):
        lh = LambdaHandler('tests.test_wsgi_script_name_settings')
        event = {
            'multiValueHeaders': {
                'a': ['c', 'd'],
                'x': ['y', 'z', 'f']
            }
        }
        merged = lh._merge_headers(event)
        self.assertEqual(merged['a'], 'c, d')
        self.assertEqual(merged['x'], 'y, z, f')

    def test_wsgi_script_name_on_aws_url(self):
        """
        Ensure that requests to the amazonaws.com host for an API with a
        domain have the correct request.url
        """
        lh = LambdaHandler('tests.test_wsgi_script_name_settings')

        event = {
            'body': '',
            'resource': '/{proxy+}',
            'requestContext': {},
            'queryStringParameters': {},
            'headers': {
                'Host': '1234567890.execute-api.us-east-1.amazonaws.com',
            },
            'pathParameters': {
{'fraction_non_alphanumeric': 0.13008580127318017, 'fraction_numerical': 0.004151674508718516, 'mean_word_length': 2.3273480662983426, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1420796', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2165, 'n_words': 434}