@@ -7,7 +7,7 @@ import { sign } from 'jsonwebtoken'
77import { ContentType } from 'allure-js-commons'
88
99import { FunctionsClient } from '../../src/index'
10-
10+ import { FunctionRegion } from '../../src/types'
1111import { Relay , runRelay } from '../relay/container'
1212import { attach , log } from '../utils/jest-custom-reporter'
1313import { str2ab } from '../utils/binaries'
@@ -146,6 +146,158 @@ describe('params reached to function', () => {
146146 ) . toBe ( true )
147147 } )
148148
149+ test ( 'invoke mirror set valid region on request' , async ( ) => {
150+ /**
151+ * @feature headers
152+ */
153+ log ( 'create FunctionsClient' )
154+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` )
155+
156+ log ( 'invoke mirror' )
157+ const customHeader = nanoid ( )
158+ const validRegion = FunctionRegion . ApNortheast1
159+
160+ const { data, error } = await fclient . invoke < MirrorResponse > ( 'mirror' , {
161+ headers : {
162+ 'custom-header' : customHeader ,
163+ Authorization : `Bearer ${ apiKey } ` ,
164+ } ,
165+ region : validRegion ,
166+ } )
167+
168+ log ( 'assert no error' )
169+ const expected = {
170+ url : 'http://localhost:8000/mirror' ,
171+ method : 'POST' ,
172+ headers : data ?. headers ?? [ ] ,
173+ body : '' ,
174+ }
175+ expect ( data ) . toEqual ( expected )
176+ attach (
177+ 'check headers from function' ,
178+ `expected to include: ${ [ 'custom-header' , customHeader ] } \n actual: ${ JSON . stringify (
179+ data ?. headers
180+ ) } `,
181+ ContentType . TEXT
182+ )
183+ console . log ( data ?. headers )
184+ expect (
185+ ( data ?. headers as [ Array < string > ] ) . filter ( ( [ k , v ] ) => k === 'x-region' && v === validRegion )
186+ . length > 0
187+ ) . toBe ( true )
188+ } )
189+
190+ test ( 'invoke with region overrides region in the client' , async ( ) => {
191+ /**
192+ * @feature headers
193+ */
194+ log ( 'create FunctionsClient' )
195+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` , {
196+ region : FunctionRegion . ApNortheast1 ,
197+ } )
198+
199+ log ( 'invoke mirror' )
200+ const customHeader = nanoid ( )
201+ const validRegion = FunctionRegion . ApSoutheast1
202+
203+ const { data, error } = await fclient . invoke < MirrorResponse > ( 'mirror' , {
204+ headers : {
205+ 'custom-header' : customHeader ,
206+ Authorization : `Bearer ${ apiKey } ` ,
207+ } ,
208+ region : validRegion ,
209+ } )
210+
211+ log ( 'assert no error' )
212+ const expected = {
213+ url : 'http://localhost:8000/mirror' ,
214+ method : 'POST' ,
215+ headers : data ?. headers ?? [ ] ,
216+ body : '' ,
217+ }
218+ expect ( data ) . toEqual ( expected )
219+ attach (
220+ 'check headers from function' ,
221+ `expected to include: ${ [ 'custom-header' , customHeader ] } \n actual: ${ JSON . stringify (
222+ data ?. headers
223+ ) } `,
224+ ContentType . TEXT
225+ )
226+ console . log ( data ?. headers )
227+ expect (
228+ ( data ?. headers as [ Array < string > ] ) . filter ( ( [ k , v ] ) => k === 'x-region' && v === validRegion )
229+ . length > 0
230+ ) . toBe ( true )
231+ } )
232+
233+ test ( 'starts client with default region, invoke reverts to any (no x-region header)' , async ( ) => {
234+ /**
235+ * @feature headers
236+ */
237+ log ( 'create FunctionsClient' )
238+ const validRegion = FunctionRegion . ApSoutheast1
239+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` , {
240+ region : validRegion ,
241+ } )
242+
243+ log ( 'invoke mirror' )
244+ const customHeader = nanoid ( )
245+
246+ const { data, error } = await fclient . invoke < MirrorResponse > ( 'mirror' , {
247+ headers : {
248+ 'custom-header' : customHeader ,
249+ Authorization : `Bearer ${ apiKey } ` ,
250+ } ,
251+ region : FunctionRegion . Any
252+ } )
253+
254+ log ( 'assert no error' )
255+ const expected = {
256+ url : 'http://localhost:8000/mirror' ,
257+ method : 'POST' ,
258+ headers : data ?. headers ?? [ ] ,
259+ body : '' ,
260+ }
261+ expect ( data ) . toEqual ( expected )
262+ attach (
263+ 'check headers from function' ,
264+ `expected to include: ${ [ 'custom-header' , customHeader ] } \n actual: ${ JSON . stringify (
265+ data ?. headers
266+ ) } `,
267+ ContentType . TEXT
268+ )
269+ console . log ( data ?. headers )
270+ expect (
271+ ( data ?. headers as [ Array < string > ] ) . filter ( ( [ k , v ] ) => k === 'x-region' && v === validRegion )
272+ . length == 0
273+ ) . toBe ( true )
274+ } )
275+
276+ test ( 'invoke region set only on the constructor' , async ( ) => {
277+ /**
278+ * @feature headers
279+ */
280+ log ( 'create FunctionsClient' )
281+ const fclient = new FunctionsClient ( `http://localhost:${ relay . container . getMappedPort ( 8081 ) } ` , { region : FunctionRegion . ApNortheast1 } )
282+
283+ log ( 'invoke mirror' )
284+ const customHeader = nanoid ( )
285+
286+
287+ const { data, error } = await fclient . invoke < MirrorResponse > ( 'mirror' , {
288+ headers : {
289+ 'custom-header' : customHeader ,
290+ Authorization : `Bearer ${ apiKey } `
291+ } ,
292+ } )
293+
294+ log ( 'assert no error' )
295+ expect (
296+ ( data ?. headers as [ Array < string > ] ) . filter ( ( [ k , v ] ) => k === 'x-region' && v === FunctionRegion . ApNortheast1 )
297+ . length > 0
298+ ) . toBe ( true )
299+ } )
300+
149301 test ( 'invoke mirror with body formData' , async ( ) => {
150302 /**
151303 * @feature body
0 commit comments