Skip to content

Commit c55425e

Browse files
authored
Merge pull request #76 from askpatrickw/fix-48
Fixes #48
2 parents eb116da + c0509bf commit c55425e

13 files changed

+328
-281
lines changed

.pre-commit-config.yaml

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repos:
2323
- id: pylint
2424
name: pylint (library code)
2525
types: [python]
26-
exclude: "^(docs/|examples/|setup.py$)"
26+
exclude: "^(docs/|examples/|tests/|setup.py$)"
2727
- repo: local
2828
hooks:
2929
- id: pylint_examples
@@ -32,3 +32,11 @@ repos:
3232
entry: /usr/bin/env bash -c
3333
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
3434
language: system
35+
- repo: local
36+
hooks:
37+
- id: pylint_tests
38+
name: pylint (tests code)
39+
description: Run pylint rules on "tests/*.py" files
40+
entry: /usr/bin/env bash -c
41+
args: ['([[ ! -d "tests" ]] || for example in $(find . -path "./tests/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
42+
language: system

adafruit_requests.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,12 @@ def wrap_socket(self, socket, server_hostname=None):
652652
def set_socket(sock, iface=None):
653653
"""Legacy API for setting the socket and network interface. Use a `Session` instead."""
654654
global _default_session # pylint: disable=global-statement,invalid-name
655-
_default_session = Session(sock, _FakeSSLContext(iface))
656-
if iface:
657-
sock.set_interface(iface)
655+
if not iface:
656+
# pylint: disable=protected-access
657+
_default_session = Session(sock, _FakeSSLContext(sock._the_interface))
658+
else:
659+
_default_session = Session(sock, _FakeSSLContext(iface))
660+
sock.set_interface(iface)
658661

659662

660663
def request(method, url, data=None, json=None, headers=None, stream=False, timeout=1):

examples/requests_simpletest.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
# esp32_ready = DigitalInOut(board.D10)
3030
# esp32_reset = DigitalInOut(board.D5)
3131

32+
# If you have an AirLift Featherwing or ItsyBitsy Airlift:
33+
# esp32_cs = DigitalInOut(board.D13)
34+
# esp32_ready = DigitalInOut(board.D11)
35+
# esp32_reset = DigitalInOut(board.D12)
36+
3237
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
3338
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
3439

@@ -46,8 +51,8 @@
4651
requests.set_socket(socket, esp)
4752

4853
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
49-
JSON_GET_URL = "http://httpbin.org/get"
50-
JSON_POST_URL = "http://httpbin.org/post"
54+
JSON_GET_URL = "https://httpbin.org/get"
55+
JSON_POST_URL = "https://httpbin.org/post"
5156

5257
print("Fetching text from %s" % TEXT_URL)
5358
response = requests.get(TEXT_URL)

tests/chunk_test.py

+43-34
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5+
""" Chunk Tests """
6+
57
from unittest import mock
68
import mocket
79
import adafruit_requests
810

9-
ip = "1.2.3.4"
10-
host = "wifitest.adafruit.com"
11-
path = "/testwifi/index.html"
12-
text = b"This is a test of Adafruit WiFi!\r\nIf you can read this, its working :)"
13-
headers = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
14-
headers_extra_space = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
11+
IP = "1.2.3.4"
12+
HOST = "wifitest.adafruit.com"
13+
PATH = "/testwifi/index.html"
14+
TEXT = b"This is a test of Adafruit WiFi!\r\nIf you can read this, its working :)"
15+
HEADERS = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
16+
HEADERS_EXTRA_SPACE = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
1517

1618

1719
def _chunk(response, split, extra=b""):
@@ -36,18 +38,20 @@ def _chunk(response, split, extra=b""):
3638
return chunked
3739

3840

39-
def do_test_get_text(extra=b""):
41+
def do_test_get_text(
42+
extra=b"",
43+
):
4044
pool = mocket.MocketPool()
41-
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
42-
c = _chunk(text, 33, extra)
43-
print(c)
44-
sock = mocket.Mocket(headers + c)
45+
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
46+
chunk = _chunk(TEXT, 33, extra)
47+
print(chunk)
48+
sock = mocket.Mocket(HEADERS + chunk)
4549
pool.socket.return_value = sock
4650

47-
s = adafruit_requests.Session(pool)
48-
r = s.get("http://" + host + path)
51+
requests_session = adafruit_requests.Session(pool)
52+
response = requests_session.get("http://" + HOST + PATH)
4953

50-
sock.connect.assert_called_once_with((ip, 80))
54+
sock.connect.assert_called_once_with((IP, 80))
5155

5256
sock.send.assert_has_calls(
5357
[
@@ -63,7 +67,7 @@ def do_test_get_text(extra=b""):
6367
mock.call(b"wifitest.adafruit.com"),
6468
]
6569
)
66-
assert r.text == str(text, "utf-8")
70+
assert response.text == str(TEXT, "utf-8")
6771

6872

6973
def test_get_text():
@@ -74,19 +78,22 @@ def test_get_text_extra():
7478
do_test_get_text(b";blahblah; blah")
7579

7680

77-
def do_test_close_flush(extra=b""):
78-
"""Test that a chunked response can be closed even when the request contents were not accessed."""
81+
def do_test_close_flush(
82+
extra=b"",
83+
):
84+
"""Test that a chunked response can be closed even when the
85+
request contents were not accessed."""
7986
pool = mocket.MocketPool()
80-
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
81-
c = _chunk(text, 33, extra)
82-
print(c)
83-
sock = mocket.Mocket(headers + c)
87+
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
88+
chunk = _chunk(TEXT, 33, extra)
89+
print(chunk)
90+
sock = mocket.Mocket(HEADERS + chunk)
8491
pool.socket.return_value = sock
8592

86-
s = adafruit_requests.Session(pool)
87-
r = s.get("http://" + host + path)
93+
requests_session = adafruit_requests.Session(pool)
94+
response = requests_session.get("http://" + HOST + PATH)
8895

89-
sock.connect.assert_called_once_with((ip, 80))
96+
sock.connect.assert_called_once_with((IP, 80))
9097

9198
sock.send.assert_has_calls(
9299
[
@@ -103,7 +110,7 @@ def do_test_close_flush(extra=b""):
103110
]
104111
)
105112

106-
r.close()
113+
response.close()
107114

108115

109116
def test_close_flush():
@@ -114,18 +121,20 @@ def test_close_flush_extra():
114121
do_test_close_flush(b";blahblah; blah")
115122

116123

117-
def do_test_get_text_extra_space(extra=b""):
124+
def do_test_get_text_extra_space(
125+
extra=b"",
126+
):
118127
pool = mocket.MocketPool()
119-
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
120-
c = _chunk(text, 33, extra)
121-
print(c)
122-
sock = mocket.Mocket(headers_extra_space + c)
128+
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
129+
chunk = _chunk(TEXT, 33, extra)
130+
print(chunk)
131+
sock = mocket.Mocket(HEADERS_EXTRA_SPACE + chunk)
123132
pool.socket.return_value = sock
124133

125-
s = adafruit_requests.Session(pool)
126-
r = s.get("http://" + host + path)
134+
requests_session = adafruit_requests.Session(pool)
135+
response = requests_session.get("http://" + HOST + PATH)
127136

128-
sock.connect.assert_called_once_with((ip, 80))
137+
sock.connect.assert_called_once_with((IP, 80))
129138

130139
sock.send.assert_has_calls(
131140
[
@@ -141,4 +150,4 @@ def do_test_get_text_extra_space(extra=b""):
141150
mock.call(b"wifitest.adafruit.com"),
142151
]
143152
)
144-
assert r.text == str(text, "utf-8")
153+
assert response.text == str(TEXT, "utf-8")

tests/concurrent_test.py

+33-34
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,35 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5+
""" Concurrent Tests """
6+
7+
import errno
58
from unittest import mock
69
import mocket
7-
import pytest
8-
import errno
910
import adafruit_requests
1011

11-
ip = "1.2.3.4"
12-
host = "wifitest.adafruit.com"
13-
host2 = "wifitest2.adafruit.com"
14-
path = "/testwifi/index.html"
15-
text = b"This is a test of Adafruit WiFi!\r\nIf you can read this, its working :)"
16-
response = b"HTTP/1.0 200 OK\r\nContent-Length: 70\r\n\r\n" + text
12+
IP = "1.2.3.4"
13+
HOST = "wifitest.adafruit.com"
14+
HOST2 = "test.adafruit.com"
15+
PATH = "/testwifi/index.html"
16+
TEXT = b"This is a test of Adafruit WiFi!\r\nIf you can read this, its working :)"
17+
RESPONSE = b"HTTP/1.0 200 OK\r\nContent-Length: 70\r\n\r\n" + TEXT
1718

1819

19-
def test_second_connect_fails_memoryerror():
20+
def test_second_connect_fails_memoryerror(): # pylint: disable=invalid-name
2021
pool = mocket.MocketPool()
21-
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
22-
sock = mocket.Mocket(response)
23-
sock2 = mocket.Mocket(response)
24-
sock3 = mocket.Mocket(response)
22+
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
23+
sock = mocket.Mocket(RESPONSE)
24+
sock2 = mocket.Mocket(RESPONSE)
25+
sock3 = mocket.Mocket(RESPONSE)
2526
pool.socket.call_count = 0 # Reset call count
2627
pool.socket.side_effect = [sock, sock2, sock3]
2728
sock2.connect.side_effect = MemoryError()
2829

2930
ssl = mocket.SSLContext()
3031

31-
s = adafruit_requests.Session(pool, ssl)
32-
r = s.get("https://" + host + path)
32+
requests_session = adafruit_requests.Session(pool, ssl)
33+
response = requests_session.get("https://" + HOST + PATH)
3334

3435
sock.send.assert_has_calls(
3536
[
@@ -44,35 +45,34 @@ def test_second_connect_fails_memoryerror():
4445
mock.call(b"\r\n"),
4546
]
4647
)
47-
assert r.text == str(text, "utf-8")
48+
assert response.text == str(TEXT, "utf-8")
4849

49-
host2 = "test.adafruit.com"
50-
s.get("https://" + host2 + path)
50+
requests_session.get("https://" + HOST2 + PATH)
5151

52-
sock.connect.assert_called_once_with((host, 443))
53-
sock2.connect.assert_called_once_with((host2, 443))
54-
sock3.connect.assert_called_once_with((host2, 443))
52+
sock.connect.assert_called_once_with((HOST, 443))
53+
sock2.connect.assert_called_once_with((HOST2, 443))
54+
sock3.connect.assert_called_once_with((HOST2, 443))
5555
# Make sure that the socket is closed after send fails.
5656
sock.close.assert_called_once()
5757
sock2.close.assert_called_once()
5858
assert sock3.close.call_count == 0
5959
assert pool.socket.call_count == 3
6060

6161

62-
def test_second_connect_fails_oserror():
62+
def test_second_connect_fails_oserror(): # pylint: disable=invalid-name
6363
pool = mocket.MocketPool()
64-
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
65-
sock = mocket.Mocket(response)
66-
sock2 = mocket.Mocket(response)
67-
sock3 = mocket.Mocket(response)
64+
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
65+
sock = mocket.Mocket(RESPONSE)
66+
sock2 = mocket.Mocket(RESPONSE)
67+
sock3 = mocket.Mocket(RESPONSE)
6868
pool.socket.call_count = 0 # Reset call count
6969
pool.socket.side_effect = [sock, sock2, sock3]
7070
sock2.connect.side_effect = OSError(errno.ENOMEM)
7171

7272
ssl = mocket.SSLContext()
7373

74-
s = adafruit_requests.Session(pool, ssl)
75-
r = s.get("https://" + host + path)
74+
requests_session = adafruit_requests.Session(pool, ssl)
75+
response = requests_session.get("https://" + HOST + PATH)
7676

7777
sock.send.assert_has_calls(
7878
[
@@ -87,14 +87,13 @@ def test_second_connect_fails_oserror():
8787
mock.call(b"\r\n"),
8888
]
8989
)
90-
assert r.text == str(text, "utf-8")
90+
assert response.text == str(TEXT, "utf-8")
9191

92-
host2 = "test.adafruit.com"
93-
s.get("https://" + host2 + path)
92+
requests_session.get("https://" + HOST2 + PATH)
9493

95-
sock.connect.assert_called_once_with((host, 443))
96-
sock2.connect.assert_called_once_with((host2, 443))
97-
sock3.connect.assert_called_once_with((host2, 443))
94+
sock.connect.assert_called_once_with((HOST, 443))
95+
sock2.connect.assert_called_once_with((HOST2, 443))
96+
sock3.connect.assert_called_once_with((HOST2, 443))
9897
# Make sure that the socket is closed after send fails.
9998
sock.close.assert_called_once()
10099
sock2.close.assert_called_once()

tests/header_test.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@
22
#
33
# SPDX-License-Identifier: Unlicense
44

5-
from unittest import mock
5+
""" Header Tests """
6+
67
import mocket
7-
import json
88
import adafruit_requests
99

10-
ip = "1.2.3.4"
11-
host = "httpbin.org"
12-
response_headers = b"HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n"
10+
IP = "1.2.3.4"
11+
HOST = "httpbin.org"
12+
RESPONSE_HEADERS = b"HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n"
1313

1414

1515
def test_json():
1616
pool = mocket.MocketPool()
17-
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
18-
sock = mocket.Mocket(response_headers)
17+
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
18+
sock = mocket.Mocket(RESPONSE_HEADERS)
1919
pool.socket.return_value = sock
2020
sent = []
2121

2222
def _send(data):
23-
sent.append(data)
23+
sent.append(data) # pylint: disable=no-member
2424
return len(data)
2525

2626
sock.send.side_effect = _send
2727

28-
s = adafruit_requests.Session(pool)
28+
requests_session = adafruit_requests.Session(pool)
2929
headers = {"user-agent": "blinka/1.0.0"}
30-
r = s.get("http://" + host + "/get", headers=headers)
30+
requests_session.get("http://" + HOST + "/get", headers=headers)
3131

32-
sock.connect.assert_called_once_with((ip, 80))
32+
sock.connect.assert_called_once_with((IP, 80))
3333
sent = b"".join(sent).lower()
3434
assert b"user-agent: blinka/1.0.0\r\n" in sent
3535
# The current implementation sends two user agents. Fix it, and uncomment below.

0 commit comments

Comments
 (0)