@@ -21,6 +21,7 @@ describe("SmartlingFileTranslationsApi class tests.", () => {
2121 let fileTranslationsApiFetchStub ;
2222 let fileTranslationsApiUaStub ;
2323 let responseMockJsonStub ;
24+ let getHeaderStub ;
2425
2526 beforeEach ( ( ) => {
2627 fileTranslationsApi = new SmartlingFileTranslationsApi (
@@ -37,12 +38,15 @@ describe("SmartlingFileTranslationsApi class tests.", () => {
3738 responseMockJsonStub . returns ( {
3839 response : { }
3940 } ) ;
41+ getHeaderStub = sinon . stub ( responseMock . headers , "get" ) ;
42+ getHeaderStub . returns ( null ) ;
4043 } ) ;
4144
4245 afterEach ( ( ) => {
4346 fileTranslationsApiFetchStub . restore ( ) ;
4447 fileTranslationsApiUaStub . restore ( ) ;
4548 responseMockJsonStub . restore ( ) ;
49+ getHeaderStub . restore ( ) ;
4650 } ) ;
4751
4852 describe ( "Methods" , ( ) => {
@@ -359,6 +363,89 @@ describe("SmartlingFileTranslationsApi class tests.", () => {
359363 ) ;
360364 } ) ;
361365
366+ it ( "Download translated file with metadata when no headers" , async ( ) => {
367+ const localeId = "de-DE" ;
368+
369+ const fileWithMetadata = await fileTranslationsApi . downloadTranslatedFileWithMetadata (
370+ accountUid , fileUid , mtUid , localeId
371+ ) ;
372+
373+ sinon . assert . calledOnceWithExactly (
374+ fileTranslationsApiFetchStub ,
375+ `https://test.com/file-translations-api/v2/accounts/${ accountUid } /files/${ fileUid } /mt/${ mtUid } /locales/${ localeId } /file` ,
376+ {
377+ method : "get" ,
378+ headers : {
379+ Authorization : "test_token_type test_access_token" ,
380+ "Content-Type" : "application/json" ,
381+ "User-Agent" : userAgent
382+ }
383+ }
384+ ) ;
385+
386+ assert . ok ( fileWithMetadata . contentType === undefined ) ;
387+ assert . ok ( fileWithMetadata . fileName === undefined ) ;
388+ assert . ok ( fileWithMetadata . fileContent . byteLength === 1 ) ;
389+ } ) ;
390+
391+ it ( "Download translated file with metadata" , async ( ) => {
392+ const localeId = "de-DE" ;
393+
394+ getHeaderStub . onCall ( 0 ) . returns ( "application/xml" ) ;
395+ getHeaderStub . onCall ( 1 ) . returns ( "attachment; filename=\"test.xml\"" ) ;
396+ getHeaderStub . returns ( null ) ;
397+
398+ const fileWithMetadata = await fileTranslationsApi . downloadTranslatedFileWithMetadata (
399+ accountUid , fileUid , mtUid , localeId
400+ ) ;
401+
402+ sinon . assert . calledOnceWithExactly (
403+ fileTranslationsApiFetchStub ,
404+ `https://test.com/file-translations-api/v2/accounts/${ accountUid } /files/${ fileUid } /mt/${ mtUid } /locales/${ localeId } /file` ,
405+ {
406+ method : "get" ,
407+ headers : {
408+ Authorization : "test_token_type test_access_token" ,
409+ "Content-Type" : "application/json" ,
410+ "User-Agent" : userAgent
411+ }
412+ }
413+ ) ;
414+
415+ assert . ok ( fileWithMetadata . contentType === "application/xml" ) ;
416+ assert . ok ( fileWithMetadata . fileName === "test.xml" ) ;
417+ assert . ok ( fileWithMetadata . fileContent . byteLength === 1 ) ;
418+ } ) ;
419+
420+ it ( "Download translated file with escaped quotes in the file name" , async ( ) => {
421+ const localeId = "de-DE" ;
422+
423+ getHeaderStub . onCall ( 0 ) . returns ( "application/xml" ) ;
424+ getHeaderStub . onCall ( 1 ) . returns ( "attachment; filename=\"test - \\\"phase 1\\\".xml\"" ) ;
425+ getHeaderStub . returns ( null ) ;
426+
427+ const fileWithMetadata = await fileTranslationsApi . downloadTranslatedFileWithMetadata (
428+ accountUid , fileUid , mtUid , localeId
429+ ) ;
430+
431+ sinon . assert . calledOnceWithExactly (
432+ fileTranslationsApiFetchStub ,
433+ `https://test.com/file-translations-api/v2/accounts/${ accountUid } /files/${ fileUid } /mt/${ mtUid } /locales/${ localeId } /file` ,
434+ {
435+ method : "get" ,
436+ headers : {
437+ Authorization : "test_token_type test_access_token" ,
438+ "Content-Type" : "application/json" ,
439+ "User-Agent" : userAgent
440+ }
441+ }
442+ ) ;
443+
444+ assert . ok ( fileWithMetadata . contentType === "application/xml" ) ;
445+ assert . ok ( fileWithMetadata . fileName === "test - \"phase 1\".xml" ) ;
446+ assert . ok ( fileWithMetadata . fileContent . byteLength === 1 ) ;
447+ } ) ;
448+
362449 it ( "Download translated files" , async ( ) => {
363450 await fileTranslationsApi . downloadTranslatedFiles ( accountUid , fileUid , mtUid ) ;
364451
@@ -376,6 +463,56 @@ describe("SmartlingFileTranslationsApi class tests.", () => {
376463 ) ;
377464 } ) ;
378465
466+ it ( "Download translated files with metadata when no headers" , async ( ) => {
467+ const fileWithMetadata = await fileTranslationsApi . downloadTranslatedFilesWithMetadata (
468+ accountUid , fileUid , mtUid
469+ ) ;
470+
471+ sinon . assert . calledOnceWithExactly (
472+ fileTranslationsApiFetchStub ,
473+ `https://test.com/file-translations-api/v2/accounts/${ accountUid } /files/${ fileUid } /mt/${ mtUid } /locales/all/file/zip` ,
474+ {
475+ method : "get" ,
476+ headers : {
477+ Authorization : "test_token_type test_access_token" ,
478+ "Content-Type" : "application/json" ,
479+ "User-Agent" : userAgent
480+ }
481+ }
482+ ) ;
483+
484+ assert . ok ( fileWithMetadata . contentType === undefined ) ;
485+ assert . ok ( fileWithMetadata . fileName === undefined ) ;
486+ assert . ok ( fileWithMetadata . fileContent . byteLength === 1 ) ;
487+ } ) ;
488+
489+ it ( "Download translated files with metadata" , async ( ) => {
490+ getHeaderStub . onCall ( 0 ) . returns ( "application/zip" ) ;
491+ getHeaderStub . onCall ( 1 ) . returns ( "attachment; filename=\"test.zip\"" ) ;
492+ getHeaderStub . returns ( null ) ;
493+
494+ const fileWithMetadata = await fileTranslationsApi . downloadTranslatedFilesWithMetadata (
495+ accountUid , fileUid , mtUid
496+ ) ;
497+
498+ sinon . assert . calledOnceWithExactly (
499+ fileTranslationsApiFetchStub ,
500+ `https://test.com/file-translations-api/v2/accounts/${ accountUid } /files/${ fileUid } /mt/${ mtUid } /locales/all/file/zip` ,
501+ {
502+ method : "get" ,
503+ headers : {
504+ Authorization : "test_token_type test_access_token" ,
505+ "Content-Type" : "application/json" ,
506+ "User-Agent" : userAgent
507+ }
508+ }
509+ ) ;
510+
511+ assert . ok ( fileWithMetadata . contentType === "application/zip" ) ;
512+ assert . ok ( fileWithMetadata . fileName === "test.zip" ) ;
513+ assert . ok ( fileWithMetadata . fileContent . byteLength === 1 ) ;
514+ } ) ;
515+
379516 it ( "Cancel file translation" , async ( ) => {
380517 await fileTranslationsApi . cancelFileTranslation ( accountUid , fileUid , mtUid ) ;
381518
0 commit comments