-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest-utils.ts
More file actions
70 lines (67 loc) · 2.36 KB
/
test-utils.ts
File metadata and controls
70 lines (67 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { APIGatewayProxyEventV2, Context } from 'aws-lambda';
import { JSONRPCMessage } from '@modelcontextprotocol/sdk/types.js';
type HandlerContext = Context & { jsonRPCMessages: JSONRPCMessage[] };
/**
* Creates a mock APIGatewayProxyEventV2 object for testing.
* Allows overriding specific properties.
*/
export function createMockEvent(overrides: Partial<APIGatewayProxyEventV2> = {}): APIGatewayProxyEventV2 {
const defaultEvent: APIGatewayProxyEventV2 = {
version: '2.0',
routeKey: '$default',
rawPath: '/test',
rawQueryString: '',
headers: {
'content-type': 'application/json',
accept: 'application/json',
jsonrpc: '2.0',
...overrides.headers,
},
requestContext: {
accountId: '123456789012',
apiId: 'api-id',
domainName: 'id.execute-api.us-east-1.amazonaws.com',
domainPrefix: 'id',
http: {
method: 'POST',
path: '/test',
protocol: 'HTTP/1.1',
sourceIp: '127.0.0.1',
userAgent: 'Test Agent',
},
requestId: 'request-id',
routeKey: '$default',
stage: '$default',
time: '01/Mar/2020:00:00:00 +0000',
timeEpoch: 1583011200000,
...(overrides.requestContext as any),
},
body: '{}',
isBase64Encoded: false,
...overrides,
};
return defaultEvent;
}
/**
* Creates a mock Lambda Context object for testing.
* Includes the jsonRPCMessages property expected by the middleware.
*/
export function createMockContext(overrides: Partial<HandlerContext> = {}): HandlerContext {
const defaultContext: HandlerContext = {
callbackWaitsForEmptyEventLoop: true,
functionName: 'test-function',
functionVersion: '$LATEST',
invokedFunctionArn: 'arn:aws:lambda:us-east-1:123456789012:function:test-function',
memoryLimitInMB: '128',
awsRequestId: 'test-request-id',
logGroupName: '/aws/lambda/test-function',
logStreamName: '2023/01/01/[$LATEST]abcdef1234567890',
getRemainingTimeInMillis: () => 5 * 60 * 1000,
done: () => {},
fail: () => {},
succeed: () => {},
jsonRPCMessages: [],
...overrides,
};
return defaultContext;
}