Skip to content

Commit

Permalink
Merge branch 'main' into formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Apr 28, 2022
2 parents 0e51d67 + 2d55176 commit 777e460
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions requests/models.py
Expand Up @@ -965,6 +965,8 @@ def json(self, **kwargs):
# and the server didn't bother to tell us what codec *was*
# used.
pass
except JSONDecodeError as e:
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

try:
return complexjson.loads(self.text, **kwargs)
Expand Down
24 changes: 23 additions & 1 deletion tests/test_lowlevel.py
Expand Up @@ -4,7 +4,6 @@
from tests.testserver.server import Server, consume_socket_content

import requests

from .utils import override_environ


Expand Down Expand Up @@ -403,3 +402,26 @@ def response_handler(sock):
assert r.url == f'http://{host}:{port}/final-url/#relevant-section'

close_server.set()

def test_json_decode_compatibility_for_alt_utf_encodings():

def response_handler(sock):
consume_socket_content(sock, timeout=0.5)
sock.send(
b'HTTP/1.1 200 OK\r\n'
b'Content-Length: 18\r\n\r\n'
b'\xff\xfe{\x00"\x00K0"\x00=\x00"\x00\xab0"\x00\r\n'
)

close_server = threading.Event()
server = Server(response_handler, wait_to_close_event=close_server)

with server as (host, port):
url = 'http://{}:{}/'.format(host, port)
r = requests.get(url)
r.encoding = None
with pytest.raises(requests.exceptions.JSONDecodeError) as excinfo:
r.json()
assert isinstance(excinfo.value, requests.exceptions.RequestException)
assert isinstance(excinfo.value, JSONDecodeError)
assert r.text not in str(excinfo.value)

0 comments on commit 777e460

Please sign in to comment.