|
1 | 1 | // tslint:disable:no-shadowed-variable |
| 2 | + |
| 3 | +import {NextFunction, Request, Response} from 'express'; |
2 | 4 | import {MongoClient} from 'mongodb'; |
3 | 5 | import * as request from 'supertest'; |
4 | 6 | import {SakuraApiConfig} from '../boot/sakura-api-config'; |
5 | 7 | import {testMongoDbUrl, testSapi, testUrl} from '../spec/helpers/sakuraapi'; |
| 8 | +import {Json} from './@model/json'; |
| 9 | +import {Model} from './@model/model'; |
| 10 | +import {SakuraApiModel} from './@model/sakura-api-model'; |
6 | 11 | import {Routable, Route} from './@routable/'; |
| 12 | +import {SakuraApi, SakuraApiPluginResult} from './sakura-api'; |
7 | 13 | import Spy = jasmine.Spy; |
8 | 14 |
|
9 | 15 | describe('core/SakuraApi', () => { |
@@ -317,5 +323,137 @@ describe('core/SakuraApi', () => { |
317 | 323 | .catch(done.fail); |
318 | 324 | }); |
319 | 325 | }); |
| 326 | + |
| 327 | + describe('plugins', () => { |
| 328 | + |
| 329 | + function testPluginA(sapi: SakuraApi, options: any): SakuraApiPluginResult { |
| 330 | + |
| 331 | + function testHandler(req: Request, res: Response, next: NextFunction) { |
| 332 | + if (!res.locals.handlerTrace) { |
| 333 | + res.locals.handlerTrace = ''; |
| 334 | + } |
| 335 | + res.locals.handlerTrace += res.locals.handlerTrace = options.value; |
| 336 | + next(); |
| 337 | + } |
| 338 | + |
| 339 | + @Model() |
| 340 | + class TestModelPlugin extends SakuraApiModel { |
| 341 | + @Json() |
| 342 | + modelValue = 'found'; |
| 343 | + } |
| 344 | + |
| 345 | + @Routable() |
| 346 | + class TestRoutablePlugin { |
| 347 | + @Route({ |
| 348 | + method: 'get', |
| 349 | + path: 'TestRoutablePlugin' |
| 350 | + }) |
| 351 | + getHandler(req: Request, res: Response, next: NextFunction) { |
| 352 | + const result = new TestModelPlugin(); |
| 353 | + |
| 354 | + res |
| 355 | + .status(200) |
| 356 | + .json(result.toJson()); |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + return { |
| 361 | + middlewareHandlers: [testHandler], |
| 362 | + models: [TestModelPlugin], |
| 363 | + routables: [TestRoutablePlugin] |
| 364 | + }; |
| 365 | + } |
| 366 | + |
| 367 | + function testPluginB(sapi: SakuraApi, options: any): SakuraApiPluginResult { |
| 368 | + function testHandler(req: Request, res: Response, next: NextFunction) { |
| 369 | + if (!res.locals.handlerTrace) { |
| 370 | + res.locals.handlerTrace = ''; |
| 371 | + } |
| 372 | + res.locals.handlerTrace += res.locals.handlerTrace = options.value; |
| 373 | + next(); |
| 374 | + } |
| 375 | + |
| 376 | + return { |
| 377 | + middlewareHandlers: [testHandler] |
| 378 | + }; |
| 379 | + } |
| 380 | + |
| 381 | + @Routable() |
| 382 | + class RoutableTestStub { |
| 383 | + response = 'testRouterGet worked'; |
| 384 | + |
| 385 | + @Route({ |
| 386 | + method: 'get', |
| 387 | + path: 'plugins_test' |
| 388 | + }) |
| 389 | + testRouterGet(req, res) { |
| 390 | + res |
| 391 | + .status(200) |
| 392 | + .json({ |
| 393 | + testHandlerResult: res.locals.handlerTrace |
| 394 | + }); |
| 395 | + } |
| 396 | + } |
| 397 | + |
| 398 | + const sapi = testSapi({ |
| 399 | + plugins: [ |
| 400 | + { |
| 401 | + options: { |
| 402 | + value: 'A' |
| 403 | + }, |
| 404 | + order: 1, |
| 405 | + plugin: testPluginA |
| 406 | + }, |
| 407 | + { |
| 408 | + options: { |
| 409 | + value: 'B' |
| 410 | + }, |
| 411 | + order: 0, |
| 412 | + plugin: testPluginB |
| 413 | + } |
| 414 | + ], |
| 415 | + routables: [ |
| 416 | + RoutableTestStub |
| 417 | + ] |
| 418 | + }); |
| 419 | + |
| 420 | + beforeEach((done) => { |
| 421 | + sapi |
| 422 | + .listen({bootMessage: ''}) |
| 423 | + .then(done) |
| 424 | + .catch(done.fail); |
| 425 | + }); |
| 426 | + |
| 427 | + afterEach((done) => { |
| 428 | + sapi |
| 429 | + .close() |
| 430 | + .then(done) |
| 431 | + .catch(done.fail); |
| 432 | + }); |
| 433 | + |
| 434 | + it('adds plugin handlers in the proper order', (done) => { |
| 435 | + request(sapi.app) |
| 436 | + .get(testUrl('plugins_test')) |
| 437 | + .expect(200) |
| 438 | + .then((result) => { |
| 439 | + const body = result.body; |
| 440 | + expect(body.testHandlerResult).toBe('BA'); |
| 441 | + }) |
| 442 | + .then(done) |
| 443 | + .catch(done.fail); |
| 444 | + }); |
| 445 | + |
| 446 | + it('adds plugin models and routables', (done) => { |
| 447 | + request(sapi.app) |
| 448 | + .get(testUrl('TestRoutablePlugin')) |
| 449 | + .expect(200) |
| 450 | + .then((result) => { |
| 451 | + const body = result.body; |
| 452 | + expect(body.modelValue).toBe('found'); |
| 453 | + }) |
| 454 | + .then(done) |
| 455 | + .catch(done.fail); |
| 456 | + }); |
| 457 | + }); |
320 | 458 | }); |
321 | 459 | // tslint:enable:no-shadowed-variable |
0 commit comments