@@ -6,6 +6,20 @@ import util from 'node:util';
66import Spotify from '@stainless-commons/spotify' ;
77import { APIUserAbortError } from '@stainless-commons/spotify' ;
88const defaultFetch = fetch ;
9+ const mockTokenFetch : typeof defaultFetch = async ( url , init ) => {
10+ const urlStr = String ( url ) ;
11+ if ( urlStr . includes ( '/api/token' ) ) {
12+ return new Response (
13+ JSON . stringify ( {
14+ access_token : 'mock-test-token' ,
15+ token_type : 'Bearer' ,
16+ expires_in : 3600 ,
17+ } ) ,
18+ { status : 200 , headers : { 'Content-Type' : 'application/json' } } ,
19+ ) ;
20+ }
21+ return defaultFetch ( url , init ) ;
22+ } ;
923
1024describe ( 'instantiate client' , ( ) => {
1125 const env = process . env ;
@@ -23,6 +37,7 @@ describe('instantiate client', () => {
2337 const client = new Spotify ( {
2438 baseURL : 'http://localhost:5000/' ,
2539 defaultHeaders : { 'X-My-Default-Header' : '2' } ,
40+ fetch : mockTokenFetch ,
2641 } ) ;
2742
2843 test ( 'they are used in the request' , async ( ) => {
@@ -91,14 +106,19 @@ describe('instantiate client', () => {
91106 logLevel : 'debug' ,
92107 clientID : 'My Client ID' ,
93108 clientSecret : 'My Client Secret' ,
109+ fetch : mockTokenFetch ,
94110 } ) ;
95111
96112 await forceAPIResponseForClient ( client ) ;
97113 expect ( debugMock ) . toHaveBeenCalled ( ) ;
98114 } ) ;
99115
100116 test ( 'default logLevel is warn' , async ( ) => {
101- const client = new Spotify ( { clientID : 'My Client ID' , clientSecret : 'My Client Secret' } ) ;
117+ const client = new Spotify ( {
118+ clientID : 'My Client ID' ,
119+ clientSecret : 'My Client Secret' ,
120+ fetch : mockTokenFetch ,
121+ } ) ;
102122 expect ( client . logLevel ) . toBe ( 'warn' ) ;
103123 } ) ;
104124
@@ -116,6 +136,7 @@ describe('instantiate client', () => {
116136 logLevel : 'info' ,
117137 clientID : 'My Client ID' ,
118138 clientSecret : 'My Client Secret' ,
139+ fetch : mockTokenFetch ,
119140 } ) ;
120141
121142 await forceAPIResponseForClient ( client ) ;
@@ -136,6 +157,7 @@ describe('instantiate client', () => {
136157 logger : logger ,
137158 clientID : 'My Client ID' ,
138159 clientSecret : 'My Client Secret' ,
160+ fetch : mockTokenFetch ,
139161 } ) ;
140162 expect ( client . logLevel ) . toBe ( 'debug' ) ;
141163
@@ -157,6 +179,7 @@ describe('instantiate client', () => {
157179 logger : logger ,
158180 clientID : 'My Client ID' ,
159181 clientSecret : 'My Client Secret' ,
182+ fetch : mockTokenFetch ,
160183 } ) ;
161184 expect ( client . logLevel ) . toBe ( 'warn' ) ;
162185 expect ( warnMock ) . toHaveBeenCalledWith (
@@ -179,6 +202,7 @@ describe('instantiate client', () => {
179202 logLevel : 'off' ,
180203 clientID : 'My Client ID' ,
181204 clientSecret : 'My Client Secret' ,
205+ fetch : mockTokenFetch ,
182206 } ) ;
183207
184208 await forceAPIResponseForClient ( client ) ;
@@ -200,6 +224,7 @@ describe('instantiate client', () => {
200224 logLevel : 'debug' ,
201225 clientID : 'My Client ID' ,
202226 clientSecret : 'My Client Secret' ,
227+ fetch : mockTokenFetch ,
203228 } ) ;
204229 expect ( client . logLevel ) . toBe ( 'debug' ) ;
205230 expect ( warnMock ) . not . toHaveBeenCalled ( ) ;
@@ -213,6 +238,7 @@ describe('instantiate client', () => {
213238 defaultQuery : { apiVersion : 'foo' } ,
214239 clientID : 'My Client ID' ,
215240 clientSecret : 'My Client Secret' ,
241+ fetch : mockTokenFetch ,
216242 } ) ;
217243 expect ( client . buildURL ( '/foo' , null ) ) . toEqual ( 'http://localhost:5000/foo?apiVersion=foo' ) ;
218244 } ) ;
@@ -223,6 +249,7 @@ describe('instantiate client', () => {
223249 defaultQuery : { apiVersion : 'foo' , hello : 'world' } ,
224250 clientID : 'My Client ID' ,
225251 clientSecret : 'My Client Secret' ,
252+ fetch : mockTokenFetch ,
226253 } ) ;
227254 expect ( client . buildURL ( '/foo' , null ) ) . toEqual ( 'http://localhost:5000/foo?apiVersion=foo&hello=world' ) ;
228255 } ) ;
@@ -233,6 +260,7 @@ describe('instantiate client', () => {
233260 defaultQuery : { hello : 'world' } ,
234261 clientID : 'My Client ID' ,
235262 clientSecret : 'My Client Secret' ,
263+ fetch : mockTokenFetch ,
236264 } ) ;
237265 expect ( client . buildURL ( '/foo' , { hello : undefined } ) ) . toEqual ( 'http://localhost:5000/foo' ) ;
238266 } ) ;
@@ -406,6 +434,7 @@ describe('instantiate client', () => {
406434 maxRetries : 3 ,
407435 clientID : 'My Client ID' ,
408436 clientSecret : 'My Client Secret' ,
437+ fetch : mockTokenFetch ,
409438 } ) ;
410439
411440 const newClient = client . withOptions ( {
@@ -431,6 +460,7 @@ describe('instantiate client', () => {
431460 baseURL : 'http://localhost:5000/' ,
432461 defaultHeaders : { 'X-Test-Header' : 'test-value' } ,
433462 defaultQuery : { 'test-param' : 'test-value' } ,
463+ fetch : mockTokenFetch ,
434464 } ) ;
435465
436466 const newClient = client . withOptions ( {
@@ -450,6 +480,7 @@ describe('instantiate client', () => {
450480 timeout : 1000 ,
451481 clientID : 'My Client ID' ,
452482 clientSecret : 'My Client Secret' ,
483+ fetch : mockTokenFetch ,
453484 } ) ;
454485
455486 // Modify the client properties directly after creation
0 commit comments