-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathodp_event_api_manager.spec.ts
225 lines (195 loc) · 7.56 KB
/
odp_event_api_manager.spec.ts
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
/**
* Copyright 2022-2024, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { describe, it, expect, vi } from 'vitest';
import { DefaultOdpEventApiManager, eventApiRequestGenerator, LOGGER_NAME, pixelApiRequestGenerator } from './odp_event_api_manager';
import { OdpEvent } from './odp_event';
import { OdpConfig } from '../odp_config';
const data1 = new Map<string, unknown>();
data1.set('key11', 'value-1');
data1.set('key12', true);
data1.set('key13', 3.5);
data1.set('key14', null);
const data2 = new Map<string, unknown>();
data2.set('key2', 'value-2');
const ODP_EVENTS = [
new OdpEvent('t1', 'a1', new Map([['id-key-1', 'id-value-1']]), data1),
new OdpEvent('t2', 'a2', new Map([['id-key-2', 'id-value-2']]), data2),
];
const API_KEY = 'test-api-key';
const API_HOST = 'https://odp.example.com';
const PIXEL_URL = 'https://odp.pixel.com';
const odpConfig = new OdpConfig(API_KEY, API_HOST, PIXEL_URL, []);
import { getMockRequestHandler } from '../../tests/mock/mock_request_handler';
import { getMockLogger } from '../../tests/mock/mock_logger';
describe('DefaultOdpEventApiManager', () => {
it('should set name on the logger passed into the constructor', () => {
const logger = getMockLogger();
const requestHandler = getMockRequestHandler();
const manager = new DefaultOdpEventApiManager(requestHandler, vi.fn(), logger);
expect(logger.setName).toHaveBeenCalledWith(LOGGER_NAME);
});
it('should set name on the logger set by setLogger', () => {
const logger = getMockLogger();
const requestHandler = getMockRequestHandler();
const manager = new DefaultOdpEventApiManager(requestHandler, vi.fn());
manager.setLogger(logger);
expect(logger.setName).toHaveBeenCalledWith(LOGGER_NAME);
});
it('should generate the event request using the correct odp config and event', async () => {
const mockRequestHandler = getMockRequestHandler();
mockRequestHandler.makeRequest.mockReturnValue({
responsePromise: Promise.resolve({
statusCode: 200,
body: '',
headers: {},
}),
});
const requestGenerator = vi.fn().mockReturnValue({
method: 'PATCH',
endpoint: 'https://odp.example.com/v3/events',
headers: {
'x-api-key': 'test-api',
},
data: 'event-data',
});
const manager = new DefaultOdpEventApiManager(mockRequestHandler, requestGenerator);
manager.sendEvents(odpConfig, ODP_EVENTS);
expect(requestGenerator.mock.calls[0][0]).toEqual(odpConfig);
expect(requestGenerator.mock.calls[0][1]).toEqual(ODP_EVENTS);
});
it('should send the correct request using the request handler', async () => {
const mockRequestHandler = getMockRequestHandler();
mockRequestHandler.makeRequest.mockReturnValue({
responsePromise: Promise.resolve({
statusCode: 200,
body: '',
headers: {},
}),
});
const requestGenerator = vi.fn().mockReturnValue({
method: 'PATCH',
endpoint: 'https://odp.example.com/v3/events',
headers: {
'x-api-key': 'test-api',
},
data: 'event-data',
});
const manager = new DefaultOdpEventApiManager(mockRequestHandler, requestGenerator);
manager.sendEvents(odpConfig, ODP_EVENTS);
expect(mockRequestHandler.makeRequest.mock.calls[0][0]).toEqual('https://odp.example.com/v3/events');
expect(mockRequestHandler.makeRequest.mock.calls[0][1]).toEqual({
'x-api-key': 'test-api',
});
expect(mockRequestHandler.makeRequest.mock.calls[0][2]).toEqual('PATCH');
expect(mockRequestHandler.makeRequest.mock.calls[0][3]).toEqual('event-data');
});
it('should return a promise that fails if the requestHandler response promise fails', async () => {
const mockRequestHandler = getMockRequestHandler();
mockRequestHandler.makeRequest.mockReturnValue({
responsePromise: Promise.reject(new Error('REQUEST_FAILED')),
});
const requestGenerator = vi.fn().mockReturnValue({
method: 'PATCH',
endpoint: 'https://odp.example.com/v3/events',
headers: {
'x-api-key': 'test-api',
},
data: 'event-data',
});
const manager = new DefaultOdpEventApiManager(mockRequestHandler, requestGenerator);
const response = manager.sendEvents(odpConfig, ODP_EVENTS);
await expect(response).rejects.toThrow();
});
it('should return a promise that resolves with correct response code from the requestHandler', async () => {
const mockRequestHandler = getMockRequestHandler();
mockRequestHandler.makeRequest.mockReturnValue({
responsePromise: Promise.resolve({
statusCode: 226,
body: '',
headers: {},
}),
});
const requestGenerator = vi.fn().mockReturnValue({
method: 'PATCH',
endpoint: 'https://odp.example.com/v3/events',
headers: {
'x-api-key': 'test-api',
},
data: 'event-data',
});
const manager = new DefaultOdpEventApiManager(mockRequestHandler, requestGenerator);
const response = manager.sendEvents(odpConfig, ODP_EVENTS);
await expect(response).resolves.not.toThrow();
const statusCode = await response.then((r) => r.statusCode);
expect(statusCode).toBe(226);
});
});
describe('pixelApiRequestGenerator', () => {
it('should generate the correct request for the pixel API using only the first event', () => {
const request = pixelApiRequestGenerator(odpConfig, ODP_EVENTS);
expect(request.method).toBe('GET');
const endpoint = new URL(request.endpoint);
expect(endpoint.origin).toBe(PIXEL_URL);
expect(endpoint.pathname).toBe('/v2/zaius.gif');
expect(endpoint.searchParams.get('id-key-1')).toBe('id-value-1');
expect(endpoint.searchParams.get('key11')).toBe('value-1');
expect(endpoint.searchParams.get('key12')).toBe('true');
expect(endpoint.searchParams.get('key13')).toBe('3.5');
expect(endpoint.searchParams.get('key14')).toBe('null');
expect(endpoint.searchParams.get('tracker_id')).toBe(API_KEY);
expect(endpoint.searchParams.get('event_type')).toBe('t1');
expect(endpoint.searchParams.get('vdl_action')).toBe('a1');
expect(request.headers).toEqual({});
expect(request.data).toBe('');
});
});
describe('eventApiRequestGenerator', () => {
it('should generate the correct request for the event API using all events', () => {
const request = eventApiRequestGenerator(odpConfig, ODP_EVENTS);
expect(request.method).toBe('POST');
expect(request.endpoint).toBe('https://odp.example.com/v3/events');
expect(request.headers).toEqual({
'Content-Type': 'application/json',
'x-api-key': API_KEY,
});
const data = JSON.parse(request.data);
expect(data).toEqual([
{
type: 't1',
action: 'a1',
identifiers: {
'id-key-1': 'id-value-1',
},
data: {
key11: 'value-1',
key12: true,
key13: 3.5,
key14: null,
},
},
{
type: 't2',
action: 'a2',
identifiers: {
'id-key-2': 'id-value-2',
},
data: {
key2: 'value-2',
},
},
]);
});
});