Added more tests files.

This commit is contained in:
rafaelsouzaf
2017-12-06 22:19:33 -03:00
parent ffad3c7f82
commit c159a74456
11 changed files with 173 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
// From https://hapijs.com/tutorials/getting-started#creating-a-server
import { Server, ServerOptions } from 'hapi';
import {Server, ServerOptions} from 'hapi';
const options: ServerOptions = {
port: 8000,

View File

@@ -4,16 +4,15 @@ import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi
const options: ServerOptions = {
port: 8000,
};
const server = new Server(options);
const serverRoute: ServerRoute = {
path: '/user',
path: '/test',
method: 'GET',
handler: function (request: Request, h: ResponseToolkit) {
console.log(request);
return 'ok: ' + request.path;
}
}
};
const serverRoutes: ServerRoute[] = [
{
@@ -34,8 +33,9 @@ const serverRoutes: ServerRoute[] = [
},
];
const server = new Server(options);
server.route(serverRoute);
server.route(serverRoutes);
server.start();
console.log(`Server running at: ${server.info!.uri}`);
console.log('Server started at: ' + server.info.uri);

View File

@@ -12,6 +12,12 @@
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/redirect';
export * from './reply/reply';
export * from './reply/state_cookie';
export * from './response/error';
export * from './server/app';
export * from './server/info';
export * from './server/table';

View File

@@ -1,5 +1,5 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#catch-all-route
import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi";
import {Request, ResponseToolkit, Server, ServerOptions} from "hapi";
const options: ServerOptions = {
port: 8000,
@@ -12,4 +12,4 @@ const handler = function (request: Request, h: ResponseToolkit) {
server.route({ method: '*', path: '/{p*}', handler });
server.start();
console.log(`Server running at: ${server.info!.uri}`);
console.log('Server started at: ' + server.info.uri);

View File

@@ -0,0 +1,38 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-requestparams
import {Lifecycle, Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi";
const options: ServerOptions = {
port: 8000,
};
// Example 1
// http://localhost:8000/album-name/song-optional
const getAlbum: Lifecycle.Method = function(request: Request, h: ResponseToolkit) {
console.log(request.params);
return 'ok: ' + request.path;
}
const serverRoute1: ServerRoute = {
path: '/{album}/{song?}',
method: 'GET',
handler: getAlbum
}
// Example 2
// http://localhost:8000/person/rafael/fijalkowski
const getPerson: Lifecycle.Method = function(request: Request, h: ResponseToolkit) {
const nameParts = request.params.name.split('/');
return { first: nameParts[0], last: nameParts[1] };
}
const serverRoute2: ServerRoute = {
path: '/person/{name*2}',
method: 'GET',
handler: getPerson
}
const server = new Server(options);
server.route(serverRoute1);
server.route(serverRoute2);
server.start();
console.log('Server started at: ' + server.info.uri);

View File

@@ -0,0 +1,20 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-hredirecturi
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 h.redirect('http://example.com');
}
}
const server = new Server(options);
server.route(serverRoute);
server.start();
console.log('Server started at: ' + server.info.uri);

View File

@@ -0,0 +1,37 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-hresponsevalue
import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi";
const options: ServerOptions = {
port: 8000,
};
const serverRoutes: ServerRoute[] = [
// verbose notation
{
path: '/test1',
method: 'GET',
handler: function (request: Request, h: ResponseToolkit) {
const response = h.response('success');
response.type('text/plain');
response.header('X-Custom', 'some-value');
return response;
}
},
// Chained notation
{
path: '/test2',
method: 'GET',
handler: function (request: Request, h: ResponseToolkit) {
return h.response('success')
.type('text/plain')
.header('X-Custom', 'some-value');
}
},
];
const server = new Server(options);
server.route(serverRoutes);
server.start();
console.log('Server started at: ' + server.info.uri);

View File

@@ -0,0 +1,29 @@
// from https://hapijs.com/tutorials/cookies?lang=en_US
import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute, ServerStateCookieOptions} from "hapi";
const options: ServerOptions = {
port: 8000,
};
const serverRoute: ServerRoute = {
path: '/say-hello',
method: 'GET',
handler: function (request: Request, h: ResponseToolkit) {
return h.response('Hello').state('data', { firstVisit: false });
}
}
const server = new Server(options);
server.route(serverRoute);
const stateOption: ServerStateCookieOptions = {
ttl: 24 * 60 * 60 * 1000, // One day
isSecure: false,
isHttpOnly: false,
encoding: 'base64json',
}
server.state('data', stateOption);
server.start();
console.log('Server started at: ' + server.info.uri);

View File

@@ -0,0 +1,31 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#errors
import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi";
import * as Boom from "boom";
const options: ServerOptions = {
port: 8000,
};
const serverRoutes: ServerRoute[] = [
{
path: '/badRequest',
method: 'GET',
handler: function (request: Request, h: ResponseToolkit) {
throw Boom.badRequest('Unsupported parameter');
}
},
{
path: '/internal',
method: 'GET',
handler: function (request: Request, h: ResponseToolkit) {
throw new Error('unexpect error');
}
},
];
const server = new Server(options);
server.route(serverRoutes);
server.start();
console.log('Server started at: ' + server.info.uri);

View File

@@ -1,8 +1,8 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-serverinfo
import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi";
import {Server, ServerOptions} from "hapi";
import assert = require('assert');
let options: ServerOptions = {
const options: ServerOptions = {
port: 8000,
};
const server = new Server(options);
@@ -12,4 +12,4 @@ if(server.info) assert(server.info.port === 8000);
assert(server.info !== null);
server.start();
console.log(`Server running at: ${server.info!.uri}`);
console.log('Server started at: ' + server.info.uri);

View File

@@ -1,5 +1,5 @@
// From https://github.com/hapijs/hapi/blob/master/API.md#-servertablehost
import {Request, ResponseToolkit, Server, ServerOptions, ServerRoute} from "hapi";
import {Request, ResponseToolkit, Server, ServerOptions} from "hapi";
const options: ServerOptions = {
port: 8000,
@@ -19,4 +19,4 @@ server.route({
server.start();
const table = server.table();
console.log(table);
console.log(`Server running at: ${server.info!.uri}`);
console.log('Server started at: ' + server.info.uri);