本文整理汇总了Python中requests.models.Response.headers方法的典型用法代码示例。如果您正苦于以下问题:Python Response.headers方法的具体用法?Python Response.headers怎么用?Python Response.headers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests.models.Response
的用法示例。
在下文中一共展示了Response.headers方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_301_circular_redirect
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def test_get_301_circular_redirect(self, mock_request):
response0 = Response()
response0.url = 'http://www.test.com/path0'
response0.status_code = 301
response0.headers = {'Location': 'http://www.test.com/path1'}
response1 = Response()
response1.url = 'http://www.test.com/path1'
response1.status_code = 301
response1.headers = {'Location': 'http://www.test.com/path0'}
response2 = Response()
response2.url = 'http://www.test.com/path2'
response2.status_code = 200
response2._content = 'Mocked response content'
response2.history = [response0, response1]
mock_request.return_value = response2
r = get('http://www.test.com/path0')
self.assertEqual(mock_request.call_count, 1)
self.assertEqual(r.status_code, 200)
self.assertEqual(r.content, 'Mocked response content')
with self.assertRaises(TooManyRedirects):
get('http://www.test.com/path0')
示例2: test_disable_default_redirect_cache
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def test_disable_default_redirect_cache(self, mock_request):
"""
Test disable default redirect cache (by setting default redirect cache to None)
"""
response0 = Response()
response0.url = 'http://www.test.com/neverseemeagain'
response0.status_code = 301
response0.headers = {
'Location': 'http://www.test.com/redirect_here',
}
response1 = Response()
response1.url = 'http://www.test.com/redirect_here'
response1.status_code = 200
response1._content = 'Mocked response content'
response1.headers = {
'Vary': 'Accept',
}
response1.history = [response0]
mock_request.return_value = response1
get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)
set_default_redirect_cache(None)
get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
示例3: test_get_301_only_once
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def test_get_301_only_once(self, mock_request):
response0 = Response()
response0.url = 'http://www.test.com/neverseemeagain'
response0.status_code = 301
response0.headers = {
'Location': 'http://www.test.com/redirect_here',
}
response1 = Response()
response1.url = 'http://www.test.com/redirect_here'
response1.status_code = 200
response1._content = 'Mocked response content'
response1.headers = {
'Vary': 'Accept',
}
response1.history = [response0]
mock_request.return_value = response1
r = get('http://www.test.com/neverseemeagain')
self.assertEqual(mock_request.call_count, 1)
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
self.assertEqual(r.status_code, 200)
#assert we not make request to 301 again
r = get('http://www.test.com/neverseemeagain')
self.assertEqual(mock_request.call_count, 2)
mock_request.assert_called_with('GET', 'http://www.test.com/redirect_here', allow_redirects=True)
self.assertEqual(r.status_code, 200)
示例4: test_get_301_thrice
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def test_get_301_thrice(self, mock_get):
response0 = Response()
response0.url = 'http://www.test.com/neverseemeagain'
response0.status_code = 301
response0.headers = {
'Location': 'http://www.test.com/redirect_1',
}
response1 = Response()
response1.url = 'http://www.test.com/redirect_1'
response1.status_code = 301
response1.headers = {
'Location': 'http://www.test.com/redirect_2',
}
response2 = Response()
response2.url = 'http://www.test.com/redirect_2'
response2.status_code = 301
response2.headers = {
'Location': 'http://www.test.com/redirect_3',
}
response3 = Response()
response3.url = 'http://www.test.com/redirect_3'
response3.status_code = 200
response3._content = 'Mocked response content'
response3.headers = {
'Vary': 'Accept',
}
response3.history = [response0, response1, response2]
mock_get.return_value = response3
r = get('http://www.test.com/neverseemeagain')
self.assertEqual(mock_get.call_count, 1)
mock_get.assert_called_with('http://www.test.com/neverseemeagain')
self.assertEqual(r.status_code, 200)
#assert we not make request to 301 again
r = get('http://www.test.com/neverseemeagain')
self.assertEqual(mock_get.call_count, 2)
mock_get.assert_called_with('http://www.test.com/redirect_3')
self.assertEqual(r.status_code, 200)
r = get('http://www.test.com/redirect_1')
self.assertEqual(mock_get.call_count, 3)
mock_get.assert_called_with('http://www.test.com/redirect_3')
self.assertEqual(r.status_code, 200)
r = get('http://www.test.com/redirect_2')
self.assertEqual(mock_get.call_count, 4)
mock_get.assert_called_with('http://www.test.com/redirect_3')
self.assertEqual(r.status_code, 200)
r = get('http://www.test.com/redirect_3')
self.assertEqual(mock_get.call_count, 5)
mock_get.assert_called_with('http://www.test.com/redirect_3')
self.assertEqual(r.status_code, 200)
示例5: test_domain_cookie
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def test_domain_cookie(self, mock_request):
"""
Test domain cookies without 'Path'
"""
response0 = Response()
response0.status_code = 200
response0._content = 'Mocked response content'
response0.headers = {
'Set-Cookie': 'a=apple; Domain=fruits.com;, ' +
'b=banana; Domain=fruits.com;, ' +
'c=citrus; Domain=mediterranean.fruits.com;, ' +
'm=mango; Domain=tropical.fruits.com;'
}
response0.url = 'http://mediterranean.fruits.com/path0'
mock_request.return_value = response0
get('http://mediterranean.fruits.com/path0') # Initial request. No cookies.
mock_request.assert_called_with('GET', 'http://mediterranean.fruits.com/path0', allow_redirects=True)
get('http://mediterranean.fruits.com/path1') # 'a', 'b', and 'c' cookies should be present.
mock_request.assert_called_with('GET', 'http://mediterranean.fruits.com/path1', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana', 'c': 'citrus'})
get('http://tropical.fruits.com/path2') # 'a', 'b', and 'm' cookies should be present.
mock_request.assert_called_with('GET', 'http://tropical.fruits.com/path2', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana', 'm': 'mango'})
get('http://www.fruits.com/path3') # 'a' and 'b' cookies should be present.
mock_request.assert_called_with('GET', 'http://www.fruits.com/path3', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana'})
get('http://fruits.com/path4') # 'a' and 'b' cookies should be present.
mock_request.assert_called_with('GET', 'http://fruits.com/path4', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana'})
get('http://animals.com/path5') # Different domain. No cookies should be present.
mock_request.assert_called_with('GET', 'http://animals.com/path5', allow_redirects=True)
response1 = Response()
response1.status_code = 200
response1._content = 'Mocked response content'
response1.headers = {
'Set-Cookie': 'a=apricot; Domain=fruits.com;, ' +
'b=; Domain=fruits.com;, ' +
'm=melon; Domain=tropical.fruits.com;'
}
response1.url = 'http://tropical.fruits.com/path0'
mock_request.return_value = response1
get('http://tropical.fruits.com/path0') # Still called with previous cookies
mock_request.assert_called_with('GET', 'http://tropical.fruits.com/path0', allow_redirects=True,
cookies={'a': 'apple', 'b': 'banana', 'm': 'mango'})
get('http://tropical.fruits.com/path1') # called with new cookies
mock_request.assert_called_with('GET', 'http://tropical.fruits.com/path1', allow_redirects=True,
cookies={'a': 'apricot', 'b': '', 'm': 'melon'})
示例6: setUp
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def setUp(self):
self.cassette = cassette.Cassette(
TestCassette.cassette_name,
'json',
'w+'
)
r = Response()
r.status_code = 200
r.encoding = 'utf-8'
r.headers = CaseInsensitiveDict({'Content-Type': decode('foo')})
r.url = 'http://example.com'
cassette.add_urllib3_response({
'body': {
'string': decode('foo'),
'encoding': 'utf-8'
}
}, r)
self.response = r
r = Request()
r.method = 'GET'
r.url = 'http://example.com'
r.headers = {}
r.data = {'key': 'value'}
self.response.request = r.prepare()
self.response.request.headers.update(
{'User-Agent': 'betamax/test header'}
)
self.json = {
'request': {
'body': 'key=value',
'headers': {
'User-Agent': 'betamax/test header',
'Content-Length': '9',
'Content-Type': 'application/x-www-form-urlencoded',
},
'method': 'GET',
'uri': 'http://example.com/',
},
'response': {
'body': {
'string': decode('foo'),
'encoding': 'utf-8',
},
'headers': {'Content-Type': decode('foo')},
'status_code': 200,
'url': 'http://example.com',
},
'recorded_at': '2013-08-31T00:00:00',
}
self.date = datetime(2013, 8, 31)
self.cassette.save_interaction(self.response, self.response.request)
self.interaction = self.cassette.interactions[0]
self.interaction.recorded_at = self.date
示例7: setUp
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def setUp(self):
# Make a new serializer to test with
self.test_serializer = Serializer()
serializers.serializer_registry["test"] = self.test_serializer
# Instantiate the cassette to test with
self.cassette = cassette.Cassette(TestCassette.cassette_name, "test", record_mode="once")
# Create a new object to serialize
r = Response()
r.status_code = 200
r.reason = "OK"
r.encoding = "utf-8"
r.headers = CaseInsensitiveDict({"Content-Type": decode("foo")})
r.url = "http://example.com"
util.add_urllib3_response({"body": {"string": decode("foo"), "encoding": "utf-8"}}, r)
self.response = r
# Create an associated request
r = Request()
r.method = "GET"
r.url = "http://example.com"
r.headers = {}
r.data = {"key": "value"}
self.response.request = r.prepare()
self.response.request.headers.update({"User-Agent": "betamax/test header"})
# Expected serialized cassette data.
self.json = {
"request": {
"body": {"encoding": "utf-8", "string": "key=value"},
"headers": {
"User-Agent": ["betamax/test header"],
"Content-Length": ["9"],
"Content-Type": ["application/x-www-form-urlencoded"],
},
"method": "GET",
"uri": "http://example.com/",
},
"response": {
"body": {"string": decode("foo"), "encoding": "utf-8"},
"headers": {"Content-Type": [decode("foo")]},
"status": {"code": 200, "message": "OK"},
"url": "http://example.com",
},
"recorded_at": "2013-08-31T00:00:00",
}
self.date = datetime(2013, 8, 31)
self.cassette.save_interaction(self.response, self.response.request)
self.interaction = self.cassette.interactions[0]
self.interaction.recorded_at = self.date
示例8: test_redirect
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def test_redirect(self, mock_request):
"""
Test that each session has its own redirect "sandbox".
"""
response0 = Response()
response0.url = 'http://www.test.com/neverseemeagain'
response0.status_code = 301
response0.headers = {'Location': 'http://www.test.com/redirect_1'}
response1 = Response()
response1.url = 'http://www.test.com/redirect_1'
response1.status_code = 301
response1.headers = {'Location': 'http://www.test.com/redirect_2'}
response2 = Response()
response2.url = 'http://www.test.com/redirect_2'
response2.status_code = 301
response2.headers = {'Location': 'http://www.test.com/redirect_3'}
response3 = Response()
response3.url = 'http://www.test.com/redirect_3'
response3.status_code = 200
response3._content = 'Mocked response content'
response3.history = [response0, response1, response2]
mock_request.return_value = response3
s0 = Session()
s1 = Session()
# s0 make a request
r = s0.get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
self.assertEqual(r.status_code, 200)
# s0 make a request again. Assert we not make request to 301 again.
r = s0.get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/redirect_3', allow_redirects=True)
self.assertEqual(r.status_code, 200)
# s1 make a request
r = s1.get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/neverseemeagain', allow_redirects=True)
self.assertEqual(r.status_code, 200)
# s1 make a request again. Assert we not make request to 301 again.
r = s1.get('http://www.test.com/neverseemeagain')
mock_request.assert_called_with('GET', 'http://www.test.com/redirect_3', allow_redirects=True)
self.assertEqual(r.status_code, 200)
示例9: build_response
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def build_response(self, req, resp):
"""Builds a :class:`Response <requests.Response>` object from a urllib3
response. This should not be called from user code, and is only exposed
for use when subclassing the
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`
:param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response.
:param resp: The urllib3 response object.
"""
response = Response()
# Fallback to None if there's no status_code, for whatever reason.
response.status_code = getattr(resp, 'status', None)
# Make headers case-insensitive.
response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))
# Set encoding.
response.encoding = get_encoding_from_headers(response.headers)
response.raw = resp
response.reason = response.raw.reason
if isinstance(req.url, bytes):
response.url = req.url.decode('utf-8')
else:
response.url = req.url
# Add new cookies from the server.
extract_cookies_to_jar(response.cookies, req, resp)
# Give the Response some context.
response.request = req
response.connection = self
return response
示例10: request
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def request(method, url, **kwargs):
if 'data' in kwargs:
kwargs['params'] = kwargs.pop('data')
elif 'params' in kwargs and kwargs['params'] is None:
kwargs.pop('params')
auth = None
if 'auth' in kwargs:
auth = kwargs.pop('auth')
for i in ['auth', 'allow_redirects', 'stream']:
if i in kwargs:
kwargs.pop(i)
if app.app.registry.api_url in url:
if auth:
authorization = api.authorization
api.authorization = ('Basic', auth)
resp = api._gen_request(method.upper(), url, expect_errors=True, **kwargs)
if auth:
api.authorization = authorization
else:
resp = app._gen_request(method.upper(), url, expect_errors=True, **kwargs)
response = Response()
response.status_code = resp.status_int
response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))
response.encoding = get_encoding_from_headers(response.headers)
response.raw = resp
response._content = resp.body
response.reason = resp.status
if isinstance(url, bytes):
response.url = url.decode('utf-8')
else:
response.url = url
response.request = resp.request
return response
示例11: send
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def send(self, request, stream=None, timeout=None, verify=None, cert=None,
proxies=None):
parsed_url = urlparse.urlparse(request.url)
# We only work for requests with a host of localhost
if parsed_url.netloc.lower() != "localhost":
raise InvalidURL("Invalid URL %r: Only localhost is allowed" %
request.url)
real_url = urlparse.urlunparse(parsed_url[:1] + ("",) + parsed_url[2:])
pathname = url_to_path(real_url)
resp = Response()
resp.status_code = 200
resp.url = real_url
stats = os.stat(pathname)
modified = email.utils.formatdate(stats.st_mtime, usegmt=True)
resp.headers = CaseInsensitiveDict({
"Content-Type": mimetypes.guess_type(pathname)[0] or "text/plain",
"Content-Length": stats.st_size,
"Last-Modified": modified,
})
resp.raw = LocalFSResponse(open(pathname, "rb"))
resp.close = resp.raw.close
return resp
示例12: create_mock_response
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def create_mock_response(cls, status_code, data, filter=None, order_by=None, page=None, error=None, headers=None):
""" Build a fake response
Args:
status_code: the status code
data: the NURESTObject
filter: a string representing a filter
order_by: a string representing an order by
page: a page number
"""
content = None
if type(data) == list:
content = list()
for obj in data:
content.append(obj.to_dict())
elif data:
content = data.to_dict()
response = Response()
response.status_code = status_code
response._content = json.dumps(content)
if headers:
response.headers = headers
return MagicMock(return_value=response)
示例13: test_set_default_cookie_cache
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def test_set_default_cookie_cache(self, mock_request):
response = Response()
response.headers = {
'Set-Cookie': 'name=value',
}
response.url = 'http://www.test.com/path'
mock_request.return_value = response
C0 = self.cookie_cache
C1 = Cache()
C2 = Cache()
get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', cookies={'name': 'value'}, allow_redirects=True)
set_default_cookie_cache(C1)
get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', cookies={'name': 'value'}, allow_redirects=True)
set_default_cookie_cache(C2)
get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', cookies={'name': 'value'}, allow_redirects=True)
set_default_cookie_cache(C0)
get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', cookies={'name': 'value'}, allow_redirects=True)
示例14: test_disable_default_cache
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def test_disable_default_cache(self, mock_request):
"""
Test disable default cache (by setting default cache to None)
"""
response = Response()
response.status_code = 200
response._content = 'Mocked response content'
response.headers = {
'Cache-Control': 'max-age=100',
}
mock_request.return_value = response
get('http://www.test.com/path')
self.assertEqual(mock_request.call_count, 1)
get('http://www.test.com/path')
self.assertEqual(mock_request.call_count, 1)
set_default_cache(None)
get('http://www.test.com/path')
self.assertEqual(mock_request.call_count, 2)
get('http://www.test.com/path')
self.assertEqual(mock_request.call_count, 3)
get('http://www.test.com/path')
self.assertEqual(mock_request.call_count, 4)
示例15: test_cookie
# 需要导入模块: from requests.models import Response [as 别名]
# 或者: from requests.models.Response import headers [as 别名]
def test_cookie(self, mock_request):
"""
Test that each session has its own cookie "sandbox".
"""
response = Response()
response.status_code = 200
response._content = 'Mocked response content'
response.headers = {'Set-Cookie': 'name=value'}
response.url = 'http://www.test.com/path'
mock_request.return_value = response
s0 = Session()
s1 = Session()
# s0 make requests
s0.get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
s0.get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True, cookies={'name': 'value'})
# s1 make requests
s1.get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True)
s1.get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True, cookies={'name': 'value'})
# s0 make requests again
s0.get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True, cookies={'name': 'value'})
s0.get('http://www.test.com/path')
mock_request.assert_called_with('GET', 'http://www.test.com/path', allow_redirects=True, cookies={'name': 'value'})