Skip to content

Commit

Permalink
🔖 Release 3.1.4 (#39)
Browse files Browse the repository at this point in the history
**Fixed**
- Static type checker not accepting **iterable\[str\]** for **data**. A fix in urllib3.future allows it since v2.1.902.
- Unattended override of manually provided **Authorization** if `.netrc` existed with an eligible entry.
  Taken from closed PR psf#6555 and initially raised in psf#3929

**Added**
- **oheaders** property in `Request`, and `PreparedRequest` in addition to `Response`.
  • Loading branch information
Ousret authored Oct 23, 2023
1 parent 7de4041 commit 92c7548
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ repos:
- id: mypy
args: [--check-untyped-defs]
exclude: 'tests/'
additional_dependencies: ['charset_normalizer', 'urllib3.future>=2.1.900', 'wassima>=1.0.1', 'idna', 'kiss_headers']
additional_dependencies: ['charset_normalizer', 'urllib3.future>=2.1.902', 'wassima>=1.0.1', 'idna', 'kiss_headers']
11 changes: 11 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Release History
===============

3.1.4 (2023-10-23)
------------------

**Fixed**
- Static type checker not accepting **iterable\[str\]** for **data**. A fix in urllib3.future allows it since v2.1.902.
- Unattended override of manually provided **Authorization** if `.netrc` existed with an eligible entry.
Taken from closed PR https://github.com/psf/requests/pull/6555 and initially raised in https://github.com/psf/requests/issues/3929

**Added**
- **oheaders** property in `Request`, and `PreparedRequest` in addition to `Response`.

3.1.3 (2023-10-19)
------------------

Expand Down
7 changes: 3 additions & 4 deletions docs/user/authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ Providing the credentials in a tuple like this is exactly the same as the
netrc Authentication
~~~~~~~~~~~~~~~~~~~~

If no authentication method is given with the ``auth`` argument, Niquests will
attempt to get the authentication credentials for the URL's hostname from the
user's netrc file. The netrc file overrides raw HTTP authentication headers
set with `headers=`.
If no authentication method is given with the ``auth`` argument and the
Authorization header has not been set, Requests will attempt to get the
authentication credentials for the URL's hostname from the user's netrc file.

If credentials for the hostname are found, the request is sent with HTTP Basic
Auth.
Expand Down
4 changes: 2 additions & 2 deletions src/niquests/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
__url__: str = "https://niquests.readthedocs.io"

__version__: str
__version__ = "3.1.3"
__version__ = "3.1.4"

__build__: int = 0x030103
__build__: int = 0x030104
__author__: str = "Kenneth Reitz"
__author_email__: str = "[email protected]"
__license__: str = "Apache-2.0"
Expand Down
1 change: 1 addition & 0 deletions src/niquests/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
typing.IO,
BodyFormType,
typing.Iterable[bytes],
typing.Iterable[str],
]
#: HTTP Headers can be represented through three ways. 1) typical dict, 2) internal insensitive dict, and 3) list of tuple.
HeadersType: typing.TypeAlias = typing.Union[
Expand Down
4 changes: 4 additions & 0 deletions src/niquests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ def __init__(
self.auth = auth
self.cookies = cookies

@property
def oheaders(self) -> Headers:
return parse_it(self.headers)

def __repr__(self) -> str:
return f"<Request [{self.method}]>"

Expand Down
6 changes: 5 additions & 1 deletion src/niquests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ def prepare_request(self, request: Request) -> PreparedRequest:

# Set environment's basic authentication if not explicitly set.
auth = request.auth
if self.trust_env and not auth and not self.auth:
has_authorization_set = (
"authorization" in self.headers or "authorization" in request.oheaders
)

if self.trust_env and not auth and not self.auth and not has_authorization_set:
auth = get_netrc_auth(request.url)

p = PreparedRequest()
Expand Down
8 changes: 8 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,9 @@ def get_netrc_auth_mock(url):
r = niquests.get(url, auth=wrong_auth)
assert r.status_code == 401

r = niquests.get(url, headers={"Authorization": "SHOULD DISCARD NETRC!"})
assert r.status_code == 401

s = niquests.Session()

# Should use netrc and work.
Expand All @@ -679,6 +682,11 @@ def get_netrc_auth_mock(url):
s.auth = wrong_auth
r = s.get(url)
assert r.status_code == 401

s.auth = None
s.headers["Authorization"] = "SHOULD DISCARD NETRC!"
r = s.get(url)
assert r.status_code == 401
finally:
niquests.sessions.get_netrc_auth = old_auth

Expand Down

0 comments on commit 92c7548

Please sign in to comment.