From 29180fd11f51d1472bd1c77c88f7895f2c8768ef Mon Sep 17 00:00:00 2001 From: timonson Date: Fri, 1 Sep 2023 03:06:55 +0200 Subject: [PATCH] Update API --- server/response.ts | 6 +++-- server/response_test.ts | 56 ++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/server/response.ts b/server/response.ts index 7f8d8f4..fed71ef 100644 --- a/server/response.ts +++ b/server/response.ts @@ -30,11 +30,11 @@ export type AuthData = { verify?: ReturnType; }; -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 => { + return async (request: Request, methods: Methods): Promise => { const authData = { verify, headers: request.headers }; const validationObjectOrBatch = validateRequest( await request.text(), @@ -61,3 +61,5 @@ export function respond(methods: Methods, options: Options = {}) { } }; } + +export const makeRpcResponse = respond(); diff --git a/server/response_test.ts b/server/response_test.ts index 10ef7d5..5ca03bf 100644 --- a/server/response_test.ts +++ b/server/response_test.ts @@ -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) { @@ -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), ); }); @@ -64,7 +64,7 @@ Deno.test("rpc call with named parameters", async function (): Promise { 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), ); }); @@ -74,13 +74,13 @@ Deno.test("rpc call as a notification", async function (): Promise { '{"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(), "", ); }); @@ -91,7 +91,7 @@ Deno.test("rpc call of non-existent method", async function (): Promise { '{"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), ); }); @@ -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), ); }); @@ -116,7 +116,7 @@ Deno.test("rpc call invalid params", async function (): Promise { '{"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), ); }); @@ -128,7 +128,7 @@ Deno.test("rpc call internal error", async function (): Promise { '{"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), ); }); @@ -140,7 +140,7 @@ Deno.test("rpc call with invalid JSON", async function (): Promise { '{"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), ); }); @@ -152,7 +152,7 @@ Deno.test("rpc call Batch, invalid JSON", async function (): Promise { '{"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), ); }); @@ -163,7 +163,7 @@ Deno.test("rpc call with an empty Array", 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), ); }); @@ -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), ); }, @@ -188,7 +188,7 @@ Deno.test("rpc call with invalid Batch", async function (): Promise { '[ {"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), ); }); @@ -200,7 +200,7 @@ Deno.test("rpc call Batch", async function (): Promise { '[ {"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), ); }); @@ -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(), "", ); }); @@ -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", ); @@ -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), ); }); @@ -258,20 +258,20 @@ Deno.test("rpc call with jwt", async function (): Promise { 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}', ), @@ -281,9 +281,9 @@ Deno.test("rpc call with jwt", async function (): Promise { ); 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}', ), @@ -292,9 +292,9 @@ Deno.test("rpc call with jwt", async function (): Promise { '{"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}', ),