Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.
8 changes: 5 additions & 3 deletions consul/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import six
from six.moves import urllib


log = logging.getLogger(__name__)


Expand Down Expand Up @@ -199,10 +198,11 @@ def json(
*one* returns only the first item of the list of items. empty lists are
coerced to None.

*decode* if specified this key will be base64 decoded.
*decode* if specified this key will be base64 decoded

*is_id* only the 'ID' field of the json object will be returned.
"""

def cb(response):
CB.__status(response, allow_404=allow_404)
if response.code == 404:
Expand All @@ -213,7 +213,9 @@ def cb(response):
if decode:
for item in data:
if item.get(decode) is not None:
item[decode] = base64.b64decode(item[decode])
raw_value = base64.b64decode(item[decode])
item[decode] = raw_value.decode('latin-1')

if is_id:
data = data['ID']
if one:
Expand Down
13 changes: 7 additions & 6 deletions tests/test_aio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import base64
import pytest
import six
import struct
import sys

Expand Down Expand Up @@ -39,7 +38,7 @@ def main():
response = yield from c.kv.put('foo', 'bar')
assert response is True
index, data = yield from c.kv.get('foo')
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'
c.close()

loop.run_until_complete(main())
Expand All @@ -52,7 +51,8 @@ def main():
assert c._loop is loop
yield from c.kv.put('foo', struct.pack('i', 1000))
index, data = yield from c.kv.get('foo')
assert struct.unpack('i', data['Value']) == (1000,)
encoded = data['Value'].encode('latin-1')
assert struct.unpack('i', encoded) == (1000,)
c.close()

asyncio.set_event_loop(loop)
Expand All @@ -64,7 +64,8 @@ def main():
c = consul.aio.Consul(port=consul_port, loop=loop)
yield from c.kv.put('foo', struct.pack('i', 1000))
index, data = yield from c.kv.get('foo')
assert struct.unpack('i', data['Value']) == (1000,)
encoded = data['Value'].encode('latin-1')
assert struct.unpack('i', encoded) == (1000,)
c.close()

loop.run_until_complete(main())
Expand All @@ -79,7 +80,7 @@ def main():
index, data = yield from c.kv.get('foo')
assert data is None
index, data = yield from c.kv.get('foo', index=index)
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'
yield from fut
c.close()

Expand Down Expand Up @@ -137,7 +138,7 @@ def get():
index, data = yield from c.kv.get('foo')
assert data is None
index, data = yield from c.kv.get('foo', index=index)
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'
yield from fut
c.close()

Expand Down
21 changes: 10 additions & 11 deletions tests/test_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import time

import pytest
import six

import consul
import consul.std
Expand All @@ -28,7 +27,7 @@ def test_kv(self, consul_port):
assert data is None
assert c.kv.put('foo', 'bar') is True
index, data = c.kv.get('foo')
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'

def test_kv_wait(self, consul_port):
c = consul.Consul(port=consul_port)
Expand All @@ -43,12 +42,12 @@ def test_kv_encoding(self, consul_port):
# test binary
c.kv.put('foo', struct.pack('i', 1000))
index, data = c.kv.get('foo')
assert struct.unpack('i', data['Value']) == (1000,)
assert struct.unpack('i', data['Value'].encode('latin-1')) == (1000,)

# test unicode
c.kv.put('foo', u'bar')
index, data = c.kv.get('foo')
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'

# test empty-string comes back as `None`
c.kv.put('foo', '')
Expand All @@ -72,7 +71,7 @@ def test_kv_put_cas(self, consul_port):
assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex']-1) is False
assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex']) is True
index, data = c.kv.get('foo')
assert data['Value'] == six.b('bar2')
assert data['Value'] == 'bar2'

def test_kv_put_flags(self, consul_port):
c = consul.Consul(port=consul_port)
Expand Down Expand Up @@ -100,7 +99,7 @@ def test_kv_recurse(self, consul_port):
assert [x['Key'] for x in data] == [
'foo/', 'foo/bar1', 'foo/bar2', 'foo/bar3']
assert [x['Value'] for x in data] == [
None, six.b('1'), six.b('2'), six.b('3')]
None, '1', '2', '3']

def test_kv_delete(self, consul_port):
c = consul.Consul(port=consul_port)
Expand Down Expand Up @@ -691,7 +690,7 @@ def test_session_delete_ttl_renew(self, consul_port):
# trying out the behavior
assert c.kv.put('foo', '1', acquire=s) is True
index, data = c.kv.get('foo')
assert data['Value'] == six.b('1')
assert data['Value'] == '1'

c.session.destroy(s)
index, data = c.kv.get('foo')
Expand Down Expand Up @@ -757,13 +756,13 @@ def test_acl_explict_token_use(self, acl_consul):
c.kv.put('foo', 'bar')
c.kv.put('private/foo', 'bar')

assert c.kv.get('foo', token=token)[1]['Value'] == six.b('bar')
assert c.kv.get('foo', token=token)[1]['Value'] == 'bar'
pytest.raises(
consul.ACLPermissionDenied, c.kv.put, 'foo', 'bar2', token=token)
pytest.raises(
consul.ACLPermissionDenied, c.kv.delete, 'foo', token=token)

assert c.kv.get('private/foo')[1]['Value'] == six.b('bar')
assert c.kv.get('private/foo')[1]['Value'] == 'bar'
assert c.kv.get('private/foo', token=token)[1] is None
pytest.raises(
consul.ACLPermissionDenied,
Expand Down Expand Up @@ -828,13 +827,13 @@ def test_acl_implicit_token_use(self, acl_consul):
c.kv.put('private/foo', 'bar')

c_limited = consul.Consul(port=acl_consul.port, token=token)
assert c_limited.kv.get('foo')[1]['Value'] == six.b('bar')
assert c_limited.kv.get('foo')[1]['Value'] == 'bar'
pytest.raises(
consul.ACLPermissionDenied, c_limited.kv.put, 'foo', 'bar2')
pytest.raises(
consul.ACLPermissionDenied, c_limited.kv.delete, 'foo')

assert c.kv.get('private/foo')[1]['Value'] == six.b('bar')
assert c.kv.get('private/foo')[1]['Value'] == 'bar'
assert c_limited.kv.get('private/foo')[1] is None
pytest.raises(
consul.ACLPermissionDenied,
Expand Down
15 changes: 8 additions & 7 deletions tests/test_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import time

import pytest
import six

from tornado import ioloop
from tornado import gen
Expand Down Expand Up @@ -39,7 +38,7 @@ def main():
response = yield c.kv.put('foo', 'bar')
assert response is True
index, data = yield c.kv.get('foo')
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'
loop.stop()
loop.run_sync(main)

Expand All @@ -49,7 +48,8 @@ def main():
c = consul.tornado.Consul(port=consul_port)
yield c.kv.put('foo', struct.pack('i', 1000))
index, data = yield c.kv.get('foo')
assert struct.unpack('i', data['Value']) == (1000,)
encoded = data['Value'].encode('latin-1')
assert struct.unpack('i', encoded) == (1000,)
loop.stop()
loop.run_sync(main)

Expand All @@ -62,7 +62,7 @@ def main():
index, data = yield c.kv.get('foo')
assert data is None
index, data = yield c.kv.get('foo', index=index)
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'
loop.stop()

@gen.coroutine
Expand Down Expand Up @@ -116,7 +116,7 @@ def get():
index, data = yield c.kv.get('foo')
assert data is None
index, data = yield c.kv.get('foo', index=index)
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'
loop.stop()

@gen.coroutine
Expand All @@ -136,13 +136,14 @@ def main():
response = yield c.kv.put('foo', struct.pack('i', 1000))
assert response is True
index, data = yield c.kv.get('foo')
assert struct.unpack('i', data['Value']) == (1000,)
encoded = data['Value'].encode('latin-1')
assert struct.unpack('i', encoded) == (1000,)

# test unicode
response = yield c.kv.put('foo', u'bar')
assert response is True
index, data = yield c.kv.get('foo')
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'

# test empty-string comes back as `None`
response = yield c.kv.put('foo', '')
Expand Down
9 changes: 4 additions & 5 deletions tests/test_twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import struct

import pytest
import six
from twisted.internet import defer, reactor

import consul
Expand Down Expand Up @@ -32,14 +31,14 @@ def test_kv(self, consul_port):
response = yield c.kv.put('foo', 'bar')
assert response is True
index, data = yield c.kv.get('foo')
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'

@pytest.inlineCallbacks
def test_kv_binary(self, consul_port):
c = consul.twisted.Consul(port=consul_port)
yield c.kv.put('foo', struct.pack('i', 1000))
index, data = yield c.kv.get('foo')
assert struct.unpack('i', data['Value']) == (1000,)
assert struct.unpack('i', data['Value'].encode('latin-1')) == (1000,)

@pytest.inlineCallbacks
def test_kv_missing(self, consul_port):
Expand All @@ -49,7 +48,7 @@ def test_kv_missing(self, consul_port):
index, data = yield c.kv.get('foo')
assert data is None
index, data = yield c.kv.get('foo', index=index)
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'

@pytest.inlineCallbacks
def test_kv_put_flags(self, consul_port):
Expand Down Expand Up @@ -94,7 +93,7 @@ def put():
index, data = yield c.kv.get('foo')
assert data is None
index, data = yield c.kv.get('foo', index=index)
assert data['Value'] == six.b('bar')
assert data['Value'] == 'bar'

@pytest.inlineCallbacks
def test_transaction(self, consul_port):
Expand Down