mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
add test for closing connection on emitting closure events
This commit is contained in:
@@ -5,29 +5,36 @@ import {
|
|||||||
AppleNotificationSender,
|
AppleNotificationSender,
|
||||||
NotificationAlertArguments
|
NotificationAlertArguments
|
||||||
} from "../../../src/notifications/senders/AppleNotificationSender";
|
} from "../../../src/notifications/senders/AppleNotificationSender";
|
||||||
|
import { ClientHttp2Session } from "node:http2";
|
||||||
|
|
||||||
jest.mock("http2");
|
jest.mock("http2");
|
||||||
|
|
||||||
const sampleKeyBase64 = "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JR1RBZ0VBTUJNR0J5cUdTTTQ5QWdFR0NDcUdTTTQ5QXdFSEJIa3dkd0lCQVFRZ3NybVNBWklhZ09mQ1A4c0IKV2kyQ0JYRzFPbzd2MWJpc3BJWkN3SXI0UkRlZ0NnWUlLb1pJemowREFRZWhSQU5DQUFUWkh4VjJ3UUpMTUJxKwp5YSt5ZkdpM2cyWlV2NmhyZmUrajA4eXRla1BIalhTMHF6Sm9WRUx6S0hhNkVMOVlBb1pEWEJ0QjZoK2ZHaFhlClNPY09OYmFmCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K";
|
const sampleKeyBase64 = "LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JR1RBZ0VBTUJNR0J5cUdTTTQ5QWdFR0NDcUdTTTQ5QXdFSEJIa3dkd0lCQVFRZ3NybVNBWklhZ09mQ1A4c0IKV2kyQ0JYRzFPbzd2MWJpc3BJWkN3SXI0UkRlZ0NnWUlLb1pJemowREFRZWhSQU5DQUFUWkh4VjJ3UUpMTUJxKwp5YSt5ZkdpM2cyWlV2NmhyZmUrajA4eXRla1BIalhTMHF6Sm9WRUx6S0hhNkVMOVlBb1pEWEJ0QjZoK2ZHaFhlClNPY09OYmFmCi0tLS0tRU5EIFBSSVZBVEUgS0VZLS0tLS0K";
|
||||||
|
|
||||||
function mockHttp2Connect(status: number) {
|
class MockClient extends EventEmitter {
|
||||||
class MockClient extends EventEmitter {
|
constructor(
|
||||||
|
private status: number,
|
||||||
|
) {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
request = jest.fn((_) => {
|
request = jest.fn((_) => {
|
||||||
const mockRequest: any = new EventEmitter();
|
const mockRequest: any = new EventEmitter();
|
||||||
mockRequest.setEncoding = jest.fn();
|
mockRequest.setEncoding = jest.fn();
|
||||||
mockRequest.write = jest.fn();
|
mockRequest.write = jest.fn();
|
||||||
mockRequest.end = jest.fn(() => {
|
mockRequest.end = jest.fn(() => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
mockRequest.emit('response', { ':status': status });
|
mockRequest.emit('response', { ':status': this.status });
|
||||||
}, 10);
|
}, 10);
|
||||||
});
|
});
|
||||||
return mockRequest;
|
return mockRequest;
|
||||||
});
|
});
|
||||||
|
|
||||||
close() {};
|
close = jest.fn(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
(http2.connect as jest.Mock) = jest.fn(() => new MockClient());
|
function mockHttp2Connect(status: number) {
|
||||||
|
(http2.connect as jest.Mock) = jest.fn(() => new MockClient(status));
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("AppleNotificationSender", () => {
|
describe("AppleNotificationSender", () => {
|
||||||
@@ -159,5 +166,25 @@ describe("AppleNotificationSender", () => {
|
|||||||
expect(http2.connect).not.toHaveBeenCalled();
|
expect(http2.connect).not.toHaveBeenCalled();
|
||||||
expect(result).toBe(true);
|
expect(result).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("registers a handler to close the connection if `close` event fired", async () => {
|
||||||
|
const connectionCloseEvents = ['close', 'goaway', 'error'];
|
||||||
|
|
||||||
|
await Promise.all(connectionCloseEvents.map(async (event) => {
|
||||||
|
const mockClient = new MockClient(200);
|
||||||
|
notificationSender = new AppleNotificationSender(true, mockClient as unknown as ClientHttp2Session);
|
||||||
|
|
||||||
|
const notificationArguments: NotificationAlertArguments = {
|
||||||
|
title: 'Test notification',
|
||||||
|
body: ''
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await notificationSender.sendNotificationImmediately('1', notificationArguments);
|
||||||
|
|
||||||
|
mockClient.emit(event);
|
||||||
|
|
||||||
|
expect(mockClient.close).toHaveBeenCalled();
|
||||||
|
}));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user