[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) agent = Agent(reactor)
bad_body = '{ method": "auth.login" }' bad_body = b'{ method": "auth.login" }'
d = yield agent.request( d = yield agent.request(
'POST', b'POST',
'http://127.0.0.1:%s/json' % self.webserver_listen_port, b'http://127.0.0.1:%s/json' % self.webserver_listen_port,
Headers({'User-Agent': ['Twisted Web Client Example'], Headers({b'User-Agent': [b'Twisted Web Client Example'],
'Content-Type': ['application/json']}), b'Content-Type': [b'application/json']}),
FileBodyProducer(StringIO(bad_body))) FileBodyProducer(StringIO(bad_body)))
yield d yield d

View File

@ -18,6 +18,8 @@ from twisted.trial.unittest import SkipTest
from twisted.web.client import Agent, FileBodyProducer from twisted.web.client import Agent, FileBodyProducer
from twisted.web.http_headers import Headers from twisted.web.http_headers import Headers
from deluge.common import convert_to_utf8
from . import common from . import common
from .common import get_test_data_file from .common import get_test_data_file
from .common_web import WebServerMockBase, WebServerTestBase 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: # 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 # UnicodeDecodeError: 'utf8' codec can't decode byte 0xe5 in position 0: invalid continuation byte
filename = get_test_data_file('filehash_field.torrent') 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( d = yield agent.request(b'POST', url.encode('utf-8'), Headers(convert_to_utf8(headers)),
'POST', FileBodyProducer(StringIO(input_file.encode('utf-8'))))
'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)))
try: try:
body = yield twisted.web.client.readBody(d) body = yield twisted.web.client.readBody(d)
except AttributeError: except AttributeError:

View File

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

View File

@ -213,7 +213,7 @@ class JSON(resource.Resource, component.Component):
if request._disconnected: if request._disconnected:
return '' return ''
response = json.dumps(response) 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.write(compress(response, request))
request.finish() request.finish()
return server.NOT_DONE_YET return server.NOT_DONE_YET

View File

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