@@ -2490,6 +2490,7 @@ describe('File', () => {
24902490
24912491 describe ( 'download' , ( ) => {
24922492 let fileReadStream : Readable ;
2493+ let originalSetEncryptionKey : Function ;
24932494
24942495 beforeEach ( ( ) => {
24952496 fileReadStream = new Readable ( ) ;
@@ -2502,6 +2503,13 @@ describe('File', () => {
25022503 file . createReadStream = ( ) => {
25032504 return fileReadStream ;
25042505 } ;
2506+
2507+ originalSetEncryptionKey = file . setEncryptionKey ;
2508+ file . setEncryptionKey = sinon . stub ( ) ;
2509+ } ) ;
2510+
2511+ afterEach ( ( ) => {
2512+ file . setEncryptionKey = originalSetEncryptionKey ;
25052513 } ) ;
25062514
25072515 it ( 'should accept just a callback' , done => {
@@ -2532,6 +2540,31 @@ describe('File', () => {
25322540 file . download ( readOptions , assert . ifError ) ;
25332541 } ) ;
25342542
2543+ it ( 'should call setEncryptionKey with the provided key and not pass it to createReadStream' , done => {
2544+ const encryptionKey = Buffer . from ( 'encryption-key' ) ;
2545+ const downloadOptions = {
2546+ encryptionKey : encryptionKey ,
2547+ userProject : 'user-project-id' ,
2548+ } ;
2549+
2550+ file . createReadStream = ( options : { } ) => {
2551+ assert . deepStrictEqual ( options , { userProject : 'user-project-id' } ) ;
2552+ return fileReadStream ;
2553+ } ;
2554+
2555+ file . download ( downloadOptions , ( err : Error ) => {
2556+ assert . ifError ( err ) ;
2557+ // Verify that setEncryptionKey was called with the correct key
2558+ assert . ok (
2559+ ( file . setEncryptionKey as sinon . SinonStub ) . calledWith ( encryptionKey )
2560+ ) ;
2561+ done ( ) ;
2562+ } ) ;
2563+
2564+ fileReadStream . push ( 'some data' ) ;
2565+ fileReadStream . push ( null ) ;
2566+ } ) ;
2567+
25352568 it ( 'should only execute callback once' , done => {
25362569 Object . assign ( fileReadStream , {
25372570 _read ( this : Readable ) {
0 commit comments