This repository has been archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(middleware): Add tests for all middleware classes and decorators
- Loading branch information
Showing
16 changed files
with
313 additions
and
32 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
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,15 @@ | ||
import { Logger, LogLevel } from './logger.service'; | ||
import { Injectable } from '@angular/core'; | ||
|
||
@Injectable() | ||
export class LoggerMock extends Logger { | ||
|
||
constructor() { | ||
super(LoggerMock); | ||
} | ||
|
||
public persistLog(logLevel: LogLevel, messages: any[]): Promise<this>|this { | ||
return this; | ||
} | ||
|
||
} |
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
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,96 @@ | ||
import { | ||
it, | ||
inject, | ||
beforeEachProviders, | ||
expect, | ||
describe, | ||
fdescribe | ||
} from '@angular/core/testing'; | ||
import { IsolatedMiddlewareFactory } from './index'; | ||
import { Request } from '../controllers/request'; | ||
import { Response } from '../controllers/response'; | ||
import { Action } from '../controllers/action.decorator'; | ||
import { AfterAll, BeforeAll, Before, After } from './middleware.decorator'; | ||
import { AbstractController } from '../controllers/abstract.controller'; | ||
import { Injectable, Injector, Provider, provide } from '@angular/core'; | ||
import { Logger } from '../../common/services/logger.service'; | ||
import { Server, RouteConfig } from '../servers/abstract.server'; | ||
import { LoggerMock } from '../../common/services/logger.service.spec'; | ||
import { ServerMock } from '../servers/abstract.server.spec'; | ||
import { RemoteCli } from '../services/remoteCli.service'; | ||
import { RemoteCliMock } from '../services/remoteCli.service.spec'; | ||
import { PromiseFactory } from '../../common/util/serialPromise'; | ||
import { debugLog, DebugLogMiddleware } from './debugLog.middleware'; | ||
|
||
@Injectable() | ||
class MiddlewareController extends AbstractController { | ||
|
||
constructor(server: Server, logger: Logger) { | ||
super(server, logger); | ||
} | ||
|
||
@Action('GET', '/test') | ||
@Before(debugLog('test log input')) | ||
public testMethod(request: Request, response: Response): Response { | ||
return response; | ||
} | ||
|
||
} | ||
|
||
let source: string, logs: any[] = []; | ||
|
||
let mockLogger = { | ||
source: (input: string) => { | ||
source = input; | ||
return mockLogger; | ||
}, | ||
debug: (input: string) => { | ||
logs.push(input); | ||
} | ||
}; | ||
|
||
const providers = [ | ||
MiddlewareController, | ||
provide(Server, {useClass: ServerMock}), | ||
provide(Logger, {useClass: LoggerMock}), | ||
provide(RemoteCli, {useClass: RemoteCliMock}), | ||
provide(DebugLogMiddleware, { | ||
deps: [], | ||
useFactory: () => { | ||
return new DebugLogMiddleware(<Logger>mockLogger) | ||
}, | ||
}) | ||
]; | ||
|
||
describe('debugLog middleware', () => { | ||
|
||
let controller: MiddlewareController; | ||
|
||
beforeEachProviders(() => providers); | ||
|
||
it('Calls debug.log on the passed value to the middleware decorator', | ||
inject([MiddlewareController, Injector, Server], | ||
(c: MiddlewareController, i: Injector, s: Server) => { | ||
|
||
controller = c.registerInjector(i) | ||
.registerRoutes(); | ||
|
||
const callStackHandler: any = s.getRoutes() | ||
.find((route: RouteConfig) => route.methodName == 'testMethod').callStackHandler; | ||
|
||
let request = new Request(); | ||
let response = new Response(); | ||
|
||
return callStackHandler(request, response) | ||
.then(() => { | ||
|
||
expect(source) | ||
.toEqual('debugLog'); | ||
expect(logs) | ||
.toEqual(['test log input']); | ||
|
||
}); | ||
|
||
})); | ||
|
||
}); |
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
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
Oops, something went wrong.