diff --git a/src/FunctionsClient.ts b/src/FunctionsClient.ts index ed1849f..be2d44b 100644 --- a/src/FunctionsClient.ts +++ b/src/FunctionsClient.ts @@ -57,8 +57,11 @@ export class FunctionsClient { if (!region) { region = this.region } + // Add region as query parameter using URL API + const url = new URL(`${this.url}/${functionName}`) if (region && region !== 'any') { _headers['x-region'] = region + url.searchParams.set('forceFunctionRegion', region) } let body: any if ( @@ -88,7 +91,7 @@ export class FunctionsClient { } } - const response = await this.fetch(`${this.url}/${functionName}`, { + const response = await this.fetch(url.toString(), { method: method || 'POST', // headers priority is (high to low): // 1. invoke-level headers diff --git a/test/spec/params.spec.ts b/test/spec/params.spec.ts index 1383d48..5d715df 100644 --- a/test/spec/params.spec.ts +++ b/test/spec/params.spec.ts @@ -166,13 +166,17 @@ describe('params reached to function', () => { }) log('assert no error') - const expected = { - url: 'http://localhost:8000/mirror', - method: 'POST', - headers: data?.headers ?? [], - body: '', - } - expect(data).toEqual(expected) + expect(error).toBeNull() + + // Check that x-region header is present + expect( + (data?.headers as [Array]).filter(([k, v]) => k === 'x-region' && v === validRegion) + .length > 0 + ).toBe(true) + + // Check that the URL contains the forceFunctionRegion query parameter + expect(data?.url).toContain(`forceFunctionRegion=${validRegion}`) + attach( 'check headers from function', `expected to include: ${['custom-header', customHeader]}\n actual: ${JSON.stringify( @@ -180,11 +184,6 @@ describe('params reached to function', () => { )}`, ContentType.TEXT ) - console.log(data?.headers) - expect( - (data?.headers as [Array]).filter(([k, v]) => k === 'x-region' && v === validRegion) - .length > 0 - ).toBe(true) }) test('invoke with region overrides region in the client', async () => { @@ -210,7 +209,7 @@ describe('params reached to function', () => { log('assert no error') const expected = { - url: 'http://localhost:8000/mirror', + url: `http://localhost:8000/mirror?forceFunctionRegion=${FunctionRegion.ApSoutheast1}`, method: 'POST', headers: data?.headers ?? [], body: '',