forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile-store.test.js
224 lines (189 loc) · 6.13 KB
/
profile-store.test.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import {
uploadBinaryProfileData,
deleteProfileOnServer,
} from 'firefox-profiler/profile-logic/profile-store';
describe('profile upload', () => {
function setup() {
function fakeXMLHttpRequest() {
// eslint-disable-next-line @babel/no-invalid-this
Object.assign(this, {
abort: jest.fn(),
upload: {},
open: jest.fn(),
setRequestHeader: jest.fn(),
send: jest.fn(),
});
// eslint-disable-next-line @babel/no-invalid-this
fakeXMLHttpRequest.instances.push(this);
}
fakeXMLHttpRequest.instances = [];
jest.spyOn(window, 'XMLHttpRequest').mockImplementation(fakeXMLHttpRequest);
function getLastXhr() {
const xhr =
fakeXMLHttpRequest.instances[fakeXMLHttpRequest.instances.length - 1];
if (!xhr) {
throw new Error(`No XHR has been created yet.`);
}
return xhr;
}
return {
getLastXhr,
sendProgress({ loaded, total }) {
const e = new ProgressEvent('progress', {
lengthComputable: true,
loaded,
total,
});
const xhr = getLastXhr();
if (xhr.upload && xhr.upload.onprogress) {
xhr.upload.onprogress(e);
}
},
sendLoad({
status,
statusText,
responseText,
}: {
status: number,
statusText?: string,
responseText?: string,
}) {
const xhr = getLastXhr();
xhr.status = status;
xhr.statusText = statusText;
xhr.responseText = responseText;
if (xhr.onload) {
xhr.onload();
}
},
sendError() {
const xhr = getLastXhr();
if (xhr.onerror) {
xhr.onerror();
}
},
};
}
it('uploads with the right information', async () => {
const { getLastXhr, sendProgress, sendLoad } = setup();
const data = new Uint8Array(10);
const responseText = 'response';
const progressCallback = jest.fn();
const uploadPromise = uploadBinaryProfileData().startUpload(
data,
progressCallback
);
sendProgress({ loaded: 1, total: 4 });
sendProgress({ loaded: 2, total: 4 });
sendProgress({ loaded: 3, total: 4 });
sendLoad({ status: 200, responseText });
const result = await uploadPromise;
expect(result).toBe(responseText);
expect(progressCallback).toHaveBeenCalledTimes(3);
expect(progressCallback).toHaveBeenCalledWith(0.25);
expect(progressCallback).toHaveBeenCalledWith(0.5);
expect(progressCallback).toHaveBeenCalledWith(0.75);
const xhr = getLastXhr();
expect(xhr.open).toHaveBeenCalledWith(
'POST',
'https://api.profiler.firefox.com/compressed-store'
);
expect(xhr.setRequestHeader).toHaveBeenCalledWith(
'Accept',
expect.stringMatching(/^application\/vnd\.firefox-profiler\+json;/)
);
expect(xhr.send).toHaveBeenCalledWith(data);
});
it('returns a proper error when receiving a 413 status', async () => {
const { sendLoad } = setup();
const data = new Uint8Array(10);
const uploadPromise = uploadBinaryProfileData().startUpload(data);
sendLoad({ status: 413 });
await expect(uploadPromise).rejects.toThrow(/too large/);
});
it('returns a proper error when receiving another non-2xx status', async () => {
const { sendLoad } = setup();
const data = new Uint8Array(10);
const uploadPromise = uploadBinaryProfileData().startUpload(data);
sendLoad({ status: 400, statusText: 'configuration error' });
await expect(uploadPromise).rejects.toThrow(
/statusText: configuration error/
);
});
it('returns a proper error when encoutnering a network error', async () => {
jest.spyOn(console, 'error').mockImplementation(() => {});
const { sendError, getLastXhr } = setup();
const data = new Uint8Array(10);
const uploadPromise = uploadBinaryProfileData().startUpload(data);
sendError();
await expect(uploadPromise).rejects.toThrow(
'Unable to make a connection to publish the profile.'
);
expect(console.error).toHaveBeenCalledWith(
expect.stringMatching(/network error/),
getLastXhr()
);
});
it('can abort the request', async () => {
const { getLastXhr } = setup();
const { startUpload, abortUpload } = uploadBinaryProfileData();
startUpload(new Uint8Array(10));
abortUpload();
expect(getLastXhr().abort).toHaveBeenCalled();
});
});
describe('profile deletion', () => {
function mockFetchForDeleteProfile({
endpointUrl,
jwtToken,
}: {
endpointUrl: string,
jwtToken: string,
}) {
window.fetch
.catch(404) // catchall
.mock(endpointUrl, async (urlString, options) => {
const { method, headers } = options;
if (method !== 'DELETE') {
return new Response(null, {
status: 405,
statusText: 'Method not allowed',
});
}
if (
headers['Content-Type'] !== 'application/json' ||
headers.Accept !== 'application/vnd.firefox-profiler+json;version=1.0'
) {
return new Response(null, {
status: 406,
statusText: 'Not acceptable',
});
}
if (headers.Authorization !== `Bearer ${jwtToken}`) {
return new Response(null, {
status: 401,
statusText: 'Forbidden',
});
}
return new Response('Profile successfully deleted.', { status: 200 });
});
}
it('can delete a profile', async () => {
const PROFILE_TOKEN = 'FAKE_PROFILE_TOKEN';
const JWT_TOKEN = 'FAKE_JWT_TOKEN';
const endpointUrl = `https://api.profiler.firefox.com/profile/${PROFILE_TOKEN}`;
mockFetchForDeleteProfile({
endpointUrl,
jwtToken: JWT_TOKEN,
});
await deleteProfileOnServer({
profileToken: PROFILE_TOKEN,
jwtToken: JWT_TOKEN,
});
expect(window.fetch).toHaveBeenCalledWith(endpointUrl, expect.anything());
});
});