Skip to content

Commit 46e8eec

Browse files
committed
fix(batching): add error tests
1 parent c867faa commit 46e8eec

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/RequestManager.test.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,28 @@ describe("client-js", () => {
3939
c.request("foo", []);
4040
});
4141

42-
it("can batch a request", async () => {
42+
it("can error on batchng a request", async () => {
43+
const transport = new EventEmitterTransport("foo://unique-uri");
44+
const c = new RequestManager([transport]);
45+
return c.connect().then(() => {
46+
expect(() => c.endBatch()).toThrow();
47+
});
48+
});
49+
50+
it("can return errors on batchng requests", async () => {
4351
const transport = new EventEmitterTransport("foo://unique-uri");
4452
transport.sendData = (data) => {
4553
const result = JSON.stringify([
4654
{
4755
jsonrpc: "2.0",
4856
id: 3,
49-
result: "foo",
57+
error: {
58+
code: 509,
59+
message: "too much 509",
60+
data: {
61+
test: "data",
62+
},
63+
},
5064
},
5165
{
5266
jsonrpc: "2.0",
@@ -57,6 +71,43 @@ describe("client-js", () => {
5771
transport.connection.emit("message", result);
5872
};
5973

74+
const c = new RequestManager([transport]);
75+
return c.connect().then(() => {
76+
c.startBatch();
77+
const requests = [
78+
c.request("foo", []),
79+
c.request("foo", []),
80+
];
81+
c.endBatch();
82+
expect(Promise.all(requests)).rejects.toEqual({
83+
code: 509,
84+
message: "too much 509",
85+
data: {
86+
test: "data",
87+
},
88+
});
89+
c.close();
90+
});
91+
});
92+
93+
it("can batch a request", async () => {
94+
const transport = new EventEmitterTransport("foo://unique-uri");
95+
transport.sendData = (data) => {
96+
const result = JSON.stringify([
97+
{
98+
jsonrpc: "2.0",
99+
id: 5,
100+
result: "foo",
101+
},
102+
{
103+
jsonrpc: "2.0",
104+
id: 6,
105+
result: "bar",
106+
},
107+
]);
108+
transport.connection.emit("message", result);
109+
};
110+
60111
const c = new RequestManager([transport]);
61112
return c.connect().then(() => {
62113
c.startBatch();
@@ -81,7 +132,7 @@ describe("client-js", () => {
81132
transport.connection.on("message", () => {
82133
fn(JSON.stringify({
83134
jsonrpc: "2.0",
84-
id: 3,
135+
id: 7,
85136
error: {
86137
code: 0,
87138
message: "out of order",

src/RequestManager.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,16 @@ class RequestManager {
114114

115115
private onData(data: string): void {
116116
const parsedData: IJSONRPCResponse[] | IJSONRPCResponse = JSON.parse(data);
117+
// handle batch requests
117118
if (Array.isArray(parsedData)) {
118119
parsedData.forEach((response) => {
119-
if (this.requests[response.id]) {
120-
if (response.error) {
121-
this.requests[response.id].reject(new Error(response.error.message));
122-
} else {
123-
this.requests[response.id].resolve(response.result);
124-
}
120+
if (!this.requests[response.id]) {
121+
return;
122+
}
123+
if (response.error) {
124+
this.requests[response.id].reject(response.error);
125+
} else {
126+
this.requests[response.id].resolve(response.result);
125127
}
126128
});
127129
return;

0 commit comments

Comments
 (0)