From 2cbd2d48e8cc91839437d77aa3ea55fe8260842d Mon Sep 17 00:00:00 2001 From: rafaelsouzaf Date: Sat, 9 Dec 2017 16:41:54 -0300 Subject: [PATCH] Add more test files. --- types/hapi/v17/test/index.test.d.ts | 30 +++++++---- .../v17/test/{path => request}/catch-all.ts | 0 types/hapi/v17/test/request/get-log.ts | 1 - .../v17/test/{path => request}/parameters.ts | 0 .../v17/test/{reply => response}/continue.ts | 0 .../v17/test/{reply => response}/redirect.ts | 0 .../v17/test/{reply => response}/reply.ts | 4 +- .../test/{reply => response}/state_cookie.ts | 0 .../adding-routes.ts} | 5 +- types/hapi/v17/test/route/config.ts | 54 +++++++++++++++++++ types/hapi/v17/test/route/handler.ts | 9 ++++ .../a-simple-server.ts} | 0 types/hapi/v17/test/server/app.ts | 2 +- types/hapi/v17/test/server/info.ts | 16 +++--- types/hapi/v17/test/server/select.ts | 17 ++++++ .../v17/test/server/server-events-once.ts | 8 ++- types/hapi/v17/test/server/settings.ts | 12 +++++ types/hapi/v17/test/server/start.ts | 11 ++++ types/hapi/v17/test/server/stop.ts | 17 ++++++ types/hapi/v17/test/server/version.ts | 9 ++++ 20 files changed, 164 insertions(+), 31 deletions(-) rename types/hapi/v17/test/{path => request}/catch-all.ts (100%) rename types/hapi/v17/test/{path => request}/parameters.ts (100%) rename types/hapi/v17/test/{reply => response}/continue.ts (100%) rename types/hapi/v17/test/{reply => response}/redirect.ts (100%) rename types/hapi/v17/test/{reply => response}/reply.ts (92%) rename types/hapi/v17/test/{reply => response}/state_cookie.ts (100%) rename types/hapi/v17/test/{getting-started/02-adding-routes.ts => route/adding-routes.ts} (88%) create mode 100644 types/hapi/v17/test/route/config.ts create mode 100644 types/hapi/v17/test/route/handler.ts rename types/hapi/v17/test/{getting-started/01-creating-a-server.ts => server/a-simple-server.ts} (100%) create mode 100644 types/hapi/v17/test/server/select.ts create mode 100644 types/hapi/v17/test/server/settings.ts create mode 100644 types/hapi/v17/test/server/start.ts create mode 100644 types/hapi/v17/test/server/stop.ts create mode 100644 types/hapi/v17/test/server/version.ts diff --git a/types/hapi/v17/test/index.test.d.ts b/types/hapi/v17/test/index.test.d.ts index fb3babbbec..228ffdc02c 100644 --- a/types/hapi/v17/test/index.test.d.ts +++ b/types/hapi/v17/test/index.test.d.ts @@ -8,26 +8,36 @@ /// -/** Getting Started */ -export * from './getting-started/01-creating-a-server'; -export * from './getting-started/02-adding-routes'; -export * from './path/catch-all'; -export * from './path/parameters'; -export * from './reply/continue'; -export * from './reply/redirect'; -export * from './reply/reply'; -export * from './reply/state_cookie'; - +/** REQUEST */ +export * from './request/catch-all'; export * from './request/event-types'; export * from './request/get-log'; +export * from './request/parameters'; export * from './request/query'; +/** RESPONSE */ +export * from './response/continue'; export * from './response/error'; +export * from './response/redirect'; +export * from './response/reply'; +export * from './response/state_cookie'; +/** ROUTE */ +export * from './route/adding-routes'; +export * from './route/config'; +export * from './route/handler'; + +/** SERVER */ +export * from './server/a-simple-server'; export * from './server/app'; export * from './server/info'; +export * from './server/select'; export * from './server/server-events-once'; +export * from './server/settings'; +export * from './server/start'; +export * from './server/stop'; export * from './server/table'; +export * from './server/version'; diff --git a/types/hapi/v17/test/path/catch-all.ts b/types/hapi/v17/test/request/catch-all.ts similarity index 100% rename from types/hapi/v17/test/path/catch-all.ts rename to types/hapi/v17/test/request/catch-all.ts diff --git a/types/hapi/v17/test/request/get-log.ts b/types/hapi/v17/test/request/get-log.ts index 89084cbb07..047799c394 100644 --- a/types/hapi/v17/test/request/get-log.ts +++ b/types/hapi/v17/test/request/get-log.ts @@ -22,7 +22,6 @@ server.start(); console.log('Server started at: ' + server.info.uri); server.events.on('request', (request: Request, event: any, tags: any) => { - console.log(event); console.log(tags); if (tags.error) { console.log(event); diff --git a/types/hapi/v17/test/path/parameters.ts b/types/hapi/v17/test/request/parameters.ts similarity index 100% rename from types/hapi/v17/test/path/parameters.ts rename to types/hapi/v17/test/request/parameters.ts diff --git a/types/hapi/v17/test/reply/continue.ts b/types/hapi/v17/test/response/continue.ts similarity index 100% rename from types/hapi/v17/test/reply/continue.ts rename to types/hapi/v17/test/response/continue.ts diff --git a/types/hapi/v17/test/reply/redirect.ts b/types/hapi/v17/test/response/redirect.ts similarity index 100% rename from types/hapi/v17/test/reply/redirect.ts rename to types/hapi/v17/test/response/redirect.ts diff --git a/types/hapi/v17/test/reply/reply.ts b/types/hapi/v17/test/response/reply.ts similarity index 92% rename from types/hapi/v17/test/reply/reply.ts rename to types/hapi/v17/test/response/reply.ts index 185f80ed3d..a2d4c250ed 100644 --- a/types/hapi/v17/test/reply/reply.ts +++ b/types/hapi/v17/test/response/reply.ts @@ -17,7 +17,7 @@ const serverRoutes: ServerRoute[] = [ return response; } }, - // Chained notation + // chained notation { path: '/test2', method: 'GET', @@ -34,4 +34,4 @@ const server = new Server(options); server.route(serverRoutes); server.start(); -console.log('Server started at: ' + server.info.uri); \ No newline at end of file +console.log('Server started at: ' + server.info.uri); diff --git a/types/hapi/v17/test/reply/state_cookie.ts b/types/hapi/v17/test/response/state_cookie.ts similarity index 100% rename from types/hapi/v17/test/reply/state_cookie.ts rename to types/hapi/v17/test/response/state_cookie.ts diff --git a/types/hapi/v17/test/getting-started/02-adding-routes.ts b/types/hapi/v17/test/route/adding-routes.ts similarity index 88% rename from types/hapi/v17/test/getting-started/02-adding-routes.ts rename to types/hapi/v17/test/route/adding-routes.ts index 4f383c15dc..7d503502c2 100644 --- a/types/hapi/v17/test/getting-started/02-adding-routes.ts +++ b/types/hapi/v17/test/route/adding-routes.ts @@ -6,10 +6,9 @@ const options: ServerOptions = { }; const serverRoute: ServerRoute = { - path: '/test', + path: '/', method: 'GET', handler: function (request: Request, h: ResponseToolkit) { - console.log(request); return 'ok: ' + request.path; } }; @@ -19,7 +18,6 @@ const serverRoutes: ServerRoute[] = [ path: '/test1', method: 'GET', handler: function (request: Request, h: ResponseToolkit) { - console.log(request); return 'ok: ' + request.path; } }, @@ -27,7 +25,6 @@ const serverRoutes: ServerRoute[] = [ path: '/test2', method: 'GET', handler: function (request: Request, h: ResponseToolkit) { - console.log(request); return 'ok: ' + request.path; } }, diff --git a/types/hapi/v17/test/route/config.ts b/types/hapi/v17/test/route/config.ts new file mode 100644 index 0000000000..1ecabd0c56 --- /dev/null +++ b/types/hapi/v17/test/route/config.ts @@ -0,0 +1,54 @@ +import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute, RouteOptions} from "hapi"; + +const options: ServerOptions = { + port: 8000, +}; + + +// different methods +var routeConfig: ServerRoute = { + path: '/signin', + method: 'PUT', + vhost: 'site.coms', +}; +var routeConfig: ServerRoute = { + path: '/signin', + method: '*' +}; +var routeConfig: ServerRoute = { + path: '/signin', + method: ['OPTIONS', '*'] +}; + +// different handlers +var routeConfig: ServerRoute = { + path: '/signin', + method: 'PUT', + handler: function (request: Request, h: ResponseToolkit) { + return 'ok'; + } +}; +var routeConfig: ServerRoute = { + path: '/signin', + method: 'PUT', + handler: function (request: Request, h: ResponseToolkit) { + return 'ok'; + } +}; + +const server = new Server(options); +server.route(routeConfig); + +// Handler in config +const user: RouteOptions = { + cache: { expiresIn: 5000 }, + handler: function (request: Request, h: ResponseToolkit) { + return { name: 'John' }; + } +}; + +server.route({method: 'GET', path: '/user', options: user }); +//server.route({ method: 'GET', path: '/public/{path*}', options: { cache: { privacy: 'public', expiresIn: 24 * 60 * 60 * 1000 } }, handler: { directory: { path: __dirname, listing: false, index: false } } }); + +server.start(); +console.log('Server started at: ' + server.info.uri); diff --git a/types/hapi/v17/test/route/handler.ts b/types/hapi/v17/test/route/handler.ts new file mode 100644 index 0000000000..fa20d7b677 --- /dev/null +++ b/types/hapi/v17/test/route/handler.ts @@ -0,0 +1,9 @@ +import {Lifecycle, Request, ResponseToolkit} from "hapi"; + +const handler: Lifecycle.Method = function(request: Request, h: ResponseToolkit) { + return 'success'; +} + +const strictHandler: Lifecycle.Method = function(request: Request, h: ResponseToolkit) { + return 123; +} diff --git a/types/hapi/v17/test/getting-started/01-creating-a-server.ts b/types/hapi/v17/test/server/a-simple-server.ts similarity index 100% rename from types/hapi/v17/test/getting-started/01-creating-a-server.ts rename to types/hapi/v17/test/server/a-simple-server.ts diff --git a/types/hapi/v17/test/server/app.ts b/types/hapi/v17/test/server/app.ts index ea376560b7..cfd0bd91a4 100644 --- a/types/hapi/v17/test/server/app.ts +++ b/types/hapi/v17/test/server/app.ts @@ -19,4 +19,4 @@ const serverRoute: ServerRoute = { server.route(serverRoute); server.start(); -console.log(`Server running at: ${server.info!.uri}`); +console.log('Server started at: ' + server.info.uri); diff --git a/types/hapi/v17/test/server/info.ts b/types/hapi/v17/test/server/info.ts index 1fd15b240b..253a1b72aa 100644 --- a/types/hapi/v17/test/server/info.ts +++ b/types/hapi/v17/test/server/info.ts @@ -1,15 +1,15 @@ // From https://github.com/hapijs/hapi/blob/master/API.md#-serverinfo -import {Server, ServerOptions} from "hapi"; -import assert = require('assert'); +import {Server} from "hapi"; +import * as assert from "assert"; -const options: ServerOptions = { +const server = new Server({ port: 8000, -}; -const server = new Server(options); +}); +server.start(); // check the correct port -if(server.info) assert(server.info.port === 8000); -assert(server.info !== null); +console.log(server.info); +if (server.info) assert(server.info.port === 8000); -server.start(); +assert(server.info !== null); console.log('Server started at: ' + server.info.uri); diff --git a/types/hapi/v17/test/server/select.ts b/types/hapi/v17/test/server/select.ts new file mode 100644 index 0000000000..cdf2cfd1e7 --- /dev/null +++ b/types/hapi/v17/test/server/select.ts @@ -0,0 +1,17 @@ +// From https://hapijs.com/api/16.1.1#serverselectlabels +import {Server} from "hapi"; + +/* +const server = new Hapi.Server(); +server.connection({ port: 80, labels: ['a', 'b'] }); +server.connection({ port: 8080, labels: ['a', 'c'] }); +server.connection({ port: 8081, labels: ['b', 'c'] }); + +const a = server.select('a'); // 80, 8080 +const ac = a.select('c'); // 8080 +*/ +// TODO How do this with v17? + +const server = new Server({ + port: 8000, +}); diff --git a/types/hapi/v17/test/server/server-events-once.ts b/types/hapi/v17/test/server/server-events-once.ts index d36deb10bb..022cdb6f33 100644 --- a/types/hapi/v17/test/server/server-events-once.ts +++ b/types/hapi/v17/test/server/server-events-once.ts @@ -1,10 +1,6 @@ // 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', @@ -13,7 +9,9 @@ const serverRoute: ServerRoute = { } }; -const server = new Server(options); +const server = new Server({ + port: 8000, +}); server.route(serverRoute); server.event('test1'); server.event('test2'); diff --git a/types/hapi/v17/test/server/settings.ts b/types/hapi/v17/test/server/settings.ts new file mode 100644 index 0000000000..ebe7588b88 --- /dev/null +++ b/types/hapi/v17/test/server/settings.ts @@ -0,0 +1,12 @@ +// From https://github.com/hapijs/hapi/blob/master/API.md#-serversettings +import {Server} from "hapi"; + +const server = new Server({ + port: 8000, + app: { + key: 'value' + } +}); +server.start(); + +console.log(server.settings.app); // { key: 'value' } diff --git a/types/hapi/v17/test/server/start.ts b/types/hapi/v17/test/server/start.ts new file mode 100644 index 0000000000..425c287842 --- /dev/null +++ b/types/hapi/v17/test/server/start.ts @@ -0,0 +1,11 @@ +// From https://github.com/hapijs/hapi/blob/master/API.md#-await-serverstart +import {Server} from "hapi"; + +const server = new Server({ + port: 8000, +}); +server.start(); + +server.events.on('start', () => { + console.log('Server started at: ' + server.info.uri); +}); diff --git a/types/hapi/v17/test/server/stop.ts b/types/hapi/v17/test/server/stop.ts new file mode 100644 index 0000000000..2e7f3f9b2b --- /dev/null +++ b/types/hapi/v17/test/server/stop.ts @@ -0,0 +1,17 @@ +// From https://github.com/hapijs/hapi/blob/master/API.md#-await-serverstopoptions +import {Server} from "hapi"; + +const server = new Server({ + port: 8000, +}); + +server.start(); +server.events.on('start', () => { + console.log('Server started at: ' + server.info.uri); +}); +server.events.on('stop', () => { + console.log('Server stoped.'); +}); +setTimeout(function() { + server.stop({ timeout: 10 * 1000 }); +}, 5 * 1000); diff --git a/types/hapi/v17/test/server/version.ts b/types/hapi/v17/test/server/version.ts new file mode 100644 index 0000000000..041a565f48 --- /dev/null +++ b/types/hapi/v17/test/server/version.ts @@ -0,0 +1,9 @@ +// From https://github.com/hapijs/hapi/blob/master/API.md#-serverversion +import {Server} from "hapi"; + +const server = new Server({ + port: 8000, +}); + +server.start(); +console.log(server.version); // 17.x.x