-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8b094b
commit 47fe9d0
Showing
2 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// deno-lint-ignore-file no-explicit-any | ||
import { assertEquals, assertObjectMatch } from 'https://deno.land/[email protected]/assert/mod.ts'; | ||
import { beforeEach, describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts'; | ||
import { spy } from "https://deno.land/[email protected]/testing/mock.ts"; | ||
|
||
import { AppObjectRegistry } from '../../AppObjectRegistry.ts'; | ||
import { assertInstanceOf } from "https://deno.land/[email protected]/assert/assert_instance_of.ts"; | ||
import { JsonRpcError } from "jsonrpc-lite"; | ||
import { IApiEndpoint } from "@rocket.chat/apps-engine/definition/api/IApiEndpoint.ts"; | ||
import apiHandler from "../api-handler.ts"; | ||
|
||
describe('handlers > api', () => { | ||
const mockEndpoint: IApiEndpoint = { | ||
path: '/test', | ||
// deno-lint-ignore no-unused-vars | ||
get: (request: any, endpoint: any, read: any, modify: any, http: any, persis: any) => Promise.resolve('ok'), | ||
// deno-lint-ignore no-unused-vars | ||
post: (request: any, endpoint: any, read: any, modify: any, http: any, persis: any) => Promise.resolve('ok'), | ||
// deno-lint-ignore no-unused-vars | ||
put: (request: any, endpoint: any, read: any, modify: any, http: any, persis: any) => { throw new Error('Method execution error example') }, | ||
} | ||
|
||
beforeEach(() => { | ||
AppObjectRegistry.clear(); | ||
AppObjectRegistry.set('api:/test', mockEndpoint); | ||
}); | ||
|
||
it('correctly handles execution of an api endpoint method GET', async () => { | ||
const _spy = spy(mockEndpoint, 'get'); | ||
|
||
const result = await apiHandler('api:/test:get', ['request', 'endpointInfo']); | ||
|
||
assertEquals(result, 'ok'); | ||
assertEquals(_spy.calls[0].args.length, 6); | ||
assertEquals(_spy.calls[0].args[0], 'request'); | ||
assertEquals(_spy.calls[0].args[1], 'endpointInfo'); | ||
}); | ||
|
||
it('correctly handles execution of an api endpoint method POST', async () => { | ||
const _spy = spy(mockEndpoint, 'post'); | ||
|
||
const result = await apiHandler('api:/test:post', ['request', 'endpointInfo']); | ||
|
||
assertEquals(result, 'ok'); | ||
assertEquals(_spy.calls[0].args.length, 6); | ||
assertEquals(_spy.calls[0].args[0], 'request'); | ||
assertEquals(_spy.calls[0].args[1], 'endpointInfo'); | ||
}); | ||
|
||
it('correctly handles an error if the method not exists for the selected endpoint', async () => { | ||
const result = await apiHandler(`api:/test:delete`, ['request', 'endpointInfo']); | ||
|
||
assertInstanceOf(result, JsonRpcError) | ||
assertObjectMatch(result, { | ||
message: `/test's delete not exists`, | ||
code: -32000 | ||
}) | ||
}); | ||
|
||
it('correctly handles an error if endpoint not exists', async () => { | ||
const result = await apiHandler(`api:/error:get`, ['request', 'endpointInfo']); | ||
|
||
assertInstanceOf(result, JsonRpcError) | ||
assertObjectMatch(result, { | ||
message: `Endpoint /error not found`, | ||
code: -32000 | ||
}) | ||
}); | ||
|
||
it('correctly handles an error if the method execution fails', async () => { | ||
const result = await apiHandler(`api:/test:put`, ['request', 'endpointInfo']); | ||
|
||
assertInstanceOf(result, JsonRpcError) | ||
assertObjectMatch(result, { | ||
message: `Method execution error example`, | ||
code: -32000 | ||
}) | ||
}); | ||
}); |