Skip to content

Commit

Permalink
Merge pull request #114 from kuefmz/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
JJ-Author authored Oct 26, 2024
2 parents 826a2cc + 2511c2b commit 5bc7186
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 119 deletions.
6 changes: 3 additions & 3 deletions ontologytimemachine/custom_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def before_upstream_connection(self, request: HttpParser) -> HttpParser | None:
wrapped_request = HttpRequestWrapper(request)

if (self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.REQUIRED or self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.OPTIONAL):
logger.info('Setting up config from auth')
config_from_auth = evaluate_configuration(wrapped_request, self.config)
if (not config_from_auth and self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.REQUIRED):
logger.info( "Client configuration via proxy auth is required btu configuration is not provided, return 500.")
Expand Down Expand Up @@ -135,12 +136,11 @@ def queue_response(self, response):
response.status_code,
reason=bytes(responses[response.status_code], "utf-8"),
headers={
b"Content-Type": bytes(
response.headers.get("Content-Type"), "utf-8"
)
bytes(key, "utf-8"): bytes(value, "utf-8") for key, value in response.headers.items()
},
body=response.content,
)

)


Expand Down
1 change: 1 addition & 0 deletions ontologytimemachine/proxy_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,6 @@ def get_authentication_from_request(self) -> str:
if auth_type.lower() != "basic":
return None
decoded_credentials = base64.b64decode(encoded_credentials).decode()
logger.info(f'Decoded credentials: {decoded_credentials}')
return decoded_credentials
return None
12 changes: 6 additions & 6 deletions ontologytimemachine/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ def parse_arguments(config_str: str = "") -> Config:
type=lambda s: enum_parser(OntoFormat, s),
default=default_cfg.ontoFormatConf.format,
choices=list(OntoFormat),
help=f"Format of the ontology: turtle, ntriples, rdfxml, htmldocu. {help_suffix_template}",
help=f"Format of the ontology. {help_suffix_template}",
)

parser.add_argument(
"--ontoPrecedence",
type=lambda s: enum_parser(OntoPrecedence, s),
default=default_cfg.ontoFormatConf.precedence,
choices=list(OntoPrecedence),
help=f"Precedence of the ontology: default, enforcedPriority, always. {help_suffix_template}",
help=f"Precedence of the ontology. {help_suffix_template}",
)

parser.add_argument(
Expand All @@ -176,7 +176,7 @@ def parse_arguments(config_str: str = "") -> Config:
type=lambda s: enum_parser(OntoVersion, s),
default=default_cfg.ontoVersion,
choices=list(OntoVersion),
help=f"Version of the ontology: original, originalFailoverLive, originalFailoverArchivoMonitor, latestArchive, timestampArchive, dependencyManifest. {help_suffix_template}",
help=f"Version of the ontology. {help_suffix_template}",
)

# Enable/disable mode to only proxy requests to ontologies
Expand All @@ -193,7 +193,7 @@ def parse_arguments(config_str: str = "") -> Config:
type=lambda s: enum_parser(HttpsInterception, s),
default=default_cfg.httpsInterception,
choices=list(HttpsInterception),
help=f"Enable HTTPS interception for specific domains: none, archivo, all, listfilename. {help_suffix_template}",
help=f"Enable HTTPS interception for specific domains. {help_suffix_template}",
)

# Enable/disable inspecting or removing redirects
Expand All @@ -218,7 +218,7 @@ def parse_arguments(config_str: str = "") -> Config:
type=lambda s: enum_parser(LogLevel, s),
default=default_cfg.logLevelTimeMachine,
choices=list(LogLevel),
help=f"Level of the logging: debug, info, warning, error. {help_suffix_template}",
help=f"Level of the logging. {help_suffix_template}",
)

# Log level
Expand All @@ -227,7 +227,7 @@ def parse_arguments(config_str: str = "") -> Config:
type=lambda s: enum_parser(LogLevel, s),
default=default_cfg.logLevelTimeMachine,
choices=list(LogLevel),
help=f"Level of the logging: debug, info, warning, error. {help_suffix_template}",
help=f"Level of the logging. {help_suffix_template}",
)

# Host
Expand Down
9 changes: 6 additions & 3 deletions ontologytimemachine/utils/proxy_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ def request_ontology(
url=url, headers=headers, allow_redirects=allow_redirects, timeout=5
)
logger.info("Successfully fetched ontology")
print(response.content)
return response
except Exception as e:
logger.error(f"Error fetching original ontology: {e}")
Expand All @@ -165,7 +164,7 @@ def request_ontology(

# change the function definition and pass only the config
def proxy_logic(wrapped_request, config):
logger.info("Proxy has to intervene")
logger.info("Proxy starting to analyze request")

response = mock_response_500 #default if we somehow forget to set the response
set_onto_format_headers(wrapped_request, config)
Expand All @@ -180,15 +179,19 @@ def proxy_logic(wrapped_request, config):
return mock_response_500

if config.ontoVersion == OntoVersion.ORIGINAL:
logger.info('OntoVersion ORIGINAL')
ontology, _, _ = wrapped_request.get_request_url_host_path()
response = fetch_original(wrapped_request, ontology, headers, config)
elif config.ontoVersion == OntoVersion.ORIGINAL_FAILOVER_LIVE_LATEST:
logger.info('OntoVersion ORIGINAL_FAILOVER_LIVE_LATEST')
response = fetch_failover(
wrapped_request, headers, config.disableRemovingRedirects
)
elif config.ontoVersion == OntoVersion.LATEST_ARCHIVED:
logger.info('OntoVersion LATEST_ARCHIVED')
response = fetch_latest_archived(wrapped_request, ontology, headers)
elif config.ontoVersion == OntoVersion.LATEST_ARCHIVED:
elif config.ontoVersion == OntoVersion.TIMESTAMP_ARCHIVED:
logger.info('OntoVersion TIMESTAMP_ARCHIVED')
response = fetch_timestamp_archived(wrapped_request, headers, config)
# Commenting the manifest related part because it is not supported in the current version
# elif ontoVersion == 'dependencyManifest':
Expand Down
29 changes: 12 additions & 17 deletions tests/test_proxy_new.py → tests/test_proxy_auth_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
import csv
from typing import List, Tuple
from requests.auth import HTTPBasicAuth
from requests.auth import _basic_auth_str
from ontologytimemachine.custom_proxy import IP, PORT

# Proxy settings
PROXY = f"0.0.0.0:{PORT}"
PROXY = f"0.0.0.0:8894"
HTTP_PROXY = f"http://{PROXY}"
HTTPS_PROXY = f"http://{PROXY}"
PROXIES = {"http": HTTP_PROXY, "https": HTTPS_PROXY}

logging.basicConfig(
level=logging.DEBUG,
level=logging.ERROR,
format="%(asctime)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
Expand All @@ -35,25 +36,27 @@ def create_fake_response(status_code='error'):
def make_request_without_proxy(iri: str) -> Tuple[int, str]:
"""Make a direct request to the IRI without using the proxy."""
headers = {
"Content-Type": "text/turtle"
"Accept": "text/turtle"
}
try:
response = requests.get(iri, timeout=10, headers=headers)
response = requests.get(iri, timeout=10, headers=headers, allow_redirects=True)
return response
except Exception as e:
# logger.info(f'Error: {e}')
# logger.info('Error with the connection')
return create_fake_response()

def make_request_with_proxy(iri: str, mode: str) -> Tuple[int, str]:
logger.info('Run')
"""Make a request to the IRI using the proxy."""
username = f"--ontoVersion {mode}"
password = "my_password"
headers = {
"Content-Type": "text/turtle"
"Accept": "text/turtle",
"Proxy-Authorization": _basic_auth_str(username, password)
}
try:
response = requests.get(iri, proxies=PROXIES, timeout=10, headers=headers, auth=HTTPBasicAuth(username, password))
response = requests.get(iri, proxies=PROXIES, headers=headers, allow_redirects=True)
return response
except Exception as e:
# logger.info(f'Error: {e}')
Expand All @@ -72,18 +75,10 @@ def test_proxy_responses(test_case):
# Make direct and proxy requests
direct_response = make_request_without_proxy(iri)
proxy_response = make_request_with_proxy(iri, 'original')
#proxy_response = make_request_with_proxy(iri, 'original')
#proxy_response = make_request_with_proxy(iri, 'laters')
#proxy_response = make_request_with_proxy(iri, 'original')


try:
direct_response = requests.get(iri)
except Exception as e:
logger.error(f"Error making direct request to {iri}: {e}")

try:
proxy_response = requests.get(iri, proxies=PROXIES)
except Exception as e:
logger.error(f"Error making proxy request to {iri} using proxy {PROXY}: {e}")

# Evaluation based on error_dimension
if error_dimension == 'http-code':
logger.info(f"Comparing direct response status code: expected {expected_error}, got {direct_response.status_code}")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_proxypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def wait_for_server(proxy_port: int, wait_for_seconds: float = 10.0) -> None:
def make_request_without_proxy(self, iri: str) -> Tuple[int, str]:
"""Make a direct request to the IRI without using the proxy."""
headers = {
"Content-Type": "text/turtle"
"Accept": "text/turtle"
}
try:
response = requests.get(iri, timeout=10, headers=headers)
Expand All @@ -105,7 +105,7 @@ def make_request_with_proxy(self, iri: str, proxy_port: int, mode: str) -> Tuple
username = f"--ontoVersion {mode}"
password = "my_password"
headers = {
"Content-Type": "text/turtle"
"Accept": "text/turtle"
}
try:
response = requests.get(iri, proxies=proxies, timeout=10, headers=headers, auth=HTTPBasicAuth(username, password))
Expand Down
Loading

0 comments on commit 5bc7186

Please sign in to comment.