Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit ecc277e

Browse files
Encoding fixes, for py3
1 parent 48d1560 commit ecc277e

7 files changed

Lines changed: 44 additions & 36 deletions

File tree

coreapi/codecs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def load(self, bytes, base_url=None):
178178
Takes a bytestring and returns a document.
179179
"""
180180
try:
181-
data = json.loads(bytes)
181+
data = json.loads(bytes.decode('utf-8'))
182182
except ValueError as exc:
183183
raise ParseError('Malformed JSON. %s' % exc)
184184

coreapi/transport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ def transition(self, url, trans=None, parameters=None):
5353
opts = {}
5454

5555
response = requests.request(method, url, **opts)
56-
if not response.text:
56+
if not response.content:
5757
return None
5858

5959
content_type = response.headers.get('content-type')
6060
codec_class = _get_registered_codec(content_type)
6161
codec = codec_class()
62-
return codec.load(response.text, base_url=url)
62+
return codec.load(response.content, base_url=url)
6363

6464

6565
REGISTERED_SCHEMES = {

tests/test_codecs.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_minimal_document(json_codec):
6767
"""
6868
Ensure we can load the smallest possible valid JSON encoding.
6969
"""
70-
doc = json_codec.load('{"_type":"document"}')
70+
doc = json_codec.load(b'{"_type":"document"}')
7171
assert isinstance(doc, Document)
7272
assert doc.url == ''
7373
assert doc.title == ''
@@ -81,7 +81,7 @@ def test_malformed_json(json_codec):
8181
Invalid JSON should raise a ParseError.
8282
"""
8383
with pytest.raises(ParseError):
84-
json_codec.load('_')
84+
json_codec.load(b'_')
8585

8686

8787
def test_not_a_document(json_codec):
@@ -90,7 +90,7 @@ def test_not_a_document(json_codec):
9090
should raise a ParseError.
9191
"""
9292
with pytest.raises(ParseError):
93-
json_codec.load('{}')
93+
json_codec.load(b'{}')
9494

9595

9696
# Encodings may have a verbose and a compact style.
@@ -140,27 +140,27 @@ def test_link_encodings(json_codec):
140140
# Tests for graceful ommissions.
141141

142142
def test_invalid_document_meta_ignored(json_codec):
143-
doc = json_codec.load('{"_type": "document", "_meta": 1, "a": 1}')
143+
doc = json_codec.load(b'{"_type": "document", "_meta": 1, "a": 1}')
144144
assert doc == Document(content={"a": 1})
145145

146146

147147
def test_invalid_document_url_ignored(json_codec):
148-
doc = json_codec.load('{"_type": "document", "_meta": {"url": 1}, "a": 1}')
148+
doc = json_codec.load(b'{"_type": "document", "_meta": {"url": 1}, "a": 1}')
149149
assert doc == Document(content={"a": 1})
150150

151151

152152
def test_invalid_document_title_ignored(json_codec):
153-
doc = json_codec.load('{"_type": "document", "_meta": {"title": 1}, "a": 1}')
153+
doc = json_codec.load(b'{"_type": "document", "_meta": {"title": 1}, "a": 1}')
154154
assert doc == Document(content={"a": 1})
155155

156156

157157
def test_invalid_link_url_ignored(json_codec):
158-
doc = json_codec.load('{"_type": "document", "link": {"_type": "link", "url": 1}}')
158+
doc = json_codec.load(b'{"_type": "document", "link": {"_type": "link", "url": 1}}')
159159
assert doc == Document(content={"link": Link()})
160160

161161

162162
def test_invalid_link_fields_ignored(json_codec):
163-
doc = json_codec.load('{"_type": "document", "link": {"_type": "link", "fields": 1}}')
163+
doc = json_codec.load(b'{"_type": "document", "link": {"_type": "link", "fields": 1}}')
164164
assert doc == Document(content={"link": Link()})
165165

166166

tests/test_document.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ def test_array_equality(array):
342342
assert array == [{'a': 1}, {'b': 2}, {'c': 3}]
343343

344344

345+
# Container types support len.
346+
347+
def test_document_len(doc):
348+
assert len(doc) == 5
349+
350+
351+
def test_object_len(obj):
352+
assert len(obj) == 2
353+
354+
345355
# Documents meet the Core API constraints.
346356

347357
def test_document_url_must_be_string():

tests/test_integration.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,39 @@
44
import pytest
55

66

7-
@pytest.fixture
8-
def document():
9-
return load(bytestring())
7+
encoded = (
8+
b'{"_type":"document","_meta":{"url":"http://example.org"},'
9+
b'"a":123,"next":{"_type":"link"}}'
10+
)
1011

1112

1213
@pytest.fixture
13-
def bytestring():
14-
return (
15-
b'{"_type":"document","_meta":{"url":"http://example.org"},'
16-
b'"a":123,"next":{"_type":"link"}}'
17-
)
14+
def document():
15+
return load(encoded)
1816

1917

2018
class MockResponse(object):
21-
def __init__(self, text):
22-
self.text = text
19+
def __init__(self, content):
20+
self.content = content
2321
self.headers = {}
2422

2523

2624
# Basic integration tests.
2725

28-
def test_load(bytestring):
29-
assert load(bytestring) == {
26+
def test_load():
27+
assert load(encoded) == {
3028
"a": 123,
3129
"next": Link(url='http://example.org')
3230
}
3331

3432

3533
def test_dump(document):
36-
assert dump(document) == bytestring()
34+
assert dump(document) == encoded
3735

3836

3937
def test_get(monkeypatch):
4038
def mockreturn(method, url):
41-
return MockResponse('{"_type": "document", "example": 123}')
39+
return MockResponse(b'{"_type": "document", "example": 123}')
4240

4341
monkeypatch.setattr(requests, 'request', mockreturn)
4442

@@ -48,7 +46,7 @@ def mockreturn(method, url):
4846

4947
def test_follow(monkeypatch, document):
5048
def mockreturn(method, url):
51-
return MockResponse('{"_type": "document", "example": 123}')
49+
return MockResponse(b'{"_type": "document", "example": 123}')
5250

5351
monkeypatch.setattr(requests, 'request', mockreturn)
5452

tests/test_transport.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ def http():
1111

1212

1313
class MockResponse(object):
14-
def __init__(self, text):
15-
self.text = text
14+
def __init__(self, content):
15+
self.content = content
1616
self.headers = {}
1717

1818

@@ -37,7 +37,7 @@ def test_missing_hostname():
3737

3838
def test_follow(monkeypatch, http):
3939
def mockreturn(method, url):
40-
return MockResponse('{"_type": "document", "example": 123}')
40+
return MockResponse(b'{"_type": "document", "example": 123}')
4141

4242
monkeypatch.setattr(requests, 'request', mockreturn)
4343

@@ -50,8 +50,9 @@ def mockreturn(method, url):
5050

5151
def test_follow_with_parameters(monkeypatch, http):
5252
def mockreturn(method, url, params):
53+
insert = params['example'].encode('utf-8')
5354
return MockResponse(
54-
'{"_type": "document", "example": "%s"}' % params['example']
55+
b'{"_type": "document", "example": "' + insert + b'"}'
5556
)
5657

5758
monkeypatch.setattr(requests, 'request', mockreturn)
@@ -66,7 +67,8 @@ def mockreturn(method, url, params):
6667

6768
def test_create(monkeypatch, http):
6869
def mockreturn(method, url, data, headers):
69-
return MockResponse('{"_type": "document", "data": %s}' % data)
70+
insert = data.encode('utf-8')
71+
return MockResponse(b'{"_type": "document", "data": ' + insert + b'}')
7072

7173
monkeypatch.setattr(requests, 'request', mockreturn)
7274

tox.ini

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
[tox]
2-
envlist = py34, py33, py27
2+
envlist = py34,py33,py27
3+
[testenv]
4+
deps = -rrequirements.txt
35
commands = ./runtests
4-
setenv =
5-
PYTHONDONTWRITEBYTECODE=1
6-
deps =
7-
-rrequirements.txt

0 commit comments

Comments
 (0)