1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-21 07:38:07 +02:00

[api-minor] Ensure that the getDocument Promise is rejected if the loadingTask is destroyed, or an Error is thrown, inside of the onPassword callback (issue 7806)

This patch also removes the `UpdatePassword` message, in favour of using the `sendWithPromise` method of `MessageHandler`.
Furthermore, the patch also refactors the `BasePdfManager_updatePassword`/`BasePdfManager_passwordChanged` methods (in pdf_manager.js), and the `pdfManagerReady` function (in worker.js).
This commit is contained in:
Jonas Jenwald 2016-12-31 13:59:07 +01:00
parent 59afb4b9f0
commit 27513cd23b
4 changed files with 112 additions and 71 deletions

View file

@ -191,7 +191,7 @@ describe('api', function() {
});
var result1 = passwordNeededLoadingTask.promise.then(function () {
done.fail('shall fail with no password');
return passwordNeededLoadingTask.destroy();
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (data) {
expect(data instanceof PasswordException).toEqual(true);
expect(data.code).toEqual(PasswordResponses.NEED_PASSWORD);
@ -203,7 +203,7 @@ describe('api', function() {
});
var result2 = passwordIncorrectLoadingTask.promise.then(function () {
done.fail('shall fail with wrong password');
return passwordNeededLoadingTask.destroy();
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (data) {
expect(data instanceof PasswordException).toEqual(true);
expect(data.code).toEqual(PasswordResponses.INCORRECT_PASSWORD);
@ -224,6 +224,53 @@ describe('api', function() {
done.fail(reason);
});
});
it('creates pdf doc from password protected PDF file and aborts/throws ' +
'in the onPassword callback (issue 7806)', function (done) {
var url = new URL('../pdfs/issue3371.pdf', window.location).href;
var passwordNeededLoadingTask = PDFJS.getDocument(url);
var passwordIncorrectLoadingTask = PDFJS.getDocument({
url: url, password: 'qwerty',
});
passwordNeededLoadingTask.onPassword = function (callback, reason) {
if (reason === PasswordResponses.NEED_PASSWORD) {
passwordNeededLoadingTask.destroy();
return;
}
// Shouldn't get here.
expect(false).toEqual(true);
};
var result1 = passwordNeededLoadingTask.promise.then(function () {
done.fail('shall fail since the loadingTask should be destroyed');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (reason) {
expect(reason instanceof PasswordException).toEqual(true);
expect(reason.code).toEqual(PasswordResponses.NEED_PASSWORD);
});
passwordIncorrectLoadingTask.onPassword = function (callback, reason) {
if (reason === PasswordResponses.INCORRECT_PASSWORD) {
throw new Error('Incorrect password');
}
// Shouldn't get here.
expect(false).toEqual(true);
};
var result2 = passwordIncorrectLoadingTask.promise.then(function () {
done.fail('shall fail since the onPassword callback should throw');
return Promise.reject(new Error('loadingTask should be rejected'));
}, function (reason) {
expect(reason instanceof PasswordException).toEqual(true);
expect(reason.code).toEqual(PasswordResponses.INCORRECT_PASSWORD);
return passwordIncorrectLoadingTask.destroy();
});
Promise.all([result1, result2]).then(function () {
done();
}).catch(function (reason) {
done.fail(reason);
});
});
});
});
describe('PDFWorker', function() {