@@ -16,6 +16,9 @@ import * as http from 'http';
16
16
import * as fs from 'fs' ;
17
17
import * as path from 'path' ;
18
18
19
+ let convert ;
20
+ try { convert = require ( 'encoding' ) . convert ; } catch ( e ) { }
21
+
19
22
chai . use ( chaiPromised ) ;
20
23
chai . use ( chaiIterator ) ;
21
24
chai . use ( chaiString ) ;
@@ -1090,109 +1093,6 @@ describe('node-fetch', () => {
1090
1093
} ) ;
1091
1094
} ) ;
1092
1095
1093
- it ( 'should only use UTF-8 decoding with text()' , function ( ) {
1094
- url = `${ base } encoding/euc-jp` ;
1095
- return fetch ( url ) . then ( res => {
1096
- expect ( res . status ) . to . equal ( 200 ) ;
1097
- return res . text ( ) . then ( result => {
1098
- expect ( result ) . to . equal ( '<?xml version="1.0" encoding="EUC-JP"?><title>\ufffd\ufffd\ufffd\u0738\ufffd</title>' ) ;
1099
- } ) ;
1100
- } ) ;
1101
- } ) ;
1102
-
1103
- it ( 'should support encoding decode, xml dtd detect' , function ( ) {
1104
- url = `${ base } encoding/euc-jp` ;
1105
- return fetch ( url ) . then ( res => {
1106
- expect ( res . status ) . to . equal ( 200 ) ;
1107
- return res . textConverted ( ) . then ( result => {
1108
- expect ( result ) . to . equal ( '<?xml version="1.0" encoding="EUC-JP"?><title>日本語</title>' ) ;
1109
- } ) ;
1110
- } ) ;
1111
- } ) ;
1112
-
1113
- it ( 'should support encoding decode, content-type detect' , function ( ) {
1114
- url = `${ base } encoding/shift-jis` ;
1115
- return fetch ( url ) . then ( res => {
1116
- expect ( res . status ) . to . equal ( 200 ) ;
1117
- return res . textConverted ( ) . then ( result => {
1118
- expect ( result ) . to . equal ( '<div>日本語</div>' ) ;
1119
- } ) ;
1120
- } ) ;
1121
- } ) ;
1122
-
1123
- it ( 'should support encoding decode, html5 detect' , function ( ) {
1124
- url = `${ base } encoding/gbk` ;
1125
- return fetch ( url ) . then ( res => {
1126
- expect ( res . status ) . to . equal ( 200 ) ;
1127
- return res . textConverted ( ) . then ( result => {
1128
- expect ( result ) . to . equal ( '<meta charset="gbk"><div>中文</div>' ) ;
1129
- } ) ;
1130
- } ) ;
1131
- } ) ;
1132
-
1133
- it ( 'should support encoding decode, html4 detect' , function ( ) {
1134
- url = `${ base } encoding/gb2312` ;
1135
- return fetch ( url ) . then ( res => {
1136
- expect ( res . status ) . to . equal ( 200 ) ;
1137
- return res . textConverted ( ) . then ( result => {
1138
- expect ( result ) . to . equal ( '<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><div>中文</div>' ) ;
1139
- } ) ;
1140
- } ) ;
1141
- } ) ;
1142
-
1143
- it ( 'should default to utf8 encoding' , function ( ) {
1144
- url = `${ base } encoding/utf8` ;
1145
- return fetch ( url ) . then ( res => {
1146
- expect ( res . status ) . to . equal ( 200 ) ;
1147
- expect ( res . headers . get ( 'content-type' ) ) . to . be . null ;
1148
- return res . textConverted ( ) . then ( result => {
1149
- expect ( result ) . to . equal ( '中文' ) ;
1150
- } ) ;
1151
- } ) ;
1152
- } ) ;
1153
-
1154
- it ( 'should support uncommon content-type order, charset in front' , function ( ) {
1155
- url = `${ base } encoding/order1` ;
1156
- return fetch ( url ) . then ( res => {
1157
- expect ( res . status ) . to . equal ( 200 ) ;
1158
- return res . textConverted ( ) . then ( result => {
1159
- expect ( result ) . to . equal ( '中文' ) ;
1160
- } ) ;
1161
- } ) ;
1162
- } ) ;
1163
-
1164
- it ( 'should support uncommon content-type order, end with qs' , function ( ) {
1165
- url = `${ base } encoding/order2` ;
1166
- return fetch ( url ) . then ( res => {
1167
- expect ( res . status ) . to . equal ( 200 ) ;
1168
- return res . textConverted ( ) . then ( result => {
1169
- expect ( result ) . to . equal ( '中文' ) ;
1170
- } ) ;
1171
- } ) ;
1172
- } ) ;
1173
-
1174
- it ( 'should support chunked encoding, html4 detect' , function ( ) {
1175
- url = `${ base } encoding/chunked` ;
1176
- return fetch ( url ) . then ( res => {
1177
- expect ( res . status ) . to . equal ( 200 ) ;
1178
- const padding = 'a' . repeat ( 10 ) ;
1179
- return res . textConverted ( ) . then ( result => {
1180
- expect ( result ) . to . equal ( `${ padding } <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" /><div>日本語</div>` ) ;
1181
- } ) ;
1182
- } ) ;
1183
- } ) ;
1184
-
1185
- it ( 'should only do encoding detection up to 1024 bytes' , function ( ) {
1186
- url = `${ base } encoding/invalid` ;
1187
- return fetch ( url ) . then ( res => {
1188
- expect ( res . status ) . to . equal ( 200 ) ;
1189
- const padding = 'a' . repeat ( 1200 ) ;
1190
- return res . textConverted ( ) . then ( result => {
1191
- expect ( result ) . to . not . equal ( `${ padding } 中文` ) ;
1192
- } ) ;
1193
- } ) ;
1194
- } ) ;
1195
-
1196
1096
it ( 'should allow piping response body as stream' , function ( ) {
1197
1097
url = `${ base } hello` ;
1198
1098
return fetch ( url ) . then ( res => {
@@ -1930,3 +1830,130 @@ function streamToPromise(stream, dataHandler) {
1930
1830
stream . on ( 'error' , reject ) ;
1931
1831
} ) ;
1932
1832
}
1833
+
1834
+ describe ( 'external encoding' , ( ) => {
1835
+ const hasEncoding = typeof convert === 'function' ;
1836
+
1837
+ describe ( 'with optional `encoding`' , function ( ) {
1838
+ before ( function ( ) {
1839
+ if ( ! hasEncoding ) this . skip ( ) ;
1840
+ } ) ;
1841
+
1842
+ it ( 'should only use UTF-8 decoding with text()' , function ( ) {
1843
+ url = `${ base } encoding/euc-jp` ;
1844
+ return fetch ( url ) . then ( res => {
1845
+ expect ( res . status ) . to . equal ( 200 ) ;
1846
+ return res . text ( ) . then ( result => {
1847
+ expect ( result ) . to . equal ( '<?xml version="1.0" encoding="EUC-JP"?><title>\ufffd\ufffd\ufffd\u0738\ufffd</title>' ) ;
1848
+ } ) ;
1849
+ } ) ;
1850
+ } ) ;
1851
+
1852
+ it ( 'should support encoding decode, xml dtd detect' , function ( ) {
1853
+ url = `${ base } encoding/euc-jp` ;
1854
+ return fetch ( url ) . then ( res => {
1855
+ expect ( res . status ) . to . equal ( 200 ) ;
1856
+ return res . textConverted ( ) . then ( result => {
1857
+ expect ( result ) . to . equal ( '<?xml version="1.0" encoding="EUC-JP"?><title>日本語</title>' ) ;
1858
+ } ) ;
1859
+ } ) ;
1860
+ } ) ;
1861
+
1862
+ it ( 'should support encoding decode, content-type detect' , function ( ) {
1863
+ url = `${ base } encoding/shift-jis` ;
1864
+ return fetch ( url ) . then ( res => {
1865
+ expect ( res . status ) . to . equal ( 200 ) ;
1866
+ return res . textConverted ( ) . then ( result => {
1867
+ expect ( result ) . to . equal ( '<div>日本語</div>' ) ;
1868
+ } ) ;
1869
+ } ) ;
1870
+ } ) ;
1871
+
1872
+ it ( 'should support encoding decode, html5 detect' , function ( ) {
1873
+ url = `${ base } encoding/gbk` ;
1874
+ return fetch ( url ) . then ( res => {
1875
+ expect ( res . status ) . to . equal ( 200 ) ;
1876
+ return res . textConverted ( ) . then ( result => {
1877
+ expect ( result ) . to . equal ( '<meta charset="gbk"><div>中文</div>' ) ;
1878
+ } ) ;
1879
+ } ) ;
1880
+ } ) ;
1881
+
1882
+ it ( 'should support encoding decode, html4 detect' , function ( ) {
1883
+ url = `${ base } encoding/gb2312` ;
1884
+ return fetch ( url ) . then ( res => {
1885
+ expect ( res . status ) . to . equal ( 200 ) ;
1886
+ return res . textConverted ( ) . then ( result => {
1887
+ expect ( result ) . to . equal ( '<meta http-equiv="Content-Type" content="text/html; charset=gb2312"><div>中文</div>' ) ;
1888
+ } ) ;
1889
+ } ) ;
1890
+ } ) ;
1891
+
1892
+ it ( 'should default to utf8 encoding' , function ( ) {
1893
+ url = `${ base } encoding/utf8` ;
1894
+ return fetch ( url ) . then ( res => {
1895
+ expect ( res . status ) . to . equal ( 200 ) ;
1896
+ expect ( res . headers . get ( 'content-type' ) ) . to . be . null ;
1897
+ return res . textConverted ( ) . then ( result => {
1898
+ expect ( result ) . to . equal ( '中文' ) ;
1899
+ } ) ;
1900
+ } ) ;
1901
+ } ) ;
1902
+
1903
+ it ( 'should support uncommon content-type order, charset in front' , function ( ) {
1904
+ url = `${ base } encoding/order1` ;
1905
+ return fetch ( url ) . then ( res => {
1906
+ expect ( res . status ) . to . equal ( 200 ) ;
1907
+ return res . textConverted ( ) . then ( result => {
1908
+ expect ( result ) . to . equal ( '中文' ) ;
1909
+ } ) ;
1910
+ } ) ;
1911
+ } ) ;
1912
+
1913
+ it ( 'should support uncommon content-type order, end with qs' , function ( ) {
1914
+ url = `${ base } encoding/order2` ;
1915
+ return fetch ( url ) . then ( res => {
1916
+ expect ( res . status ) . to . equal ( 200 ) ;
1917
+ return res . textConverted ( ) . then ( result => {
1918
+ expect ( result ) . to . equal ( '中文' ) ;
1919
+ } ) ;
1920
+ } ) ;
1921
+ } ) ;
1922
+
1923
+ it ( 'should support chunked encoding, html4 detect' , function ( ) {
1924
+ url = `${ base } encoding/chunked` ;
1925
+ return fetch ( url ) . then ( res => {
1926
+ expect ( res . status ) . to . equal ( 200 ) ;
1927
+ const padding = 'a' . repeat ( 10 ) ;
1928
+ return res . textConverted ( ) . then ( result => {
1929
+ expect ( result ) . to . equal ( `${ padding } <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" /><div>日本語</div>` ) ;
1930
+ } ) ;
1931
+ } ) ;
1932
+ } ) ;
1933
+
1934
+ it ( 'should only do encoding detection up to 1024 bytes' , function ( ) {
1935
+ url = `${ base } encoding/invalid` ;
1936
+ return fetch ( url ) . then ( res => {
1937
+ expect ( res . status ) . to . equal ( 200 ) ;
1938
+ const padding = 'a' . repeat ( 1200 ) ;
1939
+ return res . textConverted ( ) . then ( result => {
1940
+ expect ( result ) . to . not . equal ( `${ padding } 中文` ) ;
1941
+ } ) ;
1942
+ } ) ;
1943
+ } ) ;
1944
+ } ) ;
1945
+
1946
+ describe ( 'without optional `encoding`' , function ( ) {
1947
+ before ( function ( ) {
1948
+ if ( hasEncoding ) this . skip ( )
1949
+ } ) ;
1950
+
1951
+ it ( 'should throw a FetchError if res.textConverted() is called without `encoding` in require cache' , ( ) => {
1952
+ url = `${ base } hello` ;
1953
+ return fetch ( url ) . then ( ( res ) => {
1954
+ return expect ( res . textConverted ( ) ) . to . eventually . be . rejected
1955
+ . and . have . property ( 'message' ) . which . includes ( 'encoding' )
1956
+ } ) ;
1957
+ } ) ;
1958
+ } ) ;
1959
+ } ) ;
0 commit comments