uplink icon indicating copy to clipboard operation
uplink copied to clipboard

base_url doesn't take into consideration /

Open yiss opened this issue 4 years ago • 1 comments

Describe the bug A clear and concise description of what the bug is.

When using a base_url contains a slash / it's not taking into into consideration For example if the base_url is http://localhost/foo/bar the parsed base_url is http://localhost

To Reproduce Steps to reproduce the behavior:

from uplink import Consumer, get

class HttpBin(Consumer):
    @get("/test/one")
    def test_one(self):
        """test one"""

    @get("anything/test/one")
    def anything_test_one(self):
        """test one"""


if __name__ == '__main__':
    client_one = HttpBin(base_url="https://httpbin.org/anything")
    response = client_one.test_one()
    print(response.status_code)
    print(response.text)
    print(response.request.url)

    client_two = HttpBin(base_url="https://httpbin.org")
    response = client_two.anything_test_one()
    print(response.status_code)
    print(response.text)
    print(response.request.url)

The output :

404
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server.  If you entered the URL manually please check your spelling and try again.</p>

https://httpbin.org/test/one
200
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.25.1", 
    "X-Amzn-Trace-Id": "Root=1-602f80b2-596be12323cad3731a6af15a"
  }, 
  "json": null, 
  "method": "GET", 
  "origin": "136.252.163.126", 
  "url": "https://httpbin.org/anything/test/one"
}

https://httpbin.org/anything/test/one

Expected behavior A clear and concise description of what you expected to happen.

The base_url shouldn't be modified when creating the RequestBuilder

Additional context Add any other context about the problem here.

yiss avatar Feb 19 '21 09:02 yiss

This seems to be same as https://github.com/prkumar/uplink/issues/2 and https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urljoin

In [46]: from urllib.parse import urljoin

In [47]: urljoin("http://example.com/anything/", "anything/test")
Out[47]: 'http://example.com/anything/anything/test'

In [48]: urljoin("http://example.com/anything", "/anything/test")
Out[48]: 'http://example.com/anything/test'

In [49]: urljoin("http://example.com/anything", "anything/test")
Out[49]: 'http://example.com/anything/test'

tirkarthi avatar Mar 08 '21 09:03 tirkarthi