def callback(request: Request, ext: Dict[str, Any]) -> Response: assert request.headers["Authorization"] == f"token {token}" assert request.method == "GET" if str(request.url) == GitHubProvider._USER_URL: return to_response( json={ "login": user_info.username, "id": user_info.uid, "name": user_info.name, }) elif str(request.url) == GitHubProvider._TEAMS_URL: teams = [] for team in user_info.teams: data = { "slug": team.slug, "id": team.gid, "organization": { "login": team.organization }, } teams.append(data) return to_response(json=teams) elif str(request.url) == GitHubProvider._EMAILS_URL: return to_response(json=[ { "email": "*****@*****.**", "primary": False }, { "email": user_info.email, "primary": True }, ]) else: assert False, f"unexpected request for {request.url}"
def callback(request: Request, ext: Dict[str, Any]) -> Response: assert self.config.oidc if str(request.url) != self.config.oidc.token_url: assert request.method == "GET" return to_response(status_code=404) assert request.method == "POST" assert request.headers["Accept"] == "application/json" assert parse_qs(request.read().decode()) == { "grant_type": ["authorization_code"], "client_id": [self.config.oidc.client_id], "client_secret": [self.config.oidc.client_secret], "code": [code], "redirect_uri": [self.config.oidc.redirect_url], } return to_response(json={ "id_token": token.encoded, "token_type": "Bearer" })
def callback(request: Request, ext: Dict[str, Any]) -> Response: assert self.config.github assert str(request.url) == GitHubProvider._TOKEN_URL assert request.method == "POST" assert request.headers["Accept"] == "application/json" assert parse_qs(request.read().decode()) == { "client_id": [self.config.github.client_id], "client_secret": [self.config.github.client_secret], "code": [code], "state": [ANY], } return to_response( json={ "access_token": token, "scope": ",".join(GitHubProvider._SCOPES), "token_type": "bearer", })
def custom_matcher(self, request: httpx.Request, *args, **kwargs): """Match URLs to sample files.""" port_suffix = "" if request.url.port == 8080: port_suffix = "-8080" try: if request.url.path == STATUS_URL: content = get_sample_content( "{receiver}-formMainZone_MainZoneXmlStatus{port}" ".xml".format(receiver=self.testing_receiver, port=port_suffix)) elif request.url.path == STATUS_Z2_URL: content = get_sample_content( "{receiver}-formZone2_Zone2XmlStatus{port}.xml".format( receiver=self.testing_receiver, port=port_suffix)) elif request.url.path == STATUS_Z3_URL: content = get_sample_content( "{receiver}-formZone3_Zone3XmlStatus{port}.xml".format( receiver=self.testing_receiver, port=port_suffix)) elif request.url.path == MAINZONE_URL: content = get_sample_content( "{receiver}-formMainZone_MainZoneXml{port}.xml".format( receiver=self.testing_receiver, port=port_suffix)) elif request.url.path == DEVICEINFO_URL: content = get_sample_content( "{receiver}-Deviceinfo{port}.xml".format( receiver=self.testing_receiver, port=port_suffix)) elif request.url.path == NETAUDIOSTATUS_URL: content = get_sample_content( "{receiver}-formNetAudio_StatusXml{port}.xml".format( receiver=self.testing_receiver, port=port_suffix)) elif request.url.path == TUNERSTATUS_URL: content = get_sample_content( "{receiver}-formTuner_TunerXml{port}.xml".format( receiver=self.testing_receiver, port=port_suffix)) elif request.url.path == HDTUNERSTATUS_URL: content = get_sample_content( "{receiver}-formTuner_HdXml{port}.xml".format( receiver=self.testing_receiver, port=port_suffix)) elif request.url.path == APPCOMMAND_URL: content_str = request.read().decode("utf-8") if "GetFriendlyName" in content_str: ep_suffix = "-setup" else: ep_suffix = "-update" content = get_sample_content( "{receiver}-AppCommand{port}{ep}.xml".format( receiver=self.testing_receiver, port=port_suffix, ep=ep_suffix)) elif request.url.path in [DESCRIPTION_URL1, DESCRIPTION_URL2]: content = get_sample_content("AVR-X1600H_upnp.xml") else: content = "DATA" except FileNotFoundError: content = "Error 403: Forbidden\nAccess Forbidden" status_code = 403 else: status_code = 200 resp = to_response(status_code=status_code, data=content) return resp
def custom_response(*args, **kwargs) -> httpx.Response: return to_response(json=["content"])
def custom_response(*args, **kwargs): return to_response(json=["content"])
def custom_response(request: httpx.Request, *args, **kwargs): return to_response(json={"url": str(request.url)})
def custom_response2(request: httpx.Request, *args, **kwargs): return to_response(http_version="HTTP/2.0", json={"url": str(request.url)})
def raise_exception_once(*args, **kwargs): nonlocal times_called times_called += 1 if times_called == 1: raise httpcore.ConnectError() return pytest_httpx.to_response()