forked from aws-observability/aws-rum-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-utils.ts
More file actions
259 lines (230 loc) · 7.13 KB
/
test-utils.ts
File metadata and controls
259 lines (230 loc) · 7.13 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { EventCache } from '../event-cache/EventCache';
import { AwsCredentialIdentity } from '@aws-sdk/types';
import {
Config,
defaultConfig,
defaultCookieAttributes
} from '../orchestration/Orchestration';
import {
GetSession,
InternalPluginContext,
RecordEvent,
RecordPageView
} from '../plugins/types';
import {
AppMonitorDetails,
PutRumEventsRequest,
UserDetails
} from '../dispatch/dataplane';
import { ReadableStream } from 'web-streams-polyfill';
import EventBus from '../event-bus/EventBus';
import { INSTALL_MODULE } from '../utils/constants';
export const AWS_RUM_ENDPOINT = new URL(
'https://rumservicelambda.us-west-2.amazonaws.com'
);
export const WEB_CLIENT_VERSION = '2.0.0';
export const AWS_RUM_REGION = 'us-west-2';
export const APPLICATION_ID = 'application123';
export const APPLICATION_VERSION = '1.2';
export const BATCH_ID = 'batch123';
export const USER_ID = 'user123';
export const SESSION_ID = 'session123';
export const AUTO_DISPATCH_OFF = 0;
export const EVENT_ID = 'event123';
export const EVENT_TYPE = 'com.amazon.rum.event1';
export const EVENT_DETAILS = '{}';
export const EVENT_TIMESTAMP = new Date(0);
export const APP_MONITOR_DETAILS: AppMonitorDetails = {
id: APPLICATION_ID,
version: APPLICATION_VERSION
};
export const USER_DETAILS: UserDetails = {
userId: USER_ID,
sessionId: SESSION_ID
};
export const PUT_RUM_EVENTS_REQUEST: PutRumEventsRequest = {
BatchId: BATCH_ID,
AppMonitorDetails: APP_MONITOR_DETAILS,
UserDetails: USER_DETAILS,
RumEvents: [
{
id: EVENT_ID,
timestamp: EVENT_TIMESTAMP,
type: EVENT_TYPE,
details: EVENT_DETAILS
}
]
};
export const DEFAULT_CONFIG: Config = defaultConfig(defaultCookieAttributes());
export const createDefaultEventCache = (): EventCache => {
return new EventCache(APP_MONITOR_DETAILS, DEFAULT_CONFIG);
};
export const createEventCache = (
config: Partial<Config> = {},
bus?: EventBus
): EventCache => {
return new EventCache(
APP_MONITOR_DETAILS,
{ ...DEFAULT_CONFIG, ...config },
bus
);
};
export const createDefaultEventCacheWithEvents = (): EventCache => {
const EVENT1_SCHEMA = 'com.amazon.rum.event1';
const EVENT2_SCHEMA = 'com.amazon.rum.event2';
const eventCache = new EventCache(APP_MONITOR_DETAILS, DEFAULT_CONFIG);
eventCache.recordEvent(EVENT1_SCHEMA, {});
eventCache.recordEvent(EVENT2_SCHEMA, {});
return eventCache;
};
export const createAwsCredentials = (): AwsCredentialIdentity => {
return {
accessKeyId: 'abc123',
secretAccessKey: 'abc123xyz'
};
};
export const createAwsError = () => {
return {
name: 'error-code',
code: 'error-code',
message: 'Something went wrong.',
statusCode: 500,
time: new Date()
};
};
export const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
export const record: jest.MockedFunction<RecordEvent> = jest.fn();
export const recordCandidate: jest.MockedFunction<RecordEvent> = jest.fn();
export const recordPageView: jest.MockedFunction<RecordPageView> = jest.fn();
export const getSession: jest.MockedFunction<GetSession> = jest.fn(() => ({
sessionId: 'abc123',
record: true,
eventCount: 0
}));
export const context: InternalPluginContext = {
applicationId: 'b',
applicationVersion: '1.0',
config: DEFAULT_CONFIG,
record,
recordCandidate,
recordPageView,
getSession,
eventBus: new EventBus()
};
export const xRayOffContext: InternalPluginContext = {
...context,
config: { ...DEFAULT_CONFIG, ...{ enableXRay: false } }
};
export const xRayOnContext: InternalPluginContext = {
...context,
config: { ...DEFAULT_CONFIG, ...{ enableXRay: true } }
};
export const w3cTraceIdOnContext: InternalPluginContext = {
...context,
config: {
...DEFAULT_CONFIG,
...{ enableXRay: true, enableW3CTraceId: true }
}
};
export const xRayOffW3COnContext: InternalPluginContext = {
...context,
config: {
...DEFAULT_CONFIG,
...{ enableXRay: false, enableW3CTraceId: true }
}
};
export const stringToUtf16 = (inputString: string) => {
const utf16array = [];
for (let index = 0; index < inputString.length; index++) {
utf16array[index] = inputString.charCodeAt(index);
}
return utf16array;
};
export const getReadableStream = (mockString: string) =>
new ReadableStream({
start(c) {
c.enqueue(stringToUtf16(mockString));
c.close();
}
});
export const mockFetch = jest.fn(
(input: RequestInfo, init?: RequestInit): Promise<Response> =>
Promise.resolve({
status: 200,
statusText: 'OK',
headers: new Headers({ 'Content-Length': '125' }),
body: '{}',
ok: true
} as any)
);
export const mockFetchWith500 = jest.fn(
(input: RequestInfo, init?: RequestInit): Promise<Response> =>
Promise.resolve({
status: 500,
statusText: 'InternalError',
headers: new Headers({ 'Content-Length': '125' }),
body: '',
ok: false
} as any)
);
export const mockFetchWith400 = jest.fn(
(input: RequestInfo, init?: RequestInit): Promise<Response> =>
Promise.resolve({
status: 404,
statusText: 'Not Found',
headers: new Headers({ 'Content-Length': '125' }),
body: '',
ok: false
} as any)
);
export const mockFetchWith429 = jest.fn(
(input: RequestInfo, init?: RequestInit): Promise<Response> =>
Promise.resolve({
status: 429,
statusText: 'Too Many Requests',
headers: new Headers({ 'Content-Length': '125' }),
body: '',
ok: false
} as any)
);
export const mockFetchWithError = jest.fn(
(input: RequestInfo, init?: RequestInit): Promise<Response> =>
Promise.reject('Timeout')
);
export const mockFetchWithErrorObject = jest.fn(
(input: RequestInfo, init?: RequestInit): Promise<Response> =>
Promise.reject(new Error('Timeout'))
);
export const mockFetchWithErrorObjectAndStack = jest.fn(
(input: RequestInfo, init?: RequestInit): Promise<Response> =>
Promise.reject({
name: 'FetchError',
message: 'timeout',
stack: 'stack trace'
})
);
export const createExpectedEvents = (types: string[], expect: any) =>
types.map((type) => ({
id: expect.stringMatching(/[0-9a-f\-]+/),
timestamp: new Date(),
type,
metadata: `{"version":"1.0.0","aws:client":"${INSTALL_MODULE}","aws:clientVersion":"${WEB_CLIENT_VERSION}"}`,
details: '{}'
}));
export const testMetaData = {
version: '1.0.0',
'aws:client': INSTALL_MODULE,
'aws:clientVersion': WEB_CLIENT_VERSION,
domain: 'us-east-1.console.aws.amazon.com',
browserLanguage: 'en-US',
browserName: 'WebKit',
browserVersion: '537.36',
osName: 'unknown',
osVersion: 'unknown',
deviceType: 'desktop',
platformType: 'web',
pageId: '/console/home',
title: ''
};