def test_new_file_and_clobber(self): writer = AntiClobberFileWriter(self.get_path_namer()) session = writer.session() request1 = HTTPRequest('http://example.com/my_file.txt') response1 = HTTPResponse(status_code=200, reason='OK', request=request1) session.process_request(request1) session.process_response(response1) session.save_document(response1) self.assertTrue(os.path.exists('my_file.txt')) session = writer.session() request2 = HTTPRequest('http://example.com/my_file.txt') response2 = HTTPResponse(status_code=200, reason='OK', request=request2) session.process_request(request2) session.process_response(response2) session.save_document(response2) self.assertTrue(os.path.exists('my_file.txt'))
def test_content_disposition(self): writer = AntiClobberFileWriter(self.get_path_namer(), content_disposition=True) test_data = [ ('hello1.txt', 'hello1.txt'), ('hello2.txt;', 'hello2.txt'), ('"hello3.txt"', 'hello3.txt'), ('\'hello4.txt\'', 'hello4.txt'), ] for raw_filename, filename in test_data: session = writer.session() request = HTTPRequest('http://example.com') response = HTTPResponse(status_code=200, reason='OK', request=request) response.fields[ 'Content-Disposition'] = 'attachment; filename={}'.format( raw_filename) session.process_request(request) session.process_response(response) session.save_document(response) print(list(os.walk('.'))) self.assertTrue(os.path.exists(filename))
def test_trust_server_names(self): writer = AntiClobberFileWriter(self.get_path_namer(), trust_server_names=True) session = writer.session() request1 = HTTPRequest('http://example.com') response1 = HTTPResponse(status_code=302, reason='Moved', request=request1) session.process_request(request1) session.process_response(response1) request2 = HTTPRequest('http://example.com/my_file.html') response2 = HTTPResponse(status_code=200, reason='OK', request=request2) session.process_request(request2) session.process_response(response2) session.save_document(response2) print(list(os.walk('.'))) self.assertTrue(os.path.exists('my_file.html'))
def test_adjust_extension(self): writer = AntiClobberFileWriter(self.get_path_namer(), adjust_extension=True) test_data = [ ('text/html', '/mordor', 'mordor.html'), ('text/html', '/mordor?ring.asp', 'mordor?ring.asp.html'), ('text/html', '/mordor?ring.htm', 'mordor?ring.htm'), ('text/plain', '/static/my_file.txt', 'static/my_file.txt'), ('text/css', '/static/style.css', 'static/style.css'), ('text/css', '/static/style.css?hamster.exe', 'static/style.css?hamster.exe.css'), ('text/html', '/static/mojibake.html', 'static/mojibake.html'), ('text/html', '/static/mojibake.html?dolphin.png', 'static/mojibake.html?dolphin.png.html'), ] for mime_type, path, filename in test_data: session = writer.session() request = HTTPRequest('http://example.com' + path) response = HTTPResponse(status_code=200, reason='OK', request=request) response.fields['Content-Type'] = mime_type session.process_request(request) session.process_response(response) session.save_document(response) print(filename, list(os.walk('.'))) self.assertTrue(os.path.exists(filename))
def test_content_disposition(self): writer = AntiClobberFileWriter(self.get_path_namer(), content_disposition=True) test_data = [ ('hello1.txt', 'hello1.txt'), ('hello2.txt;', 'hello2.txt'), ('"hello3.txt"', 'hello3.txt'), ('\'hello4.txt\'', 'hello4.txt'), ] for raw_filename, filename in test_data: session = writer.session() request = HTTPRequest('http://example.com') response = HTTPResponse(status_code=200, reason='OK', request=request) response.fields['Content-Disposition'] = 'attachment; filename={}'.format(raw_filename) session.process_request(request) session.process_response(response) session.save_document(response) print(list(os.walk('.'))) self.assertTrue(os.path.exists(filename))