Skip to content

Commit

Permalink
Have DELETE on /torrent use the query string to set remove options as…
Browse files Browse the repository at this point in the history
… having a body with a body in a DELETE isn't a good idea.
  • Loading branch information
aresch committed Feb 15, 2016
1 parent bcead08 commit 02bac62
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
2 changes: 2 additions & 0 deletions spritzle/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def setup_routes(self):
spritzle.resource.torrent.post_torrent)
app.router.add_route('DELETE', '/torrent',
spritzle.resource.torrent.delete_torrent)
app.router.add_route('DELETE', '/torrent/{tid}',
spritzle.resource.torrent.delete_torrent)

def stop(self):
core.stop()
Expand Down
10 changes: 4 additions & 6 deletions spritzle/resource/torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_valid_handle(tid):
handle = core.session.find_torrent(lt.sha1_hash(binascii.unhexlify(tid)))
if not handle.is_valid():
raise HttpProcessingError(
code=400, message='Invalid info-hash: ' + tid)
code=404, message='Torrent not found: ' + tid)

return handle

Expand Down Expand Up @@ -127,10 +127,8 @@ async def delete_torrent(request):
# see libtorrent.options_t for valid options
options = 0

body = await request.json()
if body:
for key in body.keys():
options = options | lt.options_t.names[key]
for key in request.GET.keys():
options = options | lt.options_t.names[key]

if tid is None:
# If tid is None, we remove all the torrents
Expand All @@ -142,4 +140,4 @@ async def delete_torrent(request):
handle = get_valid_handle(tid)
core.session.remove_torrent(handle, options)

return web.json_response('')
return web.Response()
32 changes: 17 additions & 15 deletions spritzle/tests/resource/test_torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ async def test_get_torrent():

with assert_raises(aiohttp.errors.HttpProcessingError) as e:
request.match_info['tid'] = 'a0'*20
ts, response = await json_response(torrent.get_torrent(request))
assert e.code == 400
response = await torrent.get_torrent(request)

assert e.exception.code == 404


@run_until_complete
Expand Down Expand Up @@ -136,7 +137,7 @@ async def test_add_torrent_lt_runtime_error():
with patch('spritzle.core.core.session.add_torrent', add_torrent):
with assert_raises(aiohttp.errors.HttpProcessingError) as e:
await json_response(torrent.post_torrent(request))
assert e.exception.code == 500
assert e.exception.code == 500


@run_until_complete
Expand Down Expand Up @@ -165,32 +166,33 @@ async def test_remove_torrent():
await test_post_torrent()
tid = '44a040be6d74d8d290cd20128788864cbf770719'

async def json():
return {'delete_files': True}

request = MagicMock()
request.match_info = {
'tid': tid
}
request.json = json
request.GET = {
'delete_files': True,
}

response = await torrent.delete_torrent(request)
assert response.status == 200

await json_response(torrent.delete_torrent(request))
request = MagicMock()
request.match_info = {}

assert tid not in await json_response(torrent.get_torrent(request))
response = await torrent.get_torrent(request)
assert response.status == 200


@run_until_complete
async def test_remove_torrent_all():
await test_post_torrent()

async def json():
return {'delete_files': True}

request = MagicMock()
request.json = json
request.match_info = {}
request.GET = {
'delete_files': True,
}

await json_response(torrent.delete_torrent(request))
response = await torrent.delete_torrent(request)
assert response.status == 200
assert len(torrent.get_torrent_list()) == 0

0 comments on commit 02bac62

Please sign in to comment.