Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'responses' in functional components in Python. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
def test_almost_empty_notification():
set_cwd(tempfile.mkdtemp())
conf = {
"notification": {
"enabled": True,
"url": "http://localhost/notification",
},
}
responses.add(responses.POST, "http://localhost/notification")
task(1, {}, conf, {})
assert len(responses.calls) == 1
assert responses.calls[0].request.body == "data=null&task_id=1"
responses.add(responses.POST, "http://localhost/notification")
task(1, {}, conf, {
"info": {
"id": 1,
},
})
assert len(responses.calls) == 2
assert sorted(responses.calls[1].request.body.split("&")) == [
"data=%7B%22id%22%3A+1%7D", "task_id=1"
]
def test_token_expired_automatically_refresh_authentication(self):
self.first_call = True
def request_callback(request):
if self.first_call:
self.first_call = False
return (401, {'content_type': 'application/json'}, json.dumps({"error": "Token expired"}))
else:
self.first_call = None
return (201, {'content_type': 'application/json'}, '')
responses.add_callback(
responses.POST, self.wrapper.test().data,
callback=request_callback,
content_type='application/json',
)
response = self.wrapper.test().post()
# refresh_authentication method should be able to update api_params
self.assertEqual(response._api_params['token'], 'new_token')
def test_transfer_edit(self):
param = {'on_hold': False}
result = mock_file('fake_transfer')
url = "{}/dummy_id".format(self.base_url)
responses.add(responses.PATCH, url, status=200, body=json.dumps(result),
match_querystring=True)
self.assertEqual(self.client.transfer.edit('dummy_id', param), result)
def test_patch_request(self):
responses.add(responses.PATCH, self.wrapper.test().data,
body='{"data": {"key": "value"}}',
status=201,
content_type='application/json')
response = self.wrapper.test().patch()
self.assertEqual(response().data, {'data': {'key': 'value'}})
def test_unicode_get_search(self):
responses.add(responses.GET, DEFAULT_URL, body=b'{}', status=200)
try:
self.api.GetSearch(term="#ابشري_قابوس_جاء")
except Exception as e:
self.fail(e)
def test_iter_from_network():
"""
Test that a CoNLL file over a network can be iterated.
"""
TEST_CONLL_URL = 'https://myconllrepo.com/english/train'
with open(fixture_location('basic.conll')) as f:
responses.add(responses.GET, TEST_CONLL_URL, body=f.read())
expected_ids = ['fr-ud-dev_0000{}'.format(i) for i in range(1, 5)]
actual_ids = [sent.id for sent in iter_from_url(TEST_CONLL_URL)]
assert expected_ids == actual_ids
def test_invoice_cancel(self):
result = mock_file('fake_invoice_cancel')
url = '{}/{}/cancel'.format(self.base_url, 'fake_invoice_id')
responses.add(responses.POST, url, status=200, body=json.dumps(result),
match_querystring=True)
self.assertEqual(self.client.invoice.cancel('fake_invoice_id'), result)
def test_an_exception_is_raised_when_we_hit_an_error():
responses.add(responses.GET, rest_url('bug', 1017315),
body="It's all broken", status=500,
content_type='application/json', match_querystring=True)
bugzilla = Bugsy()
with pytest.raises(BugsyException) as e:
bugzilla.get(1017315)
assert str(e.value) == "Message: We received a 500 error with the following: It's all broken Code: None"
def test_api_500_no_answer(self):
responses.add(
responses.Response(method="GET", url=self.api_url, status=500)
)
response = self.client.get(self.badge_url)
assert len(responses.calls) == 1
called = responses.calls[0]
assert called.request.url == self.api_url
assert response.status_code == 502
def test_pedastrianroute_route_short(self):
with codecs.open('testdata/models/routing_pedestrian.json', mode='r', encoding='utf-8') as f:
expectedResponse = f.read()
responses.add(responses.GET, 'https://route.ls.hereapi.com/routing/7.2/calculateroute.json',
expectedResponse, status=200)
response = self._api.pedastrian_route([11.0, 12.0], [22.0, 23.0], [herepy.RouteMode.pedestrian, herepy.RouteMode.fastest])
expected_short_route = (
"Mannheim Rd; W Belmont Ave; Cullerton St; E Fullerton Ave; "
"La Porte Ave; E Palmer Ave; N Railroad Ave; W North Ave; "
"E North Ave; E Third St"
)
self.assertEqual(response.route_short, expected_short_route)