From 37baf3de3c826a5cfa08fe06e8f21a2ffa230c21 Mon Sep 17 00:00:00 2001 From: bendikro Date: Wed, 19 Oct 2016 10:09:22 +0200 Subject: [PATCH] [#2875][Web] Fix: WebUI Json dumps Error --- deluge/tests/data/filehash_field.torrent | 2 + deluge/tests/test_webserver.py | 51 ++++++++++++++++++++++++ deluge/ui/common.py | 2 + 3 files changed, 55 insertions(+) create mode 100644 deluge/tests/data/filehash_field.torrent create mode 100644 deluge/tests/test_webserver.py diff --git a/deluge/tests/data/filehash_field.torrent b/deluge/tests/data/filehash_field.torrent new file mode 100644 index 000000000..99e41f022 --- /dev/null +++ b/deluge/tests/data/filehash_field.torrent @@ -0,0 +1,2 @@ +d13:creation datei1476342472e4:infod5:filesld4:ed2k16:2M2&XE 18:filehash20:f4^S96P՝R6:lengthi54e4:pathl8:tull.txteed4:ed2k16:_G L@8:filehash20:vXd/n136:lengthi54e4:pathl56:還在一個人無聊嗎~還不趕緊上來聊天美.txteee4:name16:torrent_filehash12:piece lengthi32768e6:pieces20:G@g\&\ fB +ee \ No newline at end of file diff --git a/deluge/tests/test_webserver.py b/deluge/tests/test_webserver.py new file mode 100644 index 000000000..f4be92700 --- /dev/null +++ b/deluge/tests/test_webserver.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2016 bendikro +# +# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with +# the additional special exception to link portions of this program with the OpenSSL library. +# See LICENSE for more details. +# + +import json as json_lib +from StringIO import StringIO + +import twisted.web.client +from twisted.internet import defer, reactor +from twisted.web.client import Agent, FileBodyProducer +from twisted.web.http_headers import Headers + +from . import common +from .common import get_test_data_file +from .common_web import WebServerMockBase, WebServerTestBase + +common.disable_new_release_check() + + +class WebServerTestCase(WebServerTestBase, WebServerMockBase): + + @defer.inlineCallbacks + def test_get_torrent_info(self): + """ + + """ + agent = Agent(reactor) + + self.mock_authentication_ignore(self.deluge_web.auth) + self.mock_compress_body() + + # This torrent file contains an uncommon field 'filehash' which must be hex + # 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") + + 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))) + + body = yield twisted.web.client.readBody(d) + json = json_lib.loads(body) + self.assertEqual(None, json["error"]) diff --git a/deluge/ui/common.py b/deluge/ui/common.py index 0ed96b80c..94e531d2d 100644 --- a/deluge/ui/common.py +++ b/deluge/ui/common.py @@ -117,6 +117,8 @@ class TorrentInfo(object): f["sha1"] = f["sha1"].encode('hex') if "ed2k" in f and len(f["ed2k"]) == 16: f["ed2k"] = f["ed2k"].encode('hex') + if "filehash" in f and len(f["filehash"]) == 20: + f["filehash"] = f["filehash"].encode('hex') paths[path] = f dirname = os.path.dirname(path) while dirname: