1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 | const pickup = require('../')
|
6 | const { test } = require('tap')
|
7 |
|
8 | test('guess encoding', (t) => {
|
9 | const f = pickup.cribEncoding
|
10 |
|
11 | const found = [
|
12 | f(''),
|
13 | f('<?xml?>'),
|
14 | f('<?xml encoding>'),
|
15 | f('<?xml encoding="UTF-8"?>'),
|
16 | f('<?xml encoding="utf-8"?>'),
|
17 | f('<?xml encoding="ISO-8859-1"?>'),
|
18 | f('<?xml encoding="iso-8859-1"?>')
|
19 | ]
|
20 |
|
21 | const wanted = [
|
22 | 'utf8',
|
23 | 'utf8',
|
24 | 'utf8',
|
25 | 'utf8',
|
26 | 'utf8',
|
27 | 'binary',
|
28 | 'binary'
|
29 | ]
|
30 |
|
31 | t.plan(wanted.length)
|
32 |
|
33 | wanted.forEach((it, i) => {
|
34 | t.is(found[i], it)
|
35 | })
|
36 | })
|
37 |
|
38 | test('set encoding', (t) => {
|
39 | function go (objs) {
|
40 | const obj = objs.shift()
|
41 | if (!obj) return
|
42 |
|
43 | const parser = pickup({ charset: obj.charset })
|
44 |
|
45 | parser.on('readable', () => {
|
46 | while (parser.read()) {}
|
47 | })
|
48 |
|
49 | parser.on('encoding', (enc) => {
|
50 | t.is(enc, obj.encoding, 'should emit encoding')
|
51 | })
|
52 |
|
53 | parser.on('end', () => {
|
54 | go(objs)
|
55 | })
|
56 |
|
57 | parser.end(obj.xml)
|
58 | }
|
59 |
|
60 | const tests = [
|
61 | { xml: '<?xml version="1.0"?><feed></feed>', encoding: 'utf8' },
|
62 | { xml: '<?xml version="1.0" encoding="UTF-8"?><feed></feed>', encoding: 'utf8' },
|
63 | { xml: '<?xml version="1.0" encoding="ISO-8859-1"?><feed></feed>', encoding: 'binary' },
|
64 | { charset: 'joker', xml: '<?xml version="1.0"?><feed></feed>', encoding: 'utf8' }
|
65 | ]
|
66 |
|
67 | t.plan(tests.length)
|
68 | go(tests)
|
69 | })
|