diff --git a/scrapy/http/request/form.py b/scrapy/http/request/form.py index c2413b431..3ce8fc48e 100644 --- a/scrapy/http/request/form.py +++ b/scrapy/http/request/form.py @@ -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) diff --git a/tests/test_http_request.py b/tests/test_http_request.py index 1bab37c0b..53d18d4aa 100644 --- a/tests/test_http_request.py +++ b/tests/test_http_request.py @@ -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 = """
+ +
""" + + 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)