node/test/parallel/test-fs-whatwg-url.js
matzavinos 219932a9f7
errors: convert 'fs'
covert lib/fs.js over to using lib/internal/errors.js
i have not addressed the cases that use errnoException(),
for reasons described in GH-12926

- throw the ERR_INVALID_CALLBACK error
  when the the callback is invalid
- replace the ['object', 'string'] with
  ['string', 'object'] in the error constructor call,
  to better match the previous err msg
  in the getOptions() function
- add error ERR_VALUE_OUT_OF_RANGE in lib/internal/errors.js,
  this error is thrown when a numeric value is out of range
- document the ERR_VALUE_OUT_OF_RANGE err in errors.md
- correct the expected args, in the error thrown in the function
  fs._toUnixTimestamp() to ['Date', 'time in seconds'] (lib/fs.js)
- update the listener error type in the fs.watchFile() function,
  from Error to TypeError (lib/fs.js)
- update errors from ERR_INVALID_OPT_VALUE to ERR_INVALID_ARG_TYPE
  in the functions fs.ReadStream() and fs.WriteStream(),
  for the cases of range errors use the new error:
  ERR_VALUE_OUT_OF_RANGE (lib/fs.js)

PR-URL: https://github.com/nodejs/node/pull/15043
Refs: https://github.com/nodejs/node/issues/11273
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
2017-08-31 19:37:39 -04:00

74 lines
2.1 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const os = require('os');
const URL = require('url').URL;
function pathToFileURL(p) {
if (!path.isAbsolute(p))
throw new Error('Path must be absolute');
if (common.isWindows && p.startsWith('\\\\'))
p = p.slice(2);
return new URL(`file://${p}`);
}
const p = path.resolve(common.fixturesDir, 'a.js');
const url = pathToFileURL(p);
assert(url instanceof URL);
// Check that we can pass in a URL object successfully
fs.readFile(url, common.mustCall((err, data) => {
assert.ifError(err);
assert(Buffer.isBuffer(data));
}));
// Check that using a non file:// URL reports an error
const httpUrl = new URL('http://example.org');
fs.readFile(httpUrl, common.expectsError({
code: 'ERR_INVALID_URL_SCHEME',
type: TypeError,
message: 'The URL must be of scheme file'
}));
// pct-encoded characters in the path will be decoded and checked
fs.readFile(new URL('file:///c:/tmp/%00test'), common.mustCall((err) => {
common.expectsError(
() => {
throw err;
},
{
code: 'ERR_INVALID_ARG_TYPE',
type: Error
});
}));
if (common.isWindows) {
// encoded back and forward slashes are not permitted on windows
['%2f', '%2F', '%5c', '%5C'].forEach((i) => {
fs.readFile(new URL(`file:///c:/tmp/${i}`), common.expectsError({
code: 'ERR_INVALID_FILE_URL_PATH',
type: TypeError,
message: 'File URL path must not include encoded \\ or / characters'
}));
});
} else {
// encoded forward slashes are not permitted on other platforms
['%2f', '%2F'].forEach((i) => {
fs.readFile(new URL(`file:///c:/tmp/${i}`), common.expectsError({
code: 'ERR_INVALID_FILE_URL_PATH',
type: TypeError,
message: 'File URL path must not include encoded / characters'
}));
});
fs.readFile(new URL('file://hostname/a/b/c'), common.expectsError({
code: 'ERR_INVALID_FILE_URL_HOST',
type: TypeError,
message: `File URL host must be "localhost" or empty on ${os.platform()}`
}));
}