File size: 18,037 Bytes
fc0f7bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | # -*- coding: utf-8 -*-
from datetime import datetime
import functools
import io
try:
import ujson as json
except ImportError:
import json
import random
import sys
import pytest
import six
import testtools
import falcon
from falcon import testing
from falcon import util
from falcon.util import uri
def _arbitrary_uris(count, length):
return (
u''.join(
[random.choice(uri._ALL_ALLOWED)
for _ in range(length)]
) for __ in range(count)
)
class TestFalconUtils(testtools.TestCase):
def setUp(self):
super(TestFalconUtils, self).setUp()
# NOTE(cabrera): for DRYness - used in uri.[de|en]code tests
# below.
self.uris = _arbitrary_uris(count=100, length=32)
def test_deprecated_decorator(self):
msg = 'Please stop using this thing. It is going away.'
@util.deprecated(msg)
def old_thing():
pass
if six.PY3:
stream = io.StringIO()
else:
stream = io.BytesIO()
old_stderr = sys.stderr
sys.stderr = stream
old_thing()
sys.stderr = old_stderr
self.assertIn(msg, stream.getvalue())
def test_http_now(self):
expected = datetime.utcnow()
actual = falcon.http_date_to_dt(falcon.http_now())
delta = actual - expected
delta_sec = abs(delta.days * 86400 + delta.seconds)
self.assertLessEqual(delta_sec, 1)
def test_dt_to_http(self):
self.assertEqual(
falcon.dt_to_http(datetime(2013, 4, 4)),
'Thu, 04 Apr 2013 00:00:00 GMT')
self.assertEqual(
falcon.dt_to_http(datetime(2013, 4, 4, 10, 28, 54)),
'Thu, 04 Apr 2013 10:28:54 GMT')
def test_http_date_to_dt(self):
self.assertEqual(
falcon.http_date_to_dt('Thu, 04 Apr 2013 00:00:00 GMT'),
datetime(2013, 4, 4))
self.assertEqual(
falcon.http_date_to_dt('Thu, 04 Apr 2013 10:28:54 GMT'),
datetime(2013, 4, 4, 10, 28, 54))
self.assertRaises(
ValueError,
falcon.http_date_to_dt, 'Thu, 04-Apr-2013 10:28:54 GMT')
self.assertEqual(
falcon.http_date_to_dt('Thu, 04-Apr-2013 10:28:54 GMT',
obs_date=True),
datetime(2013, 4, 4, 10, 28, 54))
self.assertRaises(
ValueError,
falcon.http_date_to_dt, 'Sun Nov 6 08:49:37 1994')
self.assertRaises(
ValueError,
falcon.http_date_to_dt, 'Nov 6 08:49:37 1994', obs_date=True)
self.assertEqual(
falcon.http_date_to_dt('Sun Nov 6 08:49:37 1994', obs_date=True),
datetime(1994, 11, 6, 8, 49, 37))
self.assertEqual(
falcon.http_date_to_dt('Sunday, 06-Nov-94 08:49:37 GMT',
obs_date=True),
datetime(1994, 11, 6, 8, 49, 37))
def test_pack_query_params_none(self):
self.assertEqual(
falcon.to_query_str({}),
'')
def test_pack_query_params_one(self):
self.assertEqual(
falcon.to_query_str({'limit': 10}),
'?limit=10')
self.assertEqual(
falcon.to_query_str({'things': [1, 2, 3]}),
'?things=1,2,3')
self.assertEqual(
falcon.to_query_str({'things': ['a']}),
'?things=a')
self.assertEqual(
falcon.to_query_str({'things': ['a', 'b']}),
'?things=a,b')
expected = ('?things=a&things=b&things=&things=None'
'&things=true&things=false&things=0')
actual = falcon.to_query_str(
{'things': ['a', 'b', '', None, True, False, 0]},
comma_delimited_lists=False
)
self.assertEqual(actual, expected)
def test_pack_query_params_several(self):
garbage_in = {
'limit': 17,
'echo': True,
'doit': False,
'x': 'val',
'y': 0.2
}
query_str = falcon.to_query_str(garbage_in)
fields = query_str[1:].split('&')
garbage_out = {}
for field in fields:
k, v = field.split('=')
garbage_out[k] = v
expected = {
'echo': 'true',
'limit': '17',
'x': 'val',
'y': '0.2',
'doit': 'false'}
self.assertEqual(expected, garbage_out)
def test_uri_encode(self):
url = 'http://example.com/v1/fizbit/messages?limit=3&echo=true'
self.assertEqual(uri.encode(url), url)
url = 'http://example.com/v1/fiz bit/messages'
expected = 'http://example.com/v1/fiz%20bit/messages'
self.assertEqual(uri.encode(url), expected)
url = u'http://example.com/v1/fizbit/messages?limit=3&e\u00e7ho=true'
expected = ('http://example.com/v1/fizbit/messages'
'?limit=3&e%C3%A7ho=true')
self.assertEqual(uri.encode(url), expected)
def test_uri_encode_double(self):
url = 'http://example.com/v1/fiz bit/messages'
expected = 'http://example.com/v1/fiz%20bit/messages'
self.assertEqual(uri.encode(uri.encode(url)), expected)
url = u'http://example.com/v1/fizbit/messages?limit=3&e\u00e7ho=true'
expected = ('http://example.com/v1/fizbit/messages'
'?limit=3&e%C3%A7ho=true')
self.assertEqual(uri.encode(uri.encode(url)), expected)
url = 'http://example.com/v1/fiz%bit/mess%ages/%'
expected = 'http://example.com/v1/fiz%25bit/mess%25ages/%25'
self.assertEqual(uri.encode(uri.encode(url)), expected)
url = 'http://example.com/%%'
expected = 'http://example.com/%25%25'
self.assertEqual(uri.encode(uri.encode(url)), expected)
# NOTE(kgriffs): Specific example cited in GH issue
url = 'http://something?redirect_uri=http%3A%2F%2Fsite'
self.assertEqual(uri.encode(url), url)
hex_digits = 'abcdefABCDEF0123456789'
for c1 in hex_digits:
for c2 in hex_digits:
url = 'http://example.com/%' + c1 + c2
encoded = uri.encode(uri.encode(url))
self.assertEqual(encoded, url)
def test_uri_encode_value(self):
self.assertEqual(uri.encode_value('abcd'), 'abcd')
self.assertEqual(uri.encode_value(u'abcd'), u'abcd')
self.assertEqual(uri.encode_value(u'ab cd'), u'ab%20cd')
self.assertEqual(uri.encode_value(u'\u00e7'), '%C3%A7')
self.assertEqual(uri.encode_value(u'\u00e7\u20ac'),
'%C3%A7%E2%82%AC')
self.assertEqual(uri.encode_value('ab/cd'), 'ab%2Fcd')
self.assertEqual(uri.encode_value('ab+cd=42,9'),
'ab%2Bcd%3D42%2C9')
def test_uri_decode(self):
self.assertEqual(uri.decode('abcd'), 'abcd')
self.assertEqual(uri.decode(u'abcd'), u'abcd')
self.assertEqual(uri.decode(u'ab%20cd'), u'ab cd')
self.assertEqual(uri.decode('This thing is %C3%A7'),
u'This thing is \u00e7')
self.assertEqual(uri.decode('This thing is %C3%A7%E2%82%AC'),
u'This thing is \u00e7\u20ac')
self.assertEqual(uri.decode('ab%2Fcd'), 'ab/cd')
self.assertEqual(uri.decode('http://example.com?x=ab%2Bcd%3D42%2C9'),
'http://example.com?x=ab+cd=42,9')
def test_prop_uri_encode_models_stdlib_quote(self):
equiv_quote = functools.partial(
six.moves.urllib.parse.quote, safe=uri._ALL_ALLOWED
)
for case in self.uris:
expect = equiv_quote(case)
actual = uri.encode(case)
self.assertEqual(expect, actual)
def test_prop_uri_encode_value_models_stdlib_quote_safe_tilde(self):
equiv_quote = functools.partial(
six.moves.urllib.parse.quote, safe='~'
)
for case in self.uris:
expect = equiv_quote(case)
actual = uri.encode_value(case)
self.assertEqual(expect, actual)
def test_prop_uri_decode_models_stdlib_unquote_plus(self):
stdlib_unquote = six.moves.urllib.parse.unquote_plus
for case in self.uris:
case = uri.encode_value(case)
expect = stdlib_unquote(case)
actual = uri.decode(case)
self.assertEqual(expect, actual)
def test_parse_query_string(self):
query_strinq = (
'a=http%3A%2F%2Ffalconframework.org%3Ftest%3D1'
'&b=%7B%22test1%22%3A%20%22data1%22%'
'2C%20%22test2%22%3A%20%22data2%22%7D'
'&c=1,2,3'
'&d=test'
'&e=a,,%26%3D%2C'
'&f=a&f=a%3Db'
'&%C3%A9=a%3Db'
)
decoded_url = 'http://falconframework.org?test=1'
decoded_json = '{"test1": "data1", "test2": "data2"}'
result = uri.parse_query_string(query_strinq)
self.assertEqual(result['a'], decoded_url)
self.assertEqual(result['b'], decoded_json)
self.assertEqual(result['c'], ['1', '2', '3'])
self.assertEqual(result['d'], 'test')
self.assertEqual(result['e'], ['a', '&=,'])
self.assertEqual(result['f'], ['a', 'a=b'])
self.assertEqual(result[u'é'], 'a=b')
result = uri.parse_query_string(query_strinq, True)
self.assertEqual(result['a'], decoded_url)
self.assertEqual(result['b'], decoded_json)
self.assertEqual(result['c'], ['1', '2', '3'])
self.assertEqual(result['d'], 'test')
self.assertEqual(result['e'], ['a', '', '&=,'])
self.assertEqual(result['f'], ['a', 'a=b'])
self.assertEqual(result[u'é'], 'a=b')
def test_parse_host(self):
self.assertEqual(uri.parse_host('::1'), ('::1', None))
self.assertEqual(uri.parse_host('2001:ODB8:AC10:FE01::'),
('2001:ODB8:AC10:FE01::', None))
self.assertEqual(
uri.parse_host('2001:ODB8:AC10:FE01::', default_port=80),
('2001:ODB8:AC10:FE01::', 80))
ipv6_addr = '2001:4801:1221:101:1c10::f5:116'
self.assertEqual(uri.parse_host(ipv6_addr), (ipv6_addr, None))
self.assertEqual(uri.parse_host('[' + ipv6_addr + ']'),
(ipv6_addr, None))
self.assertEqual(uri.parse_host('[' + ipv6_addr + ']:28080'),
(ipv6_addr, 28080))
self.assertEqual(uri.parse_host('[' + ipv6_addr + ']:8080'),
(ipv6_addr, 8080))
self.assertEqual(uri.parse_host('[' + ipv6_addr + ']:123'),
(ipv6_addr, 123))
self.assertEqual(uri.parse_host('[' + ipv6_addr + ']:42'),
(ipv6_addr, 42))
self.assertEqual(uri.parse_host('173.203.44.122'),
('173.203.44.122', None))
self.assertEqual(uri.parse_host('173.203.44.122', default_port=80),
('173.203.44.122', 80))
self.assertEqual(uri.parse_host('173.203.44.122:27070'),
('173.203.44.122', 27070))
self.assertEqual(uri.parse_host('173.203.44.122:123'),
('173.203.44.122', 123))
self.assertEqual(uri.parse_host('173.203.44.122:42'),
('173.203.44.122', 42))
self.assertEqual(uri.parse_host('example.com'),
('example.com', None))
self.assertEqual(uri.parse_host('example.com', default_port=443),
('example.com', 443))
self.assertEqual(uri.parse_host('falcon.example.com'),
('falcon.example.com', None))
self.assertEqual(uri.parse_host('falcon.example.com:9876'),
('falcon.example.com', 9876))
self.assertEqual(uri.parse_host('falcon.example.com:42'),
('falcon.example.com', 42))
def test_get_http_status(self):
self.assertEqual(falcon.get_http_status(404), falcon.HTTP_404)
self.assertEqual(falcon.get_http_status(404.3), falcon.HTTP_404)
self.assertEqual(falcon.get_http_status('404.3'), falcon.HTTP_404)
self.assertEqual(falcon.get_http_status(404.9), falcon.HTTP_404)
self.assertEqual(falcon.get_http_status('404'), falcon.HTTP_404)
self.assertEqual(falcon.get_http_status(123), '123 Unknown')
self.assertRaises(ValueError, falcon.get_http_status, 'not_a_number')
self.assertRaises(ValueError, falcon.get_http_status, 0)
self.assertRaises(ValueError, falcon.get_http_status, 99)
self.assertRaises(ValueError, falcon.get_http_status, -404.3)
self.assertRaises(ValueError, falcon.get_http_status, '-404')
self.assertRaises(ValueError, falcon.get_http_status, '-404.3')
self.assertEqual(falcon.get_http_status(123, 'Go Away'), '123 Go Away')
class TestFalconTesting(testing.TestBase):
"""Catch some uncommon branches not covered elsewhere."""
def test_path_escape_chars_in_create_environ(self):
env = testing.create_environ('/hello%20world%21')
self.assertEqual(env['PATH_INFO'], '/hello world!')
def test_no_prefix_allowed_for_query_strings_in_create_environ(self):
self.assertRaises(ValueError, testing.create_environ,
query_string='?foo=bar')
def test_unicode_path_in_create_environ(self):
if six.PY3:
self.skip('Test does not apply to Py3K')
env = testing.create_environ(u'/fancy/unícode')
self.assertEqual(env['PATH_INFO'], '/fancy/un\xc3\xadcode')
env = testing.create_environ(u'/simple')
self.assertEqual(env['PATH_INFO'], '/simple')
def test_none_header_value_in_create_environ(self):
env = testing.create_environ('/', headers={'X-Foo': None})
self.assertEqual(env['HTTP_X_FOO'], '')
def test_decode_empty_result(self):
body = self.simulate_request('/', decode='utf-8')
self.assertEqual(body, '')
def test_httpnow_alias_for_backwards_compat(self):
self.assertIs(testing.httpnow, util.http_now)
@pytest.mark.parametrize(
'protocol,method',
zip(
['https'] * len(falcon.HTTP_METHODS) + ['http'] * len(falcon.HTTP_METHODS),
falcon.HTTP_METHODS * 2
)
)
def test_simulate_request_protocol(protocol, method):
sink_called = [False]
def sink(req, resp):
sink_called[0] = True
assert req.protocol == protocol
app = falcon.API()
app.add_sink(sink, '/test')
client = testing.TestClient(app)
try:
simulate = client.getattr('simulate_' + method.lower())
simulate('/test', protocol=protocol)
assert sink_called[0]
except AttributeError:
# NOTE(kgriffs): simulate_* helpers do not exist for all methods
pass
class TestFalconTestCase(testing.TestCase):
"""Verify some branches not covered elsewhere."""
def test_status(self):
resource = testing.SimpleTestResource(status=falcon.HTTP_702)
self.api.add_route('/', resource)
result = self.simulate_get()
self.assertEqual(result.status, falcon.HTTP_702)
def test_wsgi_iterable_not_closeable(self):
result = testing.Result([], falcon.HTTP_200, [])
self.assertFalse(result.content)
def test_path_must_start_with_slash(self):
self.assertRaises(ValueError, self.simulate_get, 'foo')
def test_cached_text_in_result(self):
self.api.add_route('/', testing.SimpleTestResource(body='test'))
result = self.simulate_get()
self.assertEqual(result.text, result.text)
def test_simple_resource_body_json_xor(self):
self.assertRaises(
ValueError,
testing.SimpleTestResource,
body='',
json={},
)
def test_query_string(self):
class SomeResource(object):
def on_get(self, req, resp):
doc = {}
doc['oid'] = req.get_param_as_int('oid')
doc['detailed'] = req.get_param_as_bool('detailed')
doc['things'] = req.get_param_as_list('things', int)
doc['query_string'] = req.query_string
resp.body = json.dumps(doc)
self.api.add_route('/', SomeResource())
result = self.simulate_get(query_string='oid=42&detailed=no&things=1')
self.assertEqual(result.json['oid'], 42)
self.assertFalse(result.json['detailed'])
self.assertEqual(result.json['things'], [1])
params = {'oid': 42, 'detailed': False}
result = self.simulate_get(params=params)
self.assertEqual(result.json['oid'], params['oid'])
self.assertFalse(result.json['detailed'])
self.assertEqual(result.json['things'], None)
params = {'oid': 1978, 'detailed': 'yes', 'things': [1, 2, 3]}
result = self.simulate_get(params=params)
self.assertEqual(result.json['oid'], params['oid'])
self.assertTrue(result.json['detailed'])
self.assertEqual(result.json['things'], params['things'])
expected_qs = 'things=1,2,3'
result = self.simulate_get(params={'things': [1, 2, 3]})
self.assertEqual(result.json['query_string'], expected_qs)
expected_qs = 'things=1&things=2&things=3'
result = self.simulate_get(params={'things': [1, 2, 3]},
params_csv=False)
self.assertEqual(result.json['query_string'], expected_qs)
def test_query_string_no_question(self):
self.assertRaises(ValueError, self.simulate_get, query_string='?x=1')
def test_query_string_in_path(self):
self.assertRaises(ValueError, self.simulate_get, path='/thing?x=1')
class FancyAPI(falcon.API):
pass
class FancyTestCase(testing.TestCase):
api_class = FancyAPI
def test_something(self):
self.assertTrue(isinstance(self.api, FancyAPI))
|