@@ -11,6 +11,7 @@ import {
1111 TEAM_AUTH ,
1212 APP_AUTH ,
1313 NO_AUTH ,
14+ COOKIE ,
1415} from '../../src/constants.js' ;
1516import { Dropbox , DropboxAuth } from '../../index.js' ;
1617
@@ -28,6 +29,18 @@ describe('Dropbox', () => {
2829 } ) ;
2930 } ) ;
3031
32+ describe ( 'customHeaders' , ( ) => {
33+ it ( 'can be set in the constructor' , ( ) => {
34+ const dbx = new Dropbox ( { customHeaders : { foo : 'bar' } } ) ;
35+ chai . assert . equal ( dbx . customHeaders . foo , 'bar' ) ;
36+ } ) ;
37+
38+ it ( 'is undefined if not set in constructor' , ( ) => {
39+ const dbx = new Dropbox ( ) ;
40+ chai . assert . equal ( dbx . customHeaders , undefined ) ;
41+ } ) ;
42+ } ) ;
43+
3144 describe ( 'RPC requests' , ( ) => {
3245 it ( 'request() calls the correct request method' , ( ) => {
3346 const dbx = new Dropbox ( ) ;
@@ -86,6 +99,20 @@ describe('Dropbox', () => {
8699 chai . assert . equal ( APP_AUTH , dbx . rpcRequest . getCall ( 0 ) . args [ 2 ] ) ;
87100 } ) ;
88101
102+ it ( 'completes a cookie auth RPC request' , ( ) => {
103+ const dbxAuth = new DropboxAuth ( ) ;
104+ const dbx = new Dropbox ( { auth : dbxAuth } ) ;
105+ const rpcSpy = sinon . spy ( dbx , 'rpcRequest' ) ;
106+ dbx . request ( 'path' , { } , COOKIE , 'api' , RPC )
107+ . catch ( ( error ) => {
108+ fail ( error ) ;
109+ } ) ;
110+ chai . assert . isTrue ( rpcSpy . calledOnce ) ;
111+ chai . assert . equal ( 'path' , dbx . rpcRequest . getCall ( 0 ) . args [ 0 ] ) ;
112+ chai . assert . deepEqual ( { } , dbx . rpcRequest . getCall ( 0 ) . args [ 1 ] ) ;
113+ chai . assert . equal ( COOKIE , dbx . rpcRequest . getCall ( 0 ) . args [ 2 ] ) ;
114+ } ) ;
115+
89116 it ( 'throws an error for invalid request styles' , ( ) => {
90117 chai . assert . throws (
91118 Dropbox . prototype . request . bind ( Dropbox , '' , { } , 'user' , 'api' , 'BADTYPE' ) ,
@@ -120,6 +147,10 @@ describe('Dropbox', () => {
120147 const dbx = new Dropbox ( ) ;
121148 return chai . assert . isRejected ( dbx . uploadRequest ( 'path' , { } , NO_AUTH , 'api' ) , Error , `Unexpected auth type: ${ NO_AUTH } ` ) ;
122149 } ) ;
150+ it ( 'throws an error for cookie auth' , ( ) => {
151+ const dbx = new Dropbox ( ) ;
152+ return chai . assert . isRejected ( dbx . uploadRequest ( 'path' , { } , COOKIE , 'api' ) , Error , `Unexpected auth type: ${ COOKIE } ` ) ;
153+ } ) ;
123154 } ) ;
124155
125156 describe ( 'Download Requests' , ( ) => {
@@ -149,6 +180,11 @@ describe('Dropbox', () => {
149180 const dbx = new Dropbox ( ) ;
150181 return chai . assert . isRejected ( dbx . downloadRequest ( 'path' , { } , NO_AUTH , 'api' ) , Error , `Unexpected auth type: ${ NO_AUTH } ` ) ;
151182 } ) ;
183+
184+ it ( 'throws an error for cookie auth' , ( ) => {
185+ const dbx = new Dropbox ( ) ;
186+ return chai . assert . isRejected ( dbx . downloadRequest ( 'path' , { } , COOKIE , 'api' ) , Error , `Unexpected auth type: ${ COOKIE } ` ) ;
187+ } ) ;
152188 } ) ;
153189
154190 describe ( 'pathRoot' , ( ) => {
@@ -186,5 +222,25 @@ describe('Dropbox', () => {
186222 }
187223 }
188224 } ) ;
225+
226+ it ( 'sets custom headers correctly' , ( ) => {
227+ const dbx = new Dropbox ( {
228+ customHeaders : {
229+ foo : 'bar' ,
230+ milk : 'shake' ,
231+ cookie : 'hash' ,
232+ } ,
233+ } ) ;
234+
235+ const fetchOptions = {
236+ headers : { } ,
237+ } ;
238+
239+ dbx . setCommonHeaders ( fetchOptions ) ;
240+ const { headers } = fetchOptions ;
241+ chai . assert . equal ( headers . foo , 'bar' ) ;
242+ chai . assert . equal ( headers . milk , 'shake' ) ;
243+ chai . assert . equal ( headers . cookie , 'hash' ) ;
244+ } ) ;
189245 } ) ;
190246} ) ;
0 commit comments