From 31fdaaacb37b1f62de0d872f91750bbed1d08992 Mon Sep 17 00:00:00 2001 From: kgriffs Date: Tue, 2 Apr 2013 16:45:21 -0400 Subject: [PATCH] fix(testing): testing.create_environ(...) should allow headers to be an iterable of tuples as well as a dict. Fixes #97 --- falcon/testing/helpers.py | 7 ++++++- tests/test_hello.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/falcon/testing/helpers.py b/falcon/testing/helpers.py index df24b50..7848751 100644 --- a/falcon/testing/helpers.py +++ b/falcon/testing/helpers.py @@ -67,7 +67,8 @@ def create_environ(path='/', query_string='', protocol='HTTP/1.1', port='80', query_string: The query string to simulate (default '') protocol: The HTTP protocol to simulate (default 'HTTP/1.1') port: The TCP port to simulate (default '80') - headers: Optional headers to set (default None) + headers: Optional headers to set as a dict or an iterable of tuples + that can be converted to a dict (default None) app: Value for the SCRIPT_NAME environ variable, described in PEP-333: 'The initial portion of the request URL's "path" that corresponds to the application object, so that the application @@ -122,6 +123,10 @@ def create_environ(path='/', query_string='', protocol='HTTP/1.1', port='80', def _add_headers_to_environ(env, headers): + if not isinstance(headers, dict): + # Try to convert + headers = dict(headers) + for name, value in headers.items(): name = name.upper().replace('-', '_') diff --git a/tests/test_hello.py b/tests/test_hello.py index db30955..ea221ae 100644 --- a/tests/test_hello.py +++ b/tests/test_hello.py @@ -76,6 +76,10 @@ class TestHelloWorld(testing.TestBase): def after(self): pass + def test_env_headers_list_of_tuples(self): + env = testing.create_environ(headers=[('User-Agent', 'Falcon-Test')]) + self.assertEquals(env['HTTP_USER_AGENT'], 'Falcon-Test') + def test_empty_route(self): self.simulate_request('') self.assertTrue(self.root_resource.called)