|
| 1 | +/** |
| 2 | + * Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +require('should'); |
| 8 | + |
| 9 | +let RAPIDClient = require('lambda-runtime/RAPIDClient.js'); |
| 10 | +let runtimeErrors = require('lambda-runtime/Errors.js'); |
| 11 | + |
| 12 | +/** |
| 13 | + * Stub request object. |
| 14 | + * Provides no-op definitions of the request functions used by the rapid client. |
| 15 | + */ |
| 16 | +const noOpRequest = Object.freeze({ |
| 17 | + /* no op, return itself to allow continuations/chaining */ |
| 18 | + on: () => noOpRequest, |
| 19 | + /* no op, return itself to allow continuations/chaining */ |
| 20 | + end: () => noOpRequest, |
| 21 | +}); |
| 22 | + |
| 23 | +class StubHttp { |
| 24 | + constructor() { |
| 25 | + this.lastUsedOptions = {}; |
| 26 | + this.Agent = class FakeAgent {}; |
| 27 | + } |
| 28 | + |
| 29 | + request(options, _callback) { |
| 30 | + this.lastUsedOptions = options; |
| 31 | + return noOpRequest; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class NoOpNativeHttp { |
| 36 | + constructor() { |
| 37 | + this.lastRequestId = ''; |
| 38 | + this.lastErrorRequestId = ''; |
| 39 | + } |
| 40 | + |
| 41 | + done(requestId) { |
| 42 | + this.lastRequestId = requestId; |
| 43 | + } |
| 44 | + |
| 45 | + error(requestId) { |
| 46 | + this.lastErrorRequestId = requestId; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class EvilError extends Error { |
| 51 | + get name() { |
| 52 | + throw 'gotcha'; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +const EXPECTED_ERROR_HEADER = 'Lambda-Runtime-Function-Error-Type'; |
| 57 | + |
| 58 | +describe('building error requests with the RAPIDClient', () => { |
| 59 | + let stubHttp = new StubHttp(); |
| 60 | + let client = new RAPIDClient('notUsed:1337', stubHttp, new NoOpNativeHttp()); |
| 61 | + |
| 62 | + let errors = [ |
| 63 | + [new Error('generic failure'), 'Error'], |
| 64 | + [new runtimeErrors.ImportModuleError(), 'Runtime.ImportModuleError'], |
| 65 | + [new runtimeErrors.HandlerNotFound(), 'Runtime.HandlerNotFound'], |
| 66 | + [new runtimeErrors.MalformedHandlerName(), 'Runtime.MalformedHandlerName'], |
| 67 | + [new runtimeErrors.UserCodeSyntaxError(), 'Runtime.UserCodeSyntaxError'], |
| 68 | + [{ data: 'some random object' }, 'object'], |
| 69 | + [new EvilError(), 'handled'], |
| 70 | + ]; |
| 71 | + |
| 72 | + describe('the error header in postInitError', () => { |
| 73 | + errors.forEach(([error, name]) => { |
| 74 | + it(`should be ${name} for ${error.constructor.name}`, () => { |
| 75 | + client.postInitError(error); |
| 76 | + stubHttp.lastUsedOptions.should.have |
| 77 | + .property('headers') |
| 78 | + .have.property(EXPECTED_ERROR_HEADER, name); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('invalid request id works', () => { |
| 85 | + const nativeClient = new NoOpNativeHttp(); |
| 86 | + const client = new RAPIDClient('notUsed:1337', undefined, nativeClient); |
| 87 | + |
| 88 | + [ |
| 89 | + // Encoding expected: |
| 90 | + ['#', '%23'], |
| 91 | + ['%', '%25'], |
| 92 | + ['/', '%2F'], |
| 93 | + ['?', '%3F'], |
| 94 | + ['\x7F', '%7F'], |
| 95 | + ["<script>alert('1')</script>", "%3Cscript%3Ealert('1')%3C%2Fscript%3E"], |
| 96 | + ['⚡', '%E2%9A%A1'], |
| 97 | + |
| 98 | + // No encoding: |
| 99 | + ['.', '.'], |
| 100 | + ['..', '..'], |
| 101 | + ['a', 'a'], |
| 102 | + [ |
| 103 | + '59b22c65-fa81-47fb-a6dc-23028a63566f', |
| 104 | + '59b22c65-fa81-47fb-a6dc-23028a63566f', |
| 105 | + ], |
| 106 | + ].forEach(([requestId, expected]) => { |
| 107 | + it(`postInvocationResponse should encode requestId: '${requestId}'`, () => { |
| 108 | + client.postInvocationResponse({}, requestId, () => {}); |
| 109 | + nativeClient.lastRequestId.should.be.equal(expected); |
| 110 | + }); |
| 111 | + |
| 112 | + it(`postInvocationError should encode requestId: '${requestId}'`, () => { |
| 113 | + client.postInvocationError(new Error(), requestId, () => {}); |
| 114 | + nativeClient.lastErrorRequestId.should.be.equal(expected); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments