controller.class.test.ts 901 B

1234567891011121314151617181920212223242526
  1. import { assertEquals } from 'test-deps';
  2. import Controller from 'if/controllers/controller.class.ts';
  3. Deno.test('Controller class', async () => {
  4. const mockHandler = (req: Request) => new Response('Hello, world!');
  5. const controller = new Controller({
  6. '/_GET': mockHandler,
  7. });
  8. // Test that the handler was stored correctly
  9. assertEquals(controller.handlers['/_GET'], mockHandler);
  10. // Test the response method
  11. const req = new Request('http://localhost/');
  12. const res = Controller.response(req, 'Hello, world!');
  13. assertEquals(res.status, 200);
  14. assertEquals(
  15. new TextDecoder().decode(new Uint8Array(await res.arrayBuffer())),
  16. 'Hello, world!',
  17. );
  18. // Test the responseJSON method
  19. const jsonRes = Controller.responseJSON(req, { message: 'Hello, world!' });
  20. assertEquals(jsonRes.status, 200);
  21. assertEquals(await jsonRes.json(), { message: 'Hello, world!' });
  22. });