Skip to content

Conversation

@allrob23
Copy link
Contributor

Description

While reviewing the documentation for Python Requests, I noticed this recommendation:

The adapter will be chosen based on a longest prefix match. Be mindful prefixes such as http://localhost will also match http://localhost.other.com or http://localhost@other.com. It's recommended to terminate full hostnames with a /.

I know that the code work great and do their job, but ensuring trailing slashes would align them with the docs and make the URL prefixes more precise.

Changes

# Add this before mounting
if not uri.endswith('/'):
    uri += '/'

# Then use the updated uri
session.mount('http://{0}'.format(uri), StackInABox.hold_out('adapter'))
session.mount('https://{0}'.format(uri), StackInABox.hold_out('adapter'))

This ensures the URI always has a trailing slash, providing consistent behavior regardless of how the URI was originally provided.

@BenjamenMeyer
Copy link
Member

@allrob23 cool - I'll merge once all the checks are passing. Reviewing the build failure it seems that requests_mock dropped a module (requests_mock.compat). Would you please remove it from the requests_mock support utility? Or at least wrap it with an try/except block to ignore failure to import? Thanks!

Excerpt from the logs:

py3-requests-mock: commands[0]> python -c 'import stackinabox.util.requests_mock'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/runner/work/stackInABox/stackInABox/stackinabox/util/requests_mock/__init__.py", line 3, in <module>
    from .core import *
  File "/home/runner/work/stackInABox/stackInABox/stackinabox/util/requests_mock/core.py", line 20, in <module>
    import requests_mock.compat
ModuleNotFoundError: No module named 'requests_mock.compat'

@allrob23
Copy link
Contributor Author

Hi @BenjamenMeyer, I made a commmit, let me know if this is what you expected.

@BenjamenMeyer
Copy link
Member

Hi @BenjamenMeyer, I made a commmit, let me know if this is what you expected.

Thanks that should work

@BenjamenMeyer BenjamenMeyer merged commit f1c410f into TestInABox:master Apr 26, 2025
7 checks passed
@BenjamenMeyer
Copy link
Member

@allrob23 awesome - thanks for the contribution.

@BenjamenMeyer
Copy link
Member

@allrob23 released 0.14.0 with your changes. Thank!

@allrob23
Copy link
Contributor Author

@BenjamenMeyer thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants