| 1 |
import time |
| 2 |
from json import dumps, loads |
| 3 |
import warnings |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
from webtest import TestApp |
| 7 |
import webob |
| 8 |
|
| 9 |
from pecan import Pecan, expose, abort, Request, Response |
| 10 |
from pecan.rest import RestController |
| 11 |
from pecan.hooks import PecanHook, HookController |
| 12 |
from pecan.tests import PecanTestCase |
| 13 |
|
| 14 |
|
| 15 |
class TestThreadingLocalUsage(PecanTestCase): |
| 16 |
|
| 17 |
@property |
| 18 |
def root(self): |
| 19 |
class RootController(object): |
| 20 |
@expose() |
| 21 |
def index(self, req, resp): |
| 22 |
assert isinstance(req, webob.BaseRequest) |
| 23 |
assert isinstance(resp, webob.Response) |
| 24 |
return 'Hello, World!' |
| 25 |
|
| 26 |
@expose() |
| 27 |
def warning(self): |
| 28 |
return ("This should be unroutable because (req, resp) are not" |
| 29 |
" arguments. It should raise a TypeError.") |
| 30 |
|
| 31 |
@expose(generic=True) |
| 32 |
def generic(self): |
| 33 |
return ("This should be unroutable because (req, resp) are not" |
| 34 |
" arguments. It should raise a TypeError.") |
| 35 |
|
| 36 |
@generic.when(method='PUT') |
| 37 |
def generic_put(self, _id): |
| 38 |
return ("This should be unroutable because (req, resp) are not" |
| 39 |
" arguments. It should raise a TypeError.") |
| 40 |
|
| 41 |
return RootController |
| 42 |
|
| 43 |
def test_locals_are_not_used(self): |
| 44 |
with mock.patch('threading.local', side_effect=AssertionError()): |
| 45 |
|
| 46 |
app = TestApp(Pecan(self.root(), use_context_locals=False)) |
| 47 |
r = app.get('/') |
| 48 |
assert r.status_int == 200 |
| 49 |
assert r.body == b'Hello, World!' |
| 50 |
|
| 51 |
self.assertRaises(AssertionError, Pecan, self.root) |
| 52 |
|
| 53 |
def test_threadlocal_argument_warning(self): |
| 54 |
with mock.patch('threading.local', side_effect=AssertionError()): |
| 55 |
|
| 56 |
app = TestApp(Pecan(self.root(), use_context_locals=False)) |
| 57 |
self.assertRaises( |
| 58 |
TypeError, |
| 59 |
app.get, |
| 60 |
'/warning/' |
| 61 |
) |
| 62 |
|
| 63 |
def test_threadlocal_argument_warning_on_generic(self): |
| 64 |
with mock.patch('threading.local', side_effect=AssertionError()): |
| 65 |
|
| 66 |
app = TestApp(Pecan(self.root(), use_context_locals=False)) |
| 67 |
self.assertRaises( |
| 68 |
TypeError, |
| 69 |
app.get, |
| 70 |
'/generic/' |
| 71 |
) |
| 72 |
|
| 73 |
def test_threadlocal_argument_warning_on_generic_delegate(self): |
| 74 |
with mock.patch('threading.local', side_effect=AssertionError()): |
| 75 |
|
| 76 |
app = TestApp(Pecan(self.root(), use_context_locals=False)) |
| 77 |
self.assertRaises( |
| 78 |
TypeError, |
| 79 |
app.put, |
| 80 |
'/generic/' |
| 81 |
) |
| 82 |
|
| 83 |
|
| 84 |
class TestIndexRouting(PecanTestCase): |
| 85 |
|
| 86 |
@property |
| 87 |
def app_(self): |
| 88 |
class RootController(object): |
| 89 |
@expose() |
| 90 |
def index(self, req, resp): |
| 91 |
assert isinstance(req, webob.BaseRequest) |
| 92 |
assert isinstance(resp, webob.Response) |
| 93 |
return 'Hello, World!' |
| 94 |
|
| 95 |
return TestApp(Pecan(RootController(), use_context_locals=False)) |
| 96 |
|
| 97 |
def test_empty_root(self): |
| 98 |
r = self.app_.get('/') |
| 99 |
assert r.status_int == 200 |
| 100 |
assert r.body == b'Hello, World!' |
| 101 |
|
| 102 |
def test_index(self): |
| 103 |
r = self.app_.get('/index') |
| 104 |
assert r.status_int == 200 |
| 105 |
assert r.body == b'Hello, World!' |
| 106 |
|
| 107 |
def test_index_html(self): |
| 108 |
r = self.app_.get('/index.html') |
| 109 |
assert r.status_int == 200 |
| 110 |
assert r.body == b'Hello, World!' |
| 111 |
|
| 112 |
|
| 113 |
class TestManualResponse(PecanTestCase): |
| 114 |
|
| 115 |
def test_manual_response(self): |
| 116 |
|
| 117 |
class RootController(object): |
| 118 |
@expose() |
| 119 |
def index(self, req, resp): |
| 120 |
resp = webob.Response(resp.environ) |
| 121 |
resp.body = b'Hello, World!' |
| 122 |
return resp |
| 123 |
|
| 124 |
app = TestApp(Pecan(RootController(), use_context_locals=False)) |
| 125 |
r = app.get('/') |
| 126 |
assert r.body == b'Hello, World!', r.body |
| 127 |
|
| 128 |
|
| 129 |
class TestDispatch(PecanTestCase): |
| 130 |
|
| 131 |
@property |
| 132 |
def app_(self): |
| 133 |
class SubSubController(object): |
| 134 |
@expose() |
| 135 |
def index(self, req, resp): |
| 136 |
assert isinstance(req, webob.BaseRequest) |
| 137 |
assert isinstance(resp, webob.Response) |
| 138 |
return '/sub/sub/' |
| 139 |
|
| 140 |
@expose() |
| 141 |
def deeper(self, req, resp): |
| 142 |
assert isinstance(req, webob.BaseRequest) |
| 143 |
assert isinstance(resp, webob.Response) |
| 144 |
return '/sub/sub/deeper' |
| 145 |
|
| 146 |
class SubController(object): |
| 147 |
@expose() |
| 148 |
def index(self, req, resp): |
| 149 |
assert isinstance(req, webob.BaseRequest) |
| 150 |
assert isinstance(resp, webob.Response) |
| 151 |
return '/sub/' |
| 152 |
|
| 153 |
@expose() |
| 154 |
def deeper(self, req, resp): |
| 155 |
assert isinstance(req, webob.BaseRequest) |
| 156 |
assert isinstance(resp, webob.Response) |
| 157 |
return '/sub/deeper' |
| 158 |
|
| 159 |
sub = SubSubController() |
| 160 |
|
| 161 |
class RootController(object): |
| 162 |
@expose() |
| 163 |
def index(self, req, resp): |
| 164 |
assert isinstance(req, webob.BaseRequest) |
| 165 |
assert isinstance(resp, webob.Response) |
| 166 |
return '/' |
| 167 |
|
| 168 |
@expose() |
| 169 |
def deeper(self, req, resp): |
| 170 |
assert isinstance(req, webob.BaseRequest) |
| 171 |
assert isinstance(resp, webob.Response) |
| 172 |
return '/deeper' |
| 173 |
|
| 174 |
sub = SubController() |
| 175 |
|
| 176 |
return TestApp(Pecan(RootController(), use_context_locals=False)) |
| 177 |
|
| 178 |
def test_index(self): |
| 179 |
r = self.app_.get('/') |
| 180 |
assert r.status_int == 200 |
| 181 |
assert r.body == b'/' |
| 182 |
|
| 183 |
def test_one_level(self): |
| 184 |
r = self.app_.get('/deeper') |
| 185 |
assert r.status_int == 200 |
| 186 |
assert r.body == b'/deeper' |
| 187 |
|
| 188 |
def test_one_level_with_trailing(self): |
| 189 |
r = self.app_.get('/sub/') |
| 190 |
assert r.status_int == 200 |
| 191 |
assert r.body == b'/sub/' |
| 192 |
|
| 193 |
def test_two_levels(self): |
| 194 |
r = self.app_.get('/sub/deeper') |
| 195 |
assert r.status_int == 200 |
| 196 |
assert r.body == b'/sub/deeper' |
| 197 |
|
| 198 |
def test_two_levels_with_trailing(self): |
| 199 |
r = self.app_.get('/sub/sub/') |
| 200 |
assert r.status_int == 200 |
| 201 |
|
| 202 |
def test_three_levels(self): |
| 203 |
r = self.app_.get('/sub/sub/deeper') |
| 204 |
assert r.status_int == 200 |
| 205 |
assert r.body == b'/sub/sub/deeper' |
| 206 |
|
| 207 |
|
| 208 |
class TestLookups(PecanTestCase): |
| 209 |
|
| 210 |
@property |
| 211 |
def app_(self): |
| 212 |
class LookupController(object): |
| 213 |
def __init__(self, someID): |
| 214 |
self.someID = someID |
| 215 |
|
| 216 |
@expose() |
| 217 |
def index(self, req, resp): |
| 218 |
return '/%s' % self.someID |
| 219 |
|
| 220 |
@expose() |
| 221 |
def name(self, req, resp): |
| 222 |
return '/%s/name' % self.someID |
| 223 |
|
| 224 |
class RootController(object): |
| 225 |
@expose() |
| 226 |
def index(self, req, resp): |
| 227 |
return '/' |
| 228 |
|
| 229 |
@expose() |
| 230 |
def _lookup(self, someID, *remainder): |
| 231 |
return LookupController(someID), remainder |
| 232 |
|
| 233 |
return TestApp(Pecan(RootController(), use_context_locals=False)) |
| 234 |
|
| 235 |
def test_index(self): |
| 236 |
r = self.app_.get('/') |
| 237 |
assert r.status_int == 200 |
| 238 |
assert r.body == b'/' |
| 239 |
|
| 240 |
def test_lookup(self): |
| 241 |
r = self.app_.get('/100/') |
| 242 |
assert r.status_int == 200 |
| 243 |
assert r.body == b'/100' |
| 244 |
|
| 245 |
def test_lookup_with_method(self): |
| 246 |
r = self.app_.get('/100/name') |
| 247 |
assert r.status_int == 200 |
| 248 |
assert r.body == b'/100/name' |
| 249 |
|
| 250 |
def test_lookup_with_wrong_argspec(self): |
| 251 |
class RootController(object): |
| 252 |
@expose() |
| 253 |
def _lookup(self, someID): |
| 254 |
return 'Bad arg spec' # pragma: nocover |
| 255 |
|
| 256 |
with warnings.catch_warnings(): |
| 257 |
warnings.simplefilter("ignore") |
| 258 |
app = TestApp(Pecan(RootController(), use_context_locals=False)) |
| 259 |
r = app.get('/foo/bar', expect_errors=True) |
| 260 |
assert r.status_int == 404 |
| 261 |
|
| 262 |
|
| 263 |
class TestCanonicalLookups(PecanTestCase): |
| 264 |
|
| 265 |
@property |
| 266 |
def app_(self): |
| 267 |
class LookupController(object): |
| 268 |
def __init__(self, someID): |
| 269 |
self.someID = someID |
| 270 |
|
| 271 |
@expose() |
| 272 |
def index(self, req, resp): |
| 273 |
return self.someID |
| 274 |
|
| 275 |
class UserController(object): |
| 276 |
@expose() |
| 277 |
def _lookup(self, someID, *remainder): |
| 278 |
return LookupController(someID), remainder |
| 279 |
|
| 280 |
class RootController(object): |
| 281 |
users = UserController() |
| 282 |
|
| 283 |
return TestApp(Pecan(RootController(), use_context_locals=False)) |
| 284 |
|
| 285 |
def test_canonical_lookup(self): |
| 286 |
assert self.app_.get('/users', expect_errors=404).status_int == 404 |
| 287 |
assert self.app_.get('/users/', expect_errors=404).status_int == 404 |
| 288 |
assert self.app_.get('/users/100').status_int == 302 |
| 289 |
assert self.app_.get('/users/100/').body == b'100' |
| 290 |
|
| 291 |
|
| 292 |
class TestControllerArguments(PecanTestCase): |
| 293 |
|
| 294 |
@property |
| 295 |
def app_(self): |
| 296 |
class RootController(object): |
| 297 |
@expose() |
| 298 |
def index(self, req, resp, id): |
| 299 |
return 'index: %s' % id |
| 300 |
|
| 301 |
@expose() |
| 302 |
def multiple(self, req, resp, one, two): |
| 303 |
return 'multiple: %s, %s' % (one, two) |
| 304 |
|
| 305 |
@expose() |
| 306 |
def optional(self, req, resp, id=None): |
| 307 |
return 'optional: %s' % str(id) |
| 308 |
|
| 309 |
@expose() |
| 310 |
def multiple_optional(self, req, resp, one=None, two=None, |
| 311 |
three=None): |
| 312 |
return 'multiple_optional: %s, %s, %s' % (one, two, three) |
| 313 |
|
| 314 |
@expose() |
| 315 |
def variable_args(self, req, resp, *args): |
| 316 |
return 'variable_args: %s' % ', '.join(args) |
| 317 |
|
| 318 |
@expose() |
| 319 |
def variable_kwargs(self, req, resp, **kwargs): |
| 320 |
data = [ |
| 321 |
'%s=%s' % (key, kwargs[key]) |
| 322 |
for key in sorted(kwargs.keys()) |
| 323 |
] |
| 324 |
return 'variable_kwargs: %s' % ', '.join(data) |
| 325 |
|
| 326 |
@expose() |
| 327 |
def variable_all(self, req, resp, *args, **kwargs): |
| 328 |
data = [ |
| 329 |
'%s=%s' % (key, kwargs[key]) |
| 330 |
for key in sorted(kwargs.keys()) |
| 331 |
] |
| 332 |
return 'variable_all: %s' % ', '.join(list(args) + data) |
| 333 |
|
| 334 |
@expose() |
| 335 |
def eater(self, req, resp, id, dummy=None, *args, **kwargs): |
| 336 |
data = [ |
| 337 |
'%s=%s' % (key, kwargs[key]) |
| 338 |
for key in sorted(kwargs.keys()) |
| 339 |
] |
| 340 |
return 'eater: %s, %s, %s' % ( |
| 341 |
id, |
| 342 |
dummy, |
| 343 |
', '.join(list(args) + data) |
| 344 |
) |
| 345 |
|
| 346 |
@expose() |
| 347 |
def _route(self, args, request): |
| 348 |
if hasattr(self, args[0]): |
| 349 |
return getattr(self, args[0]), args[1:] |
| 350 |
else: |
| 351 |
return self.index, args |
| 352 |
|
| 353 |
return TestApp(Pecan(RootController(), use_context_locals=False)) |
| 354 |
|
| 355 |
def test_required_argument(self): |
| 356 |
try: |
| 357 |
r = self.app_.get('/') |
| 358 |
assert r.status_int != 200 # pragma: nocover |
| 359 |
except Exception as ex: |
| 360 |
assert isinstance(ex, TypeError) |
| 361 |
assert ex.args[0] in ( |
| 362 |
"index() takes exactly 2 arguments (1 given)", |
| 363 |
"index() missing 1 required positional argument: 'id'", |
| 364 |
( |
| 365 |
"TestControllerArguments.app_.<locals>.RootController." |
| 366 |
"index() missing 1 required positional argument: 'id'" |
| 367 |
), |
| 368 |
) # this messaging changed in Python 3.3 and again in Python 3.10 |
| 369 |
|
| 370 |
def test_single_argument(self): |
| 371 |
r = self.app_.get('/1') |
| 372 |
assert r.status_int == 200 |
| 373 |
assert r.body == b'index: 1' |
| 374 |
|
| 375 |
def test_single_argument_with_encoded_url(self): |
| 376 |
r = self.app_.get('/This%20is%20a%20test%21') |
| 377 |
assert r.status_int == 200 |
| 378 |
assert r.body == b'index: This is a test!' |
| 379 |
|
| 380 |
def test_two_arguments(self): |
| 381 |
r = self.app_.get('/1/dummy', status=404) |
| 382 |
assert r.status_int == 404 |
| 383 |
|
| 384 |
def test_keyword_argument(self): |
| 385 |
r = self.app_.get('/?id=2') |
| 386 |
assert r.status_int == 200 |
| 387 |
assert r.body == b'index: 2' |
| 388 |
|
| 389 |
def test_keyword_argument_with_encoded_url(self): |
| 390 |
r = self.app_.get('/?id=This%20is%20a%20test%21') |
| 391 |
assert r.status_int == 200 |
| 392 |
assert r.body == b'index: This is a test!' |
| 393 |
|
| 394 |
def test_argument_and_keyword_argument(self): |
| 395 |
r = self.app_.get('/3?id=three') |
| 396 |
assert r.status_int == 200 |
| 397 |
assert r.body == b'index: 3' |
| 398 |
|
| 399 |
def test_encoded_argument_and_keyword_argument(self): |
| 400 |
r = self.app_.get('/This%20is%20a%20test%21?id=three') |
| 401 |
assert r.status_int == 200 |
| 402 |
assert r.body == b'index: This is a test!' |
| 403 |
|
| 404 |
def test_explicit_kwargs(self): |
| 405 |
r = self.app_.post('/', {'id': '4'}) |
| 406 |
assert r.status_int == 200 |
| 407 |
assert r.body == b'index: 4' |
| 408 |
|
| 409 |
def test_path_with_explicit_kwargs(self): |
| 410 |
r = self.app_.post('/4', {'id': 'four'}) |
| 411 |
assert r.status_int == 200 |
| 412 |
assert r.body == b'index: 4' |
| 413 |
|
| 414 |
def test_multiple_kwargs(self): |
| 415 |
r = self.app_.get('/?id=5&dummy=dummy') |
| 416 |
assert r.status_int == 200 |
| 417 |
assert r.body == b'index: 5' |
| 418 |
|
| 419 |
def test_kwargs_from_root(self): |
| 420 |
r = self.app_.post('/', {'id': '6', 'dummy': 'dummy'}) |
| 421 |
assert r.status_int == 200 |
| 422 |
assert r.body == b'index: 6' |
| 423 |
|
| 424 |
# multiple args |
| 425 |
|
| 426 |
def test_multiple_positional_arguments(self): |
| 427 |
r = self.app_.get('/multiple/one/two') |
| 428 |
assert r.status_int == 200 |
| 429 |
assert r.body == b'multiple: one, two' |
| 430 |
|
| 431 |
def test_multiple_positional_arguments_with_url_encode(self): |
| 432 |
r = self.app_.get('/multiple/One%20/Two%21') |
| 433 |
assert r.status_int == 200 |
| 434 |
assert r.body == b'multiple: One , Two!' |
| 435 |
|
| 436 |
def test_multiple_positional_arguments_with_kwargs(self): |
| 437 |
r = self.app_.get('/multiple?one=three&two=four') |
| 438 |
assert r.status_int == 200 |
| 439 |
assert r.body == b'multiple: three, four' |
| 440 |
|
| 441 |
def test_multiple_positional_arguments_with_url_encoded_kwargs(self): |
| 442 |
r = self.app_.get('/multiple?one=Three%20&two=Four%20%21') |
| 443 |
assert r.status_int == 200 |
| 444 |
assert r.body == b'multiple: Three , Four !' |
| 445 |
|
| 446 |
def test_positional_args_with_dictionary_kwargs(self): |
| 447 |
r = self.app_.post('/multiple', {'one': 'five', 'two': 'six'}) |
| 448 |
assert r.status_int == 200 |
| 449 |
assert r.body == b'multiple: five, six' |
| 450 |
|
| 451 |
def test_positional_args_with_url_encoded_dictionary_kwargs(self): |
| 452 |
r = self.app_.post('/multiple', {'one': 'Five%20', 'two': 'Six%20%21'}) |
| 453 |
assert r.status_int == 200 |
| 454 |
assert r.body == b'multiple: Five%20, Six%20%21' |
| 455 |
|
| 456 |
# optional arg |
| 457 |
def test_optional_arg(self): |
| 458 |
r = self.app_.get('/optional') |
| 459 |
assert r.status_int == 200 |
| 460 |
assert r.body == b'optional: None' |
| 461 |
|
| 462 |
def test_multiple_optional(self): |
| 463 |
r = self.app_.get('/optional/1') |
| 464 |
assert r.status_int == 200 |
| 465 |
assert r.body == b'optional: 1' |
| 466 |
|
| 467 |
def test_multiple_optional_url_encoded(self): |
| 468 |
r = self.app_.get('/optional/Some%20Number') |
| 469 |
assert r.status_int == 200 |
| 470 |
assert r.body == b'optional: Some Number' |
| 471 |
|
| 472 |
def test_multiple_optional_missing(self): |
| 473 |
r = self.app_.get('/optional/2/dummy', status=404) |
| 474 |
assert r.status_int == 404 |
| 475 |
|
| 476 |
def test_multiple_with_kwargs(self): |
| 477 |
r = self.app_.get('/optional?id=2') |
| 478 |
assert r.status_int == 200 |
| 479 |
assert r.body == b'optional: 2' |
| 480 |
|
| 481 |
def test_multiple_with_url_encoded_kwargs(self): |
| 482 |
r = self.app_.get('/optional?id=Some%20Number') |
| 483 |
assert r.status_int == 200 |
| 484 |
assert r.body == b'optional: Some Number' |
| 485 |
|
| 486 |
def test_multiple_args_with_url_encoded_kwargs(self): |
| 487 |
r = self.app_.get('/optional/3?id=three') |
| 488 |
assert r.status_int == 200 |
| 489 |
assert r.body == b'optional: 3' |
| 490 |
|
| 491 |
def test_url_encoded_positional_args(self): |
| 492 |
r = self.app_.get('/optional/Some%20Number?id=three') |
| 493 |
assert r.status_int == 200 |
| 494 |
assert r.body == b'optional: Some Number' |
| 495 |
|
| 496 |
def test_optional_arg_with_kwargs(self): |
| 497 |
r = self.app_.post('/optional', {'id': '4'}) |
| 498 |
assert r.status_int == 200 |
| 499 |
assert r.body == b'optional: 4' |
| 500 |
|
| 501 |
def test_optional_arg_with_url_encoded_kwargs(self): |
| 502 |
r = self.app_.post('/optional', {'id': 'Some%20Number'}) |
| 503 |
assert r.status_int == 200 |
| 504 |
assert r.body == b'optional: Some%20Number' |
| 505 |
|
| 506 |
def test_multiple_positional_arguments_with_dictionary_kwargs(self): |
| 507 |
r = self.app_.post('/optional/5', {'id': 'five'}) |
| 508 |
assert r.status_int == 200 |
| 509 |
assert r.body == b'optional: 5' |
| 510 |
|
| 511 |
def test_multiple_positional_url_encoded_arguments_with_kwargs(self): |
| 512 |
r = self.app_.post('/optional/Some%20Number', {'id': 'five'}) |
| 513 |
assert r.status_int == 200 |
| 514 |
assert r.body == b'optional: Some Number' |
| 515 |
|
| 516 |
def test_optional_arg_with_multiple_kwargs(self): |
| 517 |
r = self.app_.get('/optional?id=6&dummy=dummy') |
| 518 |
assert r.status_int == 200 |
| 519 |
assert r.body == b'optional: 6' |
| 520 |
|
| 521 |
def test_optional_arg_with_multiple_url_encoded_kwargs(self): |
| 522 |
r = self.app_.get('/optional?id=Some%20Number&dummy=dummy') |
| 523 |
assert r.status_int == 200 |
| 524 |
assert r.body == b'optional: Some Number' |
| 525 |
|
| 526 |
def test_optional_arg_with_multiple_dictionary_kwargs(self): |
| 527 |
r = self.app_.post('/optional', {'id': '7', 'dummy': 'dummy'}) |
| 528 |
assert r.status_int == 200 |
| 529 |
assert r.body == b'optional: 7' |
| 530 |
|
| 531 |
def test_optional_arg_with_multiple_url_encoded_dictionary_kwargs(self): |
| 532 |
r = self.app_.post('/optional', { |
| 533 |
'id': 'Some%20Number', |
| 534 |
'dummy': 'dummy' |
| 535 |
}) |
| 536 |
assert r.status_int == 200 |
| 537 |
assert r.body == b'optional: Some%20Number' |
| 538 |
|
| 539 |
# multiple optional args |
| 540 |
|
| 541 |
def test_multiple_optional_positional_args(self): |
| 542 |
r = self.app_.get('/multiple_optional') |
| 543 |
assert r.status_int == 200 |
| 544 |
assert r.body == b'multiple_optional: None, None, None' |
| 545 |
|
| 546 |
def test_multiple_optional_positional_args_one_arg(self): |
| 547 |
r = self.app_.get('/multiple_optional/1') |
| 548 |
assert r.status_int == 200 |
| 549 |
assert r.body == b'multiple_optional: 1, None, None' |
| 550 |
|
| 551 |
def test_multiple_optional_positional_args_one_url_encoded_arg(self): |
| 552 |
r = self.app_.get('/multiple_optional/One%21') |
| 553 |
assert r.status_int == 200 |
| 554 |
assert r.body == b'multiple_optional: One!, None, None' |
| 555 |
|
| 556 |
def test_multiple_optional_positional_args_all_args(self): |
| 557 |
r = self.app_.get('/multiple_optional/1/2/3') |
| 558 |
assert r.status_int == 200 |
| 559 |
assert r.body == b'multiple_optional: 1, 2, 3' |
| 560 |
|
| 561 |
def test_multiple_optional_positional_args_all_url_encoded_args(self): |
| 562 |
r = self.app_.get('/multiple_optional/One%21/Two%21/Three%21') |
| 563 |
assert r.status_int == 200 |
| 564 |
assert r.body == b'multiple_optional: One!, Two!, Three!' |
| 565 |
|
| 566 |
def test_multiple_optional_positional_args_too_many_args(self): |
| 567 |
r = self.app_.get('/multiple_optional/1/2/3/dummy', status=404) |
| 568 |
assert r.status_int == 404 |
| 569 |
|
| 570 |
def test_multiple_optional_positional_args_with_kwargs(self): |
| 571 |
r = self.app_.get('/multiple_optional?one=1') |
| 572 |
assert r.status_int == 200 |
| 573 |
assert r.body == b'multiple_optional: 1, None, None' |
| 574 |
|
| 575 |
def test_multiple_optional_positional_args_with_url_encoded_kwargs(self): |
| 576 |
r = self.app_.get('/multiple_optional?one=One%21') |
| 577 |
assert r.status_int == 200 |
| 578 |
assert r.body == b'multiple_optional: One!, None, None' |
| 579 |
|
| 580 |
def test_multiple_optional_positional_args_with_string_kwargs(self): |
| 581 |
r = self.app_.get('/multiple_optional/1?one=one') |
| 582 |
assert r.status_int == 200 |
| 583 |
assert r.body == b'multiple_optional: 1, None, None' |
| 584 |
|
| 585 |
def test_multiple_optional_positional_args_with_encoded_str_kwargs(self): |
| 586 |
r = self.app_.get('/multiple_optional/One%21?one=one') |
| 587 |
assert r.status_int == 200 |
| 588 |
assert r.body == b'multiple_optional: One!, None, None' |
| 589 |
|
| 590 |
def test_multiple_optional_positional_args_with_dict_kwargs(self): |
| 591 |
r = self.app_.post('/multiple_optional', {'one': '1'}) |
| 592 |
assert r.status_int == 200 |
| 593 |
assert r.body == b'multiple_optional: 1, None, None' |
| 594 |
|
| 595 |
def test_multiple_optional_positional_args_with_encoded_dict_kwargs(self): |
| 596 |
r = self.app_.post('/multiple_optional', {'one': 'One%21'}) |
| 597 |
assert r.status_int == 200 |
| 598 |
assert r.body == b'multiple_optional: One%21, None, None' |
| 599 |
|
| 600 |
def test_multiple_optional_positional_args_and_dict_kwargs(self): |
| 601 |
r = self.app_.post('/multiple_optional/1', {'one': 'one'}) |
| 602 |
assert r.status_int == 200 |
| 603 |
assert r.body == b'multiple_optional: 1, None, None' |
| 604 |
|
| 605 |
def test_multiple_optional_encoded_positional_args_and_dict_kwargs(self): |
| 606 |
r = self.app_.post('/multiple_optional/One%21', {'one': 'one'}) |
| 607 |
assert r.status_int == 200 |
| 608 |
assert r.body == b'multiple_optional: One!, None, None' |
| 609 |
|
| 610 |
def test_multiple_optional_args_with_multiple_kwargs(self): |
| 611 |
r = self.app_.get('/multiple_optional?one=1&two=2&three=3&four=4') |
| 612 |
assert r.status_int == 200 |
| 613 |
assert r.body == b'multiple_optional: 1, 2, 3' |
| 614 |
|
| 615 |
def test_multiple_optional_args_with_multiple_encoded_kwargs(self): |
| 616 |
r = self.app_.get( |
| 617 |
'/multiple_optional?one=One%21&two=Two%21&three=Three%21&four=4' |
| 618 |
) |
| 619 |
assert r.status_int == 200 |
| 620 |
assert r.body == b'multiple_optional: One!, Two!, Three!' |
| 621 |
|
| 622 |
def test_multiple_optional_args_with_multiple_dict_kwargs(self): |
| 623 |
r = self.app_.post( |
| 624 |
'/multiple_optional', |
| 625 |
{'one': '1', 'two': '2', 'three': '3', 'four': '4'} |
| 626 |
) |
| 627 |
assert r.status_int == 200 |
| 628 |
assert r.body == b'multiple_optional: 1, 2, 3' |
| 629 |
|
| 630 |
def test_multiple_optional_args_with_multiple_encoded_dict_kwargs(self): |
| 631 |
r = self.app_.post( |
| 632 |
'/multiple_optional', |
| 633 |
{ |
| 634 |
'one': 'One%21', |
| 635 |
'two': 'Two%21', |
| 636 |
'three': 'Three%21', |
| 637 |
'four': '4' |
| 638 |
} |
| 639 |
) |
| 640 |
assert r.status_int == 200 |
| 641 |
assert r.body == b'multiple_optional: One%21, Two%21, Three%21' |
| 642 |
|
| 643 |
def test_multiple_optional_args_with_last_kwarg(self): |
| 644 |
r = self.app_.get('/multiple_optional?three=3') |
| 645 |
assert r.status_int == 200 |
| 646 |
assert r.body == b'multiple_optional: None, None, 3' |
| 647 |
|
| 648 |
def test_multiple_optional_args_with_last_encoded_kwarg(self): |
| 649 |
r = self.app_.get('/multiple_optional?three=Three%21') |
| 650 |
assert r.status_int == 200 |
| 651 |
assert r.body == b'multiple_optional: None, None, Three!' |
| 652 |
|
| 653 |
def test_multiple_optional_args_with_middle_arg(self): |
| 654 |
r = self.app_.get('/multiple_optional', {'two': '2'}) |
| 655 |
assert r.status_int == 200 |
| 656 |
assert r.body == b'multiple_optional: None, 2, None' |
| 657 |
|
| 658 |
def test_variable_args(self): |
| 659 |
r = self.app_.get('/variable_args') |
| 660 |
assert r.status_int == 200 |
| 661 |
assert r.body == b'variable_args: ' |
| 662 |
|
| 663 |
def test_multiple_variable_args(self): |
| 664 |
r = self.app_.get('/variable_args/1/dummy') |
| 665 |
assert r.status_int == 200 |
| 666 |
assert r.body == b'variable_args: 1, dummy' |
| 667 |
|
| 668 |
def test_multiple_encoded_variable_args(self): |
| 669 |
r = self.app_.get('/variable_args/Testing%20One%20Two/Three%21') |
| 670 |
assert r.status_int == 200 |
| 671 |
assert r.body == b'variable_args: Testing One Two, Three!' |
| 672 |
|
| 673 |
def test_variable_args_with_kwargs(self): |
| 674 |
r = self.app_.get('/variable_args?id=2&dummy=dummy') |
| 675 |
assert r.status_int == 200 |
| 676 |
assert r.body == b'variable_args: ' |
| 677 |
|
| 678 |
def test_variable_args_with_dict_kwargs(self): |
| 679 |
r = self.app_.post('/variable_args', {'id': '3', 'dummy': 'dummy'}) |
| 680 |
assert r.status_int == 200 |
| 681 |
assert r.body == b'variable_args: ' |
| 682 |
|
| 683 |
def test_variable_kwargs(self): |
| 684 |
r = self.app_.get('/variable_kwargs') |
| 685 |
assert r.status_int == 200 |
| 686 |
assert r.body == b'variable_kwargs: ' |
| 687 |
|
| 688 |
def test_multiple_variable_kwargs(self): |
| 689 |
r = self.app_.get('/variable_kwargs/1/dummy', status=404) |
| 690 |
assert r.status_int == 404 |
| 691 |
|
| 692 |
def test_multiple_variable_kwargs_with_explicit_kwargs(self): |
| 693 |
r = self.app_.get('/variable_kwargs?id=2&dummy=dummy') |
| 694 |
assert r.status_int == 200 |
| 695 |
assert r.body == b'variable_kwargs: dummy=dummy, id=2' |
| 696 |
|
| 697 |
def test_multiple_variable_kwargs_with_explicit_encoded_kwargs(self): |
| 698 |
r = self.app_.get( |
| 699 |
'/variable_kwargs?id=Two%21&dummy=This%20is%20a%20test' |
| 700 |
) |
| 701 |
assert r.status_int == 200 |
| 702 |
assert r.body == b'variable_kwargs: dummy=This is a test, id=Two!' |
| 703 |
|
| 704 |
def test_multiple_variable_kwargs_with_dict_kwargs(self): |
| 705 |
r = self.app_.post('/variable_kwargs', {'id': '3', 'dummy': 'dummy'}) |
| 706 |
assert r.status_int == 200 |
| 707 |
assert r.body == b'variable_kwargs: dummy=dummy, id=3' |
| 708 |
|
| 709 |
def test_multiple_variable_kwargs_with_encoded_dict_kwargs(self): |
| 710 |
r = self.app_.post( |
| 711 |
'/variable_kwargs', |
| 712 |
{'id': 'Three%21', 'dummy': 'This%20is%20a%20test'} |
| 713 |
) |
| 714 |
assert r.status_int == 200 |
| 715 |
result = b'variable_kwargs: dummy=This%20is%20a%20test, id=Three%21' |
| 716 |
assert r.body == result |
| 717 |
|
| 718 |
def test_variable_all(self): |
| 719 |
r = self.app_.get('/variable_all') |
| 720 |
assert r.status_int == 200 |
| 721 |
assert r.body == b'variable_all: ' |
| 722 |
|
| 723 |
def test_variable_all_with_one_extra(self): |
| 724 |
r = self.app_.get('/variable_all/1') |
| 725 |
assert r.status_int == 200 |
| 726 |
assert r.body == b'variable_all: 1' |
| 727 |
|
| 728 |
def test_variable_all_with_two_extras(self): |
| 729 |
r = self.app_.get('/variable_all/2/dummy') |
| 730 |
assert r.status_int == 200 |
| 731 |
assert r.body == b'variable_all: 2, dummy' |
| 732 |
|
| 733 |
def test_variable_mixed(self): |
| 734 |
r = self.app_.get('/variable_all/3?month=1&day=12') |
| 735 |
assert r.status_int == 200 |
| 736 |
assert r.body == b'variable_all: 3, day=12, month=1' |
| 737 |
|
| 738 |
def test_variable_mixed_explicit(self): |
| 739 |
r = self.app_.get('/variable_all/4?id=four&month=1&day=12') |
| 740 |
assert r.status_int == 200 |
| 741 |
assert r.body == b'variable_all: 4, day=12, id=four, month=1' |
| 742 |
|
| 743 |
def test_variable_post(self): |
| 744 |
r = self.app_.post('/variable_all/5/dummy') |
| 745 |
assert r.status_int == 200 |
| 746 |
assert r.body == b'variable_all: 5, dummy' |
| 747 |
|
| 748 |
def test_variable_post_with_kwargs(self): |
| 749 |
r = self.app_.post('/variable_all/6', {'month': '1', 'day': '12'}) |
| 750 |
assert r.status_int == 200 |
| 751 |
assert r.body == b'variable_all: 6, day=12, month=1' |
| 752 |
|
| 753 |
def test_variable_post_mixed(self): |
| 754 |
r = self.app_.post( |
| 755 |
'/variable_all/7', |
| 756 |
{'id': 'seven', 'month': '1', 'day': '12'} |
| 757 |
) |
| 758 |
assert r.status_int == 200 |
| 759 |
assert r.body == b'variable_all: 7, day=12, id=seven, month=1' |
| 760 |
|
| 761 |
def test_no_remainder(self): |
| 762 |
try: |
| 763 |
r = self.app_.get('/eater') |
| 764 |
assert r.status_int != 200 # pragma: nocover |
| 765 |
except Exception as ex: |
| 766 |
assert isinstance(ex, TypeError) |
| 767 |
assert ex.args[0] in ( |
| 768 |
"eater() takes exactly 2 arguments (1 given)", |
| 769 |
"eater() missing 1 required positional argument: 'id'", |
| 770 |
( |
| 771 |
"TestControllerArguments.app_.<locals>.RootController." |
| 772 |
"eater() missing 1 required positional argument: 'id'" |
| 773 |
), |
| 774 |
) # this messaging changed in Python 3.3 and again in Python 3.10 |
| 775 |
|
| 776 |
def test_one_remainder(self): |
| 777 |
r = self.app_.get('/eater/1') |
| 778 |
assert r.status_int == 200 |
| 779 |
assert r.body == b'eater: 1, None, ' |
| 780 |
|
| 781 |
def test_two_remainders(self): |
| 782 |
r = self.app_.get('/eater/2/dummy') |
| 783 |
assert r.status_int == 200 |
| 784 |
assert r.body == b'eater: 2, dummy, ' |
| 785 |
|
| 786 |
def test_many_remainders(self): |
| 787 |
r = self.app_.get('/eater/3/dummy/foo/bar') |
| 788 |
assert r.status_int == 200 |
| 789 |
assert r.body == b'eater: 3, dummy, foo, bar' |
| 790 |
|
| 791 |
def test_remainder_with_kwargs(self): |
| 792 |
r = self.app_.get('/eater/4?month=1&day=12') |
| 793 |
assert r.status_int == 200 |
| 794 |
assert r.body == b'eater: 4, None, day=12, month=1' |
| 795 |
|
| 796 |
def test_remainder_with_many_kwargs(self): |
| 797 |
r = self.app_.get('/eater/5?id=five&month=1&day=12&dummy=dummy') |
| 798 |
assert r.status_int == 200 |
| 799 |
assert r.body == b'eater: 5, dummy, day=12, month=1' |
| 800 |
|
| 801 |
def test_post_remainder(self): |
| 802 |
r = self.app_.post('/eater/6') |
| 803 |
assert r.status_int == 200 |
| 804 |
assert r.body == b'eater: 6, None, ' |
| 805 |
|
| 806 |
def test_post_three_remainders(self): |
| 807 |
r = self.app_.post('/eater/7/dummy') |
| 808 |
assert r.status_int == 200 |
| 809 |
assert r.body == b'eater: 7, dummy, ' |
| 810 |
|
| 811 |
def test_post_many_remainders(self): |
| 812 |
r = self.app_.post('/eater/8/dummy/foo/bar') |
| 813 |
assert r.status_int == 200 |
| 814 |
assert r.body == b'eater: 8, dummy, foo, bar' |
| 815 |
|
| 816 |
def test_post_remainder_with_kwargs(self): |
| 817 |
r = self.app_.post('/eater/9', {'month': '1', 'day': '12'}) |
| 818 |
assert r.status_int == 200 |
| 819 |
assert r.body == b'eater: 9, None, day=12, month=1' |
| 820 |
|
| 821 |
def test_post_many_remainders_with_many_kwargs(self): |
| 822 |
r = self.app_.post( |
| 823 |
'/eater/10', |
| 824 |
{'id': 'ten', 'month': '1', 'day': '12', 'dummy': 'dummy'} |
| 825 |
) |
| 826 |
assert r.status_int == 200 |
| 827 |
assert r.body == b'eater: 10, dummy, day=12, month=1' |
| 828 |
|
| 829 |
|
| 830 |
class TestRestController(PecanTestCase): |
| 831 |
|
| 832 |
@property |
| 833 |
def app_(self): |
| 834 |
|
| 835 |
class OthersController(object): |
| 836 |
|
| 837 |
@expose() |
| 838 |
def index(self, req, resp): |
| 839 |
return 'OTHERS' |
| 840 |
|
| 841 |
@expose() |
| 842 |
def echo(self, req, resp, value): |
| 843 |
return str(value) |
| 844 |
|
| 845 |
class ThingsController(RestController): |
| 846 |
data = ['zero', 'one', 'two', 'three'] |
| 847 |
|
| 848 |
_custom_actions = {'count': ['GET'], 'length': ['GET', 'POST']} |
| 849 |
|
| 850 |
others = OthersController() |
| 851 |
|
| 852 |
@expose() |
| 853 |
def get_one(self, req, resp, id): |
| 854 |
return self.data[int(id)] |
| 855 |
|
| 856 |
@expose('json') |
| 857 |
def get_all(self, req, resp): |
| 858 |
return dict(items=self.data) |
| 859 |
|
| 860 |
@expose() |
| 861 |
def length(self, req, resp, id, value=None): |
| 862 |
length = len(self.data[int(id)]) |
| 863 |
if value: |
| 864 |
length += len(value) |
| 865 |
return str(length) |
| 866 |
|
| 867 |
@expose() |
| 868 |
def post(self, req, resp, value): |
| 869 |
self.data.append(value) |
| 870 |
resp.status = 302 |
| 871 |
return 'CREATED' |
| 872 |
|
| 873 |
@expose() |
| 874 |
def edit(self, req, resp, id): |
| 875 |
return 'EDIT %s' % self.data[int(id)] |
| 876 |
|
| 877 |
@expose() |
| 878 |
def put(self, req, resp, id, value): |
| 879 |
self.data[int(id)] = value |
| 880 |
return 'UPDATED' |
| 881 |
|
| 882 |
@expose() |
| 883 |
def get_delete(self, req, resp, id): |
| 884 |
return 'DELETE %s' % self.data[int(id)] |
| 885 |
|
| 886 |
@expose() |
| 887 |
def delete(self, req, resp, id): |
| 888 |
del self.data[int(id)] |
| 889 |
return 'DELETED' |
| 890 |
|
| 891 |
@expose() |
| 892 |
def trace(self, req, resp): |
| 893 |
return 'TRACE' |
| 894 |
|
| 895 |
@expose() |
| 896 |
def post_options(self, req, resp): |
| 897 |
return 'OPTIONS' |
| 898 |
|
| 899 |
@expose() |
| 900 |
def options(self, req, resp): |
| 901 |
abort(500) |
| 902 |
|
| 903 |
@expose() |
| 904 |
def other(self, req, resp): |
| 905 |
abort(500) |
| 906 |
|
| 907 |
class RootController(object): |
| 908 |
things = ThingsController() |
| 909 |
|
| 910 |
# create the app |
| 911 |
return TestApp(Pecan(RootController(), use_context_locals=False)) |
| 912 |
|
| 913 |
def test_get_all(self): |
| 914 |
r = self.app_.get('/things') |
| 915 |
assert r.status_int == 200 |
| 916 |
assert r.body == dumps( |
| 917 |
dict(items=['zero', 'one', 'two', 'three']) |
| 918 |
).encode('utf-8') |
| 919 |
|
| 920 |
def test_get_one(self): |
| 921 |
for i, value in enumerate([b'zero', b'one', b'two', b'three']): |
| 922 |
r = self.app_.get('/things/%d' % i) |
| 923 |
assert r.status_int == 200 |
| 924 |
assert r.body == value |
| 925 |
|
| 926 |
def test_post(self): |
| 927 |
r = self.app_.post('/things', {'value': 'four'}) |
| 928 |
assert r.status_int == 302 |
| 929 |
assert r.body == b'CREATED' |
| 930 |
|
| 931 |
def test_custom_action(self): |
| 932 |
r = self.app_.get('/things/3/edit') |
| 933 |
assert r.status_int == 200 |
| 934 |
assert r.body == b'EDIT three' |
| 935 |
|
| 936 |
def test_put(self): |
| 937 |
r = self.app_.put('/things/3', {'value': 'THREE!'}) |
| 938 |
assert r.status_int == 200 |
| 939 |
assert r.body == b'UPDATED' |
| 940 |
|
| 941 |
def test_put_with_method_parameter_and_get(self): |
| 942 |
r = self.app_.get('/things/3?_method=put', {'value': 'X'}, status=405) |
| 943 |
assert r.status_int == 405 |
| 944 |
|
| 945 |
def test_put_with_method_parameter_and_post(self): |
| 946 |
r = self.app_.post('/things/3?_method=put', {'value': 'THREE!'}) |
| 947 |
assert r.status_int == 200 |
| 948 |
assert r.body == b'UPDATED' |
| 949 |
|
| 950 |
def test_get_delete(self): |
| 951 |
r = self.app_.get('/things/3/delete') |
| 952 |
assert r.status_int == 200 |
| 953 |
assert r.body == b'DELETE three' |
| 954 |
|
| 955 |
def test_delete_method(self): |
| 956 |
r = self.app_.delete('/things/3') |
| 957 |
assert r.status_int == 200 |
| 958 |
assert r.body == b'DELETED' |
| 959 |
|
| 960 |
def test_delete_with_method_parameter(self): |
| 961 |
r = self.app_.get('/things/3?_method=DELETE', status=405) |
| 962 |
assert r.status_int == 405 |
| 963 |
|
| 964 |
def test_delete_with_method_parameter_and_post(self): |
| 965 |
r = self.app_.post('/things/3?_method=DELETE') |
| 966 |
assert r.status_int == 200 |
| 967 |
assert r.body == b'DELETED' |
| 968 |
|
| 969 |
def test_custom_method_type(self): |
| 970 |
r = self.app_.request('/things', method='TRACE') |
| 971 |
assert r.status_int == 200 |
| 972 |
assert r.body == b'TRACE' |
| 973 |
|
| 974 |
def test_custom_method_type_with_method_parameter(self): |
| 975 |
r = self.app_.get('/things?_method=TRACE') |
| 976 |
assert r.status_int == 200 |
| 977 |
assert r.body == b'TRACE' |
| 978 |
|
| 979 |
def test_options(self): |
| 980 |
r = self.app_.request('/things', method='OPTIONS') |
| 981 |
assert r.status_int == 200 |
| 982 |
assert r.body == b'OPTIONS' |
| 983 |
|
| 984 |
def test_options_with_method_parameter(self): |
| 985 |
r = self.app_.post('/things', {'_method': 'OPTIONS'}) |
| 986 |
assert r.status_int == 200 |
| 987 |
assert r.body == b'OPTIONS' |
| 988 |
|
| 989 |
def test_other_custom_action(self): |
| 990 |
with warnings.catch_warnings(): |
| 991 |
warnings.simplefilter("ignore") |
| 992 |
r = self.app_.request('/things/other', method='MISC', status=405) |
| 993 |
assert r.status_int == 405 |
| 994 |
|
| 995 |
def test_other_custom_action_with_method_parameter(self): |
| 996 |
r = self.app_.post('/things/other', {'_method': 'MISC'}, status=405) |
| 997 |
assert r.status_int == 405 |
| 998 |
|
| 999 |
def test_nested_controller_with_trailing_slash(self): |
| 1000 |
with warnings.catch_warnings(): |
| 1001 |
warnings.simplefilter("ignore") |
| 1002 |
r = self.app_.request('/things/others/', method='MISC') |
| 1003 |
assert r.status_int == 200 |
| 1004 |
assert r.body == b'OTHERS' |
| 1005 |
|
| 1006 |
def test_nested_controller_without_trailing_slash(self): |
| 1007 |
with warnings.catch_warnings(): |
| 1008 |
warnings.simplefilter("ignore") |
| 1009 |
r = self.app_.request('/things/others', method='MISC', status=302) |
| 1010 |
assert r.status_int == 302 |
| 1011 |
|
| 1012 |
def test_invalid_custom_action(self): |
| 1013 |
r = self.app_.get('/things?_method=BAD', status=405) |
| 1014 |
assert r.status_int == 405 |
| 1015 |
|
| 1016 |
def test_named_action(self): |
| 1017 |
# test custom "GET" request "length" |
| 1018 |
r = self.app_.get('/things/1/length') |
| 1019 |
assert r.status_int == 200 |
| 1020 |
assert r.body == b'3' |
| 1021 |
|
| 1022 |
def test_named_nested_action(self): |
| 1023 |
# test custom "GET" request through subcontroller |
| 1024 |
r = self.app_.get('/things/others/echo?value=test') |
| 1025 |
assert r.status_int == 200 |
| 1026 |
assert r.body == b'test' |
| 1027 |
|
| 1028 |
def test_nested_post(self): |
| 1029 |
# test custom "POST" request through subcontroller |
| 1030 |
r = self.app_.post('/things/others/echo', {'value': 'test'}) |
| 1031 |
assert r.status_int == 200 |
| 1032 |
assert r.body == b'test' |
| 1033 |
|
| 1034 |
|
| 1035 |
class TestHooks(PecanTestCase): |
| 1036 |
|
| 1037 |
def test_basic_single_hook(self): |
| 1038 |
run_hook = [] |
| 1039 |
|
| 1040 |
class RootController(object): |
| 1041 |
@expose() |
| 1042 |
def index(self, req, resp): |
| 1043 |
run_hook.append('inside') |
| 1044 |
return 'Hello, World!' |
| 1045 |
|
| 1046 |
class SimpleHook(PecanHook): |
| 1047 |
def on_route(self, state): |
| 1048 |
run_hook.append('on_route') |
| 1049 |
|
| 1050 |
def before(self, state): |
| 1051 |
run_hook.append('before') |
| 1052 |
|
| 1053 |
def after(self, state): |
| 1054 |
run_hook.append('after') |
| 1055 |
|
| 1056 |
def on_error(self, state, e): |
| 1057 |
run_hook.append('error') |
| 1058 |
|
| 1059 |
app = TestApp(Pecan( |
| 1060 |
RootController(), |
| 1061 |
hooks=[SimpleHook()], |
| 1062 |
use_context_locals=False |
| 1063 |
)) |
| 1064 |
response = app.get('/') |
| 1065 |
assert response.status_int == 200 |
| 1066 |
assert response.body == b'Hello, World!' |
| 1067 |
|
| 1068 |
assert len(run_hook) == 4 |
| 1069 |
assert run_hook[0] == 'on_route' |
| 1070 |
assert run_hook[1] == 'before' |
| 1071 |
assert run_hook[2] == 'inside' |
| 1072 |
assert run_hook[3] == 'after' |
| 1073 |
|
| 1074 |
def test_basic_multi_hook(self): |
| 1075 |
run_hook = [] |
| 1076 |
|
| 1077 |
class RootController(object): |
| 1078 |
@expose() |
| 1079 |
def index(self, req, resp): |
| 1080 |
run_hook.append('inside') |
| 1081 |
return 'Hello, World!' |
| 1082 |
|
| 1083 |
class SimpleHook(PecanHook): |
| 1084 |
def __init__(self, id): |
| 1085 |
self.id = str(id) |
| 1086 |
|
| 1087 |
def on_route(self, state): |
| 1088 |
run_hook.append('on_route' + self.id) |
| 1089 |
|
| 1090 |
def before(self, state): |
| 1091 |
run_hook.append('before' + self.id) |
| 1092 |
|
| 1093 |
def after(self, state): |
| 1094 |
run_hook.append('after' + self.id) |
| 1095 |
|
| 1096 |
def on_error(self, state, e): |
| 1097 |
run_hook.append('error' + self.id) |
| 1098 |
|
| 1099 |
app = TestApp(Pecan(RootController(), hooks=[ |
| 1100 |
SimpleHook(1), SimpleHook(2), SimpleHook(3) |
| 1101 |
], use_context_locals=False)) |
| 1102 |
response = app.get('/') |
| 1103 |
assert response.status_int == 200 |
| 1104 |
assert response.body == b'Hello, World!' |
| 1105 |
|
| 1106 |
assert len(run_hook) == 10 |
| 1107 |
assert run_hook[0] == 'on_route1' |
| 1108 |
assert run_hook[1] == 'on_route2' |
| 1109 |
assert run_hook[2] == 'on_route3' |
| 1110 |
assert run_hook[3] == 'before1' |
| 1111 |
assert run_hook[4] == 'before2' |
| 1112 |
assert run_hook[5] == 'before3' |
| 1113 |
assert run_hook[6] == 'inside' |
| 1114 |
assert run_hook[7] == 'after3' |
| 1115 |
assert run_hook[8] == 'after2' |
| 1116 |
assert run_hook[9] == 'after1' |
| 1117 |
|
| 1118 |
def test_partial_hooks(self): |
| 1119 |
run_hook = [] |
| 1120 |
|
| 1121 |
class RootController(object): |
| 1122 |
@expose() |
| 1123 |
def index(self, req, resp): |
| 1124 |
run_hook.append('inside') |
| 1125 |
return 'Hello World!' |
| 1126 |
|
| 1127 |
@expose() |
| 1128 |
def causeerror(self, req, resp): |
| 1129 |
return [][1] |
| 1130 |
|
| 1131 |
class ErrorHook(PecanHook): |
| 1132 |
def on_error(self, state, e): |
| 1133 |
run_hook.append('error') |
| 1134 |
|
| 1135 |
class OnRouteHook(PecanHook): |
| 1136 |
def on_route(self, state): |
| 1137 |
run_hook.append('on_route') |
| 1138 |
|
| 1139 |
app = TestApp(Pecan(RootController(), hooks=[ |
| 1140 |
ErrorHook(), OnRouteHook() |
| 1141 |
], use_context_locals=False)) |
| 1142 |
|
| 1143 |
response = app.get('/') |
| 1144 |
assert response.status_int == 200 |
| 1145 |
assert response.body == b'Hello World!' |
| 1146 |
|
| 1147 |
assert len(run_hook) == 2 |
| 1148 |
assert run_hook[0] == 'on_route' |
| 1149 |
assert run_hook[1] == 'inside' |
| 1150 |
|
| 1151 |
run_hook = [] |
| 1152 |
try: |
| 1153 |
response = app.get('/causeerror') |
| 1154 |
except Exception as e: |
| 1155 |
assert isinstance(e, IndexError) |
| 1156 |
|
| 1157 |
assert len(run_hook) == 2 |
| 1158 |
assert run_hook[0] == 'on_route' |
| 1159 |
assert run_hook[1] == 'error' |
| 1160 |
|
| 1161 |
def test_on_error_response_hook(self): |
| 1162 |
run_hook = [] |
| 1163 |
|
| 1164 |
class RootController(object): |
| 1165 |
@expose() |
| 1166 |
def causeerror(self, req, resp): |
| 1167 |
return [][1] |
| 1168 |
|
| 1169 |
class ErrorHook(PecanHook): |
| 1170 |
def on_error(self, state, e): |
| 1171 |
run_hook.append('error') |
| 1172 |
|
| 1173 |
r = webob.Response() |
| 1174 |
r.text = 'on_error' |
| 1175 |
|
| 1176 |
return r |
| 1177 |
|
| 1178 |
app = TestApp(Pecan(RootController(), hooks=[ |
| 1179 |
ErrorHook() |
| 1180 |
], use_context_locals=False)) |
| 1181 |
|
| 1182 |
response = app.get('/causeerror') |
| 1183 |
|
| 1184 |
assert len(run_hook) == 1 |
| 1185 |
assert run_hook[0] == 'error' |
| 1186 |
assert response.text == 'on_error' |
| 1187 |
|
| 1188 |
def test_prioritized_hooks(self): |
| 1189 |
run_hook = [] |
| 1190 |
|
| 1191 |
class RootController(object): |
| 1192 |
@expose() |
| 1193 |
def index(self, req, resp): |
| 1194 |
run_hook.append('inside') |
| 1195 |
return 'Hello, World!' |
| 1196 |
|
| 1197 |
class SimpleHook(PecanHook): |
| 1198 |
def __init__(self, id, priority=None): |
| 1199 |
self.id = str(id) |
| 1200 |
if priority: |
| 1201 |
self.priority = priority |
| 1202 |
|
| 1203 |
def on_route(self, state): |
| 1204 |
run_hook.append('on_route' + self.id) |
| 1205 |
|
| 1206 |
def before(self, state): |
| 1207 |
run_hook.append('before' + self.id) |
| 1208 |
|
| 1209 |
def after(self, state): |
| 1210 |
run_hook.append('after' + self.id) |
| 1211 |
|
| 1212 |
def on_error(self, state, e): |
| 1213 |
run_hook.append('error' + self.id) |
| 1214 |
|
| 1215 |
papp = Pecan(RootController(), hooks=[ |
| 1216 |
SimpleHook(1, 3), SimpleHook(2, 2), SimpleHook(3, 1) |
| 1217 |
], use_context_locals=False) |
| 1218 |
app = TestApp(papp) |
| 1219 |
response = app.get('/') |
| 1220 |
assert response.status_int == 200 |
| 1221 |
assert response.body == b'Hello, World!' |
| 1222 |
|
| 1223 |
assert len(run_hook) == 10 |
| 1224 |
assert run_hook[0] == 'on_route3' |
| 1225 |
assert run_hook[1] == 'on_route2' |
| 1226 |
assert run_hook[2] == 'on_route1' |
| 1227 |
assert run_hook[3] == 'before3' |
| 1228 |
assert run_hook[4] == 'before2' |
| 1229 |
assert run_hook[5] == 'before1' |
| 1230 |
assert run_hook[6] == 'inside' |
| 1231 |
assert run_hook[7] == 'after1' |
| 1232 |
assert run_hook[8] == 'after2' |
| 1233 |
assert run_hook[9] == 'after3' |
| 1234 |
|
| 1235 |
def test_basic_isolated_hook(self): |
| 1236 |
run_hook = [] |
| 1237 |
|
| 1238 |
class SimpleHook(PecanHook): |
| 1239 |
def on_route(self, state): |
| 1240 |
run_hook.append('on_route') |
| 1241 |
|
| 1242 |
def before(self, state): |
| 1243 |
run_hook.append('before') |
| 1244 |
|
| 1245 |
def after(self, state): |
| 1246 |
run_hook.append('after') |
| 1247 |
|
| 1248 |
def on_error(self, state, e): |
| 1249 |
run_hook.append('error') |
| 1250 |
|
| 1251 |
class SubSubController(object): |
| 1252 |
@expose() |
| 1253 |
def index(self, req, resp): |
| 1254 |
run_hook.append('inside_sub_sub') |
| 1255 |
return 'Deep inside here!' |
| 1256 |
|
| 1257 |
class SubController(HookController): |
| 1258 |
__hooks__ = [SimpleHook()] |
| 1259 |
|
| 1260 |
@expose() |
| 1261 |
def index(self, req, resp): |
| 1262 |
run_hook.append('inside_sub') |
| 1263 |
return 'Inside here!' |
| 1264 |
|
| 1265 |
sub = SubSubController() |
| 1266 |
|
| 1267 |
class RootController(object): |
| 1268 |
@expose() |
| 1269 |
def index(self, req, resp): |
| 1270 |
run_hook.append('inside') |
| 1271 |
return 'Hello, World!' |
| 1272 |
|
| 1273 |
sub = SubController() |
| 1274 |
|
| 1275 |
app = TestApp(Pecan(RootController(), use_context_locals=False)) |
| 1276 |
response = app.get('/') |
| 1277 |
assert response.status_int == 200 |
| 1278 |
assert response.body == b'Hello, World!' |
| 1279 |
|
| 1280 |
assert len(run_hook) == 1 |
| 1281 |
assert run_hook[0] == 'inside' |
| 1282 |
|
| 1283 |
run_hook = [] |
| 1284 |
|
| 1285 |
response = app.get('/sub/') |
| 1286 |
assert response.status_int == 200 |
| 1287 |
assert response.body == b'Inside here!' |
| 1288 |
|
| 1289 |
assert len(run_hook) == 3 |
| 1290 |
assert run_hook[0] == 'before' |
| 1291 |
assert run_hook[1] == 'inside_sub' |
| 1292 |
assert run_hook[2] == 'after' |
| 1293 |
|
| 1294 |
run_hook = [] |
| 1295 |
response = app.get('/sub/sub/') |
| 1296 |
assert response.status_int == 200 |
| 1297 |
assert response.body == b'Deep inside here!' |
| 1298 |
|
| 1299 |
assert len(run_hook) == 3 |
| 1300 |
assert run_hook[0] == 'before' |
| 1301 |
assert run_hook[1] == 'inside_sub_sub' |
| 1302 |
assert run_hook[2] == 'after' |
| 1303 |
|
| 1304 |
def test_isolated_hook_with_global_hook(self): |
| 1305 |
run_hook = [] |
| 1306 |
|
| 1307 |
class SimpleHook(PecanHook): |
| 1308 |
def __init__(self, id): |
| 1309 |
self.id = str(id) |
| 1310 |
|
| 1311 |
def on_route(self, state): |
| 1312 |
run_hook.append('on_route' + self.id) |
| 1313 |
|
| 1314 |
def before(self, state): |
| 1315 |
run_hook.append('before' + self.id) |
| 1316 |
|
| 1317 |
def after(self, state): |
| 1318 |
run_hook.append('after' + self.id) |
| 1319 |
|
| 1320 |
def on_error(self, state, e): |
| 1321 |
run_hook.append('error' + self.id) |
| 1322 |
|
| 1323 |
class SubController(HookController): |
| 1324 |
__hooks__ = [SimpleHook(2)] |
| 1325 |
|
| 1326 |
@expose() |
| 1327 |
def index(self, req, resp): |
| 1328 |
run_hook.append('inside_sub') |
| 1329 |
return 'Inside here!' |
| 1330 |
|
| 1331 |
class RootController(object): |
| 1332 |
@expose() |
| 1333 |
def index(self, req, resp): |
| 1334 |
run_hook.append('inside') |
| 1335 |
return 'Hello, World!' |
| 1336 |
|
| 1337 |
sub = SubController() |
| 1338 |
|
| 1339 |
app = TestApp(Pecan( |
| 1340 |
RootController(), |
| 1341 |
hooks=[SimpleHook(1)], |
| 1342 |
use_context_locals=False |
| 1343 |
)) |
| 1344 |
response = app.get('/') |
| 1345 |
assert response.status_int == 200 |
| 1346 |
assert response.body == b'Hello, World!' |
| 1347 |
|
| 1348 |
assert len(run_hook) == 4 |
| 1349 |
assert run_hook[0] == 'on_route1' |
| 1350 |
assert run_hook[1] == 'before1' |
| 1351 |
assert run_hook[2] == 'inside' |
| 1352 |
assert run_hook[3] == 'after1' |
| 1353 |
|
| 1354 |
run_hook = [] |
| 1355 |
|
| 1356 |
response = app.get('/sub/') |
| 1357 |
assert response.status_int == 200 |
| 1358 |
assert response.body == b'Inside here!' |
| 1359 |
|
| 1360 |
assert len(run_hook) == 6 |
| 1361 |
assert run_hook[0] == 'on_route1' |
| 1362 |
assert run_hook[1] == 'before2' |
| 1363 |
assert run_hook[2] == 'before1' |
| 1364 |
assert run_hook[3] == 'inside_sub' |
| 1365 |
assert run_hook[4] == 'after1' |
| 1366 |
assert run_hook[5] == 'after2' |
| 1367 |
|
| 1368 |
|
| 1369 |
class TestGeneric(PecanTestCase): |
| 1370 |
|
| 1371 |
@property |
| 1372 |
def root(self): |
| 1373 |
class RootController(object): |
| 1374 |
|
| 1375 |
def __init__(self, unique): |
| 1376 |
self.unique = unique |
| 1377 |
|
| 1378 |
@expose(generic=True, template='json') |
| 1379 |
def index(self, req, resp): |
| 1380 |
assert self.__class__.__name__ == 'RootController' |
| 1381 |
assert isinstance(req, Request) |
| 1382 |
assert isinstance(resp, Response) |
| 1383 |
assert self.unique == req.headers.get('X-Unique') |
| 1384 |
return {'hello': 'world'} |
| 1385 |
|
| 1386 |
@index.when(method='POST', template='json') |
| 1387 |
def index_post(self, req, resp): |
| 1388 |
assert self.__class__.__name__ == 'RootController' |
| 1389 |
assert isinstance(req, Request) |
| 1390 |
assert isinstance(resp, Response) |
| 1391 |
assert self.unique == req.headers.get('X-Unique') |
| 1392 |
return req.json |
| 1393 |
|
| 1394 |
@expose(template='json') |
| 1395 |
def echo(self, req, resp): |
| 1396 |
assert self.__class__.__name__ == 'RootController' |
| 1397 |
assert isinstance(req, Request) |
| 1398 |
assert isinstance(resp, Response) |
| 1399 |
assert self.unique == req.headers.get('X-Unique') |
| 1400 |
return req.json |
| 1401 |
|
| 1402 |
@expose(template='json') |
| 1403 |
def extra(self, req, resp, first, second): |
| 1404 |
assert self.__class__.__name__ == 'RootController' |
| 1405 |
assert isinstance(req, Request) |
| 1406 |
assert isinstance(resp, Response) |
| 1407 |
assert self.unique == req.headers.get('X-Unique') |
| 1408 |
return {'first': first, 'second': second} |
| 1409 |
|
| 1410 |
return RootController |
| 1411 |
|
| 1412 |
def test_generics_with_im_self_default(self): |
| 1413 |
uniq = str(time.time()) |
| 1414 |
with mock.patch('threading.local', side_effect=AssertionError()): |
| 1415 |
app = TestApp(Pecan(self.root(uniq), use_context_locals=False)) |
| 1416 |
r = app.get('/', headers={'X-Unique': uniq}) |
| 1417 |
assert r.status_int == 200 |
| 1418 |
json_resp = loads(r.body.decode()) |
| 1419 |
assert json_resp['hello'] == 'world' |
| 1420 |
|
| 1421 |
def test_generics_with_im_self_with_method(self): |
| 1422 |
uniq = str(time.time()) |
| 1423 |
with mock.patch('threading.local', side_effect=AssertionError()): |
| 1424 |
app = TestApp(Pecan(self.root(uniq), use_context_locals=False)) |
| 1425 |
r = app.post_json('/', {'foo': 'bar'}, headers={'X-Unique': uniq}) |
| 1426 |
assert r.status_int == 200 |
| 1427 |
json_resp = loads(r.body.decode()) |
| 1428 |
assert json_resp['foo'] == 'bar' |
| 1429 |
|
| 1430 |
def test_generics_with_im_self_with_path(self): |
| 1431 |
uniq = str(time.time()) |
| 1432 |
with mock.patch('threading.local', side_effect=AssertionError()): |
| 1433 |
app = TestApp(Pecan(self.root(uniq), use_context_locals=False)) |
| 1434 |
r = app.post_json('/echo/', {'foo': 'bar'}, |
| 1435 |
headers={'X-Unique': uniq}) |
| 1436 |
assert r.status_int == 200 |
| 1437 |
json_resp = loads(r.body.decode()) |
| 1438 |
assert json_resp['foo'] == 'bar' |
| 1439 |
|
| 1440 |
def test_generics_with_im_self_with_extra_args(self): |
| 1441 |
uniq = str(time.time()) |
| 1442 |
with mock.patch('threading.local', side_effect=AssertionError()): |
| 1443 |
app = TestApp(Pecan(self.root(uniq), use_context_locals=False)) |
| 1444 |
r = app.get('/extra/123/456', headers={'X-Unique': uniq}) |
| 1445 |
assert r.status_int == 200 |
| 1446 |
json_resp = loads(r.body.decode()) |
| 1447 |
assert json_resp['first'] == '123' |
| 1448 |
assert json_resp['second'] == '456' |