Python requests_mock.Mocker() Examples
The following are 30
code examples of requests_mock.Mocker().
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
requests_mock
, or try the search function
.
Example #1
Source File: test_pay.py From toss with The Unlicense | 6 votes |
def test_confirm_purchase(): c = TossPayClient(development=True) order_id = str(uuid4()) purchase_result = c.purchase(order_id, 40000, 'test', '', True) payment = c.get_payment(purchase_result.pay_token) with pytest.raises(APIError) as approved_result: c.approve(payment.pay_token) assert approved_result.msg == '사용자 정보가 존재하지 않습니다.' result = c.purchase(order_id, 40000, 'test', '', True) payment = c.get_payment(result.pay_token) token = payment.pay_token with requests_mock.Mocker() as m: # NOTE: toss user-side auth 가 자동화될 수가 없어 mocking 으로 우회 m.post('https://pay.toss.im/api/v1/execute', text='{"code":0,"approvalTime":"2016-11-16 13:59:59"}') approved_result = c.approve(token) assert isinstance(approved_result, ApprovedResult)
Example #2
Source File: test_CustomCaptcha.py From python3-anticaptcha with GNU Affero General Public License v3.0 | 6 votes |
def test_get_result_payload(self): customcaptcha = CustomCaptchaTask.CustomCaptchaTask( anticaptcha_key=self.anticaptcha_key_fail, assignment=self.CUSTOM_TASK ) # check response type assert isinstance(customcaptcha, CustomCaptchaTask.CustomCaptchaTask) with requests_mock.Mocker() as req_mock: req_mock.register_uri("POST", config.create_task_url, json=self.VALID_RESPONSE_JSON) req_mock.register_uri( "POST", config.get_result_url, json=self.VALID_RESPONSE_RESULT_JSON ) customcaptcha.captcha_handler(imageUrl=self.image_url) history = req_mock.request_history assert len(history) == 2 request_payload = history[1].json() # check all dict keys assert ["clientKey", "taskId"] == list(request_payload.keys()) assert request_payload["taskId"] == self.VALID_RESPONSE_JSON["taskId"]
Example #3
Source File: test_Control.py From python3-anticaptcha with GNU Affero General Public License v3.0 | 6 votes |
def test_balance_payload(self): control = AntiCaptchaControl.AntiCaptchaControl(anticaptcha_key=self.anticaptcha_key_true) # check response type assert isinstance(control, AntiCaptchaControl.AntiCaptchaControl) with requests_mock.Mocker() as req_mock: req_mock.post(AntiCaptchaControl.get_balance_url, json=self.ERROR_RESPONSE_JSON) control.get_balance() history = req_mock.request_history assert len(history) == 1 request_payload = history[0].json() # check all dict keys assert ["clientKey",] == list(request_payload.keys()) assert request_payload["clientKey"] == self.anticaptcha_key_true
Example #4
Source File: test_NoCaptcha.py From python3-anticaptcha with GNU Affero General Public License v3.0 | 6 votes |
def test_create_task_payload(self): no_captcha = NoCaptchaTask.NoCaptchaTask(anticaptcha_key=self.anticaptcha_key_fail) # check response type assert isinstance(no_captcha, NoCaptchaTask.NoCaptchaTask) with requests_mock.Mocker() as req_mock: req_mock.post(config.create_task_url, json=self.ERROR_RESPONSE_JSON) no_captcha.captcha_handler(websiteURL=self.WEBSITE_URL, websiteKey=self.WEBSITE_KEY) history = req_mock.request_history assert len(history) == 1 request_payload = history[0].json() # check all dict keys assert ["clientKey", "task", "softId"] == list(request_payload.keys()) assert request_payload["softId"] == config.app_key assert ["type", "websiteURL", "websiteKey", 'recaptchaDataSValue'] == list(request_payload["task"].keys()) assert request_payload["task"]["type"] == "NoCaptchaTask"
Example #5
Source File: test_checker.py From django-healthchecks with MIT License | 6 votes |
def test_service_timeout(settings): settings.HEALTH_CHECKS = { 'database': 'django_healthchecks.contrib.check_dummy_true', 'timeout_service': 'http://timeout.com/api/healthchecks/', } with requests_mock.Mocker() as mock: mock.register_uri( 'GET', 'http://timeout.com/api/healthchecks/', exc=requests.exceptions.Timeout) result, is_healthy = checker.create_report() expected = { 'database': True, 'timeout_service': False } assert result == expected assert is_healthy is False
Example #6
Source File: test_checker.py From django-healthchecks with MIT License | 6 votes |
def test_create_report(settings): settings.HEALTH_CHECKS = { 'database': 'django_healthchecks.contrib.check_dummy_true', 'remote_service': 'https://test.com/api/healthchecks/', } with requests_mock.Mocker() as mock: mock.get( 'https://test.com/api/healthchecks/', text='{"cache_default": true}') result, is_healthy = checker.create_report() expected = { 'database': True, 'remote_service': {'cache_default': True}, } assert result == expected assert is_healthy is True
Example #7
Source File: test_influxdb_consumer.py From panoptes with Apache License 2.0 | 6 votes |
def test_panoptes_influxdb_consumer(self): """Test sending metrics through the InfluxDB client""" output_data_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), u'output/influx_line00.data') output_data = open(output_data_file).read() MockPanoptesConsumer.files = [ u'consumers/influxdb/input/metrics_group00.json', ] with requests_mock.Mocker() as m: m.register_uri('POST', "http://127.0.0.1:8086/write", status_code=204) PanoptesInfluxDBConsumer.factory() self.assertEquals(m.last_request.body.decode("utf-8"), output_data) # Fail on first write to try _send_one_by_one with requests_mock.Mocker() as m: m.register_uri('POST', "http://127.0.0.1:8086/write", response_list=[ {u'status_code': 400}, {u'status_code': 204}]) PanoptesInfluxDBConsumer.factory() self.assertEquals(m.last_request.body.decode("utf-8"), output_data) # TODO: Test sending points in a single batch
Example #8
Source File: test_download.py From audiomate with MIT License | 6 votes |
def test_download_file(sample_zip_data, tmpdir): dl_path = 'http://some.url/thezipfile.zip' target_path = os.path.join(tmpdir.strpath, 'target.zip') with requests_mock.Mocker() as mock: # Return any size (doesn't matter, only for prints) mock.head(requests_mock.ANY, headers={'Content-Length': '100'}) mock.get(dl_path, content=sample_zip_data) download.download_file(dl_path, target_path) assert os.path.isfile(target_path) with open(target_path, 'rb') as f: assert f.read() == sample_zip_data
Example #9
Source File: test_voxforge.py From audiomate with MIT License | 6 votes |
def test_available_files(self, sample_response): with requests_mock.Mocker() as mock: # Return any size (doesn't matter, only for prints) mock.head(requests_mock.ANY, headers={'Content-Length': '100'}) url = 'http://someurl.com/some/download/dir' mock.get(url, text=sample_response) files = voxforge.VoxforgeDownloader.available_files(url) assert set(files) == { 'http://someurl.com/some/download/dir/1337ad-20170321-amr.tgz', 'http://someurl.com/some/download/dir/1337ad-20170321-bej.tgz', 'http://someurl.com/some/download/dir/1337ad-20170321-blf.tgz', 'http://someurl.com/some/download/dir/1337ad-20170321-czb.tgz', 'http://someurl.com/some/download/dir/1337ad-20170321-hii.tgz' }
Example #10
Source File: test_mailabs.py From audiomate with MIT License | 6 votes |
def test_download(self, tar_data, tmpdir): target_folder = tmpdir.strpath downloader = mailabs.MailabsDownloader(tags='de_DE') with requests_mock.Mocker() as mock: # Return any size (doesn't matter, only for prints) mock.head(mailabs.DOWNLOAD_URLS['de_DE'], headers={'Content-Length': '100'}) mock.get(mailabs.DOWNLOAD_URLS['de_DE'], content=tar_data) downloader.download(target_folder) base_path = os.path.join(target_folder, 'common_voice') assert os.path.isfile(os.path.join(base_path, 'cv-valid-dev.csv')) assert os.path.isdir(os.path.join(base_path, 'cv-valid-dev')) assert os.path.isfile(os.path.join(base_path, 'cv-valid-train.csv')) assert os.path.isdir(os.path.join(base_path, 'cv-valid-train'))
Example #11
Source File: test_tatoeba.py From audiomate with MIT License | 6 votes |
def test_download(self, sample_audio_list_tar_bz, sample_sentence_list_tar_bz, sample_audio_content, tmpdir): downloader = io.TatoebaDownloader() with requests_mock.Mocker() as mock: # Return any size (doesn't matter, only for prints) mock.head(requests_mock.ANY, headers={'Content-Length': '100'}) mock.get(tatoeba.AUDIO_LIST_URL, content=sample_audio_list_tar_bz) mock.get(tatoeba.SENTENCE_LIST_URL, content=sample_sentence_list_tar_bz) mock.get('https://audio.tatoeba.org/sentences/eng/141.mp3', content=sample_audio_content) mock.get('https://audio.tatoeba.org/sentences/fra/247.mp3', content=sample_audio_content) mock.get('https://audio.tatoeba.org/sentences/epo/1355.mp3', content=sample_audio_content) mock.get('https://audio.tatoeba.org/sentences/deu/6286.mp3', content=sample_audio_content) mock.get('https://audio.tatoeba.org/sentences/ita/6921520.mp3', content=sample_audio_content) downloader.download(tmpdir.strpath) assert os.path.isfile(os.path.join(tmpdir.strpath, 'meta.txt')) assert not os.path.isfile(os.path.join(tmpdir.strpath, 'audio', 'eng', '141.mp3')) assert os.path.isfile(os.path.join(tmpdir.strpath, 'audio', 'fra', '247.mp3')) assert not os.path.isfile(os.path.join(tmpdir.strpath, 'audio', 'epo', '1355.mp3')) assert os.path.isfile(os.path.join(tmpdir.strpath, 'audio', 'deu', '6286.mp3')) assert os.path.isfile(os.path.join(tmpdir.strpath, 'audio', 'ita', '6921520.mp3'))
Example #12
Source File: test_downloader.py From audiomate with MIT License | 6 votes |
def test_download_tar(self, tar_data, tmpdir): target_folder = tmpdir.strpath corpus_dl = MockArchiveDownloader( MOCK_URL, downloader.ArkType.TAR ) with requests_mock.Mocker() as mock: # Return any size (doesn't matter, only for prints) mock.head(MOCK_URL, headers={'Content-Length': '100'}) mock.get(MOCK_URL, content=tar_data) corpus_dl.download(target_folder) base_folder = os.path.join(target_folder, 'common_voice') assert os.path.isdir(base_folder) assert os.path.isfile(os.path.join(base_folder, 'cv-valid-dev.csv')) assert os.path.isdir(os.path.join(base_folder, 'cv-valid-dev')) assert os.path.isfile(os.path.join(base_folder, 'cv-valid-train.csv')) assert os.path.isdir(os.path.join(base_folder, 'cv-valid-train'))
Example #13
Source File: test_downloader.py From audiomate with MIT License | 6 votes |
def test_download_zip(self, zip_data, tmpdir): target_folder = tmpdir.strpath corpus_dl = MockArchiveDownloader( MOCK_URL, downloader.ArkType.ZIP ) with requests_mock.Mocker() as mock: # Return any size (doesn't matter, only for prints) mock.head(MOCK_URL, headers={'Content-Length': '100'}) mock.get(MOCK_URL, content=zip_data) corpus_dl.download(target_folder) base_folder = os.path.join(target_folder, 'subfolder') assert os.path.isdir(base_folder) assert os.path.isfile(os.path.join(base_folder, 'a.txt')) assert os.path.isfile(os.path.join(base_folder, 'subsub', 'b.txt')) assert os.path.isfile(os.path.join(base_folder, 'subsub', 'c.txt'))
Example #14
Source File: test_downloader.py From audiomate with MIT License | 6 votes |
def test_download_auto(self, zip_data, tmpdir): target_folder = tmpdir.strpath corpus_dl = MockArchiveDownloader( MOCK_URL, downloader.ArkType.AUTO ) with requests_mock.Mocker() as mock: # Return any size (doesn't matter, only for prints) mock.head(MOCK_URL, headers={'Content-Length': '100'}) mock.get(MOCK_URL, content=zip_data) corpus_dl.download(target_folder) base_folder = os.path.join(target_folder, 'subfolder') assert os.path.isdir(base_folder) assert os.path.isfile(os.path.join(base_folder, 'a.txt')) assert os.path.isfile(os.path.join(base_folder, 'subsub', 'b.txt')) assert os.path.isfile(os.path.join(base_folder, 'subsub', 'c.txt'))
Example #15
Source File: test_views.py From django-healthchecks with MIT License | 6 votes |
def test_service_view_remote(rf, settings): settings.HEALTH_CHECKS = { 'remote_service': 'https://test.com/api/healthchecks/', } with requests_mock.Mocker() as mock: mock.get( 'https://test.com/api/healthchecks/', json={"cache_default": True}) request = rf.get('/') view = views.HealthCheckServiceView() result = view.dispatch(request, service='remote_service') expected = {'cache_default': True} data = json.loads(result.content.decode(result.charset)) assert result.status_code == 200 assert result['Content-Type'] == 'application/json' assert data == expected
Example #16
Source File: test_client.py From app-search-python with Apache License 2.0 | 6 votes |
def test_update_documents(self): id = 'INscMGmhmX4' valid_document = {'id': id} other_document = {'body': 'some value'} expected_return = [ {'id': id, 'errors': []}, {'id': 'some autogenerated id', 'errors': []} ] with requests_mock.Mocker() as m: m.register_uri('PATCH', self.document_index_url, json=expected_return, status_code=200) response = self.client.update_documents( self.engine_name, [valid_document, other_document]) self.assertEqual(response, expected_return)
Example #17
Source File: test_client.py From app-search-python with Apache License 2.0 | 6 votes |
def test_get_documents(self): id = 'INscMGmhmX4' expected_return = [ { 'id': id, 'url': 'http://www.youtube.com/watch?v=v1uyQZNg2vE', 'title': 'The Original Grumpy Cat', 'body': 'this is a test' } ] with requests_mock.Mocker() as m: m.register_uri('GET', self.document_index_url, json=expected_return, status_code=200) response = self.client.get_documents(self.engine_name, [id]) self.assertEqual(response, expected_return)
Example #18
Source File: test_client.py From app-search-python with Apache License 2.0 | 6 votes |
def test_get_schema(self): expected_return = { 'square_km': 'text' } with requests_mock.Mocker() as m: url = "{}/engines/{}/schema".format( self.client.session.base_url, self.engine_name) m.register_uri('GET', url, json=expected_return, status_code=200 ) response = self.client.get_schema(self.engine_name) self.assertEqual(response, expected_return)
Example #19
Source File: test_client.py From app-search-python with Apache License 2.0 | 6 votes |
def test_list_engines(self): expected_return = [ {'name': 'myawesomeengine'} ] def match_request_text(request): data = json.loads(request.text) return data["page"]["current"] == 1 and data["page"]["size"] == 20 with requests_mock.Mocker() as m: url = "{}/{}".format(self.client.session.base_url, 'engines') m.register_uri('GET', url, additional_matcher=match_request_text, json=expected_return, status_code=200 ) response = self.client.list_engines() self.assertEqual(response, expected_return)
Example #20
Source File: test_client.py From app-search-python with Apache License 2.0 | 6 votes |
def test_list_engines_with_paging(self): expected_return = [ {'name': 'myawesomeengine'} ] def match_request_text(request): data = json.loads(request.text) return data["page"]["current"] == 10 and data["page"]["size"] == 2 with requests_mock.Mocker() as m: url = "{}/{}".format(self.client.session.base_url, 'engines') m.register_uri( 'GET', url, additional_matcher=match_request_text, json=expected_return, status_code=200 ) response = self.client.list_engines(current=10, size=2) self.assertEqual(response, expected_return)
Example #21
Source File: test_client.py From app-search-python with Apache License 2.0 | 6 votes |
def test_create_engine_with_options(self): engine_name = 'myawesomeengine' expected_return = {'name': engine_name, 'type': 'meta', 'source_engines': [ 'source-engine-1', 'source-engine-2' ]} with requests_mock.Mocker() as m: url = "{}/{}".format(self.client.session.base_url, 'engines') m.register_uri('POST', url, json=expected_return, status_code=200) response = self.client.create_engine( engine_name=engine_name, options={ 'type': 'meta', 'source_engines': [ 'source-engine-1', 'source-engine-2' ] }) self.assertEqual(response, expected_return)
Example #22
Source File: test_client.py From app-search-python with Apache License 2.0 | 6 votes |
def test_destroy_synonym_set(self): synonym_id = 'syn-5b11ac66c9f9292013220ad3' expected_return = { 'deleted': True } with requests_mock.Mocker() as m: url = "{}/engines/{}/synonyms/{}".format( self.client.session.base_url, self.engine_name, synonym_id ) m.register_uri( 'DELETE', url, json=expected_return, status_code=200 ) response = self.client.destroy_synonym_set( self.engine_name, synonym_id ) self.assertEqual(response, expected_return)
Example #23
Source File: test_hls.py From streamlink with BSD 2-Clause "Simplified" License | 6 votes |
def test_hls_non_encrypted(self): streams = [os.urandom(1024) for _ in range(4)] masterPlaylist = self.getMasterPlaylist() playlist = self.getPlaylist(None, "stream{0}.ts") + "#EXT-X-ENDLIST\n" with requests_mock.Mocker() as mock: mock.get("http://mocked/path/master.m3u8", text=masterPlaylist) mock.get("http://mocked/path/playlist.m3u8", text=playlist) for i, stream in enumerate(streams): mock.get("http://mocked/path/stream{0}.ts".format(i), content=stream) # Start streamlink on the generated stream streamlinkResult = self.start_streamlink("http://mocked/path/master.m3u8", kwargs={'start_offset': 1, 'duration': 1}) # Check result, each segment is 1 second, with duration=1 only one segment should be returned expectedResult = b''.join(streams[1:2]) self.assertEqual(streamlinkResult, expectedResult)
Example #24
Source File: test_hls.py From streamlink with BSD 2-Clause "Simplified" License | 6 votes |
def test_hls_ext_audio_es(self): """ m3u8 with ext audio but no options should not download additional streams :return: """ master_url = "http://mocked/path/master.m3u8" expected = ['http://mocked/path/playlist.m3u8', 'http://mocked/path/es.m3u8'] with requests_mock.Mocker() as mock: mock.get(master_url, text=self.playlist) master_stream = self.run_streamlink(master_url, 'es') substreams = master_stream['video'].substreams result = [x.url for x in substreams] # Check result self.assertEqual(result, expected)
Example #25
Source File: test_hls.py From streamlink with BSD 2-Clause "Simplified" License | 6 votes |
def test_hls_ext_audio_all(self): """ m3u8 with ext audio but no options should not download additional streams :return: """ master_url = "http://mocked/path/master.m3u8" expected = ['http://mocked/path/playlist.m3u8', 'http://mocked/path/en.m3u8', 'http://mocked/path/es.m3u8'] with requests_mock.Mocker() as mock: mock.get(master_url, text=self.playlist) master_stream = self.run_streamlink(master_url, 'en,es') substreams = master_stream['video'].substreams result = [x.url for x in substreams] # Check result self.assertEqual(result, expected)
Example #26
Source File: test_connection.py From python-percy-client with MIT License | 6 votes |
def test_get(self, mock): mock.get('http://api.percy.io', text='{"data":"GET Percy"}') data = self.percy_connection.get('http://api.percy.io') self.assertEqual(data['data'], 'GET Percy') auth_header = mock.request_history[0].headers['Authorization'] assert auth_header == 'Token token=foo' # TODO: This test fails, because requests_mock replaces the adapter where the # retry configuration is injected. Can we find a different way to supply mock responses? # @requests_mock.Mocker() # def test_get_error_is_retried(self, mock): # mock.get('http://api.percy.io', [{'text':'{"error":"foo"}', 'status_code':503}, # {'text':'{"data":"GET Percy"}', 'status_code':200}]) # data = self.percy_connection.get('http://api.percy.io') # self.assertEqual(data['data'], 'GET Percy')
Example #27
Source File: test_downloader.py From audiomate with MIT License | 6 votes |
def test_download_moves_files_up(self, zip_data, tmpdir): target_folder = tmpdir.strpath corpus_dl = MockArchiveDownloader( MOCK_URL, downloader.ArkType.AUTO, move_files_up=True ) with requests_mock.Mocker() as mock: # Return any size (doesn't matter, only for prints) mock.head(MOCK_URL, headers={'Content-Length': '100'}) mock.get(MOCK_URL, content=zip_data) corpus_dl.download(target_folder) assert os.path.isfile(os.path.join(target_folder, 'a.txt')) assert os.path.isfile(os.path.join(target_folder, 'subsub', 'b.txt')) assert os.path.isfile(os.path.join(target_folder, 'subsub', 'c.txt'))
Example #28
Source File: test_client.py From app-search-python with Apache License 2.0 | 5 votes |
def test_query_suggestion(self): query = 'query' expected_return = {'meta': {}, 'results': {}} with requests_mock.Mocker() as m: url = "{}/{}".format( self.client.session.base_url, "engines/{}/query_suggestion".format(self.engine_name) ) m.register_uri('GET', url, json=expected_return, status_code=200) response = self.client.query_suggestion( self.engine_name, query, {}) self.assertEqual(response, expected_return)
Example #29
Source File: test_client.py From app-search-python with Apache License 2.0 | 5 votes |
def test_multi_search(self): expected_return = [{'meta': {}, 'results': []}, {'meta': {}, 'results': []}] with requests_mock.Mocker() as m: url = "{}/{}".format( self.client.session.base_url, "engines/{}/multi_search".format(self.engine_name) ) m.register_uri('GET', url, json=expected_return, status_code=200) response = self.client.multi_search(self.engine_name, {}) self.assertEqual(response, expected_return)
Example #30
Source File: test_client.py From app-search-python with Apache License 2.0 | 5 votes |
def test_add_meta_engine_sources(self): target_source_engine_name = 'source-engine-3' expected_return = {'source_engines': [ 'source-engine-1', 'source-engine-2', target_source_engine_name], 'type': 'meta', 'name': self.engine_name} with requests_mock.Mocker() as m: url = "{}/{}".format( self.client.session.base_url, "engines/{}/source_engines".format(self.engine_name) ) m.register_uri('POST', url, json=expected_return, status_code=200) response = self.client.add_meta_engine_sources( self.engine_name, [target_source_engine_name]) self.assertEqual(response, expected_return)