parent
9d4c883d51
commit
afbef9bc39
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -1,12 +1,16 @@
|
||||
import { ENCODING_RE } from './constants';
|
||||
import iconv from 'iconv-lite';
|
||||
import { DEFAULT_ENCODING, ENCODING_RE } from './constants';
|
||||
|
||||
// check a string for encoding; this is
|
||||
// used in our fetchResource function to
|
||||
// ensure correctly encoded responses
|
||||
export default function getEncoding(str) {
|
||||
let encoding = DEFAULT_ENCODING;
|
||||
if (ENCODING_RE.test(str)) {
|
||||
return ENCODING_RE.exec(str)[1];
|
||||
const testEncode = ENCODING_RE.exec(str)[1];
|
||||
if (iconv.encodingExists(testEncode)) {
|
||||
encoding = testEncode;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return encoding;
|
||||
}
|
||||
|
@ -1,15 +1,26 @@
|
||||
import assert from 'assert';
|
||||
import cheerio from 'cheerio';
|
||||
|
||||
import getEncoding from './get-encoding';
|
||||
|
||||
// Tests are bypassed in the browser because it has an encoding
|
||||
// A shim is used /src/shims/iconv-lite.js to decrease load size
|
||||
|
||||
describe('getEncoding(str)', () => {
|
||||
if (cheerio.browser) return;
|
||||
|
||||
it('returns the encoding as a string', () => {
|
||||
const contentType = 'text/html; charset=iso-8859-15';
|
||||
assert.equal(getEncoding(contentType), 'iso-8859-15');
|
||||
});
|
||||
|
||||
it('returns null if no encoding found', () => {
|
||||
it('returns utf-8 as a default if no encoding found', () => {
|
||||
const contentType = 'text/html';
|
||||
assert.equal(getEncoding(contentType), null);
|
||||
assert.equal(getEncoding(contentType), 'utf-8');
|
||||
});
|
||||
|
||||
it('returns utf-8 if there is an invalid encoding', () => {
|
||||
const contentType = 'text/html; charset=fake-charset';
|
||||
assert.equal(getEncoding(contentType), 'utf-8');
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue