misc.spec.ts 5.2 KB
Newer Older
1
#!/usr/bin/env ts-node
2
/**
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
3
 *   Wechaty - https://github.com/chatie/wechaty
4
 *
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
5
 *   @copyright 2016-2018 Huan LI <zixia@zixia.net>
6 7 8 9 10 11 12 13 14 15 16 17
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
18 19
 *
 */
20 21 22 23
// tslint:disable:no-shadowed-variable
import * as test  from 'blue-tape'
// import * as sinon from 'sinon'
// const sinonTest   = require('sinon-test')(sinon)
24

25
import * as express   from 'express'
26

27
import Misc        from './misc'
28

29
test('stripHtml()', async t => {
30 31 32
  const HTML_BEFORE_STRIP = 'Outer<html>Inner</html>'
  const HTML_AFTER_STRIP  = 'OuterInner'

33
  const strippedHtml = Misc.stripHtml(HTML_BEFORE_STRIP)
34 35 36
  t.is(strippedHtml, HTML_AFTER_STRIP, 'should strip html as expected')
})

37
test('unescapeHtml()', async t => {
38 39 40
  const HTML_BEFORE_UNESCAPE  = '&apos;|&quot;|&gt;|&lt;|&amp;'
  const HTML_AFTER_UNESCAPE   = `'|"|>|<|&`

41
  const unescapedHtml = Misc.unescapeHtml(HTML_BEFORE_UNESCAPE)
42 43 44
  t.is(unescapedHtml, HTML_AFTER_UNESCAPE, 'should unescape html as expected')
})

45
test('plainText()', async t => {
46 47 48
  const PLAIN_BEFORE  = '&amp;<html>&amp;</html>&amp;<img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />'
  const PLAIN_AFTER   = '&&&[流汗]'

49
  const plainText = Misc.plainText(PLAIN_BEFORE)
50 51 52 53
  t.is(plainText, PLAIN_AFTER, 'should convert plain text as expected')

})

54
test('digestEmoji()', async t => {
55
  const EMOJI_XML = [
L
lijiarui 已提交
56
    '<img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />',
57 58
    '<img class="qqemoji qqemoji13" text="[呲牙]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />',
    '<img class="emoji emoji1f44d" text="_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />',
L
lijiarui 已提交
59
    '<span class="emoji emoji1f334"></span>',
60 61
  ]
  const EMOJI_AFTER_DIGEST  = [
L
lijiarui 已提交
62
    '[流汗]',
63 64
    '[呲牙]',
    '',
L
lijiarui 已提交
65
    '[emoji1f334]',
66
  ]
67

68
  for (let i = 0; i < EMOJI_XML.length; i++) {
69
    const emojiDigest = Misc.digestEmoji(EMOJI_XML[i])
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
70
    t.is(emojiDigest, EMOJI_AFTER_DIGEST[i], 'should digest emoji string ' + i + ' as expected')
71
  }
72 73
})

74
test('unifyEmoji()', async t => {
75 76 77
  const ORIGNAL_XML_LIST: [string[], string][] = [
    [
      [
L
lijiarui 已提交
78 79 80 81 82
        '<img class="emoji emoji1f602" text="_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />',
        '<span class=\"emoji emoji1f602\"></span>',
      ],
        '<emoji code="emoji1f602"/>',
    ],
83
  ]
84

85 86
  ORIGNAL_XML_LIST.forEach(([xmlList, expectedEmojiXml]) => {
    xmlList.forEach(xml => {
87
      const unifiedXml = Misc.unifyEmoji(xml)
88 89 90
      t.is(unifiedXml, expectedEmojiXml, 'should convert the emoji xml to the expected unified xml')
    })
  })
91
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
92

93
test('stripEmoji()', async t => {
94 95
  const EMOJI_STR = [
    [
L
lijiarui 已提交
96 97 98 99 100 101 102
      'ABC<img class="emoji emoji1f4a4" text="[流汗]_web" src="/zh_CN/htmledition/v2/images/spacer.gif" />DEF',
      'ABCDEF',
    ],
    [
      'UVW<span class="emoji emoji1f334"></span>XYZ',
      'UVWXYZ',
    ],
103 104 105
  ]

  EMOJI_STR.forEach(([emojiStr, expectResult]) => {
106
    const result = Misc.stripEmoji(emojiStr)
107 108 109
    t.is(result, expectResult, 'should strip to the expected str')
  })

110
  const empty = Misc.stripEmoji(undefined)
111 112 113
  t.is(empty, '', 'should return empty string for `undefined`')
})

114
test('downloadStream() for media', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
115
  const app = express()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
116 117 118
  app.use(require('cookie-parser')())
  app.get('/ding', function(req, res) {
    // console.log(req.cookies)
119
    t.ok(req.cookies, 'should has cookies in req')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
120
    t.is(req.cookies.life, '42', 'should has a cookie named life value 42')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
121 122 123 124 125 126 127 128
    res.end('dong')
  })

  const server = require('http').createServer(app)
  server.on('clientError', (err, socket) => {
    t.fail('server on clientError')
    socket.end('HTTP/1.1 400 Bad Request\r\n\r\n')
  })
129
  server.listen(65534)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
130

131
  try {
132
    const s = await Misc.urlStream('http://127.0.0.1:65534/ding', [{name: 'life', value: 42}])
133 134 135 136 137 138 139 140 141 142 143 144
    await new Promise((resolve, reject) => {
      s.on('data', (chunk) => {
        // console.log(`BODY: ${chunk}`)
        t.is(chunk.toString(), 'dong', 'should success download dong from downloadStream()')
        server.close()
        resolve()
      })
      s.on('error', reject)
    })
  } catch (e) {
    t.fail('downloadStream() exception: ' + e.message)
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
145
})
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
146

147
test('getPort() for an available socket port', async t => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
148 149
  const PORT = 8788

150
  let port = await Misc.getPort(PORT)
151 152
  t.not(port, PORT, 'should not be same port even it is available(to provent conflict between concurrency tests in AVA)')

Huan (李卓桓)'s avatar
linting  
Huan (李卓桓) 已提交
153
  let ttl = 17
154 155 156 157
  while (ttl-- > 0) {
    try {
      const app = express()
      const server = app.listen(PORT)
158
      port = await Misc.getPort(PORT)
159 160 161 162 163
      server.close()
    } catch (e) {
      t.fail('should not exception: ' + e.message + ', ' + e.stack)
    }
  }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
164
  t.pass('should has no exception after loop test')
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
165
})