Merge pull request #1061 from ryanpetrello/fix-1042

fix a unicode bug in the stdout endpoint when ?content_encoding=base64
This commit is contained in:
Ryan Petrello 2018-01-25 16:07:21 -05:00 committed by GitHub
commit e2d4ef31fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -4641,7 +4641,7 @@ class UnifiedJobStdout(RetrieveAPIView):
return Response(mark_safe(data))
if target_format == 'json':
if content_encoding == 'base64' and content_format == 'ansi':
return Response({'range': {'start': start, 'end': end, 'absolute_end': absolute_end}, 'content': b64encode(content)})
return Response({'range': {'start': start, 'end': end, 'absolute_end': absolute_end}, 'content': b64encode(content.encode('utf-8'))})
elif content_format == 'html':
return Response({'range': {'start': start, 'end': end, 'absolute_end': absolute_end}, 'content': body})
return Response(data)

View File

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import base64
import json
import re
import shutil
import tempfile
@ -251,3 +253,19 @@ def test_text_with_unicode_stdout(sqlite_copy_expert, Parent, Child, relation,
response = get(url, user=admin, expect=200)
assert response.content.splitlines() == ['%d' % i for i in range(3)]
@pytest.mark.django_db
def test_unicode_with_base64_ansi(sqlite_copy_expert, get, admin):
job = Job()
job.save()
for i in range(3):
JobEvent(job=job, stdout=u'{}\n'.format(i), start_line=i).save()
url = reverse(
'api:job_stdout',
kwargs={'pk': job.pk}
) + '?format=json&content_encoding=base64&content_format=ansi'
response = get(url, user=admin, expect=200)
content = base64.b64decode(json.loads(response.content)['content'])
assert content.splitlines() == ['%d' % i for i in range(3)]