|
| 1 | +"""Tests for URL normalization in GrpcConfig and RestConfig.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from sift_client.transport.grpc_transport import GrpcConfig |
| 6 | +from sift_client.transport.rest_transport import RestConfig |
| 7 | + |
| 8 | + |
| 9 | +class TestGrpcConfigUrl: |
| 10 | + def test_adds_https_when_missing(self): |
| 11 | + config = GrpcConfig(url="grpc.sift.com", api_key="api") |
| 12 | + assert config.uri == "https://grpc.sift.com" |
| 13 | + |
| 14 | + def test_adds_https_on_localhost(self): |
| 15 | + config = GrpcConfig(url="localhost:50051", api_key="api", use_ssl=False) |
| 16 | + assert config.uri == "http://localhost:50051" |
| 17 | + |
| 18 | + def test_adds_https_on_ip(self): |
| 19 | + conifg = GrpcConfig(url="129.10.1.1", api_key="api") |
| 20 | + assert conifg.uri == "https://129.10.1.1" |
| 21 | + |
| 22 | + def test_adds_https_on_ipv6(self): |
| 23 | + config = GrpcConfig(url="[::]:8080", api_key="api") |
| 24 | + assert config.uri == "https://[::]:8080" |
| 25 | + |
| 26 | + def test_adds_http_when_missing_local(self): |
| 27 | + config = GrpcConfig(url="grpc.sift.com", api_key="api", use_ssl=False) |
| 28 | + assert config.uri == "http://grpc.sift.com" |
| 29 | + |
| 30 | + def test_url_keeps_https(self): |
| 31 | + config = GrpcConfig(url="https://grpc.sift.com", api_key="api") |
| 32 | + assert config.uri == "https://grpc.sift.com" |
| 33 | + |
| 34 | + def test_url_keeps_http(self): |
| 35 | + config = GrpcConfig(url="http://grpc.sift.com", api_key="api", use_ssl=False) |
| 36 | + assert config.uri == "http://grpc.sift.com" |
| 37 | + |
| 38 | + def test_raises_on_invalid_url(self): |
| 39 | + with pytest.raises(ValueError, match="Invalid connection URL"): |
| 40 | + GrpcConfig(url="htp://localhost:8080", api_key="api") |
| 41 | + |
| 42 | + def test_raise_on_invalid_url2(self): |
| 43 | + with pytest.raises(ValueError, match="Invalid connection URL"): |
| 44 | + GrpcConfig(url="https:/localhost:50051", api_key="api") |
| 45 | + |
| 46 | + def test_raise_on_missing_url(self): |
| 47 | + with pytest.raises(ValueError, match="Invalid connection URL"): |
| 48 | + GrpcConfig(url="", api_key="api") |
| 49 | + |
| 50 | + |
| 51 | +class TestRestConfigUrl: |
| 52 | + def test_adds_https_when_missing(self): |
| 53 | + config = RestConfig(base_url="rest.sift.com", api_key="api") |
| 54 | + assert config.base_url == "https://rest.sift.com" |
| 55 | + |
| 56 | + def test_add_http_when_missing_local(self): |
| 57 | + config = RestConfig(base_url="rest.sift.com", api_key="api", use_ssl=False) |
| 58 | + assert config.base_url == "http://rest.sift.com" |
| 59 | + |
| 60 | + def test_url_keeps_https(self): |
| 61 | + config = RestConfig(base_url="https://rest.sift.com", api_key="api") |
| 62 | + assert config.base_url == "https://rest.sift.com" |
| 63 | + |
| 64 | + def test_url_keeps_http(self): |
| 65 | + config = RestConfig(base_url="http://rest.sift.com", api_key="api", use_ssl=False) |
| 66 | + assert config.base_url == "http://rest.sift.com" |
0 commit comments