Skip to content

Add basic tests #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Run Tests

on: [pull_request, push]

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
- name: Set up Python 3.6
uses: actions/setup-python@v1
with:
python-version: 3.6
- name: Versions
run: |
python3 --version
- name: Checkout Current Repo
uses: actions/checkout@v1
with:
submodules: true
- name: Install pytest
run: pip install pytest
- name: Install locally
run: pip install .
- name: Run tests
run: pytest
2 changes: 1 addition & 1 deletion examples/requests_advanced_ethernet.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
raise AssertionError(
"Failed to resolve hostname, \
please check your router's DNS configuration."
)
) from error
continue
print("-" * 60)

Expand Down
8 changes: 4 additions & 4 deletions examples/requests_simpletest_ethernet.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
raise AssertionError(
"Failed to resolve hostname, \
please check your router's DNS configuration."
)
) from error
continue
print("-" * 40)

Expand All @@ -55,7 +55,7 @@
raise AssertionError(
"Failed to resolve hostname, \
please check your router's DNS configuration."
)
) from error
continue
print("-" * 40)

Expand All @@ -77,7 +77,7 @@
raise AssertionError(
"Failed to resolve hostname, \
please check your router's DNS configuration."
)
) from error
continue
print("-" * 40)

Expand All @@ -101,7 +101,7 @@
raise AssertionError(
"Failed to resolve hostname, \
please check your router's DNS configuration."
)
) from error
continue
print("-" * 40)

Expand Down
26 changes: 26 additions & 0 deletions tests/header_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from unittest import mock
import mocket
import json
import adafruit_requests

ip = "1.2.3.4"
host = "httpbin.org"
response_headers = b"HTTP/1.0 200 OK\r\nContent-Length: 0\r\n\r\n"


def test_json():
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(response_headers)
mocket.socket.return_value = sock
sent = []
sock.send.side_effect = sent.append

adafruit_requests.set_socket(mocket, mocket.interface)
headers = {"user-agent": "blinka/1.0.0"}
r = adafruit_requests.get("http://" + host + "/get", headers=headers)

sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
sent = b"".join(sent).lower()
assert b"user-agent: blinka/1.0.0\r\n" in sent
# The current implementation sends two user agents. Fix it, and uncomment below.
# assert sent.count(b"user-agent:") == 1
33 changes: 33 additions & 0 deletions tests/mocket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from unittest import mock

SOCK_STREAM = 0

getaddrinfo = mock.Mock()
socket = mock.Mock()
set_interface = mock.Mock()

interface = mock.MagicMock()


class Mocket:
def __init__(self, response):
self.settimeout = mock.Mock()
self.close = mock.Mock()
self.connect = mock.Mock()
self.send = mock.Mock()
self.readline = mock.Mock(side_effect=self._readline)
self.recv = mock.Mock(side_effect=self._recv)
self._response = response
self._position = 0

def _readline(self):
i = self._response.find(b"\r\n", self._position)
r = self._response[self._position : i + 2]
self._position = i + 2
return r

def _recv(self, count):
end = self._position + count
r = self._response[self._position : end]
self._position = end
return r
23 changes: 23 additions & 0 deletions tests/parse_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from unittest import mock
import mocket
import json
import adafruit_requests

ip = "1.2.3.4"
host = "httpbin.org"
response = {"Date": "July 25, 2019"}
encoded = json.dumps(response).encode("utf-8")
headers = "HTTP/1.0 200 OK\r\nContent-Length: {}\r\n\r\n".format(len(encoded)).encode(
"utf-8"
)


def test_json():
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(headers + encoded)
mocket.socket.return_value = sock

adafruit_requests.set_socket(mocket, mocket.interface)
r = adafruit_requests.get("http://" + host + "/get")
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
assert r.json() == response
49 changes: 49 additions & 0 deletions tests/post_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from unittest import mock
import mocket
import json
import adafruit_requests

ip = "1.2.3.4"
host = "httpbin.org"
response = {}
encoded = json.dumps(response).encode("utf-8")
headers = "HTTP/1.0 200 OK\r\nContent-Length: {}\r\n\r\n".format(len(encoded)).encode(
"utf-8"
)


def test_method():
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(headers + encoded)
mocket.socket.return_value = sock

adafruit_requests.set_socket(mocket, mocket.interface)
r = adafruit_requests.post("http://" + host + "/post")
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
sock.send.assert_has_calls(
[mock.call(b"POST /post HTTP/1.0\r\n"), mock.call(b"Host: httpbin.org\r\n")]
)


def test_string():
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(headers + encoded)
mocket.socket.return_value = sock

adafruit_requests.set_socket(mocket, mocket.interface)
data = "31F"
r = adafruit_requests.post("http://" + host + "/post", data=data)
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
sock.send.assert_called_with(b"31F")


def test_json():
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(headers + encoded)
mocket.socket.return_value = sock

adafruit_requests.set_socket(mocket, mocket.interface)
json_data = {"Date": "July 25, 2019"}
r = adafruit_requests.post("http://" + host + "/post", json=json_data)
sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
sock.send.assert_called_with(b'{"Date": "July 25, 2019"}')
48 changes: 48 additions & 0 deletions tests/protocol_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from unittest import mock
import mocket
import adafruit_requests

ip = "1.2.3.4"
host = "wifitest.adafruit.com"
path = "/testwifi/index.html"
text = b"This is a test of Adafruit WiFi!\r\nIf you can read this, its working :)"
response = b"HTTP/1.0 200 OK\r\nContent-Length: 70\r\n\r\n" + text


def test_get_https_text():
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(response)
mocket.socket.return_value = sock

adafruit_requests.set_socket(mocket, mocket.interface)
r = adafruit_requests.get("https://" + host + path)

sock.connect.assert_called_once_with((host, 443), mocket.interface.TLS_MODE)
sock.send.assert_has_calls(
[
mock.call(b"GET /testwifi/index.html HTTP/1.0\r\n"),
mock.call(b"Host: wifitest.adafruit.com\r\n"),
]
)
assert r.text == str(text, "utf-8")


def test_get_http_text():
mocket.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(response)
mocket.socket.return_value = sock

adafruit_requests.set_socket(mocket, mocket.interface)
r = adafruit_requests.get("http://" + host + path)

sock.connect.assert_called_once_with((ip, 80), mocket.interface.TCP_MODE)
sock.send.assert_has_calls(
[
mock.call(b"GET /testwifi/index.html HTTP/1.0\r\n"),
mock.call(b"Host: wifitest.adafruit.com\r\n"),
]
)
assert r.text == str(text, "utf-8")


# Add a chunked response test when we support HTTP 1.1