Python httmock.urlmatch() Examples
The following are 30
code examples of httmock.urlmatch().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
httmock
, or try the search function
.
Example #1
Source File: mocked_network.py From prometheus-api-client-python with MIT License | 6 votes |
def mock_response( content, url=None, path='', headers=None, response_url=None, status_code=200, cookies=None, func=None ): """Universal handler for specify mocks inplace""" if func is None: def mocked(url, request): mock = response( status_code=status_code, content=content, request=request, headers=headers ) if cookies: mock.cookies = cookies mock.url = response_url if response_url else url return mock else: mocked = func if url: parsed = urlparse(url) return urlmatch(netloc=parsed.netloc, path=parsed.path)(func=mocked) elif path: return urlmatch(path=path)(func=mocked) else: return all_requests(func=mocked)
Example #2
Source File: test_validate_bitbucket_trigger.py From quay with Apache License 2.0 | 6 votes |
def test_validate_bitbucket_trigger(app): url_hit = [False] @urlmatch(netloc=r"bitbucket.org") def handler(url, request): url_hit[0] = True return { "status_code": 200, "content": "oauth_token=foo&oauth_token_secret=bar", } with HTTMock(handler): validator = BitbucketTriggerValidator() url_scheme_and_hostname = URLSchemeAndHostname("http", "localhost:5000") unvalidated_config = ValidatorContext( {"BITBUCKET_TRIGGER_CONFIG": {"CONSUMER_KEY": "foo", "CONSUMER_SECRET": "bar",},}, url_scheme_and_hostname=url_scheme_and_hostname, ) validator.validate(unvalidated_config) assert url_hit[0]
Example #3
Source File: test_validate_elasticsearch.py From quay with Apache License 2.0 | 6 votes |
def test_validate_elasticsearch(unvalidated_config, expected, app): validator = ElasticsearchValidator() path = unvalidated_config.get("index_prefix") or INDEX_NAME_PREFIX path = "/" + path + "*" unvalidated_config = ValidatorContext(unvalidated_config) @urlmatch(netloc=r"", path=path) def handler(url, request): return {"status_code": 200, "content": b"{}"} with HTTMock(handler): if expected is not None: with pytest.raises(expected): validator.validate(unvalidated_config) else: validator.validate(unvalidated_config)
Example #4
Source File: test_validate_google_login.py From quay with Apache License 2.0 | 6 votes |
def test_validate_google_login(app): url_hit = [False] @urlmatch(netloc=r"www.googleapis.com", path="/oauth2/v3/token") def handler(_, __): url_hit[0] = True return {"status_code": 200, "content": ""} validator = GoogleLoginValidator() with HTTMock(handler): unvalidated_config = ValidatorContext( {"GOOGLE_LOGIN_CONFIG": {"CLIENT_ID": "foo", "CLIENT_SECRET": "bar",},} ) unvalidated_config.http_client = build_requests_session() validator.validate(unvalidated_config) assert url_hit[0]
Example #5
Source File: test_k8saccessor.py From quay with Apache License 2.0 | 6 votes |
def test_cycle_qe_deployments(kube_config, deployment_names, expected_api_hits): KubernetesAccessorSingleton._instance = None config = KubernetesConfig(**kube_config) url_hit = [False] * len(expected_api_hits) i = [0] @urlmatch(netloc=r"www.customhost.com", method="PATCH") def handler(request, _): assert request.path == expected_api_hits[i[0]] url_hit[i[0]] = True i[0] += 1 return response(200, "{}") with HTTMock(handler): KubernetesAccessorSingleton.get_instance(config).cycle_qe_deployments(deployment_names) assert all(url_hit)
Example #6
Source File: test_k8saccessor.py From quay with Apache License 2.0 | 6 votes |
def test_get_qe_deployments(kube_config, expected_api, expected_query): config = KubernetesConfig(**kube_config) url_hit = [False] @urlmatch(netloc=r"www.customhost.com") def handler(request, _): assert request.path == expected_api assert request.query == expected_query url_hit[0] = True return response(200, "{}") with HTTMock(handler): KubernetesAccessorSingleton._instance = None assert KubernetesAccessorSingleton.get_instance(config).get_qe_deployments() is not None assert url_hit[0]
Example #7
Source File: test_oauth_login.py From quay with Apache License 2.0 | 6 votes |
def test_google_oauth(self): @urlmatch(netloc=r"accounts.google.com", path="/o/oauth2/token") def account_handler(_, request): parsed = dict(urllib.parse.parse_qsl(request.body)) if parsed["code"] == "somecode": content = {"access_token": "someaccesstoken"} return py_json.dumps(content) else: return {"status_code": 400, "content": '{"message": "Invalid code"}'} @urlmatch(netloc=r"www.googleapis.com", path="/oauth2/v1/userinfo") def user_handler(_, __): content = { "id": "someid", "email": "someemail@example.com", "verified_email": True, } return py_json.dumps(content) with HTTMock(account_handler, user_handler): self.invoke_oauth_tests( "google_oauth_callback", "google_oauth_attach", "google", "someid", "someemail" )
Example #8
Source File: test_oauth_login.py From quay with Apache License 2.0 | 6 votes |
def test_github_oauth(self): @urlmatch(netloc=r"github.com", path="/login/oauth/access_token") def account_handler(url, _): parsed = dict(urllib.parse.parse_qsl(url.query)) if parsed["code"] == "somecode": content = {"access_token": "someaccesstoken"} return py_json.dumps(content) else: return {"status_code": 400, "content": '{"message": "Invalid code"}'} @urlmatch(netloc=r"github.com", path="/api/v3/user") def user_handler(_, __): content = {"id": "someid", "login": "someusername"} return py_json.dumps(content) @urlmatch(netloc=r"github.com", path="/api/v3/user/emails") def email_handler(_, __): content = [{"email": "someemail@example.com", "verified": True, "primary": True,}] return py_json.dumps(content) with HTTMock(account_handler, email_handler, user_handler): self.invoke_oauth_tests( "github_oauth_callback", "github_oauth_attach", "github", "someid", "someusername" )
Example #9
Source File: unittest_util.py From aliyun-datahub-sdk-python with Apache License 2.0 | 6 votes |
def gen_mock_api(check): @urlmatch(netloc=r'(.*\.)?endpoint') def datahub_api_mock(url, request): check(request) path = url.path.replace('/', '.')[1:] res_file = os.path.join(_FIXTURE_PATH, '%s.json' % path) status_code = 200 content = { } headers = { 'Content-Type': 'application/json', 'x-datahub-request-id': 0 } try: with open(res_file, 'rb') as f: content = json.loads(f.read().decode('utf-8')) if 'ErrorCode' in content: status_code = 500 except (IOError, ValueError) as e: content['ErrorMessage'] = 'Loads fixture %s failed, error: %s' % (res_file, e) return response(status_code, content, headers, request=request) return datahub_api_mock
Example #10
Source File: unittest_util.py From aliyun-datahub-sdk-python with Apache License 2.0 | 6 votes |
def gen_pb_mock_api(check): @urlmatch(netloc=r'(.*\.)?endpoint') def datahub_pb_api_mock(url, request): check(request) path = url.path.replace('/', '.')[1:] res_file = os.path.join(_FIXTURE_PATH, '%s.bin' % path) status_code = 200 content = { } headers = { 'Content-Type': 'application/x-protobuf', 'x-datahub-request-id': 0 } try: with open(res_file, 'rb') as f: content = f.read() except (IOError, InvalidParameterException) as e: content['ErrorMessage'] = 'Loads fixture %s failed, error: %s' % (res_file, e) return response(status_code, content, headers, request=request) return datahub_pb_api_mock
Example #11
Source File: test_client.py From refstack-client with Apache License 2.0 | 6 votes |
def test_post_results(self): """ Test the post_results method, ensuring a requests call is made. """ args = rc.parse_cli_args(self.mock_argv()) client = rc.RefstackClient(args) client.logger.info = MagicMock() content = {'duration_seconds': 0, 'cpid': 'test-id', 'results': [{'name': 'tempest.passed.test', 'uid': None}]} expected_response = json.dumps({'test_id': 42}) @httmock.urlmatch(netloc=r'(.*\.)?127.0.0.1$', path='/v1/results/') def refstack_api_mock(url, request): return expected_response with httmock.HTTMock(refstack_api_mock): client.post_results("http://127.0.0.1", content) client.logger.info.assert_called_with( 'http://127.0.0.1/v1/results/ Response: ' '%s' % expected_response)
Example #12
Source File: test_pwned_passwords.py From firepwned with GNU General Public License v3.0 | 6 votes |
def test_detects_not_pwned_password(self): password = "password" hash = hashlib.sha1(bytes(password, "utf8")).hexdigest() hash_prefix = hash[0:PREFIX_LEN] @urlmatch(scheme='https', netloc=r'api.pwnedpasswords.com', path=r'/range/' + hash_prefix) def mock(url, request): return "\n".join([ "1D72CD07550416C216D8AD296BF5C0AE8E0:9", "1E2AAA439972480CEC7F16C795BBB429372:1", "1E3687A61BFCE35F69B7408158101C8E414:1", "20597F5AC10A2F67701B4AD1D3A09F72250:3", "20AEBCE40E55EDA1CE07D175EC293150A7E:1", "20FFB975547F6A33C2882CFF8CE2BC49720:1" ]) with HTTMock(mock): self.assertEqual(is_password_pwned(password), (False, 0))
Example #13
Source File: test_pwned_passwords.py From firepwned with GNU General Public License v3.0 | 6 votes |
def test_detects_pwned_password(self): password = "password" hash = hashlib.sha1(bytes(password, "utf8")).hexdigest() hash_prefix = hash[0:PREFIX_LEN] hash_suffix = hash[PREFIX_LEN:] @urlmatch(scheme='https', netloc=r'api.pwnedpasswords.com', path=r'/range/' + hash_prefix) def mock(url, request): return "\n".join([ "1D72CD07550416C216D8AD296BF5C0AE8E0:9", "1E2AAA439972480CEC7F16C795BBB429372:1", "1E3687A61BFCE35F69B7408158101C8E414:1", "%s:%s" % (hash_suffix.upper(), "100"), "20597F5AC10A2F67701B4AD1D3A09F72250:3", "20AEBCE40E55EDA1CE07D175EC293150A7E:1", "20FFB975547F6A33C2882CFF8CE2BC49720:1" ]) with HTTMock(mock): self.assertEqual(is_password_pwned(password), (True, 100))
Example #14
Source File: test_client.py From wechatpy with MIT License | 6 votes |
def test_iter_tag_users(self): @urlmatch(netloc=r"(.*\.)?api\.weixin\.qq\.com$", path=r".*user/tag/get") def next_openid_mock(url, request): """伪造第二页的请求""" data = json.loads(request.body.decode()) if not data.get("next_openid"): return wechat_api_mock(url, request) # 根据拿到的第二页请求响应 是没有data和next_openid的 content = {"count": 0} headers = {"Content-Type": "application/json"} return response(200, content, headers, request=request) with HTTMock(next_openid_mock, wechat_api_mock): users = list(self.client.tag.iter_tag_users(101)) self.assertEqual(2, len(users)) self.assertIn("OPENID1", users) self.assertIn("OPENID2", users)
Example #15
Source File: test_main.py From satori with Apache License 2.0 | 6 votes |
def test_process_single_event(self): from main import process_single_event from httmock import response, HTTMock, urlmatch @urlmatch(netloc=r'(api.nexmo.com|yunpian.com)') def response_content(url, request): print(url) headers = {'Content-Type': 'application/json'} if url.netloc == 'api.nexmo.com': return response(200, '{}', headers) elif url.netloc == 'yunpian.com': return response(200, '{"code": 0}', headers) else: raise Exception('Meh!') with HTTMock(response_content): process_single_event(alarm_example)
Example #16
Source File: test_client.py From refstack-client with Apache License 2.0 | 6 votes |
def test_post_results_with_sign(self): """ Test the post_results method, ensuring a requests call is made. """ argv = self.mock_argv(command='upload', priv_key='rsa_key') argv.append('fake.json') args = rc.parse_cli_args(argv) client = rc.RefstackClient(args) client.logger.info = MagicMock() content = {'duration_seconds': 0, 'cpid': 'test-id', 'results': [{'name': 'tempest.passed.test'}]} expected_response = json.dumps({'test_id': 42}) @httmock.urlmatch(netloc=r'(.*\.)?127.0.0.1$', path='/v1/results/') def refstack_api_mock(url, request): return expected_response with httmock.HTTMock(refstack_api_mock): rsapath = os.path.join(self.test_path, 'rsa_key') client.post_results("http://127.0.0.1", content, sign_with=rsapath) client.logger.info.assert_called_with( 'http://127.0.0.1/v1/results/ Response: %s' % expected_response)
Example #17
Source File: test_suppliers.py From endpoints-management-python with Apache License 2.0 | 6 votes |
def test_supply_jwks(self): rsa_key = PublicKey.RSA.generate(2048) jwks = jwk.KEYS() jwks.wrap_add(rsa_key) scheme = u"https" issuer = u"issuer.com" self._key_uri_supplier.supply.return_value = scheme + u"://" + issuer @httmock.urlmatch(scheme=scheme, netloc=issuer) def _mock_response_with_jwks(url, response): # pylint: disable=unused-argument return jwks.dump_jwks() with httmock.HTTMock(_mock_response_with_jwks): actual_jwks = self._jwks_uri_supplier.supply(issuer) self.assertEquals(1, len(actual_jwks)) actual_key = actual_jwks[0].key self.assertEquals(rsa_key.n, actual_key.n) self.assertEquals(rsa_key.e, actual_key.e)
Example #18
Source File: test_client.py From refstack-client with Apache License 2.0 | 5 votes |
def test_yield_results(self): """ Test the yield_results method, ensuring that results are retrieved. """ args = rc.parse_cli_args(self.mock_argv(command='list')) client = rc.RefstackClient(args) expected_response = { "pagination": { "current_page": 1, "total_pages": 1 }, "results": [ { "cpid": "42", "created_at": "2015-04-28 13:57:05", "test_id": "1", "url": "http://127.0.0.1:8000/output.html?test_id=1" }, { "cpid": "42", "created_at": "2015-04-28 13:57:05", "test_id": "2", "url": "http://127.0.0.1:8000/output.html?test_id=2" }]} @httmock.urlmatch(netloc=r'(.*\.)?127.0.0.1$', path='/v1/results/') def refstack_api_mock(url, request): return json.dumps(expected_response) with httmock.HTTMock(refstack_api_mock): results = client.yield_results("http://127.0.0.1") self.assertEqual(expected_response['results'], next(results)) # Since Python3.7 StopIteration exceptions are transformed into # RuntimeError (PEP 479): # https://docs.python.org/3/whatsnew/3.7.html self.assertRaises((StopIteration, RuntimeError), next, results)
Example #19
Source File: test_pwned_passwords.py From firepwned with GNU General Public License v3.0 | 5 votes |
def test_api_error(self): @urlmatch(scheme='https', netloc=r'api.pwnedpasswords.com', path=r'/range/.*') def mock(url, request): return { 'status_code': 503 } with HTTMock(mock): self.assertRaises(Exception, is_password_pwned, "password")
Example #20
Source File: test_validate_github.py From quay with Apache License 2.0 | 5 votes |
def test_validate_github(github_validator, app): url_hit = [False, False] @urlmatch(netloc=r"somehost") def handler(url, request): url_hit[0] = True return {"status_code": 200, "content": "", "headers": {"X-GitHub-Request-Id": "foo"}} @urlmatch(netloc=r"somehost", path=r"/api/v3/applications/foo/tokens/foo") def app_handler(url, request): url_hit[1] = True return {"status_code": 404, "content": "", "headers": {"X-GitHub-Request-Id": "foo"}} with HTTMock(app_handler, handler): unvalidated_config = ValidatorContext( { github_validator.config_key: { "GITHUB_ENDPOINT": "http://somehost", "CLIENT_ID": "foo", "CLIENT_SECRET": "bar", }, } ) unvalidated_config.http_client = build_requests_session() github_validator.validate(unvalidated_config) assert url_hit[0] assert url_hit[1]
Example #21
Source File: test_validate_oidc.py From quay with Apache License 2.0 | 5 votes |
def test_validate_oidc_login(app): url_hit = [False] @urlmatch(netloc=r"someserver", path=r"/\.well-known/openid-configuration") def handler(_, __): url_hit[0] = True data = { "token_endpoint": "foobar", } return {"status_code": 200, "content": json.dumps(data)} with HTTMock(handler): validator = OIDCLoginValidator() unvalidated_config = ValidatorContext( { "SOMETHING_LOGIN_CONFIG": { "CLIENT_ID": "foo", "CLIENT_SECRET": "bar", "OIDC_SERVER": "http://someserver", "DEBUGGING": True, # Allows for HTTP. }, } ) unvalidated_config.http_client = build_requests_session() validator.validate(unvalidated_config) assert url_hit[0]
Example #22
Source File: test_notificationworker.py From quay with Apache License 2.0 | 5 votes |
def test_notifications(method, method_config, netloc, initialized_db): url_hit = [False] @urlmatch(netloc=netloc) def url_handler(_, __): url_hit[0] = True return "" mock = Mock() def get_mock(*args, **kwargs): return mock with patch("notifications.notificationmethod.Message", get_mock): with HTTMock(url_handler): # Add a basic build notification. notification_uuid = model.create_notification_for_testing( "public", method_name=method.method_name(), method_config=method_config ) event_data = RepoPushEvent().get_sample_data("devtable", "simple", {}) # Fire off the queue processing. worker = NotificationWorker(None) worker.process_queue_item( { "notification_uuid": notification_uuid, "event_data": event_data, "performer_data": {}, } ) if netloc is not None: assert url_hit[0]
Example #23
Source File: test_exportactionlogsworker.py From quay with Apache License 2.0 | 5 votes |
def test_export_logs_failure(initialized_db): # Make all uploads fail. test_storage.put_content("local_us", "except_upload", b"true") repo = model.repository.get_repository("devtable", "simple") user = model.user.get_user("devtable") worker = ExportActionLogsWorker(None) called = [{}] @urlmatch(netloc=r"testcallback") def handle_request(url, request): called[0] = json.loads(request.body) return {"status_code": 200, "content": "{}"} def format_date(datetime): return datetime.strftime("%m/%d/%Y") now = datetime.now() with HTTMock(handle_request): with pytest.raises(IOError): worker._process_queue_item( { "export_id": "someid", "repository_id": repo.id, "namespace_id": repo.namespace_user.id, "namespace_name": "devtable", "repository_name": "simple", "start_time": format_date(now + timedelta(days=-10)), "end_time": format_date(now + timedelta(days=10)), "callback_url": "http://testcallback/", "callback_email": None, }, test_storage, ) test_storage.remove("local_us", "except_upload") assert called[0] assert called[0]["export_id"] == "someid" assert called[0]["status"] == "failed"
Example #24
Source File: test_connection.py From python-keycloak with MIT License | 5 votes |
def test_raw_post(self): @urlmatch(path="/known_path", method="post") def response_post_success(url, request): headers = {'content-type': 'application/json'} content = 'response'.encode("utf-8") return response(201, content, headers, None, 5, request) with HTTMock(response_post_success): resp = self._conn.raw_post("/known_path", {'field': 'value'}) self.assertEqual(resp.content, b'response') self.assertEqual(resp.status_code, 201)
Example #25
Source File: test_connection.py From python-keycloak with MIT License | 5 votes |
def test_raw_put(self): @urlmatch(netloc="localhost", path="/known_path", method="put") def response_put_success(url, request): headers = {'content-type': 'application/json'} content = 'response'.encode("utf-8") return response(200, content, headers, None, 5, request) with HTTMock(response_put_success): resp = self._conn.raw_put("/known_path", {'field': 'value'}) self.assertEqual(resp.content, b'response') self.assertEqual(resp.status_code, 200)
Example #26
Source File: test_connection.py From python-keycloak with MIT License | 5 votes |
def test_raw_get_fail(self): @urlmatch(netloc="localhost", path="/known_path", method="get") def response_get_fail(url, request): headers = {'content-type': 'application/json'} content = "404 page not found".encode("utf-8") return response(404, content, headers, None, 5, request) with HTTMock(response_get_fail): resp = self._conn.raw_get("/known_path") self.assertEqual(resp.content, b"404 page not found") self.assertEqual(resp.status_code, 404)
Example #27
Source File: test_oidc.py From quay with Apache License 2.0 | 5 votes |
def emptykeys_jwks_handler(): @urlmatch(netloc=r"fakeoidc", path=r"/jwks") def handler(_, __): content = {"keys": []} return {"status_code": 200, "content": json.dumps(content)} return handler
Example #28
Source File: test_connection.py From python-keycloak with MIT License | 5 votes |
def test_raw_post_fail(self): @urlmatch(netloc="localhost", path="/known_path", method="post") def response_post_fail(url, request): headers = {'content-type': 'application/json'} content = str(["Start can't be blank"]).encode("utf-8") return response(404, content, headers, None, 5, request) with HTTMock(response_post_fail): resp = self._conn.raw_post("/known_path", {'field': 'value'}) self.assertEqual(resp.content, str(["Start can't be blank"]).encode("utf-8")) self.assertEqual(resp.status_code, 404)
Example #29
Source File: test_connection.py From python-keycloak with MIT License | 5 votes |
def test_raw_put_fail(self): @urlmatch(netloc="localhost", path="/known_path", method="put") def response_put_fail(url, request): headers = {'content-type': 'application/json'} content = str(["Start can't be blank"]).encode("utf-8") return response(404, content, headers, None, 5, request) with HTTMock(response_put_fail): resp = self._conn.raw_put("/known_path", {'field': 'value'}) self.assertEqual(resp.content, str(["Start can't be blank"]).encode("utf-8")) self.assertEqual(resp.status_code, 404)
Example #30
Source File: test_client.py From refstack-client with Apache License 2.0 | 5 votes |
def test_get_cpid_from_keystone_failure_handled(self): """Test that get cpid from keystone API failure handled.""" args = rc.parse_cli_args(self.mock_argv()) client = rc.RefstackClient(args) client.tempest_dir = self.test_path client._prep_test() client.logger.warning = MagicMock() client._generate_cpid_from_endpoint = MagicMock() client.conf.add_section('auth') client.conf.set('auth', 'test_accounts_file', '%s/test-accounts.yaml' % self.test_path) self.mock_data() accounts = [ { 'tenant_name': 'tenant_name', 'tenant_id': 'admin_tenant_id', 'password': 'test' } ] self.patch( 'refstack_client.refstack_client.read_accounts_yaml', return_value=accounts) configs = client._get_keystone_config(client.conf) auth_version, url, content = client._generate_keystone_data(configs) @httmock.urlmatch(netloc=r'(.*\.)?127.0.0.1$', path='/v2/tokens') def keystone_api_mock(auth_version, url, request): return None with httmock.HTTMock(keystone_api_mock): client._get_cpid_from_keystone(auth_version, url, content) client._generate_cpid_from_endpoint.assert_called_with(url)