( request: RestRequest, response: RestResponse, script: string, set: any, get: any, exec: any, toast: any, isCollectionLevel: boolean, envName?: string, )
| 19 | }; |
| 20 | |
| 21 | async function executeResponseScript( |
| 22 | request: RestRequest, |
| 23 | response: RestResponse, |
| 24 | script: string, |
| 25 | set: any, |
| 26 | get: any, |
| 27 | exec: any, |
| 28 | toast: any, |
| 29 | isCollectionLevel: boolean, |
| 30 | envName?: string, |
| 31 | ): Promise<JasmineReport | null> { |
| 32 | const jasmine = bootJasmine(); |
| 33 | const jasmineEnv = jasmine.getEnv(); |
| 34 | const args: Record<string, any> = { |
| 35 | describe: jasmineEnv.describe, |
| 36 | it: jasmineEnv.it, |
| 37 | expect: jasmineEnv.expect, |
| 38 | fail: jasmineEnv.fail, |
| 39 | beforeEach: jasmineEnv.beforeEach, |
| 40 | afterEach: jasmineEnv.afterEach, |
| 41 | beforeAll: jasmineEnv.beforeAll, |
| 42 | afterAll: jasmineEnv.afterAll, |
| 43 | }; |
| 44 | args.env = { set, get, name: envName }; |
| 45 | args.res = { |
| 46 | body: response.body, |
| 47 | headers: kvRowsToMap(response.headers), |
| 48 | status: response.status, |
| 49 | }; |
| 50 | args.jp = jpath; |
| 51 | args.btoa = btoa; |
| 52 | args.atob = atob; |
| 53 | args.DateTime = DateTime; |
| 54 | args.rand = rand; |
| 55 | args.exec = exec; |
| 56 | if (isCollectionLevel) { |
| 57 | args.log = (...data: any[]) => |
| 58 | console.log( |
| 59 | `[Collection Response Script: ${request.collectionId} - ${envName ?? 'NO_ENV'}]`, |
| 60 | ...data, |
| 61 | ); |
| 62 | } else { |
| 63 | args.log = (...data: any[]) => |
| 64 | console.log(`[Response Script: ${request.id} - ${envName ?? 'NO_ENV'}]`, ...data); |
| 65 | } |
| 66 | try { |
| 67 | await asyncSandboxedFunction(args, script); |
| 68 | // TODO: env.name does not work |
| 69 | // TODO: async jasmine spec does not work |
| 70 | await jasmineEnv.execute(); |
| 71 | return getJasmineReport(jasmine); |
| 72 | } catch (err: any) { |
| 73 | // NOTE: this is to prevent stacked error messages due to exec loop |
| 74 | const msg = err.message.startsWith('Error in request script') |
| 75 | ? err.message |
| 76 | : `Error in request script [id: ${request.id}]: ${err}`; |
| 77 | throw Error(msg); |
| 78 | } |
no test coverage detected