Skip to content

Commit e49a815

Browse files
authored
Merge pull request #30 from tannewt/add_tests
Add basic tests
2 parents 43017e3 + d426ea6 commit e49a815

File tree

8 files changed

+213
-5
lines changed

8 files changed

+213
-5
lines changed

.github/workflows/test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Run Tests
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Dump GitHub context
10+
env:
11+
GITHUB_CONTEXT: ${{ toJson(github) }}
12+
run: echo "$GITHUB_CONTEXT"
13+
- name: Set up Python 3.6
14+
uses: actions/setup-python@v1
15+
with:
16+
python-version: 3.6
17+
- name: Versions
18+
run: |
19+
python3 --version
20+
- name: Checkout Current Repo
21+
uses: actions/checkout@v1
22+
with:
23+
submodules: true
24+
- name: Install pytest
25+
run: pip install pytest
26+
- name: Install locally
27+
run: pip install .
28+
- name: Run tests
29+
run: pytest

examples/requests_advanced_ethernet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
raise AssertionError(
3636
"Failed to resolve hostname, \
3737
please check your router's DNS configuration."
38-
)
38+
) from error
3939
continue
4040
print("-" * 60)
4141

examples/requests_simpletest_ethernet.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
raise AssertionError(
3535
"Failed to resolve hostname, \
3636
please check your router's DNS configuration."
37-
)
37+
) from error
3838
continue
3939
print("-" * 40)
4040

@@ -55,7 +55,7 @@
5555
raise AssertionError(
5656
"Failed to resolve hostname, \
5757
please check your router's DNS configuration."
58-
)
58+
) from error
5959
continue
6060
print("-" * 40)
6161

@@ -77,7 +77,7 @@
7777
raise AssertionError(
7878
"Failed to resolve hostname, \
7979
please check your router's DNS configuration."
80-
)
80+
) from error
8181
continue
8282
print("-" * 40)
8383

@@ -101,7 +101,7 @@
101101
raise AssertionError(
102102
"Failed to resolve hostname, \
103103
please check your router's DNS configuration."
104-
)
104+
) from error
105105
continue
106106
print("-" * 40)
107107

tests/header_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from unittest import mock
2+
import mocket
3+
import json
4+
import adafruit_requests
5+
6+
ip = "1.2.3.4"
7+
host = "httpbin.org"
8+
response_headers = b"HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n"
9+
10+
11+
def test_json():
12+
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
13+
sock = mocket.Mocket(response_headers)
14+
mocket.socket.return_value = sock
15+
sent = []
16+
sock.send.side_effect = sent.append
17+
18+
adafruit_requests.set_socket(mocket, mocket.interface)
19+
headers = {"user-agent": "blinka/1.0.0"}
20+
r = adafruit_requests.get("http://" + host + "/get", headers=headers)
21+
22+
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
23+
sent = b"".join(sent).lower()
24+
assert b"user-agent: blinka/1.0.0\r\n" in sent
25+
# The current implementation sends two user agents. Fix it, and uncomment below.
26+
# assert sent.count(b"user-agent:") == 1

tests/mocket.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from unittest import mock
2+
3+
SOCK_STREAM = 0
4+
5+
getaddrinfo = mock.Mock()
6+
socket = mock.Mock()
7+
set_interface = mock.Mock()
8+
9+
interface = mock.MagicMock()
10+
11+
12+
class Mocket:
13+
def __init__(self, response):
14+
self.settimeout = mock.Mock()
15+
self.close = mock.Mock()
16+
self.connect = mock.Mock()
17+
self.send = mock.Mock()
18+
self.readline = mock.Mock(side_effect=self._readline)
19+
self.recv = mock.Mock(side_effect=self._recv)
20+
self._response = response
21+
self._position = 0
22+
23+
def _readline(self):
24+
i = self._response.find(b"\r\n", self._position)
25+
r = self._response[self._position : i + 2]
26+
self._position = i + 2
27+
return r
28+
29+
def _recv(self, count):
30+
end = self._position + count
31+
r = self._response[self._position : end]
32+
self._position = end
33+
return r

tests/parse_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from unittest import mock
2+
import mocket
3+
import json
4+
import adafruit_requests
5+
6+
ip = "1.2.3.4"
7+
host = "httpbin.org"
8+
response = {"Date": "July 25, 2019"}
9+
encoded = json.dumps(response).encode("utf-8")
10+
headers = "HTTP/1.0 200 OK\r\nContent-Length: {}\r\n\r\n".format(len(encoded)).encode(
11+
"utf-8"
12+
)
13+
14+
15+
def test_json():
16+
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
17+
sock = mocket.Mocket(headers + encoded)
18+
mocket.socket.return_value = sock
19+
20+
adafruit_requests.set_socket(mocket, mocket.interface)
21+
r = adafruit_requests.get("http://" + host + "/get")
22+
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
23+
assert r.json() == response

tests/post_test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from unittest import mock
2+
import mocket
3+
import json
4+
import adafruit_requests
5+
6+
ip = "1.2.3.4"
7+
host = "httpbin.org"
8+
response = {}
9+
encoded = json.dumps(response).encode("utf-8")
10+
headers = "HTTP/1.0 200 OK\r\nContent-Length: {}\r\n\r\n".format(len(encoded)).encode(
11+
"utf-8"
12+
)
13+
14+
15+
def test_method():
16+
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
17+
sock = mocket.Mocket(headers + encoded)
18+
mocket.socket.return_value = sock
19+
20+
adafruit_requests.set_socket(mocket, mocket.interface)
21+
r = adafruit_requests.post("http://" + host + "/post")
22+
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
23+
sock.send.assert_has_calls(
24+
[mock.call(b"POST /post HTTP/1.0\r\n"), mock.call(b"Host: httpbin.org\r\n")]
25+
)
26+
27+
28+
def test_string():
29+
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
30+
sock = mocket.Mocket(headers + encoded)
31+
mocket.socket.return_value = sock
32+
33+
adafruit_requests.set_socket(mocket, mocket.interface)
34+
data = "31F"
35+
r = adafruit_requests.post("http://" + host + "/post", data=data)
36+
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
37+
sock.send.assert_called_with(b"31F")
38+
39+
40+
def test_json():
41+
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
42+
sock = mocket.Mocket(headers + encoded)
43+
mocket.socket.return_value = sock
44+
45+
adafruit_requests.set_socket(mocket, mocket.interface)
46+
json_data = {"Date": "July 25, 2019"}
47+
r = adafruit_requests.post("http://" + host + "/post", json=json_data)
48+
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
49+
sock.send.assert_called_with(b'{"Date": "July 25, 2019"}')

tests/protocol_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from unittest import mock
2+
import mocket
3+
import adafruit_requests
4+
5+
ip = "1.2.3.4"
6+
host = "wifitest.adafruit.com"
7+
path = "/testwifi/index.html"
8+
text = b"This is a test of Adafruit WiFi!\r\nIf you can read this, its working :)"
9+
response = b"HTTP/1.0 200 OK\r\nContent-Length: 70\r\n\r\n" + text
10+
11+
12+
def test_get_https_text():
13+
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
14+
sock = mocket.Mocket(response)
15+
mocket.socket.return_value = sock
16+
17+
adafruit_requests.set_socket(mocket, mocket.interface)
18+
r = adafruit_requests.get("https://" + host + path)
19+
20+
sock.connect.assert_called_once_with((host, 443), mocket.interface.TLS_MODE)
21+
sock.send.assert_has_calls(
22+
[
23+
mock.call(b"GET /testwifi/index.html HTTP/1.0\r\n"),
24+
mock.call(b"Host: wifitest.adafruit.com\r\n"),
25+
]
26+
)
27+
assert r.text == str(text, "utf-8")
28+
29+
30+
def test_get_http_text():
31+
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
32+
sock = mocket.Mocket(response)
33+
mocket.socket.return_value = sock
34+
35+
adafruit_requests.set_socket(mocket, mocket.interface)
36+
r = adafruit_requests.get("http://" + host + path)
37+
38+
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
39+
sock.send.assert_has_calls(
40+
[
41+
mock.call(b"GET /testwifi/index.html HTTP/1.0\r\n"),
42+
mock.call(b"Host: wifitest.adafruit.com\r\n"),
43+
]
44+
)
45+
assert r.text == str(text, "utf-8")
46+
47+
48+
# Add a chunked response test when we support HTTP 1.1

0 commit comments

Comments
 (0)