[WebUI] Markup byte strings for twisted request

This commit is contained in:
Calum Lind 2017-02-12 11:26:31 +00:00
parent 608ecae5fd
commit ba41110c27
5 changed files with 27 additions and 25 deletions

View file

@ -164,11 +164,11 @@ class WebAPITestCase(WebServerTestBase):
"""
agent = Agent(reactor)
bad_body = '{ method": "auth.login" }'
bad_body = b'{ method": "auth.login" }'
d = yield agent.request(
'POST',
'http://127.0.0.1:%s/json' % self.webserver_listen_port,
Headers({'User-Agent': ['Twisted Web Client Example'],
'Content-Type': ['application/json']}),
b'POST',
b'http://127.0.0.1:%s/json' % self.webserver_listen_port,
Headers({b'User-Agent': [b'Twisted Web Client Example'],
b'Content-Type': [b'application/json']}),
FileBodyProducer(StringIO(bad_body)))
yield d

View file

@ -18,6 +18,8 @@ from twisted.trial.unittest import SkipTest
from twisted.web.client import Agent, FileBodyProducer
from twisted.web.http_headers import Headers
from deluge.common import convert_to_utf8
from . import common
from .common import get_test_data_file
from .common_web import WebServerMockBase, WebServerTestBase
@ -39,13 +41,13 @@ class WebServerTestCase(WebServerTestBase, WebServerMockBase):
# encoded to allow dumping the torrent info to json. Otherwise it will fail with:
# UnicodeDecodeError: 'utf8' codec can't decode byte 0xe5 in position 0: invalid continuation byte
filename = get_test_data_file('filehash_field.torrent')
input_file = '{"params": ["%s"], "method": "web.get_torrent_info", "id": 22}' % filename
headers = {'User-Agent': ['Twisted Web Client Example'],
'Content-Type': ['application/json']}
url = 'http://127.0.0.1:%s/json' % self.webserver_listen_port
d = yield agent.request(
'POST',
'http://127.0.0.1:%s/json' % self.webserver_listen_port,
Headers({'User-Agent': ['Twisted Web Client Example'],
'Content-Type': ['application/json']}),
FileBodyProducer(StringIO('{"params": ["%s"], "method": "web.get_torrent_info", "id": 22}' % filename)))
d = yield agent.request(b'POST', url.encode('utf-8'), Headers(convert_to_utf8(headers)),
FileBodyProducer(StringIO(input_file.encode('utf-8'))))
try:
body = yield twisted.web.client.readBody(d)
except AttributeError:

View file

@ -121,8 +121,8 @@ class Auth(JSONComponent):
expires, expires_str = make_expires(self.config['session_timeout'])
checksum = str(make_checksum(session_id))
request.addCookie('_session_id', session_id + checksum,
path=request.base + 'json', expires=expires_str)
request.addCookie(b'_session_id', session_id + checksum,
path=request.base + b'json', expires=expires_str)
log.debug('Creating session for %s', login)
@ -216,8 +216,8 @@ class Auth(JSONComponent):
session['expires'] = expires
_session_id = request.getCookie('_session_id')
request.addCookie('_session_id', _session_id,
path=request.base + 'json', expires=expires_str)
request.addCookie(b'_session_id', _session_id,
path=request.base + b'json', expires=expires_str)
if method:
if not hasattr(method, '_json_export'):

View file

@ -213,7 +213,7 @@ class JSON(resource.Resource, component.Component):
if request._disconnected:
return ''
response = json.dumps(response)
request.setHeader('content-type', 'application/x-json')
request.setHeader(b'content-type', b'application/x-json')
request.write(compress(response, request))
request.finish()
return server.NOT_DONE_YET

View file

@ -78,7 +78,7 @@ def rpath(*paths):
class GetText(resource.Resource):
def render(self, request):
request.setHeader('content-type', 'text/javascript; encoding=utf-8')
request.setHeader(b'content-type', b'text/javascript; encoding=utf-8')
template = Template(filename=rpath('js', 'gettext.js'))
return compress(template.render(), request)
@ -117,7 +117,7 @@ class Upload(resource.Resource):
filenames.append(fn)
log.debug('uploaded %d file(s)', len(filenames))
request.setHeader('content-type', 'text/html')
request.setHeader(b'content-type', b'text/html')
request.setResponseCode(http.OK)
return compress(json.dumps({
'success': True,
@ -138,7 +138,7 @@ class Render(resource.Resource):
filename = os.path.join('render', request.render_file)
template = Template(filename=rpath(filename))
request.setHeader('content-type', 'text/html')
request.setHeader(b'content-type', b'text/html')
request.setResponseCode(http.OK)
return compress(template.render(), request)
@ -158,9 +158,9 @@ class Tracker(resource.Resource):
def on_got_icon(self, icon, request):
if icon:
request.setHeader('cache-control',
'public, must-revalidate, max-age=86400')
request.setHeader('content-type', icon.get_mimetype())
request.setHeader(b'cache-control',
b'public, must-revalidate, max-age=86400')
request.setHeader(b'content-type', icon.get_mimetype())
request.setResponseCode(http.OK)
request.write(icon.get_data())
request.finish()
@ -235,7 +235,7 @@ class LookupResource(resource.Resource, component.Component):
path = os.path.join(directory, filename)
log.debug('Serving path: %s', path)
mime_type = mimetypes.guess_type(path)
request.setHeader('content-type', mime_type[0])
request.setHeader(b'content-type', mime_type[0])
with open(path, 'rb') as _file:
data = _file.read()
return compress(data, request)
@ -395,7 +395,7 @@ class ScriptResource(resource.Resource, component.Component):
log.debug('Serving path: %s', path)
mime_type = mimetypes.guess_type(path)
request.setHeader('content-type', mime_type[0])
request.setHeader(b'content-type', mime_type[0])
with open(path, 'rb') as _file:
data = _file.read()
return compress(data, request)
@ -533,7 +533,7 @@ class TopLevel(resource.Resource):
scripts.insert(0, 'gettext.js')
template = Template(filename=rpath('index.html'))
request.setHeader('content-type', 'text/html; charset=utf-8')
request.setHeader(b'content-type', b'text/html; charset=utf-8')
web_config = component.get('Web').get_config()
web_config['base'] = request.base