2013-07-14 15:57:06 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-05-29 01:16:17 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
2020-03-11 16:33:15 -05:00
|
|
|
from elasticsearch.client.utils import _make_path, _escape, query_params
|
2014-05-29 01:16:17 +02:00
|
|
|
from elasticsearch.compat import PY2
|
2013-07-14 15:57:06 +02:00
|
|
|
|
2013-08-28 19:11:28 +02:00
|
|
|
from ..test_cases import TestCase, SkipTest
|
|
|
|
|
|
2019-05-10 09:16:33 -06:00
|
|
|
|
2020-03-11 16:33:15 -05:00
|
|
|
class TestQueryParams(TestCase):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.calls = []
|
|
|
|
|
|
|
|
|
|
@query_params("simple_param")
|
|
|
|
|
def func_to_wrap(self, *args, **kwargs):
|
|
|
|
|
self.calls.append((args, kwargs))
|
|
|
|
|
|
|
|
|
|
def test_handles_params(self):
|
|
|
|
|
self.func_to_wrap(params={"simple_param_2": "2"}, simple_param="3")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
self.calls,
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
(),
|
|
|
|
|
{
|
|
|
|
|
"params": {"simple_param": b"3", "simple_param_2": "2"},
|
|
|
|
|
"headers": {},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_handles_headers(self):
|
|
|
|
|
self.func_to_wrap(headers={"X-Opaque-Id": "app-1"})
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
self.calls, [((), {"params": {}, "headers": {"x-opaque-id": "app-1"}})]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_handles_opaque_id(self):
|
|
|
|
|
self.func_to_wrap(opaque_id="request-id")
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
self.calls, [((), {"params": {}, "headers": {"x-opaque-id": "request-id"}})]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2013-07-14 15:57:06 +02:00
|
|
|
class TestMakePath(TestCase):
|
|
|
|
|
def test_handles_unicode(self):
|
2014-05-29 01:16:17 +02:00
|
|
|
id = "中文"
|
2019-05-10 09:16:33 -06:00
|
|
|
self.assertEquals(
|
|
|
|
|
"/some-index/type/%E4%B8%AD%E6%96%87", _make_path("some-index", "type", id)
|
|
|
|
|
)
|
2013-07-14 15:57:06 +02:00
|
|
|
|
|
|
|
|
def test_handles_utf_encoded_string(self):
|
2014-02-21 16:53:56 +01:00
|
|
|
if not PY2:
|
2019-05-10 09:16:33 -06:00
|
|
|
raise SkipTest("Only relevant for py2")
|
|
|
|
|
id = "中文".encode("utf-8")
|
|
|
|
|
self.assertEquals(
|
|
|
|
|
"/some-index/type/%E4%B8%AD%E6%96%87", _make_path("some-index", "type", id)
|
|
|
|
|
)
|
2013-07-14 15:57:06 +02:00
|
|
|
|
2017-08-07 15:38:47 -06:00
|
|
|
|
|
|
|
|
class TestEscape(TestCase):
|
|
|
|
|
def test_handles_ascii(self):
|
|
|
|
|
string = "abc123"
|
2019-05-10 09:16:33 -06:00
|
|
|
self.assertEquals(b"abc123", _escape(string))
|
|
|
|
|
|
2017-08-07 15:38:47 -06:00
|
|
|
def test_handles_unicode(self):
|
|
|
|
|
string = "中文"
|
2019-05-10 09:16:33 -06:00
|
|
|
self.assertEquals(b"\xe4\xb8\xad\xe6\x96\x87", _escape(string))
|
2017-08-07 15:38:47 -06:00
|
|
|
|
|
|
|
|
def test_handles_bytestring(self):
|
2019-05-10 09:16:33 -06:00
|
|
|
string = b"celery-task-meta-c4f1201f-eb7b-41d5-9318-a75a8cfbdaa0"
|
|
|
|
|
self.assertEquals(string, _escape(string))
|