'Testing whether the API returns 200 if a download is being stopped'

@deferred(timeout=10)
def test_stop_download(self):

    (video_tdef, _) = self.create_local_torrent(os.path.join(TESTS_DATA_DIR, 'video.avi'))
    download = self.session.start_download_from_tdef(video_tdef, DownloadStartupConfig())
    infohash = video_tdef.get_infohash().encode('hex')
    original_stop = download.stop
    def mocked_stop():
        download.should_stop = True
        download.stop = original_stop
    def verify_removed(_):
        self.assertEqual(len(self.session.get_downloads()), 1)
        download = self.session.get_downloads()[0]
        self.assertTrue(download.should_stop)
    download.stop = mocked_stop
    request_deferred = self.do_request(('downloads/%s' % infohash), post_data={'state': 'stop'}, expected_code=200, expected_json={'modified': True}, request_type='PATCH')
    return request_deferred.addCallback(verify_removed)
