Skip to content

Commit 4f40dc1

Browse files
committed
- working test for JSON RPC Client
1 parent fbd3dec commit 4f40dc1

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

src/client-proxy.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ export class JSONRPCClient extends ClientProxy {
3030
{
3131
get(obj, prop) {
3232
return function(params: any) {
33-
axios
33+
// console.log(url);
34+
// console.log(namespace);
35+
// console.log(prop.toString());
36+
// console.log(params);
37+
return axios
3438
.post(url, { method: namespace + "." + prop.toString(), params, jsonrpc: "2.0" })
3539
.then(res => Promise.resolve(res));
3640
};

src/index.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as request from "supertest";
33
import { Test } from "@nestjs/testing";
44
import { INestMicroservice, ServiceUnavailableException } from "@nestjs/common";
55

6-
import { TestService, TestClientService } from "./test-handler";
6+
import { TestService } from "./test-handler";
77
import { JSONRPCServer } from ".";
88
import { JSONRPCClient } from "./client-proxy";
99

@@ -54,9 +54,11 @@ describe("json-rpc-e2e", () => {
5454
it(`should make and RPC call with the JSONRPCClient`, () => {
5555
console.log("Testing RPC CLient");
5656
const client = new JSONRPCClient("http://localhost:8080/rpc/v1");
57-
const service = client.getService<TestClientService>("test.service");
57+
const service = client.getService<TestService>("test");
5858

59-
return service.invokeService({ data: "hi" }).then(res => expect(res.data).toBe("hi"));
59+
return service
60+
.invokeClientService({ data: "hi" })
61+
.then(res => expect(res.data).toStrictEqual({ data: "hi" }));
6062
});
6163

6264
afterAll(async () => {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ export class JSONRPCServer extends Server implements CustomTransportStrategy {
5656
}
5757

5858
public async listen(callback: () => void) {
59-
console.log("Server listening on 8080");
6059
let app = express();
6160

6261
app.post(this.options.path, express.json(), async (req, res) => {
6362
// let handlers = this.getHandlers();
63+
console.log(req.body);
6464

6565
let handler = this.getHandlerByPattern(req.body.method);
66+
console.log(handler);
6667
if (handler == null) {
6768
return res.status(404).json({ error: "Not Found" });
6869
}
@@ -96,7 +97,6 @@ export class JSONRPCServer extends Server implements CustomTransportStrategy {
9697
}
9798

9899
public async close() {
99-
console.log("Closing server");
100100
await invokeAsync(cb => this.server && this.server.close(cb));
101101
// do nothing, maybe block further requests
102102
}

src/test-handler.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class TestGuard implements CanActivate {
6464
@JSONRpcService({
6565
namespace: "test"
6666
})
67-
export class TestService {
67+
export class TestService implements ITestClientService {
6868
constructor() {
6969
DecorationsState.serviceConstructorCount = DecorationsState.serviceConstructorCount + 1;
7070
console.log("TestService count now at", DecorationsState.serviceConstructorCount);
@@ -85,26 +85,16 @@ export class TestService {
8585
// construct the error object with some data inside
8686
throw new CodedRpcException("RPC EXCEPTION", 403, { fromService: "Test Service", params });
8787
}
88-
}
89-
90-
@JSONRpcService({
91-
namespace: "test.service"
92-
})
93-
export class TestClientService implements ITestClientService {
94-
constructor() {
95-
DecorationsState.serviceConstructorCount = DecorationsState.serviceConstructorCount + 1;
96-
console.log("TestService count now at", DecorationsState.serviceConstructorCount);
97-
}
9888

9989
@UsePipes(TestPipe)
10090
@UseInterceptors(TestInterceptor)
10191
@UseGuards(TestGuard)
102-
public async invokeService(params: any) {
92+
public async invokeClientService(params: any) {
10393
console.log("Invoke Client Service WAS called");
10494
return params;
10595
}
10696
}
10797

10898
export interface ITestClientService {
109-
invokeService(params: any): any;
99+
invokeClientService(params: any): any;
110100
}

0 commit comments

Comments
 (0)