Test Report

0
0
5
155
ID Title Duration (ms)
1 routes print route information 72
2 routes gets route information 22
3 routes gets route information with auth 25
4 routes gets route information with default 26
5 routes fails with invalid options 10

Code Coverage Report

97.14%
140
136
4

lib/index.js

97.14%
140
136
4
Line Hits Source
1 // Load modules
2 1 var Chalk = require('chalk');
3 1 var Hoek = require('hoek');
4 1 var Joi = require('joi');
5 1 var Pkg = require('../package.json');
6
7
8 // Declare internals
9 1 var internals = {
10 schema: {
11 showAuth: Joi.boolean().default(false),
12 showStart: Joi.boolean().default(true)
13 }
14 };
15
16
17 1 exports.register = function (server, options, next) {
18
19 5 var result = Joi.validate(options, internals.schema);
20 5 Hoek.assert(!result.error, result.error && result.error.annotate());
21 4 options = result.value;
22
23 4 server.expose('text', function () {
24
25 1 var info = this.json();
26 1 return internals.printableInfo(info, options);
27 });
28
29 4 server.expose('json', function () {
30
31 5 return internals.getRouteInfo(server, options);
32 });
33
34 4 if (options.showStart) {
35 1 server.on('start', function () {
36
37 1 var out = server.plugins[Pkg.name].text();
38 1 console.log(out);
39 });
40 }
41
42 4 return next();
43 };
44
45
46 1 exports.register.attributes = {
47 pkg: Pkg
48 };
49
50
51 1 internals.printableInfo = function (connections, options) {
52
53 1 var out = '';
54 1 for (var i = 0, il = connections.length; i < il; ++i) {
55 3 var connection = connections[i];
56
57 3 out += internals.printableConnection(connection, options);
58 }
59 1 return out;
60 };
61
62
63 1 internals.printableConnection = function (connection, options) {
64
65 3 var out = internals.printableTitle(connection, options) + '\n';
66 3 out += internals.printableRoutes(connection.routes, options);
67 3 return out;
68 };
69
70
71 1 internals.printableRoutes = function (routes, options) {
72
73 3 var out = '';
74 3 for (var i = 0, il = routes.length; i < il; ++i) {
75 8 var show = routes[i];
76
77 8 var method = internals.formatMethod(show.method);
78 8 var description = internals.formatDescription(show.description);
79 8 var auth = internals.formatAuth(show.auth);
80 8 var path = internals.formatPath(show.path);
81
82
if (
options.showAuth
) {
83 out += [method, path, auth, description].join(' ') + '\n';
84 } else {
85 8 out += [method, path, description].join(' ') + '\n';
86 }
87 }
88
89 3 return out;
90 };
91
92
93 1 internals.printableTitle = function (connection) {
94
95 3 var title = Chalk.underline(connection.uri);
96 3 if (connection.labels.length) {
97 2 var labels = '[' + Chalk.magenta(connection.labels.join(', ')) + ']';
98 2 title += ' ' + labels;
99 }
100 3 return Chalk.cyan(title);
101 };
102
103
104 1 internals.getRouteInfo = function (server, options) {
105
106 5 var connections = [];
107
108 5 var routingTable = server.table();
109
110 5 routingTable.forEach(function (connection) {
111
112 15 var connectionInfo = {
113 uri: connection.info.uri,
114 labels: connection.labels,
115 routes: []
116 };
117
118 15 internals.connectionInfo(connection.table, options, connectionInfo);
119 15 connections.push(connectionInfo);
120 });
121
122 5 return connections;
123 };
124
125 1 internals.connectionInfo = function (routes, options, connectionInfo) {
126
127 15 for (var i = 0, il = routes.length; i < il; ++i) {
128 40 var route = routes[i];
129
130 40 var defaultStrategy = Hoek.reach(route, 'connection.auth.settings.default.strategies');
131 40 var authStrategy = route.settings.auth ? route.settings.auth.strategies.toString() : false;
132
133 40 if (route.settings.auth === undefined) {
134 33 authStrategy = defaultStrategy ? String(defaultStrategy) : false;
135 }
136
137 40 var show = {
138 method: route.method.toUpperCase(),
139 path: route.path,
140 description: route.settings.description || ''
141 };
142
143 40 if (options.showAuth) {
144 16 show.auth = authStrategy;
145 };
146
147 40 connectionInfo.routes.push(show);
148 }
149
150 15 connectionInfo.routes.sort(function (a, b) {
151
152 35 return a.path.localeCompare(b.path);
153 });
154 };
155
156
157 1 internals.formatPath = function (path) {
158
159 8 path = internals.ljust(path, 30);
160 8 path = path.replace(/({.*?})/g, Chalk.gray('$1'));
161 8 return path;
162 };
163
164
165 1 internals.ljust = function (string, amount) {
166
167 24 var padding = ' ';
168 24 var currentLength = string.length;
169
170 24 while (string.length < amount) {
171 208 string = string + padding;
172 }
173
174 24 return string;
175 };
176
177
178 1 internals.formatMethod = function (method) {
179
180 8 method = ' ' + method.toUpperCase();
181 8 method = Chalk.green(method);
182 8 method = internals.ljust(method, 18);
183 8 return method;
184 };
185
186 1 internals.formatAuth = function (auth) {
187
188
if (
auth === false
) {
189 auth = Chalk.red('none');
190 } else {
191 8 auth = Chalk.green(auth);
192 }
193 8 auth = internals.ljust(auth, 20);
194 8 return auth;
195 };
196
197 1 internals.formatDescription = function (description) {
198
199 8 description = description || '';
200 8 description = Chalk.yellow(description);
201 8 return description;
202 };
203