|
1 |
| -import test from "ava" |
2 |
| -import { Headers } from "node-fetch" |
3 |
| -import { encode } from "iconv-lite" |
4 |
| -import convertBody from "./source" |
| 1 | +import test from 'ava'; |
| 2 | +import {Headers} from 'node-fetch'; |
| 3 | +import iconv from 'iconv-lite'; |
| 4 | +import convertBody from './source/index.js'; |
5 | 5 |
|
6 |
| -test("should support encoding decode, xml dtd detect", t => { |
7 |
| - const text = "<?xml version=\"1.0\" encoding=\"EUC-JP\"?><title>日本語</title>" |
8 |
| - t.is(convertBody(encode(text, "EUC-JP"), new Headers({ "Content-Type": "text/xml" })), text) |
9 |
| -}) |
| 6 | +test('should support encoding decode, xml dtd detect', t => { |
| 7 | + const text = '<?xml version="1.0" encoding="EUC-JP"?><title>日本語</title>'; |
| 8 | + t.is(convertBody(iconv.encode(text, 'EUC-JP'), new Headers({'Content-Type': 'text/xml'})), text); |
| 9 | +}); |
10 | 10 |
|
11 |
| -test("should support encoding decode, content-type detect", t => { |
12 |
| - const text = "<div>日本語</div>" |
13 |
| - t.is(convertBody(encode(text, "Shift_JIS"), new Headers({ "Content-Type": "text/html; charset=Shift-JIS" })), text) |
14 |
| -}) |
| 11 | +test('should support encoding decode, content-type detect', t => { |
| 12 | + const text = '<div>日本語</div>'; |
| 13 | + t.is(convertBody(iconv.encode(text, 'Shift_JIS'), new Headers({'Content-Type': 'text/html; charset=Shift-JIS'})), text); |
| 14 | +}); |
15 | 15 |
|
16 |
| -test("should support encoding decode, html5 detect", t => { |
17 |
| - const text = "<meta charset=\"gbk\"><div>中文</div>" |
18 |
| - t.is(convertBody(encode(text, "gbk"), new Headers({ "Content-Type": "text/html" })), text) |
19 |
| -}) |
| 16 | +test('should support encoding decode, html5 detect', t => { |
| 17 | + const text = '<meta charset="gbk"><div>中文</div>'; |
| 18 | + t.is(convertBody(iconv.encode(text, 'gbk'), new Headers({'Content-Type': 'text/html'})), text); |
| 19 | +}); |
20 | 20 |
|
21 |
| -test("should support encoding decode, html4 detect", t => { |
22 |
| - const text = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"><div>中文</div>" |
23 |
| - t.is(convertBody(encode(text, "gb2312"), new Headers({ "Content-Type": "text/html" })), text) |
24 |
| -}) |
| 21 | +test('should support encoding decode, html4 detect', t => { |
| 22 | + const text = '<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><div>中文</div>'; |
| 23 | + t.is(convertBody(iconv.encode(text, 'gb2312'), new Headers({'Content-Type': 'text/html'})), text); |
| 24 | +}); |
25 | 25 |
|
26 |
| -test("should support uncommon content-type order, end with qs", t => { |
27 |
| - const text = "中文" |
28 |
| - t.is(convertBody(encode(text, "gbk"), new Headers({ "Content-Type": "text/plain; charset=gbk; qs=1" })), text) |
29 |
| -}) |
| 26 | +test('should support uncommon content-type order, end with qs', t => { |
| 27 | + const text = '中文'; |
| 28 | + t.is(convertBody(iconv.encode(text, 'gbk'), new Headers({'Content-Type': 'text/plain; charset=gbk; qs=1'})), text); |
| 29 | +}); |
30 | 30 |
|
31 |
| -test("should support chunked encoding, html4 detect", t => { |
32 |
| - const text = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\" /><div>日本語</div>" |
33 |
| - const padding = "a".repeat(10) |
34 |
| - t.is(convertBody(encode(padding + text, "Shift_JIS"), new Headers({ "Content-Type": "text/html", "Transfer-Encoding": "chunked" })), padding + text) |
35 |
| -}) |
| 31 | +test('should support chunked encoding, html4 detect', t => { |
| 32 | + const text = '<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" /><div>日本語</div>'; |
| 33 | + const padding = 'a'.repeat(10); |
| 34 | + t.is(convertBody(iconv.encode(padding + text, 'Shift_JIS'), new Headers({'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked'})), padding + text); |
| 35 | +}); |
36 | 36 |
|
37 |
| -test("should only do encoding detection up to 1024 bytes", t => { |
38 |
| - const text = "中文" |
39 |
| - const padding = "a".repeat(1200) |
40 |
| - t.not(convertBody(encode(padding + text, "gbk"), new Headers({ "Content-Type": "text/html", "Transfer-Encoding": "chunked" })), text) |
41 |
| -}) |
| 37 | +test('should only do encoding detection up to 1024 bytes', t => { |
| 38 | + const text = '中文'; |
| 39 | + const padding = 'a'.repeat(1200); |
| 40 | + t.not(convertBody(iconv.encode(padding + text, 'gbk'), new Headers({'Content-Type': 'text/html', 'Transfer-Encoding': 'chunked'})), text); |
| 41 | +}); |
0 commit comments