-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from f5devcentral/dev
Dev
- Loading branch information
Showing
12 changed files
with
268 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from requests.structures import CaseInsensitiveDict | ||
|
||
def headers_cleaner(headers): | ||
""" | ||
Remove headers that contain specific substrings. | ||
Use this to make responses look nicer. | ||
""" | ||
unwanted_substrings = ['x-envoy', 'cloudfront', 'x-k8se', 'x-amz', 'z-amzn', 'via', 'x-arr-ssl', 'x-ms-containerapp'] | ||
filtered_headers = { | ||
key: value for key, value in headers.items() | ||
if not any(substring in key.lower() for substring in unwanted_substrings) | ||
} | ||
return filtered_headers | ||
|
||
def cloudapp_fetch(session, url, timeout, prop, value): | ||
""" | ||
Fetch data from URL | ||
Validate prop and value in the JSON response | ||
""" | ||
response = session.get(url, timeout=timeout) | ||
response.raise_for_status() | ||
data = response.json() | ||
if data.get(prop) != value: | ||
raise ValueError(f'Invalid {prop}: expected {value}, got {data.get(prop)}') | ||
if data.get("request_headers"): | ||
clean_headers = headers_cleaner(data['request_headers']) | ||
data['request_headers'] = clean_headers | ||
return data | ||
return data | ||
|
||
def cloudapp_req_headers(session, url, timeout, headers): | ||
""" | ||
Fetch data from URL | ||
""" | ||
response = session.get(url, timeout=timeout) | ||
response.raise_for_status() | ||
data = response.json() | ||
req_headers = CaseInsensitiveDict(data['request_headers']) | ||
for header in headers: | ||
head_value = req_headers.get(header) | ||
if not head_value: | ||
raise ValueError(f"Header {header} not found request headers.") | ||
clean_headers = headers_cleaner(data['request_headers']) | ||
data['request_headers'] = clean_headers | ||
return data | ||
|
||
def cloudapp_res_headers(session, url, timeout, headers): | ||
""" | ||
Fetch data from URL | ||
Check for response header | ||
""" | ||
response = session.get(url, timeout=timeout) | ||
response.raise_for_status() | ||
data = response.headers | ||
for header in headers: | ||
head_value = data.get(header) | ||
if not head_value: | ||
raise ValueError(f"Header {header} not found response headers from {url}.") | ||
header_dict = dict(data) | ||
return header_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.