Opened 10 years ago

Closed 9 years ago

#23922 closed Bug (invalid)

Quoting problem in RequestFactory

Reported by: Berker Peksag Owned by: Berker Peksag
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Berker Peksag Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I was trying to convert middleware tests to use RequestFactory. I've converted all tests easily except CommonMiddlewareTest.test_append_slash_quoted:

    @override_settings(APPEND_SLASH=True)
    def test_append_slash_quoted(self):
        """
        Tests that URLs which require quoting are redirected to their slash
        version ok.
        """
        request = self._get_request('needsquoting#')
        r = CommonMiddleware().process_request(request)
        self.assertEqual(r.status_code, 301)
        self.assertEqual(
            r.url,
            'http://testserver/needsquoting%23/')

https://github.com/django/django/blob/master/tests/middleware/tests.py#L94

The urlparse call in the RequestFactory.generic method swallows # in the URL. The test is passing with a plain HttpRequest object.

Here is a try to fix this problem: https://github.com/berkerpeksag/django/compare/use-requestfactory

An alternative fix would be to wrap path with quote(path, safe=b"...") or with django.utils.encoding.escape_uri_path in RequestFactory.generic:

https://github.com/django/django/blob/master/django/test/client.py#L351

Change History (5)

comment:1 by Tim Graham, 10 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Berker Peksag, 10 years ago

Has patch: set
Owner: changed from nobody to Berker Peksag
Status: newassigned

comment:3 by Tim Graham, 10 years ago

Patch needs improvement: set

Question about the implementation left on the PR.

comment:4 by Tim Graham, 9 years ago

On further investigation, I think we can simply use the urlencoded version of # when updating that test to use RequestFactory like this: rf.get('/needsquoting%23'). The test passes and works as a regression test as originally intended if I remove urlquote from urlquote(new_url[1]) in CommonMiddleware.

comment:5 by Berker Peksag, 9 years ago

Patch needs improvement: unset
Resolution: invalid
Status: assignedclosed

Thanks. I agree with your analysis. I'll convert my branch to a pull request and will close PR #3645.

Note: See TracTickets for help on using tickets.
Back to Top