mirror of https://github.com/scrapy/scrapy.git
Changed TextResponse.replace() behaviour by keeping previous encoding when not specified
This commit is contained in:
parent
b9ea9dcf1a
commit
ad2b979e0f
|
|
@ -53,7 +53,7 @@ class TextResponse(Response):
|
|||
super(TextResponse, self)._set_body(body)
|
||||
|
||||
def replace(self, *args, **kwargs):
|
||||
kwargs.setdefault('encoding', getattr(self, '_encoding', None))
|
||||
kwargs.setdefault('encoding', self.encoding)
|
||||
return Response.replace(self, *args, **kwargs)
|
||||
|
||||
@property
|
||||
|
|
|
|||
|
|
@ -287,13 +287,16 @@ class XmlResponseTest(TextResponseTest):
|
|||
r4 = r3.replace(body=body2)
|
||||
self._assert_response_values(r4, 'utf-8', body2)
|
||||
|
||||
# make sure replace() rediscovers the encoding (if not given explicitly) when changing the body
|
||||
def test_replace_encoding(self):
|
||||
# make sure replace() keeps the previous encoding unless overridden explicitly
|
||||
body = """<?xml version="1.0" encoding="iso-8859-1"?><xml></xml>"""
|
||||
r5 = self.response_class("http://www.example.com", body=body)
|
||||
body2 = """<?xml version="1.0" encoding="utf-8"?><xml></xml>"""
|
||||
r5 = self.response_class("http://www.example.com", body=body)
|
||||
r6 = r5.replace(body=body2)
|
||||
r7 = r5.replace(body=body2, encoding='utf-8')
|
||||
self._assert_response_values(r5, 'iso-8859-1', body)
|
||||
self._assert_response_values(r6, 'utf-8', body2)
|
||||
self._assert_response_values(r6, 'iso-8859-1', body2)
|
||||
self._assert_response_values(r7, 'utf-8', body2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Reference in New Issue