本文整理汇总了Python中mock.mock.MagicMock.code方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.code方法的具体用法?Python MagicMock.code怎么用?Python MagicMock.code使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.code方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_urllib2_refresh_header_processor
# 需要导入模块: from mock.mock import MagicMock [as 别名]
# 或者: from mock.mock.MagicMock import code [as 别名]
def test_urllib2_refresh_header_processor(self):
from urllib2 import Request
# setup the original request
original_url = "http://foo.bar.baz/jmx?qry=someQuery"
request = Request(original_url)
# ensure that we get back a 200 with a refresh header to redirect us
response = MagicMock(code=200)
info_response = MagicMock()
info_response.keys.return_value = ["Refresh"]
info_response.getheader.return_value = "3; url=http://foobar.baz.qux:8080"
response.info.return_value = info_response
# add a mock parent to the refresh processor
parent_mock = MagicMock()
refresh_processor = RefreshHeaderProcessor()
refresh_processor.parent = parent_mock
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was called with the modified URL
parent_mock.open.assert_called_with("http://foobar.baz.qux:8080/jmx?qry=someQuery")
# reset mocks
MagicMock.reset_mock(parent_mock)
# alter the refresh header to remove the time value
info_response.getheader.return_value = "url=http://foobar.baz.qux:8443"
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was called with the modified URL
parent_mock.open.assert_called_with("http://foobar.baz.qux:8443/jmx?qry=someQuery")
# reset mocks
MagicMock.reset_mock(parent_mock)
# use an invalid refresh header
info_response.getheader.return_value = "http://foobar.baz.qux:8443"
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was not called
self.assertFalse(parent_mock.open.called)
# reset mocks
MagicMock.reset_mock(parent_mock)
# remove the refresh header
info_response.keys.return_value = ["SomeOtherHeaders"]
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was not called
self.assertFalse(parent_mock.open.called)
# reset mocks
MagicMock.reset_mock(parent_mock)
# use and invalid http code but include a refresh header
response.code = 401
info_response.keys.return_value = ["Refresh"]
info_response.getheader.return_value = "3; url=http://foobar.baz.qux:8080"
# execute
refresh_processor.http_response(request, response)
# ensure that the parent was not called
self.assertFalse(parent_mock.open.called)