강동현

DummySocket, SocketTester 구현

1 +import { expect } from "chai";
2 +import { Connection } from "../connection/Connection";
3 +import { SocketWrapper } from "../connection/SocketWrapper";
4 +import { RoomManager } from "../room/RoomManager";
5 +import { DummySocket } from "./util/DummySocket";
6 +import { SocketTester } from "./util/SocketTester";
7 +
8 +describe("유효하지 않은 메세지", () => {
9 + it("빈 메세지는 실패합니다", () => {
10 + const socket = new SocketTester();
11 + const response = socket.testAny({});
12 + expect(response.ok).eq(false);
13 + });
14 + it("유효하지 않은 타입의 메세지는 실패합니다", () => {
15 + const socket = new SocketTester();
16 + const response = socket.testRaw({ type: "wtf", message: {} });
17 + expect(response.ok).eq(false);
18 + });
19 + it("유효한 타입이지만 내용이 유효하지 않은 메세지는 실패합니다", () => {
20 + const socket = new SocketTester();
21 + const response = socket.testRaw({ type: "login", message: {} });
22 + expect(response.ok).eq(false);
23 + });
24 +});
1 +import { RawMessage, ServerResponse } from "../../../common";
2 +import { SocketWrapper } from "../../connection/SocketWrapper";
3 +
4 +export class DummySocket implements SocketWrapper {
5 + public handler?: (raw: RawMessage) => ServerResponse<any>;
6 + public setHandler(handler: (raw: RawMessage) => ServerResponse<any>) {
7 + this.handler = handler;
8 + }
9 + public send(raw: RawMessage): void {}
10 +}
1 +import {
2 + RawMessage,
3 + ServerInboundMessage,
4 + ServerInboundMessageKey,
5 + ServerResponse,
6 +} from "../../../common";
7 +import { Connection } from "../../connection/Connection";
8 +import { RoomManager } from "../../room/RoomManager";
9 +import { DummySocket } from "./DummySocket";
10 +
11 +export class SocketTester {
12 + public readonly connection: Connection;
13 + constructor(
14 + public socket: DummySocket = new DummySocket(),
15 + roomManager: RoomManager = new RoomManager()
16 + ) {
17 + this.connection = new Connection(socket, roomManager);
18 + }
19 +
20 + public test<T extends ServerInboundMessageKey>(
21 + type: T,
22 + message: ServerInboundMessage<T>
23 + ): ServerResponse<any> {
24 + return this.connection.handleRaw({
25 + type: type as string,
26 + message: message,
27 + });
28 + }
29 +
30 + public testRaw(raw: RawMessage): ServerResponse<any> {
31 + return this.connection.handleRaw(raw);
32 + }
33 +
34 + public testAny(obj: any): ServerResponse<any> {
35 + return this.connection.handleRaw(obj);
36 + }
37 +}