From bbfeda585bd4ae6ddaa413d705e34a176f7bad30 Mon Sep 17 00:00:00 2001 From: rafaelsouzaf Date: Fri, 8 Dec 2017 12:22:32 -0300 Subject: [PATCH] Add events (server and request) test files. --- types/hapi/v17/test/index.test.d.ts | 5 ++ types/hapi/v17/test/request/event-types.ts | 71 +++++++++++++++++++ .../v17/test/server/server-events-once.ts | 26 +++++++ 3 files changed, 102 insertions(+) create mode 100644 types/hapi/v17/test/request/event-types.ts create mode 100644 types/hapi/v17/test/server/server-events-once.ts diff --git a/types/hapi/v17/test/index.test.d.ts b/types/hapi/v17/test/index.test.d.ts index 9e98fa9c31..35555aa057 100644 --- a/types/hapi/v17/test/index.test.d.ts +++ b/types/hapi/v17/test/index.test.d.ts @@ -17,10 +17,15 @@ export * from './reply/continue'; export * from './reply/redirect'; export * from './reply/reply'; export * from './reply/state_cookie'; + +export * from './request/event-types'; + export * from './response/error'; export * from './server/app'; export * from './server/info'; +export * from './server/server-events-once'; export * from './server/table'; + diff --git a/types/hapi/v17/test/request/event-types.ts b/types/hapi/v17/test/request/event-types.ts new file mode 100644 index 0000000000..ae1c0ac081 --- /dev/null +++ b/types/hapi/v17/test/request/event-types.ts @@ -0,0 +1,71 @@ +// From https://github.com/hapijs/hapi/blob/master/API.md#-servereventevents +// From https://github.com/hapijs/hapi/blob/master/API.md#-requestevents +import { + Request, RequestExtPointFunction, ResponseToolkit, RouteOptions, Server, ServerOptions, + ServerRoute +} from "hapi"; +import * as Crypto from 'crypto'; + +const options: ServerOptions = { + port: 8000, +}; + +const serverRoute: ServerRoute = { + path: '/', + method: 'GET', + handler: function (request: Request, h: ResponseToolkit) { + return 'ok: ' + request.path; + } +}; + +const onRequest: RequestExtPointFunction = function (request: Request, h: ResponseToolkit) { + + /* + * Server events + */ + request.server.events.on('request', (request: Request, event: any, tags: any) => { + console.log(request.paramsArray); + console.log(event); + console.log(tags); + }); + + request.server.events.on('response', (request: Request) => { + console.log('Response sent for request: ' + request.path); + }); + + request.server.events.on('start', (route: RouteOptions) => { + console.log('Server started'); + }); + + request.server.events.once('stop', (route: RouteOptions) => { + console.log('Server stoped'); + }); + + /* + * Request events + */ + const hash = Crypto.createHash('sha1'); + + request.events.on("peek", (chunk: any) => { + hash.update(chunk); + }); + + request.events.once("finish", () => { + console.log(hash.digest('hex')); + }); + + request.events.once("disconnect", () => { + console.error('request aborted'); + }); + + return h.continue; + +}; + +const server = new Server(options); +server.route(serverRoute); +server.ext('onRequest', onRequest); + +server.start(); +console.log('Server started at: ' + server.info.uri); + diff --git a/types/hapi/v17/test/server/server-events-once.ts b/types/hapi/v17/test/server/server-events-once.ts new file mode 100644 index 0000000000..d36deb10bb --- /dev/null +++ b/types/hapi/v17/test/server/server-events-once.ts @@ -0,0 +1,26 @@ +// from https://github.com/hapijs/hapi/blob/master/API.md#-servereventsoncecriteria-listener +import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi"; + +const options: ServerOptions = { + port: 8000, +}; + +const serverRoute: ServerRoute = { + path: '/', + method: 'GET', + handler: function (request: Request, h: ResponseToolkit) { + return 'oks: ' + request.path; + } +}; + +const server = new Server(options); +server.route(serverRoute); +server.event('test1'); +server.event('test2'); +server.events.once('test1', function(update: any) {console.log(update);}); +server.events.once('test2', function(...args: any[]) {console.log(args);}); +server.events.emit('test1', 'hello-1'); +server.events.emit('test2', 'hello-2'); // Ignored + +server.start(); +console.log('Server started at: ' + server.info.uri);