2017-02-25 03:00:17 +01:00
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
|
|
|
|
|
|
if (!common.hasIntl) {
|
|
|
|
common.skip('missing Intl');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const assert = require('assert');
|
|
|
|
const { domainToASCII, domainToUnicode } = require('url');
|
|
|
|
|
|
|
|
// Tests below are not from WPT.
|
|
|
|
const tests = require('../fixtures/url-idna.js');
|
2017-06-01 02:13:43 +02:00
|
|
|
const wptToASCIITests = require('../fixtures/url-toascii.js');
|
2017-02-25 03:00:17 +01:00
|
|
|
|
2017-03-20 22:29:54 +01:00
|
|
|
{
|
2017-04-21 20:54:58 +02:00
|
|
|
const expectedError = common.expectsError(
|
|
|
|
{ code: 'ERR_MISSING_ARGS', type: TypeError });
|
|
|
|
assert.throws(() => domainToASCII(), expectedError);
|
|
|
|
assert.throws(() => domainToUnicode(), expectedError);
|
2017-03-20 22:29:54 +01:00
|
|
|
assert.strictEqual(domainToASCII(undefined), 'undefined');
|
|
|
|
assert.strictEqual(domainToUnicode(undefined), 'undefined');
|
|
|
|
}
|
|
|
|
|
2017-02-25 03:00:17 +01:00
|
|
|
{
|
2017-06-01 02:13:43 +02:00
|
|
|
for (const [i, { ascii, unicode }] of tests.entries()) {
|
2017-02-25 03:00:17 +01:00
|
|
|
assert.strictEqual(ascii, domainToASCII(unicode),
|
|
|
|
`domainToASCII(${i + 1})`);
|
|
|
|
assert.strictEqual(unicode, domainToUnicode(ascii),
|
|
|
|
`domainToUnicode(${i + 1})`);
|
|
|
|
assert.strictEqual(ascii, domainToASCII(domainToUnicode(ascii)),
|
|
|
|
`domainToASCII(domainToUnicode(${i + 1}))`);
|
|
|
|
assert.strictEqual(unicode, domainToUnicode(domainToASCII(unicode)),
|
|
|
|
`domainToUnicode(domainToASCII(${i + 1}))`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-06-01 02:13:43 +02:00
|
|
|
for (const [i, test] of wptToASCIITests.entries()) {
|
|
|
|
if (typeof test === 'string')
|
|
|
|
continue; // skip comments
|
|
|
|
const { comment, input, output } = test;
|
|
|
|
let caseComment = `Case ${i + 1}`;
|
|
|
|
if (comment)
|
|
|
|
caseComment += ` (${comment})`;
|
|
|
|
if (output === null) {
|
|
|
|
assert.strictEqual(domainToASCII(input), '', caseComment);
|
|
|
|
assert.strictEqual(domainToUnicode(input), '', caseComment);
|
|
|
|
} else {
|
|
|
|
assert.strictEqual(domainToASCII(input), output, caseComment);
|
|
|
|
const roundtripped = domainToASCII(domainToUnicode(input));
|
|
|
|
assert.strictEqual(roundtripped, output, caseComment);
|
|
|
|
}
|
2017-05-11 09:19:15 +02:00
|
|
|
}
|
2017-02-25 03:00:17 +01:00
|
|
|
}
|