Skip to content

Commit e6beaa9

Browse files
committed
requests: Expand tests and fix MicroPython compatibility.
Order-independent mock asserts, live TCP tests, unix/ESP32 runners, prefer /lib over frozen requests on ESP32, and HEAD/auth/encoding fixes. Signed-off-by: Pablo Ventura <pablogventura@gmail.com>
1 parent 60bf1bc commit e6beaa9

6 files changed

Lines changed: 349 additions & 89 deletions

File tree

python-ecosys/requests/requests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def request(
6363
import binascii
6464

6565
username, password = auth
66-
formatted = (username + ":" + password).encode()
66+
formatted = _to_bytes(username) + b":" + _to_bytes(password)
6767
formatted = str(binascii.b2a_base64(formatted)[:-1], "ascii")
6868
headers["Authorization"] = "Basic {}".format(formatted)
6969

@@ -193,7 +193,7 @@ def request(
193193
max_body,
194194
)
195195

196-
if method == "HEAD":
196+
if method == b"HEAD":
197197
body = b""
198198
else:
199199
body = read_body(s, resp_headers, max_body)

python-ecosys/requests/requests/_http.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ def read_body(stream, headers, max_body=None):
108108
"""Read the response body according to Content-Length or chunked encoding."""
109109
max_remaining = max_body
110110
encoding = headers.get("transfer-encoding", "")
111-
if encoding and "chunked" in encoding.lower():
112-
body, _ = _read_chunked(stream, max_remaining)
113-
return body
111+
if encoding:
112+
enc = encoding if isinstance(encoding, str) else str(encoding, "utf-8")
113+
if "chunked" in enc.lower():
114+
body, _ = _read_chunked(stream, max_remaining)
115+
return body
114116
content_length = headers.get("content-length")
115117
if content_length is not None:
116118
length = int(content_length)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
# Deploy requests package to ESP32 and run mock + live tests via mpremote.
3+
set -euo pipefail
4+
5+
PORT="${PORT:-/dev/ttyACM0}"
6+
MPREMOTE="${MPREMOTE:-pipx run mpremote}"
7+
ROOT="$(cd "$(dirname "$0")" && pwd)"
8+
9+
run_mp() {
10+
$MPREMOTE connect "$PORT" "$@"
11+
}
12+
13+
cd "$ROOT"
14+
echo "== Deploy requests to :lib/requests =="
15+
run_mp fs mkdir :lib 2>/dev/null || true
16+
run_mp fs mkdir :lib/requests 2>/dev/null || true
17+
run_mp fs cp requests/__init__.py :lib/requests/__init__.py
18+
run_mp fs cp requests/_http.py :lib/requests/_http.py
19+
20+
echo "== Mock tests =="
21+
run_mp run test_requests.py
22+
23+
echo "== Live tests =="
24+
run_mp run test_requests_live.py
25+
26+
echo "== ESP32 tests passed =="
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
# Run requests package tests on MicroPython unix port (mock + live).
3+
set -euo pipefail
4+
5+
MICROPYTHON="${MICROPYTHON:-../../../micropython/ports/unix/build-standard/micropython}"
6+
ROOT="$(cd "$(dirname "$0")" && pwd)"
7+
8+
if [[ ! -x "$MICROPYTHON" ]]; then
9+
echo "Build unix port first:" >&2
10+
echo " cd micropython/ports/unix && make submodules && make" >&2
11+
exit 1
12+
fi
13+
14+
cd "$ROOT"
15+
echo "== Mock tests (test_requests.py) =="
16+
"$MICROPYTHON" test_requests.py
17+
18+
echo "== Live tests (test_requests_live.py) =="
19+
"$MICROPYTHON" test_requests_live.py
20+
21+
echo "== All unix tests passed =="

0 commit comments

Comments
 (0)