Skip to content

Fixes #48 #76

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
Mar 29, 2021
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
10 changes: 9 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ repos:
- id: pylint
name: pylint (library code)
types: [python]
exclude: "^(docs/|examples/|setup.py$)"
exclude: "^(docs/|examples/|tests/|setup.py$)"
- repo: local
hooks:
- id: pylint_examples
Expand All @@ -32,3 +32,11 @@ repos:
entry: /usr/bin/env bash -c
args: ['([[ ! -d "examples" ]] || for example in $(find . -path "./examples/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
language: system
- repo: local
hooks:
- id: pylint_tests
name: pylint (tests code)
description: Run pylint rules on "tests/*.py" files
entry: /usr/bin/env bash -c
args: ['([[ ! -d "tests" ]] || for example in $(find . -path "./tests/*.py"); do pylint --disable=missing-docstring,invalid-name $example; done)']
language: system
9 changes: 6 additions & 3 deletions adafruit_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,12 @@ def wrap_socket(self, socket, server_hostname=None):
def set_socket(sock, iface=None):
"""Legacy API for setting the socket and network interface. Use a `Session` instead."""
global _default_session # pylint: disable=global-statement,invalid-name
_default_session = Session(sock, _FakeSSLContext(iface))
if iface:
sock.set_interface(iface)
if not iface:
# pylint: disable=protected-access
_default_session = Session(sock, _FakeSSLContext(sock._the_interface))
else:
_default_session = Session(sock, _FakeSSLContext(iface))
sock.set_interface(iface)


def request(method, url, data=None, json=None, headers=None, stream=False, timeout=1):
Expand Down
9 changes: 7 additions & 2 deletions examples/requests_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

# If you have an AirLift Featherwing or ItsyBitsy Airlift:
# esp32_cs = DigitalInOut(board.D13)
# esp32_ready = DigitalInOut(board.D11)
# esp32_reset = DigitalInOut(board.D12)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

Expand All @@ -46,8 +51,8 @@
requests.set_socket(socket, esp)

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_GET_URL = "http://httpbin.org/get"
JSON_POST_URL = "http://httpbin.org/post"
JSON_GET_URL = "https://httpbin.org/get"
JSON_POST_URL = "https://httpbin.org/post"

print("Fetching text from %s" % TEXT_URL)
response = requests.get(TEXT_URL)
Expand Down
77 changes: 43 additions & 34 deletions tests/chunk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#
# SPDX-License-Identifier: Unlicense

""" Chunk Tests """

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 :)"
headers = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
headers_extra_space = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
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 :)"
HEADERS = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"
HEADERS_EXTRA_SPACE = b"HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"


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


def do_test_get_text(extra=b""):
def do_test_get_text(
extra=b"",
):
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
c = _chunk(text, 33, extra)
print(c)
sock = mocket.Mocket(headers + c)
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
chunk = _chunk(TEXT, 33, extra)
print(chunk)
sock = mocket.Mocket(HEADERS + chunk)
pool.socket.return_value = sock

s = adafruit_requests.Session(pool)
r = s.get("http://" + host + path)
requests_session = adafruit_requests.Session(pool)
response = requests_session.get("http://" + HOST + PATH)

sock.connect.assert_called_once_with((ip, 80))
sock.connect.assert_called_once_with((IP, 80))

sock.send.assert_has_calls(
[
Expand All @@ -63,7 +67,7 @@ def do_test_get_text(extra=b""):
mock.call(b"wifitest.adafruit.com"),
]
)
assert r.text == str(text, "utf-8")
assert response.text == str(TEXT, "utf-8")


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


def do_test_close_flush(extra=b""):
"""Test that a chunked response can be closed even when the request contents were not accessed."""
def do_test_close_flush(
extra=b"",
):
"""Test that a chunked response can be closed even when the
request contents were not accessed."""
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
c = _chunk(text, 33, extra)
print(c)
sock = mocket.Mocket(headers + c)
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
chunk = _chunk(TEXT, 33, extra)
print(chunk)
sock = mocket.Mocket(HEADERS + chunk)
pool.socket.return_value = sock

s = adafruit_requests.Session(pool)
r = s.get("http://" + host + path)
requests_session = adafruit_requests.Session(pool)
response = requests_session.get("http://" + HOST + PATH)

sock.connect.assert_called_once_with((ip, 80))
sock.connect.assert_called_once_with((IP, 80))

sock.send.assert_has_calls(
[
Expand All @@ -103,7 +110,7 @@ def do_test_close_flush(extra=b""):
]
)

r.close()
response.close()


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


def do_test_get_text_extra_space(extra=b""):
def do_test_get_text_extra_space(
extra=b"",
):
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
c = _chunk(text, 33, extra)
print(c)
sock = mocket.Mocket(headers_extra_space + c)
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
chunk = _chunk(TEXT, 33, extra)
print(chunk)
sock = mocket.Mocket(HEADERS_EXTRA_SPACE + chunk)
pool.socket.return_value = sock

s = adafruit_requests.Session(pool)
r = s.get("http://" + host + path)
requests_session = adafruit_requests.Session(pool)
response = requests_session.get("http://" + HOST + PATH)

sock.connect.assert_called_once_with((ip, 80))
sock.connect.assert_called_once_with((IP, 80))

sock.send.assert_has_calls(
[
Expand All @@ -141,4 +150,4 @@ def do_test_get_text_extra_space(extra=b""):
mock.call(b"wifitest.adafruit.com"),
]
)
assert r.text == str(text, "utf-8")
assert response.text == str(TEXT, "utf-8")
67 changes: 33 additions & 34 deletions tests/concurrent_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,35 @@
#
# SPDX-License-Identifier: Unlicense

""" Concurrent Tests """

import errno
from unittest import mock
import mocket
import pytest
import errno
import adafruit_requests

ip = "1.2.3.4"
host = "wifitest.adafruit.com"
host2 = "wifitest2.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
IP = "1.2.3.4"
HOST = "wifitest.adafruit.com"
HOST2 = "test.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_second_connect_fails_memoryerror():
def test_second_connect_fails_memoryerror(): # pylint: disable=invalid-name
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(response)
sock2 = mocket.Mocket(response)
sock3 = mocket.Mocket(response)
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
sock = mocket.Mocket(RESPONSE)
sock2 = mocket.Mocket(RESPONSE)
sock3 = mocket.Mocket(RESPONSE)
pool.socket.call_count = 0 # Reset call count
pool.socket.side_effect = [sock, sock2, sock3]
sock2.connect.side_effect = MemoryError()

ssl = mocket.SSLContext()

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

sock.send.assert_has_calls(
[
Expand All @@ -44,35 +45,34 @@ def test_second_connect_fails_memoryerror():
mock.call(b"\r\n"),
]
)
assert r.text == str(text, "utf-8")
assert response.text == str(TEXT, "utf-8")

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

sock.connect.assert_called_once_with((host, 443))
sock2.connect.assert_called_once_with((host2, 443))
sock3.connect.assert_called_once_with((host2, 443))
sock.connect.assert_called_once_with((HOST, 443))
sock2.connect.assert_called_once_with((HOST2, 443))
sock3.connect.assert_called_once_with((HOST2, 443))
# Make sure that the socket is closed after send fails.
sock.close.assert_called_once()
sock2.close.assert_called_once()
assert sock3.close.call_count == 0
assert pool.socket.call_count == 3


def test_second_connect_fails_oserror():
def test_second_connect_fails_oserror(): # pylint: disable=invalid-name
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(response)
sock2 = mocket.Mocket(response)
sock3 = mocket.Mocket(response)
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
sock = mocket.Mocket(RESPONSE)
sock2 = mocket.Mocket(RESPONSE)
sock3 = mocket.Mocket(RESPONSE)
pool.socket.call_count = 0 # Reset call count
pool.socket.side_effect = [sock, sock2, sock3]
sock2.connect.side_effect = OSError(errno.ENOMEM)

ssl = mocket.SSLContext()

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

sock.send.assert_has_calls(
[
Expand All @@ -87,14 +87,13 @@ def test_second_connect_fails_oserror():
mock.call(b"\r\n"),
]
)
assert r.text == str(text, "utf-8")
assert response.text == str(TEXT, "utf-8")

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

sock.connect.assert_called_once_with((host, 443))
sock2.connect.assert_called_once_with((host2, 443))
sock3.connect.assert_called_once_with((host2, 443))
sock.connect.assert_called_once_with((HOST, 443))
sock2.connect.assert_called_once_with((HOST2, 443))
sock3.connect.assert_called_once_with((HOST2, 443))
# Make sure that the socket is closed after send fails.
sock.close.assert_called_once()
sock2.close.assert_called_once()
Expand Down
22 changes: 11 additions & 11 deletions tests/header_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
#
# SPDX-License-Identifier: Unlicense

from unittest import mock
""" Header Tests """

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"
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():
pool = mocket.MocketPool()
pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)),)
sock = mocket.Mocket(response_headers)
pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
sock = mocket.Mocket(RESPONSE_HEADERS)
pool.socket.return_value = sock
sent = []

def _send(data):
sent.append(data)
sent.append(data) # pylint: disable=no-member
return len(data)

sock.send.side_effect = _send

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

sock.connect.assert_called_once_with((ip, 80))
sock.connect.assert_called_once_with((IP, 80))
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.
Expand Down
Loading