|
1 | 1 | /* eslint-disable @typescript-eslint/no-unused-vars */ |
2 | 2 | import { strict as assert } from 'assert'; |
3 | | -import axios from 'axios'; |
| 3 | +import axios, { AxiosRequestConfig } from 'axios'; |
4 | 4 |
|
5 | 5 | import { Server } from 'http'; |
| 6 | +import { Request, Response, NextFunction } from 'express'; |
6 | 7 | import { feathers, HookContext, Id, Params } from '@feathersjs/feathers'; |
7 | | -import { Service, testRest } from '@feathersjs/tests'; |
| 8 | +import { Service, restTests } from '@feathersjs/tests'; |
| 9 | +import { BadRequest } from '@feathersjs/errors'; |
8 | 10 |
|
9 | 11 | import * as express from '../src' |
10 | | -import { Request, Response, NextFunction } from 'express'; |
11 | | -import { BadRequest } from '@feathersjs/errors/lib'; |
12 | 12 |
|
13 | 13 | const expressify = express.default; |
14 | 14 | const { rest } = express; |
| 15 | +const errorHandler = express.errorHandler({ |
| 16 | + logger: false |
| 17 | +}); |
15 | 18 |
|
16 | 19 | describe('@feathersjs/express/rest provider', () => { |
17 | 20 | describe('base functionality', () => { |
@@ -99,9 +102,9 @@ describe('@feathersjs/express/rest provider', () => { |
99 | 102 |
|
100 | 103 | after(done => server.close(done)); |
101 | 104 |
|
102 | | - testRest('Services', 'todo', 4777); |
103 | | - testRest('Root Service', '/', 4777); |
104 | | - testRest('Dynamic Services', 'tasks', 4777); |
| 105 | + restTests('Services', 'todo', 4777); |
| 106 | + restTests('Root Service', '/', 4777); |
| 107 | + restTests('Dynamic Services', 'tasks', 4777); |
105 | 108 |
|
106 | 109 | describe('res.hook', () => { |
107 | 110 | const convertHook = (hook: HookContext) => { |
@@ -526,7 +529,7 @@ describe('@feathersjs/express/rest provider', () => { |
526 | 529 | }; |
527 | 530 | } |
528 | 531 | }) |
529 | | - .use(express.errorHandler()); |
| 532 | + .use(errorHandler); |
530 | 533 |
|
531 | 534 | server = await app.listen(6880); |
532 | 535 | }); |
@@ -566,62 +569,68 @@ describe('@feathersjs/express/rest provider', () => { |
566 | 569 | }); |
567 | 570 | }); |
568 | 571 |
|
569 | | - // describe('Custom methods', () => { |
570 | | - // let server: Server; |
571 | | - // let app: express.Application; |
572 | | - |
573 | | - // before(async () => { |
574 | | - // app = expressify(feathers()) |
575 | | - // .configure(rest()) |
576 | | - // .use(express.json()) |
577 | | - // .use('/todo', { |
578 | | - // async get (id) { |
579 | | - // return id; |
580 | | - // }, |
581 | | - // // httpMethod is usable as a decorator: @httpMethod('POST', '/:__feathersId/custom-path') |
582 | | - // custom: rest.httpMethod('POST')((feathers as any).activateHooks(['id', 'data', 'params'])( |
583 | | - // (id: any, data: any) => { |
584 | | - // return Promise.resolve({ |
585 | | - // id, |
586 | | - // data |
587 | | - // }); |
588 | | - // } |
589 | | - // )), |
590 | | - // other: rest.httpMethod('PATCH', ':__feathersId/second-method')( |
591 | | - // (feathers as any).activateHooks(['id', 'data', 'params'])( |
592 | | - // (id: any, data: any) => { |
593 | | - // return Promise.resolve({ |
594 | | - // id, |
595 | | - // data |
596 | | - // }); |
597 | | - // } |
598 | | - // ) |
599 | | - // ) |
600 | | - // }); |
601 | | - |
602 | | - // server = await app.listen(4781); |
603 | | - // }); |
604 | | - |
605 | | - // after(done => server.close(done)); |
606 | | - |
607 | | - // it('works with custom methods', async () => { |
608 | | - // const res = await axios.post('http://localhost:4781/todo/42/custom', { text: 'Do dishes' }); |
609 | | - |
610 | | - // assert.equal(res.headers.allow, 'GET,POST,PATCH'); |
611 | | - // assert.deepEqual(res.data, { |
612 | | - // id: '42', |
613 | | - // data: { text: 'Do dishes' } |
614 | | - // }); |
615 | | - // }); |
616 | | - |
617 | | - // it('works with custom methods - with route', async () => { |
618 | | - // const res = await axios.patch('http://localhost:4781/todo/12/second-method', { text: 'Hmm' }); |
619 | | - |
620 | | - // assert.equal(res.headers.allow, 'GET,POST,PATCH'); |
621 | | - // assert.deepEqual(res.data, { |
622 | | - // id: '12', |
623 | | - // data: { text: 'Hmm' } |
624 | | - // }); |
625 | | - // }); |
626 | | - // }); |
| 572 | + describe('Custom methods', () => { |
| 573 | + let server: Server; |
| 574 | + let app: express.Application; |
| 575 | + |
| 576 | + before(async () => { |
| 577 | + app = expressify(feathers()) |
| 578 | + .configure(rest()) |
| 579 | + .use(express.json()) |
| 580 | + .use('/todo', new Service(), { |
| 581 | + methods: ['find', 'customMethod'] |
| 582 | + }) |
| 583 | + .use(errorHandler); |
| 584 | + |
| 585 | + server = await app.listen(4781); |
| 586 | + }); |
| 587 | + |
| 588 | + after(done => server.close(done)); |
| 589 | + |
| 590 | + it('calls .customMethod with X-Service-Method header', async () => { |
| 591 | + const payload = { text: 'Do dishes' }; |
| 592 | + const res = await axios.post('http://localhost:4781/todo', payload, { |
| 593 | + headers: { |
| 594 | + 'X-Service-Method': 'customMethod' |
| 595 | + } |
| 596 | + }); |
| 597 | + |
| 598 | + assert.deepEqual(res.data, { |
| 599 | + data: payload, |
| 600 | + method: 'customMethod', |
| 601 | + provider: 'rest' |
| 602 | + }); |
| 603 | + }); |
| 604 | + |
| 605 | + it('throws MethodNotImplement for .setup, non option and default methods', async () => { |
| 606 | + const options: AxiosRequestConfig = { |
| 607 | + method: 'POST', |
| 608 | + url: 'http://localhost:4781/todo', |
| 609 | + data: { text: 'Do dishes' } |
| 610 | + }; |
| 611 | + const testMethod = (name: string) => { |
| 612 | + return assert.rejects(() => axios({ |
| 613 | + ...options, |
| 614 | + headers: { |
| 615 | + 'X-Service-Method': name |
| 616 | + } |
| 617 | + }), (error: any) => { |
| 618 | + assert.deepEqual(error.response.data, { |
| 619 | + name: 'MethodNotAllowed', |
| 620 | + message: `Method \`${name}\` is not supported by this endpoint.`, |
| 621 | + code: 405, |
| 622 | + className: 'method-not-allowed' |
| 623 | + }); |
| 624 | + |
| 625 | + return true; |
| 626 | + }); |
| 627 | + } |
| 628 | + |
| 629 | + await testMethod('setup'); |
| 630 | + await testMethod('internalMethod'); |
| 631 | + await testMethod('nonExisting'); |
| 632 | + await testMethod('create'); |
| 633 | + await testMethod('find'); |
| 634 | + }); |
| 635 | + }); |
627 | 636 | }); |
0 commit comments