mirror of https://github.com/scrapy/scrapy.git
Merge pull request #3794 from csalazar/whitelist-form-methods-in-fromresponse
[MRG+1] Fix form methods in FormRequest.from_response (#3777)
This commit is contained in:
commit
9aec7856b0
|
|
@ -18,6 +18,7 @@ from scrapy.utils.response import get_base_url
|
|||
|
||||
|
||||
class FormRequest(Request):
|
||||
valid_form_methods = ['GET', 'POST']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
formdata = kwargs.pop('formdata', None)
|
||||
|
|
@ -48,7 +49,13 @@ class FormRequest(Request):
|
|||
form = _get_form(response, formname, formid, formnumber, formxpath)
|
||||
formdata = _get_inputs(form, formdata, dont_click, clickdata, response)
|
||||
url = _get_form_url(form, kwargs.pop('url', None))
|
||||
|
||||
method = kwargs.pop('method', form.method)
|
||||
if method is not None:
|
||||
method = method.upper()
|
||||
if method not in cls.valid_form_methods:
|
||||
method = 'GET'
|
||||
|
||||
return cls(url=url, method=method, formdata=formdata, **kwargs)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1105,6 +1105,20 @@ class FormRequestTest(RequestTest):
|
|||
self.assertRaises(ValueError, self.request_class.from_response,
|
||||
response, formcss="input[name='abc']")
|
||||
|
||||
def test_from_response_valid_form_methods(self):
|
||||
body = """<form action="post.php" method="%s">
|
||||
<input type="hidden" name="one" value="1">
|
||||
</form>"""
|
||||
|
||||
for method in self.request_class.valid_form_methods:
|
||||
response = _buildresponse(body % method)
|
||||
r = self.request_class.from_response(response)
|
||||
self.assertEqual(r.method, method)
|
||||
|
||||
response = _buildresponse(body % 'UNKNOWN')
|
||||
r = self.request_class.from_response(response)
|
||||
self.assertEqual(r.method, 'GET')
|
||||
|
||||
|
||||
def _buildresponse(body, **kwargs):
|
||||
kwargs.setdefault('body', body)
|
||||
|
|
|
|||
Loading…
Reference in New Issue