Compare commits
2 Commits
9a8733a77d
...
12afcb7a82
| Author | SHA1 | Date | |
|---|---|---|---|
| 12afcb7a82 | |||
| 40d0e79358 |
49
__tests__/e2e/rpc-plugin/rpc-plugin.ctx.session.test.ts
Normal file
49
__tests__/e2e/rpc-plugin/rpc-plugin.ctx.session.test.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { RPCError } from "@/core/RPCError";
|
||||
import { CallIncomingBeforeCtx, NormalMethodReturn } from "@/core/RPCPlugin";
|
||||
import { AbstractRPCPlugin, RPCHandler } from "@/index";
|
||||
import { getRandomAvailablePort, isObject } from "@/utils/utils";
|
||||
|
||||
describe('rpc-plugin.ctx.session.test', () => {
|
||||
const userInfo = 'userinfo';
|
||||
const users = new WeakMap();
|
||||
class RPCTestPlugin implements AbstractRPCPlugin {
|
||||
onCallIncomingBefore(ctx: CallIncomingBeforeCtx): NormalMethodReturn {
|
||||
if (users.has(ctx.session)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isObject(ctx.request)) {
|
||||
if (ctx.request.fnPath === 'login') {
|
||||
users.set(ctx.session, userInfo);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RPCError({
|
||||
reason: 'not login'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
test('session', async () => {
|
||||
const server = new RPCHandler();
|
||||
const loginRes = 'login';
|
||||
const authRes = 'auth';
|
||||
const provider = {
|
||||
login() { return loginRes },
|
||||
auth() { return authRes }
|
||||
}
|
||||
server.setProvider(provider);
|
||||
server.loadPlugin(new RPCTestPlugin());
|
||||
const client = new RPCHandler();
|
||||
|
||||
const port = await getRandomAvailablePort();
|
||||
await server.listen({ port });
|
||||
|
||||
const session = await client.connect({ url: `http://localhost:${port}` });
|
||||
const api = session.getAPI<typeof provider>();
|
||||
await expect(api.auth()).rejects.toMatchObject({ reason: 'not login' });
|
||||
await expect(api.login()).resolves.toBe(loginRes);
|
||||
await expect(api.auth()).resolves.toBe(authRes);
|
||||
});
|
||||
})
|
||||
42
__tests__/e2e/rpc-plugin/rpc-plugin.hook.test.ts
Normal file
42
__tests__/e2e/rpc-plugin/rpc-plugin.hook.test.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { CallIncomingBeforeCtx, CallIncomingCtx, CallOutgoingBeforeCtx, CallOutgoingCtx, NormalMethodReturn } from "@/core/RPCPlugin";
|
||||
import { AbstractRPCPlugin, RPCHandler } from "@/index";
|
||||
import { getRandomAvailablePort } from "@/utils/utils";
|
||||
|
||||
describe('rpc-plugin.hook.test', () => {
|
||||
let count = 0;
|
||||
class RPCTestPlugin implements AbstractRPCPlugin {
|
||||
onCallIncomingBefore(ctx: CallIncomingBeforeCtx): NormalMethodReturn {
|
||||
count += 1;
|
||||
}
|
||||
onCallIncoming(ctx: CallIncomingCtx): NormalMethodReturn {
|
||||
count += 2;
|
||||
}
|
||||
onCallOutgoingBefore(ctx: CallOutgoingBeforeCtx): NormalMethodReturn {
|
||||
count += 4;
|
||||
}
|
||||
onCallOutgoing(ctx: CallOutgoingCtx): NormalMethodReturn {
|
||||
count += 8;
|
||||
}
|
||||
}
|
||||
const CountShouldBe = 15;
|
||||
|
||||
test('count', async () => {
|
||||
const server = new RPCHandler();
|
||||
const provider = {
|
||||
add(a: number, b: number) { return a + b }
|
||||
}
|
||||
server.setProvider(provider);
|
||||
server.loadPlugin(new RPCTestPlugin());
|
||||
const client = new RPCHandler();
|
||||
client.loadPlugin(new RPCTestPlugin());
|
||||
|
||||
const port = await getRandomAvailablePort();
|
||||
await server.listen({ port });
|
||||
|
||||
|
||||
const session = await client.connect({ url: `http://localhost:${port}` });
|
||||
const api = session.getAPI<typeof provider>();
|
||||
await expect(api.add(1, 2)).resolves.toBe(3);
|
||||
expect(count).toBe(CountShouldBe);
|
||||
});
|
||||
})
|
||||
@@ -242,6 +242,7 @@ export class RPCSession {
|
||||
|
||||
reject(new RPCError({
|
||||
errorCode: RPCErrorCode.UNKNOWN_ERROR,
|
||||
reason: e instanceof Error ? e.message : `${e}`
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user