Opened 5 months ago

Closed 4 months ago

#36332 closed Cleanup/optimization (fixed)

Usage notes and examples for HttpRequest.path and HttpRequest.path_info in docs are mixed up

Reported by: Kashemir001 Owned by: Kashemir001
Component: Documentation Version: 5.2
Severity: Normal Keywords: wsgi httprequest path_info
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Documentation for HttpRequest.get_full_path_info() gives the returning value example:

Example: "/minfo/music/bands/the_beatles/?print=true"

which implies that the script prefix part, here /minfo, is contained in the returning value of HttpRequest.path_info.

Now, docs for path_info itself say:

For example, if the WSGIScriptAlias for your application is set to "/minfo", then ``path`` might be "/minfo/music/bands/the_beatles/" and ``path_info`` would be "/music/bands/the_beatles/".

which implies that prefix part is not contained in HttpRequest.path_info, but in HttpRequest.path. This contradicts previous example.

Looking at the unit test for the subject, prefix is actually expected to be included in path_info. Therefore, the correct example for path_info should say:

For example, if the WSGIScriptAlias for your application is set to "/minfo", then ``path`` might be "/music/bands/the_beatles/" and ``path_info`` would be "/minfo/music/bands/the_beatles/".

The usage notes for both attributes should be updated as well, so both sections updated might look like:

``HttpRequest.path``
A string representing the path to the requested page, not including the scheme, domain, script prefix, or query string.

Example: "/music/bands/the_beatles/"

Under some web server configurations, the portion of the URL after the host name is split up into a script prefix portion and a path info portion. The ``path`` attribute always contains the path info portion of the path, no matter what web server is being used. Using this instead of ``path_info`` can make your code easier to move between test and deployment servers.

For example, If the WSGIScriptAlias for your application is set to "/minfo", then ``path`` might be "/music/bands/the_beatles/" and ``path_info`` would be "/minfo/music/bands/the_beatles/".

``HttpRequest.path_info``
The ``path_info`` attribute contains the full path, including both the script prefix portion and the path info portion of the path, unlike the ``path`` attribute.

Example: "/minfo/music/bands/the_beatles/"

Change History (8)

comment:1 by Kashemir001, 5 months ago

Summary: Usage notes and examples for HttpRequest.path_info and HttpRequest.get_full_path_info() in docs are mixed upUsage notes and examples for HttpRequest.path and HttpRequest.path_info in docs are mixed up

comment:2 by Sarah Boyce, 5 months ago

Triage Stage: UnreviewedAccepted

I agree that the examples are mixed up and we should have the following:

  • docs/ref/request-response.txt

    a b Methods  
    351351
    352352    Returns the ``path``, plus an appended query string, if applicable.
    353353
    354     Example: ``"/music/bands/the_beatles/?print=true"``
     354    Example: ``"/minfo/music/bands/the_beatles/?print=true"``
    355355
    356356.. method:: HttpRequest.get_full_path_info()
    357357
    358358    Like :meth:`get_full_path`, but uses :attr:`path_info` instead of
    359359    :attr:`path`.
    360360
    361     Example: ``"/minfo/music/bands/the_beatles/?print=true"``
     361    Example: ``"/music/bands/the_beatles/?print=true"``

The test you have linked seems to be a misleading test, the prefix is in path rather than path_info see:

Would you like to create a PR?

Version 1, edited 5 months ago by Sarah Boyce (previous) (next) (diff)

in reply to:  2 comment:3 by Kashemir001, 5 months ago

Replying to Sarah Boyce:

Would you like to create a PR?
It might make sense to also update the linked test to be less misleading

Alright, I'll give it a go. Will switching attribute values and corresponding asserts be enough for the tests? Something about:

  • tests/requests_tests/tests.py

    a b  
    5454   def test_httprequest_full_path(self):
    5555       request = HttpRequest()
    56        request.path = "/;some/?awful/=path/foo:bar/"
    57        request.path_info = "/prefix" + request.path
     56       request.path_info = "/;some/?awful/=path/foo:bar/"
     57       request.path = "/prefix" + request.path_info
    5858       request.META["QUERY_STRING"] = ";some=query&+query=string"
    5959       expected = "/%3Bsome/%3Fawful/%3Dpath/foo:bar/?;some=query&+query=string"
    60        self.assertEqual(request.get_full_path(), expected)
    61        self.assertEqual(request.get_full_path_info(), "/prefix" + expected)
     60       self.assertEqual(request.get_full_path_info(), expected)
     61       self.assertEqual(request.get_full_path(), "/prefix" + expected)
    6262
    6363   def test_httprequest_full_path_with_query_string_and_fragment(self):
    6464       request = HttpRequest()
    65        request.path = "/foo#bar"
    66        request.path_info = "/prefix" + request.path
     65       request.path_info = "/foo#bar"
     66       request.path = "/prefix" + request.path_info
    6767       request.META["QUERY_STRING"] = "baz#quux"
    68        self.assertEqual(request.get_full_path(), "/foo%23bar?baz#quux")
    69        self.assertEqual(request.get_full_path_info(), "/prefix/foo%23bar?baz#quux")
     68       self.assertEqual(request.get_full_path_info(), "/foo%23bar?baz#quux")
     69       self.assertEqual(request.get_full_path(), "/prefix/foo%23bar?baz#quux")
    7070

comment:4 by Kashemir001, 5 months ago

Owner: set to Kashemir001
Status: newassigned

comment:5 by Kashemir001, 5 months ago

Has patch: set

comment:6 by Kashemir001, 5 months ago

comment:7 by Sarah Boyce, 4 months ago

Triage Stage: AcceptedReady for checkin

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 4 months ago

Resolution: fixed
Status: assignedclosed

In 96c79be4:

Fixed #36332 -- Corrected HttpRequest.get_full_path() and HttpRequest.get_full_path_info() examples.

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