def test_second_send_fails(): pool = mocket.MocketPool() pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)), ) sock = mocket.Mocket(response + response) pool.socket.return_value = sock ssl = mocket.SSLContext() s = adafruit_requests.Session(pool, ssl) r = s.get("https://" + host + path) sock.send.assert_has_calls([ mock.call(b"testwifi/index.html"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), mock.call(b"\r\n"), ]) assert r.text == str(text, "utf-8") sock.send.side_effect = None sock.send.return_value = 0 with pytest.raises(RuntimeError): s.get("https://" + host + path + "2") sock.connect.assert_called_once_with((host, 443)) # Make sure that the socket is closed after send fails. sock.close.assert_called_once() pool.socket.assert_called_once()
def test_get_https_text(): pool = mocket.MocketPool() pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)), ) sock = mocket.Mocket(response) pool.socket.return_value = sock ssl = mocket.SSLContext() s = adafruit_requests.Session(pool, ssl) r = s.get("https://" + host + path) sock.connect.assert_called_once_with((host, 443)) sock.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html"), mock.call(b" HTTP/1.1\r\n"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), ]) assert r.text == str(text, "utf-8") # Close isn't needed but can be called to release the socket early. r.close()
def test_second_send_lies_recv_fails(): # 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) pool.socket.side_effect = [sock, sock2] ssl = mocket.SSLContext() requests_session = adafruit_requests.Session(pool, ssl) response = requests_session.get("https://" + HOST + PATH) sock.send.assert_has_calls([ mock.call(b"testwifi/index.html"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), mock.call(b"\r\n"), ]) assert response.text == str(TEXT, "utf-8") requests_session.get("https://" + HOST + PATH + "2") sock.connect.assert_called_once_with((HOST, 443)) sock2.connect.assert_called_once_with((HOST, 443)) # Make sure that the socket is closed after send fails. sock.close.assert_called_once() assert sock2.close.call_count == 0 assert pool.socket.call_count == 2
def test_get_https_text(): pool = mocket.MocketPool() pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)), ) sock = mocket.Mocket(RESPONSE) pool.socket.return_value = sock ssl = mocket.SSLContext() requests_session = adafruit_requests.Session(pool, ssl) response = requests_session.get("https://" + HOST + PATH) sock.connect.assert_called_once_with((HOST, 443)) sock.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html"), mock.call(b" HTTP/1.1\r\n"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), ]) assert response.text == str(TEXT, "utf-8") # Close isn't needed but can be called to release the socket early. response.close()
def test_connect_out_of_memory(): 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.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) sock.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html"), mock.call(b" HTTP/1.1\r\n"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), ]) assert r.text == str(text, "utf-8") r = s.get("https://" + host2 + path) sock3.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html"), mock.call(b" HTTP/1.1\r\n"), ]) sock3.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest2.adafruit.com"), ]) assert r.text == str(text, "utf-8") sock.close.assert_called_once() sock.connect.assert_called_once_with((host, 443)) sock3.connect.assert_called_once_with((host2, 443))
def test_connect_out_of_memory(): 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.socket.side_effect = [sock, sock2, sock3] sock2.connect.side_effect = MemoryError() ssl = mocket.SSLContext() requests_session = adafruit_requests.Session(pool, ssl) response = requests_session.get("https://" + HOST + PATH) sock.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html"), mock.call(b" HTTP/1.1\r\n"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), ]) assert response.text == str(TEXT, "utf-8") response = requests_session.get("https://" + HOST2 + PATH) sock3.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html"), mock.call(b" HTTP/1.1\r\n"), ]) sock3.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest2.adafruit.com"), ]) assert response.text == str(TEXT, "utf-8") sock.close.assert_called_once() sock.connect.assert_called_once_with((HOST, 443)) sock3.connect.assert_called_once_with((HOST2, 443))
def test_get_twice_after_second(): pool = mocket.MocketPool() pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)), ) sock = mocket.Mocket(response + response) pool.socket.return_value = sock ssl = mocket.SSLContext() s = adafruit_requests.Session(pool, ssl) r = s.get("https://" + host + path) sock.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html"), mock.call(b" HTTP/1.1\r\n"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), ]) r2 = s.get("https://" + host + path + "2") sock.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html2"), mock.call(b" HTTP/1.1\r\n"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), ]) sock.connect.assert_called_once_with((host, 443)) pool.socket.assert_called_once() with pytest.raises(RuntimeError): r.text == str(text, "utf-8")
def test_get_twice_after_second(): pool = mocket.MocketPool() pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)), ) sock = mocket.Mocket(RESPONSE + RESPONSE) pool.socket.return_value = sock ssl = mocket.SSLContext() requests_session = adafruit_requests.Session(pool, ssl) response = requests_session.get("https://" + HOST + PATH) sock.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html"), mock.call(b" HTTP/1.1\r\n"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), ]) requests_session.get("https://" + HOST + PATH + "2") sock.send.assert_has_calls([ mock.call(b"GET"), mock.call(b" /"), mock.call(b"testwifi/index.html2"), mock.call(b" HTTP/1.1\r\n"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), ]) sock.connect.assert_called_once_with((HOST, 443)) pool.socket.assert_called_once() with pytest.raises(RuntimeError): response.text == str(TEXT, "utf-8") # pylint: disable=expression-not-assigned
def test_second_connect_fails_oserror(): 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.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) sock.send.assert_has_calls([ mock.call(b"testwifi/index.html"), ]) sock.send.assert_has_calls([ mock.call(b"Host: "), mock.call(b"wifitest.adafruit.com"), mock.call(b"\r\n"), ]) assert r.text == str(text, "utf-8") host2 = "test.adafruit.com" s.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)) # 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