diff --git a/ate/context.py b/ate/context.py index 0b3fb508..0a82cda7 100644 --- a/ate/context.py +++ b/ate/context.py @@ -5,7 +5,6 @@ import sys from collections import OrderedDict from ate import utils -from ate.exception import ParamsError from ate.testcase import TestcaseParser @@ -135,13 +134,6 @@ class Context(object): @param request_dict: request config mapping @param level: testset or testcase """ - if "headers" in request_dict: - # convert keys in request headers to lowercase - headers = request_dict.pop("headers") - if not isinstance(headers, dict): - raise ParamsError("HTTP Request Headers invalid!") - request_dict["headers"] = {key.lower(): headers[key] for key in headers} - if level == "testset": request_dict = self.testcase_parser.parse_content_with_bindings( request_dict diff --git a/ate/runner.py b/ate/runner.py index c24a29cf..43ba8892 100644 --- a/ate/runner.py +++ b/ate/runner.py @@ -1,4 +1,4 @@ -from ate import exception, response +from ate import exception, response, utils from ate.client import HttpSession from ate.context import Context @@ -48,6 +48,9 @@ class Runner(object): } @param (str) context level, testcase or testset """ + # convert keys in request headers to lowercase + config_dict = utils.lower_dict_key(config_dict) + self.context.init_context(level) self.context.config_context(config_dict, level) diff --git a/ate/utils.py b/ate/utils.py index 02ad1058..74ef7be8 100644 --- a/ate/utils.py +++ b/ate/utils.py @@ -329,3 +329,20 @@ def search_conf_item(start_path, item_type, item_name): raise exception.VariableNotFound(err_msg) return search_conf_item(dir_path, item_type, item_name) + +def lower_dict_key(origin_dict, depth=1): + """ convert dict key to lower case, with depth control supported. + """ + new_dict = {} + + for key, value in origin_dict.items(): + if depth > 2: + new_dict[key] = value + continue + + if isinstance(value, dict): + value = lower_dict_key(value, depth+1) + + new_dict[key.lower()] = value + + return new_dict diff --git a/tests/test_context.py b/tests/test_context.py index 0b40d6f5..ef276be4 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -178,26 +178,6 @@ class VariableBindsUnittest(unittest.TestCase): SECRET_KEY = context_variables["SECRET_KEY"] self.assertEqual(SECRET_KEY, "DebugTalk") - def test_parse_request(self): - request_dict = { - "url": "http://debugtalk.com", - "method": "GET", - "headers": { - "Content-Type": "application/json", - "USER-AGENT": "ios/10.3" - } - } - - parsed_request = self.context.get_parsed_request(request_dict) - self.assertIn("content-type", parsed_request["headers"]) - self.assertIn("user-agent", parsed_request["headers"]) - - request_dict = { - "headers": "invalid headers" - } - with self.assertRaises(ParamsError): - self.context.get_parsed_request(request_dict) - def test_get_parsed_request(self): test_runner = runner.Runner() testcase = { @@ -210,14 +190,14 @@ class VariableBindsUnittest(unittest.TestCase): ], "request": { "url": "http://127.0.0.1:5000/api/users/1000", - "method": "POST", - "headers": { + "METHOD": "POST", + "Headers": { "Content-Type": "application/json", "authorization": "$authorization", "random": "$random", "SECRET_KEY": "$SECRET_KEY" }, - "data": "$data" + "Data": "$data" } } parsed_request = test_runner.init_config(testcase, level="testcase") @@ -227,7 +207,7 @@ class VariableBindsUnittest(unittest.TestCase): self.assertEqual(len(parsed_request["headers"]["random"]), 5) self.assertIn("data", parsed_request) self.assertEqual(parsed_request["data"], testcase["variable_binds"][2]["data"]) - self.assertEqual(parsed_request["headers"]["secret_key"], "DebugTalk") + self.assertEqual(parsed_request["headers"]["SECRET_KEY"], "DebugTalk") def test_exec_content_functions(self): test_runner = runner.Runner() diff --git a/tests/test_utils.py b/tests/test_utils.py index 51babe00..92e87103 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -293,3 +293,23 @@ class TestUtils(ApiServerUnittest): self.assertFalse(utils.is_variable(("os", os))) self.assertFalse(utils.is_variable(("utils", utils))) + + def test_lower_dict_key(self): + origin_dict = { + "Name": "test", + "Request": { + "url": "http://127.0.0.1:5000", + "METHOD": "POST", + "Headers": { + "Accept": "application/json", + "User-Agent": "ios/9.3" + } + } + } + new_dict = utils.lower_dict_key(origin_dict) + self.assertIn("name", new_dict) + self.assertIn("request", new_dict) + self.assertIn("method", new_dict["request"]) + self.assertIn("headers", new_dict["request"]) + self.assertIn("Accept", new_dict["request"]["headers"]) + self.assertIn("User-Agent", new_dict["request"]["headers"])