Commit ceecc962 authored by Baptiste Mispelon's avatar Baptiste Mispelon
Browse files

Fixed #21447 -- Restored code erroneously removed in 20472aa8.

Also added some tests for HttpRequest.__repr__.
Note that the added tests don't actually catch the accidental code
removal (see ticket) but they do cover a codepath that wasn't tested
before.

Thanks to Tom Christie for the report and the original patch.
parent d0117140
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -242,7 +242,7 @@ class HttpRequest(object):
                # Mark that an error occured. This allows self.__repr__ to
                # be explicit about it instead of simply representing an
                # empty POST
                # self._mark_post_parse_error()
                self._mark_post_parse_error()
                raise
        elif self.META.get('CONTENT_TYPE', '').startswith('application/x-www-form-urlencoded'):
            self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict()
+16 −0
Original line number Diff line number Diff line
@@ -42,6 +42,22 @@ class RequestsTests(SimpleTestCase):
        self.assertEqual(build_request_repr(request, path_override='/otherpath/', GET_override={'a': 'b'}, POST_override={'c': 'd'}, COOKIES_override={'e': 'f'}, META_override={'g': 'h'}),
                         str_prefix("<HttpRequest\npath:/otherpath/,\nGET:{%(_)s'a': %(_)s'b'},\nPOST:{%(_)s'c': %(_)s'd'},\nCOOKIES:{%(_)s'e': %(_)s'f'},\nMETA:{%(_)s'g': %(_)s'h'}>"))

    def test_bad_httprequest_repr(self):
        """
        If an exception occurs when parsing GET, POST, COOKIES, or META, the
        repr of the request should show it.
        """
        class Bomb(object):
            """An object that raises an exception when printed out."""
            def __repr__(self):
                raise Exception('boom!')

        bomb = Bomb()
        for attr in ['GET', 'POST', 'COOKIES', 'META']:
            request = HttpRequest()
            setattr(request, attr, {'bomb': bomb})
            self.assertIn('%s:<could not parse>' % attr, repr(request))

    def test_wsgirequest(self):
        request = WSGIRequest({'PATH_INFO': 'bogus', 'REQUEST_METHOD': 'bogus', 'wsgi.input': BytesIO(b'')})
        self.assertEqual(list(request.GET.keys()), [])