Beispiel #1
0
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_second_send_fails():
    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()

    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.fail_next_send = True
    s.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
Beispiel #3
0
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()
Beispiel #4
0
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.socket.return_value = sock

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

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

    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")
Beispiel #5
0
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.socket.return_value = sock

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

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

    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"),
        ]
    )

    r.close()
def do_test_get_text(
    extra=b"",
):
    pool = mocket.MocketPool()
    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

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

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

    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")
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)),)
    chunk = _chunk(TEXT, 33, extra)
    print(chunk)
    sock = mocket.Mocket(HEADERS + chunk)
    pool.socket.return_value = sock

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

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

    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"),
        ]
    )

    response.close()
Beispiel #8
0
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()
Beispiel #9
0
def test_get_https_no_ssl():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)), )
    sock = mocket.Mocket(RESPONSE)
    pool.socket.return_value = sock

    requests_session = adafruit_requests.Session(pool)
    with pytest.raises(RuntimeError):
        requests_session.get("https://" + HOST + PATH)
Beispiel #10
0
def test_get_https_no_ssl():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)), )
    sock = mocket.Mocket(response)
    pool.socket.return_value = sock

    s = adafruit_requests.Session(pool)
    with pytest.raises(RuntimeError):
        r = s.get("https://" + host + path)
Beispiel #11
0
def test_json():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)),)
    sock = mocket.Mocket(HEADERS + ENCODED)
    pool.socket.return_value = sock

    requests_session = adafruit_requests.Session(pool)
    response = requests_session.get("http://" + HOST + "/get")
    sock.connect.assert_called_once_with((IP, 80))
    assert response.json() == RESPONSE
def test_json():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)), )
    sock = mocket.Mocket(headers + encoded)
    pool.socket.return_value = sock

    s = adafruit_requests.Session(pool)
    r = s.get("http://" + host + "/get")
    sock.connect.assert_called_once_with((ip, 80))
    assert r.json() == response
def test_json():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)), )
    sock = mocket.Mocket(HEADERS + ENCODED)
    pool.socket.return_value = sock

    requests_session = adafruit_requests.Session(pool)
    json_data = {"Date": "July 25, 2019"}
    requests_session.post("http://" + HOST + "/post", json=json_data)
    sock.connect.assert_called_once_with((IP, 80))
    sock.send.assert_called_with(b'{"Date": "July 25, 2019"}')
def test_string():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)), )
    sock = mocket.Mocket(headers + encoded)
    pool.socket.return_value = sock

    s = adafruit_requests.Session(pool)
    data = "31F"
    r = s.post("http://" + host + "/post", data=data)
    sock.connect.assert_called_once_with((ip, 80))
    sock.send.assert_called_with(b"31F")
def test_string():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)), )
    sock = mocket.Mocket(HEADERS + ENCODED)
    pool.socket.return_value = sock

    requests_session = adafruit_requests.Session(pool)
    data = "31F"
    requests_session.post("http://" + HOST + "/post", data=data)
    sock.connect.assert_called_once_with((IP, 80))
    sock.send.assert_called_with(b"31F")
def test_json():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)), )
    sock = mocket.Mocket(headers + encoded)
    pool.socket.return_value = sock

    s = adafruit_requests.Session(pool)
    json_data = {"Date": "July 25, 2019"}
    r = s.post("http://" + host + "/post", json=json_data)
    sock.connect.assert_called_once_with((ip, 80))
    sock.send.assert_called_with(b'{"Date": "July 25, 2019"}')
Beispiel #17
0
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_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_json():
    pool = mocket.MocketPool()
    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)  # pylint: disable=no-member
        return len(data)

    sock.send.side_effect = _send

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

    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
def test_method():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (IP, 80)), )
    sock = mocket.Mocket(HEADERS + ENCODED)
    pool.socket.return_value = sock

    requests_session = adafruit_requests.Session(pool)
    requests_session.post("http://" + HOST + "/post")
    sock.connect.assert_called_once_with((IP, 80))

    sock.send.assert_has_calls([
        mock.call(b"POST"),
        mock.call(b" /"),
        mock.call(b"post"),
        mock.call(b" HTTP/1.1\r\n"),
    ])
    sock.send.assert_has_calls([
        mock.call(b"Host: "),
        mock.call(b"httpbin.org"),
    ])
Beispiel #21
0
def test_json():
    pool = mocket.MocketPool()
    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)
        return len(data)

    sock.send.side_effect = _send

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

    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
def test_method():
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)), )
    sock = mocket.Mocket(headers + encoded)
    pool.socket.return_value = sock

    s = adafruit_requests.Session(pool)
    r = s.post("http://" + host + "/post")
    sock.connect.assert_called_once_with((ip, 80))

    sock.send.assert_has_calls([
        mock.call(b"POST"),
        mock.call(b" /"),
        mock.call(b"post"),
        mock.call(b" HTTP/1.1\r\n"),
    ])
    sock.send.assert_has_calls([
        mock.call(b"Host: "),
        mock.call(b"httpbin.org"),
    ])
Beispiel #23
0
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_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")
Beispiel #25
0
def test_get_close():
    """Test that a response can be closed without the contents being accessed."""
    pool = mocket.MocketPool()
    pool.getaddrinfo.return_value = ((None, None, None, None, (ip, 80)), )
    sock = mocket.Mocket(response)
    pool.socket.return_value = sock

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

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

    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"),
    ])
    r.close()