def test_with_proxy(self): config = SearchConfig("foo", "bar") requester = Requester() requester.send = mock.Mock(name="send") requester.send.return_value = Response(200, {"foo": "bar"}) transporter = Transporter(requester, config) headers = RequestOptions.create(config).headers data = {} transporter.write("post", "endpoint/foo", data, None) request = Request( "POST", headers, {}, 2, # Default connect timeout 30, # Default timeout proxies={"https": "https://127.0.0.1:8080"}, ) request.url = "https://foo.algolia.net/endpoint/foo?" requester.send.assert_called_once_with(request)
class TestTransporter(unittest.TestCase): def setUp(self): self.requester = Requester() self.requester.send = mock.Mock(name="send") self.requester.send.return_value = Response(200, {'foo': 'bar'}) self.data = { 'data': 'foo' } self.config = SearchConfig('foo', 'bar') self.request_options = RequestOptions.create(self.config, { 'User-Agent': 'foo', 'createIfNotExists': True, 'readTimeout': 109, 'bodyParam': 'bar' }) self.transporter = Transporter(self.requester, self.config) def test_success_write(self): response = self.transporter.write('post', 'endpoint/foo', self.data, self.request_options) host = self.config.hosts.write()[ 0] # type: Host request = Request( 'POST', self.request_options.headers, self.data, 2, # Default connect timeout 30, # Default timeout ) request.url = 'https://{}/endpoint/foo?createIfNotExists=true'.format( host.url) self.requester.send.assert_called_once_with(request) self.assertEqual({'foo': 'bar'}, response) self.assertEqual(self.requester.send.call_count, 1) def test_success_read(self): response = self.transporter.read('get', 'endpoint/bar', {}, self.request_options) host = self.config.hosts.read()[0] # type: Host request = Request( 'GET', # Upper case letters self.request_options.headers, {'bodyParam': 'bar'}, 2, # Default connect timeout 109 # Customized timeout ) request.url = 'https://{}/endpoint/bar?createIfNotExists=true'.format( host.url) self.requester.send.assert_called_once_with(request) self.assertEqual({'foo': 'bar'}, response) self.assertEqual(self.requester.send.call_count, 1) def test_unreachable_hosts_exception(self): self.requester.send.return_value = Response(300, {'foo': 'bar'}) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.read('get', 'endpoint/bar', {}, self.request_options) self.assertEqual(self.requester.send.call_count, 4) self.requester.send.return_value = Response(100, {'foo': 'bar'}) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.read('get', 'endpoint/bar', {}, self.request_options) # Remains 4, all hosts here down. self.assertEqual(self.requester.send.call_count, 4) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.write('get', 'endpoint/bar', {}, self.request_options) # It's now 5, write have one different host. self.assertEqual(self.requester.send.call_count, 5) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.write('get', 'endpoint/bar', {}, self.request_options) # Remains 5, all hosts here down. self.assertEqual(self.requester.send.call_count, 5) def test_algolia_exception(self): self.requester.send.return_value = Response(401, {'foo': 'bar'}) with self.assertRaises(AlgoliaException) as _: self.transporter.read('get', 'endpoint/bar', {}, self.request_options) self.assertEqual(self.requester.send.call_count, 1)
class TestTransporter(unittest.TestCase): def setUp(self): self.requester = Requester() self.requester.send = mock.Mock(name="send") self.requester.send.return_value = Response(200, {'foo': 'bar'}) self.data = {'data': 'foo'} self.config = SearchConfig('foo', 'bar') self.request_options = RequestOptions.create( self.config, { 'User-Agent': 'foo', 'createIfNotExists': True, 'readTimeout': 109, 'bodyParam': 'bar' }) self.transporter = Transporter(self.requester, self.config) def test_success_write(self): response = self.transporter.write('post', 'endpoint/foo', self.data, self.request_options) host = self.config.hosts.write()[0] # type: Host request = Request( 'POST', self.request_options.headers, self.data, 2, # Default connect timeout 30, # Default timeout ) request.url = 'https://{}/endpoint/foo?createIfNotExists=true'.format( host.url) self.requester.send.assert_called_once_with(request) self.assertEqual({'foo': 'bar'}, response) self.assertEqual(self.requester.send.call_count, 1) def test_success_read(self): response = self.transporter.read('get', 'endpoint/bar', {}, self.request_options) host = self.config.hosts.read()[0] # type: Host request = Request( 'GET', # Upper case letters self.request_options.headers, {'bodyParam': 'bar'}, 2, # Default connect timeout 109 # Customized timeout ) request.url = 'https://{}/endpoint/bar?createIfNotExists=true'.format( host.url) self.requester.send.assert_called_once_with(request) self.assertEqual({'foo': 'bar'}, response) self.assertEqual(self.requester.send.call_count, 1) def test_unreachable_hosts_exception(self): self.requester.send.return_value = Response(300, {'foo': 'bar'}) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.read('get', 'endpoint/bar', {}, self.request_options) self.assertEqual(self.requester.send.call_count, 4) self.requester.send.return_value = Response(100, {'foo': 'bar'}) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.read('get', 'endpoint/bar', {}, self.request_options) # Remains 4, all hosts here down. self.assertEqual(self.requester.send.call_count, 4) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.write('get', 'endpoint/bar', {}, self.request_options) # It's now 5, write have one different host. self.assertEqual(self.requester.send.call_count, 5) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.write('get', 'endpoint/bar', {}, self.request_options) # Remains 5, all hosts here down. self.assertEqual(self.requester.send.call_count, 5) def test_algolia_exception(self): self.requester.send.return_value = Response(401, {'foo': 'bar'}) with self.assertRaises(AlgoliaException) as _: self.transporter.read('get', 'endpoint/bar', {}, self.request_options) self.assertEqual(self.requester.send.call_count, 1)
class TestTransporter(unittest.TestCase): def setUp(self): self.requester = Requester() self.requester.send = mock.Mock(name="send") self.requester.send.return_value = Response(200, {"foo": "bar"}) self.data = {"data": "foo"} self.config = SearchConfig("foo", "bar") self.request_options = RequestOptions.create( self.config, { "User-Agent": "foo", "createIfNotExists": True, "readTimeout": 109, "bodyParam": "bar", }, ) self.transporter = Transporter(self.requester, self.config) def test_success_write(self): response = self.transporter.write( "post", "endpoint/foo", self.data, self.request_options ) host = self.config.hosts.write()[0] # type: Host request = Request( "POST", self.request_options.headers, self.data, 2, # Default connect timeout 30, # Default timeout ) request.url = "https://{}/endpoint/foo?createIfNotExists=true".format(host.url) self.requester.send.assert_called_once_with(request) self.assertEqual({"foo": "bar"}, response) self.assertEqual(self.requester.send.call_count, 1) def test_success_read(self): response = self.transporter.read( "get", "endpoint/bar", {}, self.request_options ) host = self.config.hosts.read()[0] # type: Host request = Request( "GET", # Upper case letters self.request_options.headers, {"bodyParam": "bar"}, 2, # Default connect timeout 109, # Customized timeout ) request.url = "https://{}/endpoint/bar?createIfNotExists=true".format(host.url) self.requester.send.assert_called_once_with(request) self.assertEqual({"foo": "bar"}, response) self.assertEqual(self.requester.send.call_count, 1) def test_unreachable_hosts_exception(self): self.requester.send.return_value = Response(300, {"foo": "bar"}) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.read("get", "endpoint/bar", {}, self.request_options) self.assertEqual(self.requester.send.call_count, 4) self.requester.send.return_value = Response(100, {"foo": "bar"}) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.read("get", "endpoint/bar", {}, self.request_options) # Remains 4, all hosts here down. self.assertEqual(self.requester.send.call_count, 4) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.write("get", "endpoint/bar", {}, self.request_options) # It's now 5, write have one different host. self.assertEqual(self.requester.send.call_count, 5) with self.assertRaises(AlgoliaUnreachableHostException) as _: self.transporter.write("get", "endpoint/bar", {}, self.request_options) # Remains 5, all hosts here down. self.assertEqual(self.requester.send.call_count, 5) def test_algolia_exception(self): self.requester.send.return_value = Response(401, {"foo": "bar"}) with self.assertRaises(AlgoliaException) as _: self.transporter.read("get", "endpoint/bar", {}, self.request_options) self.assertEqual(self.requester.send.call_count, 1)