generated from Richienb/typescript-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.ts
54 lines (44 loc) · 2.61 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type {Buffer} from 'node:buffer';
import test from 'ava';
import {Headers} from 'node-fetch';
import iconv from 'iconv-lite';
import convertBody from './source/index.js';
const toArrayBuffer = (buffer: Buffer): ArrayBuffer => buffer.buffer.slice(0, buffer.byteLength);
test('should support encoding decode, xml dtd detect', t => {
const text = '<?xml version="1.0" encoding="EUC-JP"?><title>日本語</title>';
t.is(convertBody(iconv.encode(text, 'EUC-JP'), new Headers({'Content-Type': 'text/xml'})), text);
});
test('should support encoding decode, content-type detect', t => {
const text = '<div>日本語</div>';
t.is(convertBody(iconv.encode(text, 'Shift_JIS'), new Headers({'Content-Type': 'text/html; charset=Shift-JIS'})), text);
});
test('should support encoding decode, html5 detect', t => {
const text = '<meta charset="gbk"><div>中文</div>';
t.is(convertBody(iconv.encode(text, 'gbk'), new Headers({'Content-Type': 'text/html'})), text);
});
test('should support encoding decode, html4 detect', t => {
const text = '<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><div>中文</div>';
t.is(convertBody(iconv.encode(text, 'gb2312'), new Headers({'Content-Type': 'text/html'})), text);
});
test('should select the correct meta tag, html4 detect', t => {
const text = '<meta http-equiv="Language" content="de"/><meta http-equiv="Content-Language" content="de"/><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/>';
t.is(convertBody(iconv.encode(text, 'iso-8859-1'), new Headers({'Content-Type': 'text/html'})), text);
});
test('should support uncommon content-type order, end with qs', t => {
const text = '中文';
t.is(convertBody(iconv.encode(text, 'gbk'), new Headers({'Content-Type': 'text/plain; charset=gbk; qs=1'})), text);
});
test('should support chunked encoding, html4 detect', t => {
const text = '<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" /><div>日本語</div>';
const padding = 'a'.repeat(10);
t.is(convertBody(iconv.encode(padding + text, 'Shift_JIS'), new Headers({'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked'})), padding + text);
});
test('should only do encoding detection up to 1024 bytes', t => {
const text = '中文';
const padding = 'a'.repeat(1200);
t.not(convertBody(iconv.encode(padding + text, 'gbk'), new Headers({'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked'})), text);
});
test('should support typed arrays', t => {
const text = '<div>日本語</div>';
t.is(convertBody(toArrayBuffer(iconv.encode(text, 'Shift_JIS')), new Headers({'Content-Type': 'text/html; charset=Shift-JIS'})), text);
});