コード例 #1
0
ファイル: test_downloads.py プロジェクト: wenxq/httpie
    def test_download_output_from_content_disposition(self, httpbin_both):
        with tempfile.TemporaryDirectory() as tmp_dirname, open(
                os.devnull, 'w') as devnull:
            orig_cwd = os.getcwd()
            os.chdir(tmp_dirname)
            try:
                assert not os.path.isfile('filename.bin')
                downloader = Downloader(progress_file=devnull)
                downloader.start(final_response=Response(
                    url=httpbin_both.url + '/',
                    headers={
                        'Content-Length':
                        5,
                        'Content-Disposition':
                        'attachment; filename="filename.bin"',
                    }),
                                 initial_url='/')
                downloader.chunk_downloaded(b'12345')
                downloader.finish()
                downloader.failed()  # Stop the reporter
                assert not downloader.interrupted
                downloader._progress_reporter.join()

                # TODO: Auto-close the file in that case?
                downloader._output_file.close()
                assert os.path.isfile('filename.bin')
            finally:
                os.chdir(orig_cwd)
コード例 #2
0
 def test_download_no_Content_Length(self, httpbin_both):
     devnull = open(os.devnull, 'w')
     downloader = Downloader(output_file=devnull, progress_file=devnull)
     downloader.start(Response(url=httpbin_both.url + '/'))
     time.sleep(1.1)
     downloader.chunk_downloaded(b'12345')
     downloader.finish()
     assert not downloader.interrupted
コード例 #3
0
 def test_download_no_Content_Length(self, httpbin_both):
     devnull = open(os.devnull, 'w')
     downloader = Downloader(output_file=devnull, progress_file=devnull)
     downloader.start(Response(url=httpbin_both.url + '/'))
     time.sleep(1.1)
     downloader.chunk_downloaded(b'12345')
     downloader.finish()
     assert not downloader.interrupted
コード例 #4
0
 def test_download_interrupted(self, httpbin_both):
     devnull = open(os.devnull, 'w')
     downloader = Downloader(output_file=devnull, progress_file=devnull)
     downloader.start(
         Response(url=httpbin_both.url + '/', headers={'Content-Length':
                                                       5}))
     downloader.chunk_downloaded(b'1234')
     downloader.finish()
     assert downloader.interrupted
コード例 #5
0
 def test_download_interrupted(self, httpbin_both):
     devnull = open(os.devnull, 'w')
     downloader = Downloader(output_file=devnull, progress_file=devnull)
     downloader.start(Response(
         url=httpbin_both.url + '/',
         headers={'Content-Length': 5}
     ))
     downloader.chunk_downloaded(b'1234')
     downloader.finish()
     assert downloader.interrupted
コード例 #6
0
ファイル: test_downloads.py プロジェクト: yinwin/httpie
 def test_download_interrupted(self, httpbin_both):
     with open(os.devnull, 'w') as devnull:
         downloader = Downloader(output_file=devnull, progress_file=devnull)
         downloader.start(final_response=Response(
             url=httpbin_both.url + '/', headers={'Content-Length': 5}),
                          initial_url='/')
         downloader.chunk_downloaded(b'1234')
         downloader.finish()
         assert downloader.interrupted
         downloader._progress_reporter.join()
コード例 #7
0
ファイル: test_downloads.py プロジェクト: yinwin/httpie
 def test_download_no_Content_Length(self, httpbin_both):
     with open(os.devnull, 'w') as devnull:
         downloader = Downloader(output_file=devnull, progress_file=devnull)
         downloader.start(final_response=Response(url=httpbin_both.url +
                                                  '/'),
                          initial_url='/')
         time.sleep(1.1)
         downloader.chunk_downloaded(b'12345')
         downloader.finish()
         assert not downloader.interrupted
         downloader._progress_reporter.join()
コード例 #8
0
ファイル: test_downloads.py プロジェクト: PKRoma/httpie
 def test_download_with_Content_Length(self, httpbin_both):
     with open(os.devnull, 'w') as devnull:
         downloader = Downloader(output_file=devnull, progress_file=devnull)
         downloader.start(Response(
             url=httpbin_both.url + '/',
             headers={'Content-Length': 10}
         ))
         time.sleep(1.1)
         downloader.chunk_downloaded(b'12345')
         time.sleep(1.1)
         downloader.chunk_downloaded(b'12345')
         downloader.finish()
         assert not downloader.interrupted
         downloader._progress_reporter.join()
コード例 #9
0
ファイル: test_downloads.py プロジェクト: wenxq/httpie
    def test_download_resumed(self, httpbin_both):
        with tempfile.TemporaryDirectory() as tmp_dirname:
            file = os.path.join(tmp_dirname, 'file.bin')
            with open(file, 'a'):
                pass

            with open(os.devnull, 'w') as devnull, open(file,
                                                        'a+b') as output_file:
                # Start and interrupt the transfer after 3 bytes written
                downloader = Downloader(output_file=output_file,
                                        progress_file=devnull)
                downloader.start(final_response=Response(
                    url=httpbin_both.url + '/', headers={'Content-Length': 5}),
                                 initial_url='/')
                downloader.chunk_downloaded(b'123')
                downloader.finish()
                downloader.failed()
                assert downloader.interrupted
                downloader._progress_reporter.join()

            # Write bytes
            with open(file, 'wb') as fh:
                fh.write(b'123')

            with open(os.devnull, 'w') as devnull, open(file,
                                                        'a+b') as output_file:
                # Resume the transfer
                downloader = Downloader(output_file=output_file,
                                        progress_file=devnull,
                                        resume=True)

                # Ensure `pre_request()` is working as expected too
                headers = {}
                downloader.pre_request(headers)
                assert headers['Accept-Encoding'] == 'identity'
                assert headers['Range'] == 'bytes=3-'

                downloader.start(final_response=Response(
                    url=httpbin_both.url + '/',
                    headers={
                        'Content-Length': 5,
                        'Content-Range': 'bytes 3-4/5'
                    },
                    status_code=PARTIAL_CONTENT),
                                 initial_url='/')
                downloader.chunk_downloaded(b'45')
                downloader.finish()
                downloader._progress_reporter.join()
コード例 #10
0
ファイル: test_downloads.py プロジェクト: git2u/httpie
 def test_download_with_Content_Length(self, mock_env, httpbin_both):
     with open(os.devnull, 'w') as devnull:
         downloader = Downloader(mock_env, output_file=devnull)
         downloader.start(
             initial_url='/',
             final_response=Response(
                 url=httpbin_both.url + '/',
                 headers={'Content-Length': 10}
             )
         )
         time.sleep(1.1)
         downloader.chunk_downloaded(b'12345')
         time.sleep(1.1)
         downloader.chunk_downloaded(b'12345')
         downloader.finish()
         assert not downloader.interrupted