2022-01-05 16:12:58 -05:00
|
|
|
"""Contains tests for networking.py and app.py"""
|
|
|
|
|
2022-01-04 12:58:37 -05:00
|
|
|
import os
|
2022-02-27 16:44:59 -05:00
|
|
|
import urllib
|
2022-01-04 12:58:37 -05:00
|
|
|
import warnings
|
2021-11-13 00:33:59 -06:00
|
|
|
|
2023-04-03 10:34:15 -07:00
|
|
|
import pytest
|
2022-01-21 16:44:12 +03:00
|
|
|
from fastapi.testclient import TestClient
|
2021-11-09 08:48:55 -06:00
|
|
|
|
2022-03-24 22:58:07 -07:00
|
|
|
import gradio as gr
|
2022-05-09 18:05:30 -07:00
|
|
|
from gradio import Interface, networking
|
2021-10-21 15:02:52 -05:00
|
|
|
|
2022-01-04 12:58:37 -05:00
|
|
|
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
2021-10-21 15:02:52 -05:00
|
|
|
|
2021-10-24 03:56:54 -05:00
|
|
|
|
2022-11-08 01:37:55 +01:00
|
|
|
class TestPort:
|
2021-10-24 03:56:54 -05:00
|
|
|
def test_port_is_in_range(self):
|
|
|
|
start = 7860
|
|
|
|
end = 7960
|
|
|
|
try:
|
|
|
|
port = networking.get_first_available_port(start, end)
|
2022-11-08 01:37:55 +01:00
|
|
|
assert start <= port <= end
|
2021-10-24 03:56:54 -05:00
|
|
|
except OSError:
|
|
|
|
warnings.warn("Unable to test, no ports available")
|
|
|
|
|
|
|
|
def test_same_port_is_returned(self):
|
|
|
|
start = 7860
|
|
|
|
end = 7960
|
|
|
|
try:
|
|
|
|
port1 = networking.get_first_available_port(start, end)
|
|
|
|
port2 = networking.get_first_available_port(start, end)
|
2022-11-08 01:37:55 +01:00
|
|
|
assert port1 == port2
|
2021-10-24 03:56:54 -05:00
|
|
|
except OSError:
|
|
|
|
warnings.warn("Unable to test, no ports available")
|
|
|
|
|
2021-10-25 11:06:36 -05:00
|
|
|
|
2022-11-08 01:37:55 +01:00
|
|
|
class TestInterfaceErrors:
|
2022-04-27 00:32:57 -07:00
|
|
|
def test_processing_error(self):
|
2022-01-21 16:44:12 +03:00
|
|
|
io = Interface(lambda x: 1 / x, "number", "number")
|
2021-10-26 07:57:29 -05:00
|
|
|
app, _, _ = io.launch(show_error=True, prevent_thread_lock=True)
|
2022-01-04 12:58:37 -05:00
|
|
|
client = TestClient(app)
|
2022-05-09 18:05:30 -07:00
|
|
|
response = client.post("/api/predict/", json={"data": [0], "fn_index": 0})
|
2022-11-08 01:37:55 +01:00
|
|
|
assert response.status_code == 500
|
|
|
|
assert "error" in response.json()
|
2021-10-26 07:57:29 -05:00
|
|
|
io.close()
|
2022-02-27 16:47:12 -05:00
|
|
|
|
2022-04-27 00:32:57 -07:00
|
|
|
def test_validation_error(self):
|
|
|
|
io = Interface(lambda x: 1 / x, "number", "number")
|
|
|
|
app, _, _ = io.launch(show_error=True, prevent_thread_lock=True)
|
|
|
|
client = TestClient(app)
|
2022-05-09 18:05:30 -07:00
|
|
|
response = client.post("/api/predict/", json={"fn_index": [0]})
|
2022-11-08 01:37:55 +01:00
|
|
|
assert response.status_code == 422
|
2022-04-27 00:32:57 -07:00
|
|
|
io.close()
|
|
|
|
|
2022-02-27 16:47:12 -05:00
|
|
|
|
2022-11-08 01:37:55 +01:00
|
|
|
class TestStartServer:
|
2023-04-03 10:34:15 -07:00
|
|
|
|
|
|
|
# Test IPv4 and IPv6 hostnames as they would be passed from --server-name.
|
|
|
|
@pytest.mark.parametrize("host", ["127.0.0.1", "[::1]"])
|
|
|
|
def test_start_server(self, host):
|
2022-02-27 16:44:59 -05:00
|
|
|
io = Interface(lambda x: x, "number", "number")
|
2022-02-28 08:01:13 -05:00
|
|
|
io.favicon_path = None
|
|
|
|
io.config = io.get_config_file()
|
|
|
|
io.show_error = True
|
2022-03-24 22:58:07 -07:00
|
|
|
io.flagging_callback.setup(gr.Number(), io.flagging_dir)
|
2022-02-28 08:01:13 -05:00
|
|
|
io.auth = None
|
2023-04-03 10:34:15 -07:00
|
|
|
io.host = host
|
2022-02-28 08:07:54 -05:00
|
|
|
|
2022-02-27 16:44:59 -05:00
|
|
|
port = networking.get_first_available_port(
|
2022-02-27 16:47:12 -05:00
|
|
|
networking.INITIAL_PORT_VALUE,
|
|
|
|
networking.INITIAL_PORT_VALUE + networking.TRY_NUM_PORTS,
|
2022-02-27 16:44:59 -05:00
|
|
|
)
|
2022-04-26 00:35:47 +03:00
|
|
|
io.enable_queue = False
|
2022-09-05 08:48:08 +07:00
|
|
|
_, _, local_path, _, server = networking.start_server(io, server_port=port)
|
2022-02-27 16:44:59 -05:00
|
|
|
url = urllib.parse.urlparse(local_path)
|
2022-11-08 01:37:55 +01:00
|
|
|
assert url.scheme == "http"
|
|
|
|
assert url.port == port
|
2022-02-27 16:44:59 -05:00
|
|
|
server.close()
|
2021-10-26 07:57:29 -05:00
|
|
|
|
2021-10-26 13:20:18 -05:00
|
|
|
|
2022-11-08 01:37:55 +01:00
|
|
|
class TestURLs:
|
2022-02-10 00:10:00 -05:00
|
|
|
def test_url_ok(self):
|
2021-10-26 16:36:11 -05:00
|
|
|
res = networking.url_ok("https://www.gradio.app")
|
2022-11-08 01:37:55 +01:00
|
|
|
assert res
|