mirror of https://github.com/scrapy/scrapy.git
removed overriden replace method
This commit is contained in:
parent
1b2b8b4bf0
commit
cd619c1d4f
|
|
@ -12,17 +12,13 @@ from scrapy.http.request import Request
|
|||
|
||||
class JSONRequest(Request):
|
||||
def __init__(self, *args, **kwargs):
|
||||
if 'method' not in kwargs:
|
||||
kwargs['method'] = 'POST'
|
||||
data = kwargs.pop('data', None)
|
||||
if data:
|
||||
kwargs['body'] = json.dumps(data)
|
||||
|
||||
if 'method' not in kwargs:
|
||||
kwargs['method'] = 'POST'
|
||||
|
||||
data = kwargs.pop('data', {})
|
||||
kwargs['body'] = json.dumps(data)
|
||||
super(JSONRequest, self).__init__(*args, **kwargs)
|
||||
self.headers.setdefault(b'Content-Type', b'application/json')
|
||||
|
||||
def replace(self, *args, **kwargs):
|
||||
""" Create a new Request with the same attributes except for those
|
||||
given new values. """
|
||||
|
||||
kwargs.pop('body', None)
|
||||
return super(JSONRequest, self).replace(*args, **kwargs)
|
||||
self.headers.setdefault('Content-Type', 'application/json')
|
||||
self.headers.setdefault('Accept', 'application/json, text/javascript, */*; q=0.01')
|
||||
|
|
|
|||
|
|
@ -1150,54 +1150,29 @@ class XmlRpcRequestTest(RequestTest):
|
|||
|
||||
class JSONRequestTest(RequestTest):
|
||||
request_class = JSONRequest
|
||||
default_method = 'POST'
|
||||
default_headers = {b'Content-Type': [b'application/json']}
|
||||
default_method = 'GET'
|
||||
default_headers = {b'Content-Type': [b'application/json'], b'Accept': [b'application/json, text/javascript, */*; q=0.01']}
|
||||
|
||||
def test_body(self):
|
||||
def test_data(self):
|
||||
r1 = self.request_class(url="http://www.example.com/")
|
||||
self.assertEqual(r1.body, b'{}')
|
||||
self.assertEqual(r1.body, b'')
|
||||
self.assertEqual(r1.method, 'GET')
|
||||
|
||||
r2 = self.request_class(url="http://www.example.com/", body=b"")
|
||||
self.assertEqual(r2.body, b'{}')
|
||||
body = b'body'
|
||||
r2 = self.request_class(url="http://www.example.com/", body=body)
|
||||
self.assertEqual(r2.body, body)
|
||||
self.assertEqual(r2.method, 'GET')
|
||||
|
||||
data = {
|
||||
'name': 'value',
|
||||
}
|
||||
r3 = self.request_class(url="http://www.example.com/", data=data)
|
||||
self.assertEqual(r3.body, to_bytes(json.dumps(data)))
|
||||
self.assertEqual(r3.method, 'POST')
|
||||
|
||||
r4 = self.request_class(url="http://www.example.com/", body='body1', data=data)
|
||||
self.assertEqual(r3.body, to_bytes(json.dumps(data)))
|
||||
|
||||
def test_replace(self):
|
||||
"""Test Request.replace() method"""
|
||||
r1 = self.request_class("http://www.example.com")
|
||||
hdrs = Headers(r1.headers)
|
||||
hdrs[b'key'] = b'value'
|
||||
r2 = r1.replace(body="New body", headers=hdrs)
|
||||
|
||||
# body will not be replaced
|
||||
self.assertEqual(r1.body, r2.body)
|
||||
self.assertEqual(r1.url, r2.url)
|
||||
self.assertEqual((r1.headers, r2.headers), (self.default_headers, hdrs))
|
||||
|
||||
# Empty attributes (which may fail if not compared properly)
|
||||
r3 = self.request_class("http://www.example.com", meta={'a': 1}, dont_filter=True)
|
||||
r4 = r3.replace(url="http://www.example.com/2", meta={}, dont_filter=False)
|
||||
self.assertEqual(r4.url, "http://www.example.com/2")
|
||||
self.assertEqual(r4.meta, {})
|
||||
assert r4.dont_filter is False
|
||||
|
||||
data1 = {
|
||||
'name': 'value1',
|
||||
}
|
||||
data2 = {
|
||||
'name': 'value2',
|
||||
}
|
||||
r5 = self.request_class("http://www.example.com", data=data1)
|
||||
r6 = r5.replace(url="http://www.example.com/2", data=data2)
|
||||
self.assertNotEqual(r5.body, r6.body)
|
||||
self.assertEqual((r5.body, r6.body), (to_bytes(json.dumps(data1)), to_bytes(json.dumps(data2))))
|
||||
r4 = self.request_class(url="http://www.example.com/", body=body, data=data)
|
||||
self.assertEqual(r4.body, to_bytes(json.dumps(data)))
|
||||
self.assertEqual(r4.method, 'POST')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
Loading…
Reference in New Issue