MCPcopy Index your code
hub / github.com/python/cpython / add_ordered_mock_handlers

Function add_ordered_mock_handlers

Lib/test/test_urllib2.py:447–478  ·  view source on GitHub ↗

Create MockHandlers and add them to an OpenerDirector. meth_spec: list of lists of tuples and strings defining methods to define on handlers. eg: [["http_error", "ftp_open"], ["http_open"]] defines methods .http_error() and .ftp_open() on one handler, and .http_open() on anot

(opener, meth_spec)

Source from the content-addressed store, hash-verified

445
446
447def add_ordered_mock_handlers(opener, meth_spec):
448 """Create MockHandlers and add them to an OpenerDirector.
449
450 meth_spec: list of lists of tuples and strings defining methods to define
451 on handlers. eg:
452
453 [["http_error", "ftp_open"], ["http_open"]]
454
455 defines methods .http_error() and .ftp_open() on one handler, and
456 .http_open() on another. These methods just record their arguments and
457 return None. Using a tuple instead of a string causes the method to
458 perform some action (see MockHandler.handle()), eg:
459
460 [["http_error"], [("http_open", "return request")]]
461
462 defines .http_error() on one handler (which simply returns None), and
463 .http_open() on another handler, which returns a Request object.
464
465 """
466 handlers = []
467 count = 0
468 for meths in meth_spec:
469 class MockHandlerSubclass(MockHandler):
470 pass
471
472 h = MockHandlerSubclass(meths)
473 h.handler_order += count
474 h.add_parent(opener)
475 count = count + 1
476 handlers.append(h)
477 opener.add_handler(h)
478 return handlers
479
480
481def build_test_opener(*handler_instances):

Callers 8

test_handledMethod · 0.85
test_raiseMethod · 0.85
test_http_errorMethod · 0.85
test_processorsMethod · 0.85
test_proxyMethod · 0.85
test_proxy_httpsMethod · 0.85

Calls 4

add_handlerMethod · 0.80
MockHandlerSubclassClass · 0.70
add_parentMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…