forked from jaysonsantos/python-binary-memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_server_parsing.py
74 lines (57 loc) · 2.69 KB
/
test_server_parsing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
import unittest
import six
import bmemcached
if six.PY3:
from unittest import mock
else:
import mock
class TestServerParsing(unittest.TestCase):
def testAcceptStringServer(self):
client = bmemcached.Client('{}:11211'.format(os.environ['MEMCACHED_HOST']))
self.assertEqual(len(list(client.servers)), 1)
def testAcceptIterableServer(self):
client = bmemcached.Client(
['{}:11211'.format(os.environ['MEMCACHED_HOST']), '{}:11211'.format(os.environ['MEMCACHED_HOST'])])
self.assertEqual(len(list(client.servers)), 2)
def testNoPortGiven(self):
server = bmemcached.protocol.Protocol(os.environ['MEMCACHED_HOST'])
self.assertEqual(server.host, os.environ['MEMCACHED_HOST'])
self.assertEqual(server.port, 11211)
def testInvalidPort(self):
with self.assertRaises(ValueError):
bmemcached.protocol.Protocol('{}:blah'.format(os.environ['MEMCACHED_HOST']))
def testNonStandardPort(self):
server = bmemcached.protocol.Protocol('{}:5000'.format(os.environ['MEMCACHED_HOST']))
self.assertEqual(server.host, os.environ['MEMCACHED_HOST'])
self.assertEqual(server.port, 5000)
def testAcceptUnixSocket(self):
client = bmemcached.Client('/tmp/memcached.sock')
self.assertEqual(len(list(client.servers)), 1)
@mock.patch.object(bmemcached.protocol.Protocol, '_get_response')
def testPassCredentials(self, mocked_response):
"""
If username/password passed to Client, auto-authenticate.
"""
mocked_response.return_value = (0, 0, 0, 0, 0, 0, 0, 0, 0, [b'PLAIN'])
client = bmemcached.Client('{}:11211'.format(os.environ['MEMCACHED_HOST']), username='user',
password='password')
server = list(client.servers)[0]
# Force a connection. Normally this is only done when we make a request to the
# server.
server._send_authentication()
self.assertTrue(server.authenticated)
@mock.patch.object(bmemcached.protocol.Protocol, '_get_response')
def testNoCredentialsNoAuth(self, mocked_response):
mocked_response.return_value = (0, 0, 0, 0, 0, 0x01, 0, 0, 0, [b'PLAIN'])
client = bmemcached.Client('{}:11211'.format(os.environ['MEMCACHED_HOST']))
server = list(client.servers)[0]
# Force a connection. Normally this is only done when we make a request to the
# server.
server._send_authentication()
self.assertFalse(server.authenticated)
def testNoServersSupplied(self):
"""
Raise assertion if the server list is empty.
"""
self.assertRaises(AssertionError, bmemcached.Client, [])