Skip to content

Commit

Permalink
Update API
Browse files Browse the repository at this point in the history
  • Loading branch information
timonson committed Sep 1, 2023
1 parent 6697676 commit 29180fd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
6 changes: 4 additions & 2 deletions server/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export type AuthData = {
verify?: ReturnType<typeof verifyJwt>;
};

export function respond(methods: Methods, options: Options = {}) {
export function respond(options: Options = {}) {
const verify = options.auth
? verifyJwt(options.auth.input, options.auth.options)
: undefined;
return async (request: Request): Promise<Response> => {
return async (request: Request, methods: Methods): Promise<Response> => {
const authData = { verify, headers: request.headers };
const validationObjectOrBatch = validateRequest(
await request.text(),
Expand All @@ -61,3 +61,5 @@ export function respond(methods: Methods, options: Options = {}) {
}
};
}

export const makeRpcResponse = respond();
56 changes: 28 additions & 28 deletions server/response_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEquals, create, Payload } from "../test_deps.ts";
import { respond } from "./response.ts";
import { makeRpcResponse, respond } from "./response.ts";
import { CustomError } from "./custom_error.ts";

function createReq(str: string) {
Expand Down Expand Up @@ -53,7 +53,7 @@ Deno.test("rpc call with positional parameters", async function (): Promise<
const sentToClient = '{"jsonrpc": "2.0", "result": 19, "id": 1}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -64,7 +64,7 @@ Deno.test("rpc call with named parameters", async function (): Promise<void> {
const sentToClient = '{"jsonrpc": "2.0", "result": 19, "id": 3}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -74,13 +74,13 @@ Deno.test("rpc call as a notification", async function (): Promise<void> {
'{"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
"",
);

sentToServer = '{"jsonrpc": "2.0", "method": "foobar"}';
assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
"",
);
});
Expand All @@ -91,7 +91,7 @@ Deno.test("rpc call of non-existent method", async function (): Promise<void> {
'{"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "1"}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -104,7 +104,7 @@ Deno.test("rpc call with invalid request object", async function (): Promise<
'{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -116,7 +116,7 @@ Deno.test("rpc call invalid params", async function (): Promise<void> {
'{"jsonrpc": "2.0", "error": {"code": -32602, "message": "Invalid params"}, "id": 1}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -128,7 +128,7 @@ Deno.test("rpc call internal error", async function (): Promise<void> {
'{"jsonrpc": "2.0", "error": {"code": -32603, "message": "Internal error"}, "id": 1}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -140,7 +140,7 @@ Deno.test("rpc call with invalid JSON", async function (): Promise<void> {
'{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -152,7 +152,7 @@ Deno.test("rpc call Batch, invalid JSON", async function (): Promise<void> {
'{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Parse error"}, "id": null}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -163,7 +163,7 @@ Deno.test("rpc call with an empty Array", async function (): Promise<void> {
'{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -176,7 +176,7 @@ Deno.test(
'[ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null} ]';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
},
Expand All @@ -188,7 +188,7 @@ Deno.test("rpc call with invalid Batch", async function (): Promise<void> {
'[ {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null} ]';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -200,7 +200,7 @@ Deno.test("rpc call Batch", async function (): Promise<void> {
'[ {"jsonrpc": "2.0", "result": 7, "id": "1"}, {"jsonrpc": "2.0", "result": 19, "id": "2"}, {"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}, "id": null}, {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Method not found"}, "id": "5"}, {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"} ]';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -212,7 +212,7 @@ Deno.test("rpc call Batch (all notifications)", async function (): Promise<
'[ {"jsonrpc": "2.0", "method": "notify_sum", "params": [1,2,4]}, {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]} ]';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
"",
);
});
Expand All @@ -224,15 +224,15 @@ Deno.test("rpc call with publicErrorStack set to true", async function (): Promi

assertEquals(
typeof JSON.parse(
await ((await respond(methods)(createReq(sentToServer))).text()),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
).error.data,
"undefined",
);
assertEquals(
typeof JSON.parse(
await ((await respond(methods, {
await ((await respond({
publicErrorStack: true,
})(createReq(sentToServer))).text()),
})(createReq(sentToServer), methods)).text()),
).error.data,
"string",
);
Expand All @@ -247,7 +247,7 @@ Deno.test("rpc call with a custom error", async function (): Promise<
'{"jsonrpc":"2.0","error":{"code":-32000,"message":"my custom error","data":{"details":"error details"}},"id":1}';

assertEquals(
await (await respond(methods)(createReq(sentToServer))).text(),
await (await makeRpcResponse(createReq(sentToServer), methods)).text(),
removeWhiteSpace(sentToClient),
);
});
Expand All @@ -258,20 +258,20 @@ Deno.test("rpc call with jwt", async function (): Promise<void> {
const reqOne = createReq(sentToServer);
reqOne.headers.append("Authorization", `Bearer ${jwt}`);
assertEquals(
await (await respond(methods, {
await (await respond({
headers: new Headers({
"Authorization": `Bearer ${jwt}`,
}),
auth: { input: { cryptoKey, algorithm }, methods: ["login"] },
})(reqOne)).text(),
})(reqOne, methods)).text(),
removeWhiteSpace(sentToClient),
);
const reqTwo = createReq(sentToServer);
reqTwo.headers.append("Authorization", `Bearer ${jwt.slice(1)}`),
assertEquals(
await (await respond(methods, {
await (await respond({
auth: { input: { cryptoKey, algorithm }, methods: ["login"] },
})(reqTwo)).text(),
})(reqTwo, methods)).text(),
removeWhiteSpace(
'{"jsonrpc": "2.0", "error": {"code": -32020, "message": "Authorization error"}, "id": 3}',
),
Expand All @@ -281,9 +281,9 @@ Deno.test("rpc call with jwt", async function (): Promise<void> {
);
reqThree.headers.append("Authorization", `Bearer ${jwt.slice(1)}`);
assertEquals(
await (await respond(methods, {
await (await respond({
auth: { input: { cryptoKey, algorithm }, methods: new RegExp(".+") },
})(reqThree)).text(),
})(reqThree, methods)).text(),
removeWhiteSpace(
'{"jsonrpc": "2.0", "error": {"code": -32020, "message": "Authorization error"}, "id": 3}',
),
Expand All @@ -292,9 +292,9 @@ Deno.test("rpc call with jwt", async function (): Promise<void> {
'{"jsonrpc": "2.0", "method": "login", "params": {"user": "Bob"}, "id": 3}',
);
assertEquals(
await (await respond(methods, {
await (await respond({
auth: { input: { cryptoKey, algorithm }, methods: ["login"] },
})(reqFour)).text(),
})(reqFour, methods)).text(),
removeWhiteSpace(
'{"jsonrpc": "2.0", "error": {"code": -32020, "message": "Authorization error"}, "id": 3}',
),
Expand Down

0 comments on commit 29180fd

Please sign in to comment.