MCPcopy Create free account
hub / github.com/codenameone/CodenameOne / check_case

Function check_case

scripts/website/test_redirects.py:208–328  ·  view source on GitHub ↗
(base_url: str, case: Case, timeout: float)

Source from the content-addressed store, hash-verified

206
207
208def check_case(base_url: str, case: Case, timeout: float) -> CaseResult:
209 url = urllib.parse.urljoin(base_url.rstrip("/") + "/", case.source.lstrip("/"))
210 req = urllib.request.Request(url, headers={"User-Agent": "cn1-redirect-check/1.0"})
211 try:
212 with NO_REDIRECT_OPENER.open(req, timeout=timeout) as resp:
213 status = int(resp.getcode())
214 location = normalize_location(resp.headers.get("Location"))
215 body = ""
216 if case.expected_body_pattern is not None:
217 body = resp.read().decode("utf-8", errors="ignore")
218 if case.expected_status < 300:
219 ok = status == case.expected_status
220 if ok and case.require_no_redirect and location is not None:
221 ok = False
222 else:
223 ok = status == case.expected_status and location_matches(
224 case.expected_target, location
225 )
226 if ok and case.expected_body_pattern is not None:
227 if case.expected_body_pattern.startswith("re:"):
228 ok = (
229 re.search(case.expected_body_pattern[3:], body, re.DOTALL)
230 is not None
231 )
232 else:
233 ok = case.expected_body_pattern in body
234 if ok and case.expected_final_status is not None:
235 try:
236 with urllib.request.urlopen(req, timeout=timeout) as final_resp:
237 final_status = int(final_resp.getcode())
238 final_url = final_resp.geturl()
239 final_body = (
240 final_resp.read().decode("utf-8", errors="ignore")
241 if case.expected_final_body_pattern is not None
242 else ""
243 )
244 if final_status != case.expected_final_status:
245 ok = False
246 if ok and case.expected_final_path is not None:
247 final_path_q = urllib.parse.urlunsplit(
248 ("", "", urllib.parse.urlsplit(final_url).path, urllib.parse.urlsplit(final_url).query, "")
249 )
250 if final_path_q != case.expected_final_path:
251 ok = False
252 if ok and case.expected_final_body_pattern is not None:
253 if case.expected_final_body_pattern.startswith("re:"):
254 ok = (
255 re.search(
256 case.expected_final_body_pattern[3:],
257 final_body,
258 re.DOTALL,
259 )
260 is not None
261 )
262 else:
263 ok = case.expected_final_body_pattern in final_body
264 except Exception: # noqa: BLE001
265 ok = False

Callers 1

mainFunction · 0.85

Calls 9

normalize_locationFunction · 0.85
location_matchesFunction · 0.85
CaseResultClass · 0.85
RequestMethod · 0.80
openMethod · 0.65
getMethod · 0.65
readMethod · 0.65
decodeMethod · 0.45
searchMethod · 0.45

Tested by

no test coverage detected