server.spec.ts 2.4 KB
Newer Older
1 2 3 4 5 6 7
/**
 * Wechaty - Wechat for Bot. Connecting ChatBots
 *
 * Licenst: ISC
 * https://github.com/wechaty/wechaty
 *
 */
Huan (李卓桓)'s avatar
merge  
Huan (李卓桓) 已提交
8
import { test }     from 'ava'
9

Huan (李卓桓)'s avatar
merge  
Huan (李卓桓) 已提交
10 11
import * as https   from 'https'
import * as sinon   from 'sinon'
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
12

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
13
import { Server }   from '../../src/puppet-web/'
Huan (李卓桓)'s avatar
merge  
Huan (李卓桓) 已提交
14
import UtilLib      from '../../src/util-lib'
15
import { log  }     from '../../src/config'
16

17
test('create & close', async t => {
18 19
  const port = await UtilLib.getPort(18788)
  const s = new Server(port)
20
  t.is(typeof s, 'object', 'PuppetWebServer instance created')
21

22
  let httpsServer: https.Server
23

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
24
  const spy = sinon.spy()
25

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26 27
  const express = s.createExpress()
  t.is(typeof express, 'function', 'create express')
28

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
29 30 31
  httpsServer = await s.createHttpsServer(express)
  t.is(typeof httpsServer, 'object', 'create https server')
  httpsServer.on('close', _ => spy('onClose'))
32

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
33 34
  const socketio = s.createSocketIo(httpsServer)
  t.is(typeof socketio, 'object', 'should created socket io instance')
35

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
36 37 38 39
  const retClose = await new Promise((resolve, reject) => {
    ; (httpsServer as any).close(_ => {
      spy('closed')
      resolve('closed')
40
    })
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
41 42 43 44 45 46 47 48
  })
  t.is(retClose, 'closed',  'HttpsServer closed')

  t.truthy(spy.calledTwice, 'spy should be called twice after close HttpsServer')
  t.deepEqual(spy.args[0], ['onClose'], 'should fire event `close` when close HttpsServer')
  t.deepEqual(spy.args[1], ['closed']  , 'should run callback when close HttpsServer')

  await s.quit()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
49 50
})

51
test('http ding', async t => {
52 53 54
  const port = await UtilLib.getPort(18788)

  const server = new Server(port)
55 56
  t.truthy(server, 'new server instance')

57 58 59
  try {
    await server.init()
    t.pass('server:' + port + ' inited')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
60

61 62
    const retHttps = await dingHttps()
    t.is(retHttps ,  'dong', 'ding https got dong')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
63

64 65 66 67 68 69
    await server.quit()

  } catch (e) {
    t.fail('smoke testing exception: ' + e.message)
    throw e
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
70

71 72 73
  return // The following is help functions only

  //////////////////////////////////////////
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
74 75

  function dingHttps() {
76
    const options = require('url').parse(`https://localhost:${port}/ding`)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
77 78 79 80 81
    options.rejectUnauthorized = false // permit self-signed CA

    return new Promise((resolve, reject) => {
      https.get(options, res => {
        res.on('data', chunk => {
82
          log.verbose('TestingPuppetWebServer', 'https on data got: ' + chunk.toString())
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
83 84 85 86 87
          resolve(chunk.toString())
        })
      }).on('error', e => reject('https get error:' + e))
    })
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
88
})