Sitelet https://github.com/open-rpc/client-js/commit/8ba714e
Skip to content

Commit 8ba714e

Browse files
committed
fix: request manager tests
1 parent ba1e23b commit 8ba714e

2 files changed

Lines changed: 73 additions & 60 deletions

File tree

‎src/RequestManager.test.ts‎

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import RequestManager from "./RequestManager";
22
import EventEmitterTransport from "./transports/EventEmitterTransport";
33
import { EventEmitter } from "events";
4+
import { doesNotReject } from "assert";
45

56
describe("client-js", () => {
67
it("can be constructed", () => {
@@ -18,7 +19,7 @@ describe("client-js", () => {
1819
expect(typeof c.request("my_method", null).then).toEqual("function");
1920
});
2021

21-
it("can connect", () => {
22+
it("can connect", async () => {
2223
const emitter = new EventEmitter();
2324
const transport = new EventEmitterTransport(emitter, "from1", "to1");
2425
const c = new RequestManager([transport]);
@@ -37,14 +38,26 @@ describe("client-js", () => {
3738
const transport = new EventEmitterTransport(emitter, "from1", "to1");
3839
const serverTransport = new EventEmitterTransport(emitter, "to1", "from1");
3940
const c = new RequestManager([transport]);
40-
c.connect();
41-
transport.onData((data: any) => {
42-
const d = JSON.parse(data);
43-
expect(d.foo).toEqual("bar");
44-
done();
41+
c.connect().then(() => {
42+
c.request("foo", []).then(() => {
43+
done();
44+
});
45+
serverTransport.sendData(JSON.stringify({ id: 0, result: { foo: "foofoo" } }));
46+
});
47+
});
48+
49+
it("can error on malformed response", (done) => {
50+
const emitter = new EventEmitter();
51+
const transport = new EventEmitterTransport(emitter, "from1", "to1");
52+
const serverTransport = new EventEmitterTransport(emitter, "to1", "from1");
53+
const c = new RequestManager([transport]);
54+
c.connect().then(() => {
55+
c.request("foo", []).catch((e) => {
56+
expect(e.message).toContain("Malformed");
57+
done();
58+
});
59+
serverTransport.sendData(JSON.stringify({ id: 0, foo: "bar" }));
4560
});
46-
c.request("foo", []);
47-
serverTransport.sendData(JSON.stringify({ foo: "bar" }));
4861
});
4962

5063
it("can error on batchng a request", async () => {
@@ -56,46 +69,50 @@ describe("client-js", () => {
5669
});
5770
});
5871

59-
it("can return errors on batch requests", async () => {
72+
it("can return errors on batch requests", (done) => {
6073
const emitter = new EventEmitter();
6174
const transport = new EventEmitterTransport(emitter, "from1", "to1");
6275
const serverTransport = new EventEmitterTransport(emitter, "to1", "from1");
6376

6477
const c = new RequestManager([transport]);
65-
await c.connect();
66-
c.startBatch();
67-
const requests = [
68-
c.request("foo", []),
69-
c.request("foo", []),
70-
];
71-
expect(requests[0]).rejects.toEqual({
72-
code: 509,
73-
message: "too much 509",
74-
data: {
75-
test: "data",
76-
},
77-
});
78-
serverTransport.sendData(JSON.stringify([
79-
{
80-
jsonrpc: "2.0",
81-
id: "0",
82-
error: {
78+
c.connect().then(() => {
79+
c.startBatch();
80+
const requests = [
81+
c.request("foo", []),
82+
c.request("foo", []),
83+
];
84+
Promise.all(requests).catch((e) => {
85+
expect(e).toEqual({
8386
code: 509,
8487
message: "too much 509",
8588
data: {
8689
test: "data",
8790
},
91+
});
92+
c.close();
93+
done();
94+
});
95+
c.stopBatch();
96+
serverTransport.sendData(JSON.stringify([
97+
{
98+
jsonrpc: "2.0",
99+
id: "0",
100+
error: {
101+
code: 509,
102+
message: "too much 509",
103+
data: {
104+
test: "data",
105+
},
106+
},
88107
},
89-
},
90-
{
91-
jsonrpc: "2.0",
92-
id: "1",
93-
result: "bar",
94-
},
95-
]));
96-
expect(requests[1]).resolves.toEqual("bar");
97-
c.stopBatch();
98-
c.close();
108+
{
109+
jsonrpc: "2.0",
110+
id: "1",
111+
result: "bar",
112+
},
113+
]));
114+
115+
});
99116
});
100117

101118
it("can batch a request", (done) => {
@@ -132,32 +149,28 @@ describe("client-js", () => {
132149
});
133150
});
134151

135-
it("can send a request and error", async () => {
152+
it("can send a request and error", (done) => {
136153
const emitter = new EventEmitter();
137154
const transport = new EventEmitterTransport(emitter, "from1", "to1");
155+
const serverTransport = new EventEmitterTransport(emitter, "to1", "from1");
138156
const c = new RequestManager([transport]);
139-
transport.onData = (fn) => {
140-
transport.connection.on("message", () => {
141-
fn(JSON.stringify({
142-
jsonrpc: "2.0",
143-
id: 7,
144-
error: {
145-
code: 0,
146-
message: "out of order",
147-
data: {
148-
foo: "bar",
149-
},
157+
c.connect().then(() => {
158+
c.request("foo", [])
159+
.catch((e) => {
160+
expect(e.message).toEqual("out of order");
161+
done();
162+
});
163+
serverTransport.sendData(JSON.stringify({
164+
jsonrpc: "2.0",
165+
id: 0,
166+
error: {
167+
code: 0,
168+
message: "out of order",
169+
data: {
170+
foo: "bar",
150171
},
151-
}));
152-
});
153-
};
154-
c.connect();
155-
expect(c.request("foo", [])).rejects.toBe({
156-
code: 0,
157-
message: "out of order",
158-
data: {
159-
foo: "bar",
160-
},
172+
},
173+
}));
161174
});
162175
});
163176

‎src/RequestManager.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class RequestManager {
130130
} else if (response.result) {
131131
promiseForResult.resolve(response.result);
132132
} else {
133-
throw new Error(`Malformed JSON-RPC response object: ${response}`);
133+
promiseForResult.reject(new Error(`Malformed JSON-RPC response object: ${response}`));
134134
}
135135
});
136136
}

0 commit comments

Comments
 (0)