@mattduffy/banner 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mattduffy/banner",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Displays server start-up information.",
5
5
  "author": "Matthew Duffy",
6
6
  "license": "ISC",
package/src/index.js CHANGED
@@ -284,24 +284,32 @@ export class Banner {
284
284
  default:
285
285
  _g = gGET
286
286
  }
287
+ _log('ctx.request.header.host', ctx.request.header.host)
287
288
  const _urlLabel = `${ctx.request.method}:`
288
- const _url = `${ctx.request.header.host}${ctx.request.url}`
289
- const _urlLine = `${_urlLabel} ${_url}`
289
+ const _url = `${ctx.request.protocol}://${ctx.request.header.host}${ctx.request.url}`
290
+ let _urlLine = `${_urlLabel} ${_url}`
290
291
  const _refLabel = 'Referer:'
291
292
  const _ref = ctx.request.header.referer ?? '<emtpy header field>'
292
- const _refLine = `${_refLabel} ${_ref}`
293
+ let _refLine = `${_refLabel} ${_ref}`
293
294
  const _longestLabel = [_urlLabel, _refLabel].reduce((a, c) => {
294
- if (a > (c.indexOf(':') + 1)) {
295
+ if (a.length > (c.indexOf(':') + 1)) {
295
296
  return a
296
297
  }
297
- return (c.indexOf(':' + 1))
298
+ return (c.indexOf(':') + 1)
298
299
  }, '')
300
+ _refLine = _refLine.padStart(
301
+ (_longestLabel - _refLine.indexOf(':')) + _refLine.length, ' '
302
+ )
303
+ _urlLine = _urlLine.padStart(
304
+ (_longestLabel - _urlLine.indexOf(':')) + _urlLine.length, ' '
305
+ )
299
306
  const _longestLine = [_urlLine, _refLine].reduce((a, c) => {
300
307
  if (a > c.length) return a
301
308
  return c.length
302
309
  }, '')
303
310
  // _log('request banner _longestLine', _longestLine)
304
- const _requestBanner = `${_g.padEnd(_longestLine + 5, _g)}\n`
311
+ const _requestBanner =
312
+ `${_g.padEnd(_longestLine + 5, _g)}\n`
305
313
  + `${_g} ${_urlLine}\n`
306
314
  + `${_g} ${_refLine}\n`
307
315
  + `${_g.padEnd(_longestLine + 5, _g)}`
package/test/test.js CHANGED
@@ -14,16 +14,19 @@ let cfg
14
14
  let ctx
15
15
  let ctx_POST
16
16
  let ctx_GET
17
+ let ctx_PUT
18
+ let ctx_DEL
17
19
  let next
18
20
  console.log(skip)
19
21
  describe('First test suite for banner package', async () => {
20
22
  before(() => {
21
23
  ctx = {
22
24
  request: {
25
+ protocol: 'https',
23
26
  method: '',
24
27
  url: '/a/really/long/url/to/a/special/page',
25
28
  header: {
26
- host: 'https://banner.test',
29
+ host: 'banner.test',
27
30
  referer: 'https://googoogle.com',
28
31
  },
29
32
  },
@@ -31,6 +34,8 @@ describe('First test suite for banner package', async () => {
31
34
  throw new Error(`Error code ${code}: ${msg}`)
32
35
  }
33
36
  }
37
+ ctx_DEL = { ...ctx }
38
+ ctx_PUT = { ...ctx }
34
39
  ctx_POST = { ...ctx }
35
40
  ctx_GET = { ...ctx }
36
41
  next = async () => {
@@ -62,13 +67,15 @@ describe('First test suite for banner package', async () => {
62
67
  assert(!banner.print())
63
68
  })
64
69
 
65
- it('Should work as a koajs middleware function.', async () => {
70
+ it('Should work as a koajs middleware function - POST method.', async () => {
66
71
  ctx_POST = Object.assign(ctx_POST, ctx)
67
72
  ctx_POST.request.method = 'POST'
68
73
  console.log(ctx_POST)
69
74
  const post = new Banner(ctx_POST)
70
75
  assert(await post.use()(ctx, next))
76
+ })
71
77
 
78
+ it('Should work as a koajs middleware function - GET method.', async () => {
72
79
  ctx_GET = Object.assign(ctx_GET, ctx)
73
80
  ctx_GET.request.method = 'GET'
74
81
  console.log(ctx_GET)
@@ -76,7 +83,23 @@ describe('First test suite for banner package', async () => {
76
83
  assert(await get.use()(ctx, next))
77
84
  })
78
85
 
79
- it('Should fail as a koajs middleware function, missing input parameters.', async () => {
86
+ it('Should work as a koajs middleware function - PUT method.', async () => {
87
+ ctx_PUT = Object.assign(ctx_PUT, ctx)
88
+ ctx_PUT.request.method = 'PUT'
89
+ console.log(ctx_PUT)
90
+ const put = new Banner(ctx_PUT)
91
+ assert(await put.use()(ctx, next))
92
+ })
93
+
94
+ it('Should work as a koajs middleware function - DELETE method.', async () => {
95
+ ctx_DEL = Object.assign(ctx_DEL, ctx)
96
+ ctx_DEL.request.method = 'DELETE'
97
+ console.log(ctx_DEL)
98
+ const del = new Banner(ctx_DEL)
99
+ assert(await del.use()(ctx, next))
100
+ })
101
+
102
+ it('Should fail as a koajs middleware function, missing input parameters.', async () => {
80
103
  ctx.url= ''
81
104
  ctx.request.header.host = undefined
82
105
  ctx.request.url = undefined