server.spec.js 2.2 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1
const https = require('https')
2
const test  = require('tape')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
3
const co    = require('co')
4

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
5
const log = require('../../src/npmlog-env')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
6

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
7
const PuppetWebServer = require('../../src/puppet-web/server')
8
const PORT = 58788
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
test('PuppetWebServer basic tests', function(t) {
  const s = new PuppetWebServer({port: PORT})
  t.equal(typeof s, 'object', 'PuppetWebServer instance created')

  co(function* () {
    const express = s.createExpress()
    t.equal(typeof express, 'function', 'create express')

    const httpsServer = s.createHttpsServer(express)
    t.equal(typeof httpsServer, 'object', 'create https server')
    httpsServer.on('close', () => t.pass('HttpsServer quited'))

    const retClose = yield new Promise((resolve, reject) => {
      httpsServer.close(() => resolve(true))
    })
    t.ok(retClose, 'HttpsServer closed')

    const socketio = s.createSocketIo()
    t.equal(typeof socketio, 'object', 'create socket io')
29 30
  })
  .catch(e => { // Reject
31 32
    t.fail('co promise rejected:' + e)
  })
33
  .then(() => { // Finally
34 35 36
    s.quit()
    t.end()
  })
37 38 39
  .catch(e => { // Exception
    t.fail('Exception:' + e)
  })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
40 41
})

42
test('PuppetWebServer smoke testing', function(t) {
43
  const server = new PuppetWebServer({port: PORT})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
44
  t.ok(server, 'new server instance')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
45

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
46
  co(function* () {
47 48
    const retInit = yield server.init()
    t.ok(retInit, 'server:' + PORT + ' inited')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
50 51
    const retHttps = yield dingHttps()
    t.equal(retHttps ,  'dong', 'ding https   got dong')
52
  }).catch(e => { // Reject
53 54 55 56
    t.fail('co rejected:' + e)
  }).then(() => { // Finally
    server.quit()
    t.end()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57
  })
58 59 60
  .catch(e => { // Exception
    t.fail('Exception:' + e)
  })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
61

62 63 64
  return // The following is help functions only

  //////////////////////////////////////////
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
65 66 67 68 69 70 71 72

  function dingHttps() {
    const options = require('url').parse(`https://localhost:${PORT}/ding`)
    options.rejectUnauthorized = false // permit self-signed CA

    return new Promise((resolve, reject) => {
      https.get(options, res => {
        res.on('data', chunk => {
73
          log.verbose('TestingPuppetWebServer', 'https on data got: ' + chunk.toString())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
74 75 76 77 78
          resolve(chunk.toString())
        })
      }).on('error', e => reject('https get error:' + e))
    })
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
79
})