-
Notifications
You must be signed in to change notification settings - Fork 86
/
http.test.ts
371 lines (317 loc) · 11.6 KB
/
http.test.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import { HttpAgent, Nonce } from '../index';
import * as cbor from '../../cbor';
import { Expiry, makeNonceTransform } from './transforms';
import { CallRequest, Envelope, makeNonce, SubmitRequestType } from './types';
import { Principal } from '@dfinity/principal';
import { requestIdOf } from '../../request_id';
import { JSDOM } from 'jsdom';
import { AnonymousIdentity, SignIdentity } from '../..';
import { Ed25519KeyIdentity } from '../../../../identity/src/identity/ed25519';
import { toHexString } from '../../../../identity/src/buffer';
import { AgentError } from '../../errors';
const { window } = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
window.fetch = global.fetch;
global.window = window;
function createIdentity(seed: number): Ed25519KeyIdentity {
const seed1 = new Array(32).fill(0);
seed1[0] = seed;
return Ed25519KeyIdentity.generate(new Uint8Array(seed1));
}
const originalDateNowFn = global.Date.now;
const originalWindow = global.window;
const originalFetch = global.fetch;
beforeEach(() => {
global.Date.now = jest.fn(() => new Date(1000000).getTime());
global.window = originalWindow;
global.fetch = originalFetch;
});
afterEach(() => {
global.Date.now = originalDateNowFn;
global.window = originalWindow;
global.fetch = originalFetch;
});
test('call', async () => {
const mockFetch: jest.Mock = jest.fn((resource, init) => {
return Promise.resolve(
new Response(null, {
status: 200,
}),
);
});
const canisterId: Principal = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const nonce = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]) as Nonce;
const principal = Principal.anonymous();
const httpAgent = new HttpAgent({ fetch: mockFetch, host: 'http://localhost' });
const methodName = 'greet';
const arg = new Uint8Array([]);
const { requestId } = await httpAgent.call(canisterId, {
methodName,
arg,
});
const mockPartialRequest = {
request_type: SubmitRequestType.Call,
canister_id: canisterId,
method_name: methodName,
// We need a request id for the signature and at the same time we
// are checking that signature does not impact the request id.
arg,
nonce,
sender: principal,
ingress_expiry: new Expiry(300000),
};
const mockPartialsRequestId = await requestIdOf(mockPartialRequest);
const expectedRequest = {
content: mockPartialRequest,
};
const expectedRequestId = await requestIdOf(expectedRequest.content);
expect(expectedRequestId).toEqual(mockPartialsRequestId);
const { calls, results } = mockFetch.mock;
expect(calls.length).toBe(1);
expect(requestId).toEqual(expectedRequestId);
expect(calls[0][0]).toBe(`http://localhost/api/v2/canister/${canisterId.toText()}/call`);
expect(calls[0][1]).toEqual({
method: 'POST',
headers: {
'Content-Type': 'application/cbor',
},
body: cbor.encode(expectedRequest),
});
});
test.todo('query');
test('queries with the same content should have the same signature', async () => {
const mockResponse = {
status: 'replied',
reply: { arg: new Uint8Array([]) },
};
const mockFetch: jest.Mock = jest.fn((resource, init) => {
const body = cbor.encode(mockResponse);
return Promise.resolve(
new Response(body, {
status: 200,
}),
);
});
const canisterIdent = '2chl6-4hpzw-vqaaa-aaaaa-c';
const nonce = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]) as Nonce;
const principal = await Principal.anonymous();
const httpAgent = new HttpAgent({
fetch: mockFetch,
host: 'http://localhost',
disableNonce: true,
});
httpAgent.addTransform(makeNonceTransform(() => nonce));
const methodName = 'greet';
const arg = new Uint8Array([]);
const requestId = await requestIdOf({
request_type: SubmitRequestType.Call,
nonce,
canister_id: Principal.fromText(canisterIdent).toString(),
method_name: methodName,
arg,
sender: principal,
});
const paths = [
[new TextEncoder().encode('request_status'), requestId, new TextEncoder().encode('reply')],
];
const response1 = await httpAgent.readState(canisterIdent, { paths });
const response2 = await httpAgent.readState(canisterIdent, { paths });
const response3 = await httpAgent.query(canisterIdent, { arg, methodName });
const response4 = await httpAgent.query(canisterIdent, { methodName, arg });
const { calls } = mockFetch.mock;
expect(calls.length).toBe(4);
expect(calls[0]).toEqual(calls[1]);
expect(response1).toEqual(response2);
expect(calls[2]).toEqual(calls[3]);
expect(response3).toEqual(response4);
});
test('redirect avoid', async () => {
function checkUrl(base: string, result: string) {
const httpAgent = new HttpAgent({ host: base });
expect(httpAgent['_host'].hostname).toBe(result);
}
checkUrl('https://ic0.app', 'ic0.app');
checkUrl('https://IC0.app', 'ic0.app');
checkUrl('https://foo.ic0.app', 'ic0.app');
checkUrl('https://foo.IC0.app', 'ic0.app');
checkUrl('https://foo.Ic0.app', 'ic0.app');
checkUrl('https://foo.iC0.app', 'ic0.app');
checkUrl('https://foo.bar.ic0.app', 'ic0.app');
checkUrl('https://ic0.app/foo/', 'ic0.app');
checkUrl('https://foo.ic0.app/foo/', 'ic0.app');
checkUrl('https://ic1.app', 'ic1.app');
checkUrl('https://foo.ic1.app', 'foo.ic1.app');
checkUrl('https://ic0.app.ic1.app', 'ic0.app.ic1.app');
checkUrl('https://fooic0.app', 'fooic0.app');
checkUrl('https://fooic0.app.ic0.app', 'ic0.app');
});
test('use anonymous principal if unspecified', async () => {
const mockFetch: jest.Mock = jest.fn((resource, init) => {
return Promise.resolve(
new Response(new Uint8Array([]), {
status: 200,
}),
);
});
const canisterId: Principal = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const nonce = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]) as Nonce;
const principal = Principal.anonymous();
const httpAgent = new HttpAgent({
fetch: mockFetch,
host: 'http://localhost',
disableNonce: true,
});
httpAgent.addTransform(makeNonceTransform(() => nonce));
const methodName = 'greet';
const arg = new Uint8Array([]);
const { requestId } = await httpAgent.call(canisterId, {
methodName,
arg,
});
const mockPartialRequest: CallRequest = {
request_type: SubmitRequestType.Call,
canister_id: canisterId,
method_name: methodName,
// We need a request id for the signature and at the same time we
// are checking that signature does not impact the request id.
arg,
nonce,
sender: principal,
ingress_expiry: new Expiry(300000),
};
const mockPartialsRequestId = await requestIdOf(mockPartialRequest);
const expectedRequest: Envelope<CallRequest> = {
content: mockPartialRequest,
};
const expectedRequestId = await requestIdOf(expectedRequest.content);
expect(expectedRequestId).toEqual(mockPartialsRequestId);
const { calls, results } = mockFetch.mock;
expect(calls.length).toBe(1);
expect(requestId).toEqual(expectedRequestId);
expect(calls[0][0]).toBe(`http://localhost/api/v2/canister/${canisterId.toText()}/call`);
expect(calls[0][1]).toEqual({
method: 'POST',
headers: {
'Content-Type': 'application/cbor',
},
body: cbor.encode(expectedRequest),
});
});
describe('getDefaultFetch', () => {
it("should use fetch from window if it's available", async () => {
const generateAgent = () => new HttpAgent({ host: 'localhost:8000' });
expect(generateAgent).not.toThrowError();
});
it('should throw an error if fetch is not available on the window object', async () => {
delete window.fetch;
const generateAgent = () => new HttpAgent({ host: 'localhost:8000' });
expect(generateAgent).toThrowError('Fetch implementation was not available');
});
it('should throw error for defaultFetch with no window or global fetch', () => {
delete global.window;
delete global.fetch;
const generateAgent = () => new HttpAgent({ host: 'localhost:8000' });
expect(generateAgent).toThrowError('Fetch implementation was not available');
});
it('should fall back to global.fetch if window is not available', () => {
delete global.window;
global.fetch = originalFetch;
const generateAgent = () => new HttpAgent({ host: 'localhost:8000' });
expect(generateAgent).not.toThrowError();
});
it.skip('should throw an error if window, global, and fetch are not available', () => {
// TODO: Figure out how to test the self and default case errors
});
});
describe('invalidate identity', () => {
const mockFetch: jest.Mock = jest.fn();
it('should allow its identity to be invalidated', () => {
const identity = new AnonymousIdentity();
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://localhost' });
const invalidate = () => agent.invalidateIdentity();
expect(invalidate).not.toThrowError();
});
it('should throw an error instead of making a call if its identity is invalidated', async () => {
const canisterId: Principal = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const identity = new AnonymousIdentity();
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://localhost' });
agent.invalidateIdentity();
const expectedError =
"This identity has expired due this application's security policy. Please refresh your authentication.";
// Test Agent.call
try {
await agent.call(canisterId, {
methodName: 'test',
arg: new ArrayBuffer(16),
});
} catch (error) {
expect(error.message).toBe(expectedError);
}
// Test Agent.query
try {
await agent.query(canisterId, {
methodName: 'test',
arg: new ArrayBuffer(16),
});
} catch (error) {
expect(error.message).toBe(expectedError);
}
// Test readState
try {
await agent.readState(canisterId, {
paths: [[new ArrayBuffer(16)]],
});
} catch (error) {
expect(error.message).toBe(expectedError);
}
});
});
describe('replace identity', () => {
const mockFetch: jest.Mock = jest.fn();
it('should allow an actor to replace its identity', () => {
const identity = new AnonymousIdentity();
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://localhost' });
const identity2 = new AnonymousIdentity();
const replace = () => agent.replaceIdentity(identity2);
expect(replace).not.toThrowError();
});
it('should use the new identity in calls', async () => {
const mockFetch: jest.Mock = jest.fn((resource, init) => {
return Promise.resolve(
new Response(null, {
status: 200,
}),
);
});
const expectedError =
"This identity has expired due this application's security policy. Please refresh your authentication.";
const canisterId: Principal = Principal.fromText('2chl6-4hpzw-vqaaa-aaaaa-c');
const identity = new AnonymousIdentity();
const agent = new HttpAgent({ identity, fetch: mockFetch, host: 'http://localhost' });
// First invalidate identity
agent.invalidateIdentity();
await agent
.query(canisterId, {
methodName: 'test',
arg: new ArrayBuffer(16),
})
.catch((reason: AgentError) => {
// This should fail
expect(reason.message).toBe(expectedError);
});
// Then, add new identity
const identity2 = createIdentity(0) as unknown as SignIdentity;
agent.replaceIdentity(identity2);
await agent.call(canisterId, {
methodName: 'test',
arg: new ArrayBuffer(16),
});
expect(mockFetch).toBeCalledTimes(1);
});
});
describe('makeNonce should create unique values', () => {
const nonces = new Set();
for (let i = 0; i < 100; i++) {
nonces.add(toHexString(makeNonce()));
}
expect(nonces.size).toBe(100);
});