Skip to content

Commit

Permalink
test: replace assert.throws w/ common.expectsError
Browse files Browse the repository at this point in the history
PR-URL: #17557
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
apapirovski authored and MylesBorins committed Dec 12, 2017
1 parent cbd0be5 commit 49d6628
Show file tree
Hide file tree
Showing 20 changed files with 140 additions and 140 deletions.
6 changes: 3 additions & 3 deletions test/async-hooks/test-embedder.api.async-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ common.expectsError(
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
});
assert.throws(() => {
common.expectsError(() => {
new AsyncResource('invalid_trigger_id', { triggerAsyncId: null });
}, common.expectsError({
}, {
code: 'ERR_INVALID_ASYNC_ID',
type: RangeError,
}));
});

assert.strictEqual(
new AsyncResource('default_trigger_id').triggerAsyncId(),
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-assert-fail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

/* eslint-disable prefer-common-expectserror */

const common = require('../common');
const assert = require('assert');

Expand Down
3 changes: 3 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';

/* eslint-disable prefer-common-expectserror */

const common = require('../common');
const assert = require('assert');
const a = assert;
Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,12 +974,11 @@ assert.strictEqual(SlowBuffer.prototype.offset, undefined);
}

// ParseArrayIndex() should reject values that don't fit in a 32 bits size_t.
assert.throws(() => {
common.expectsError(() => {
const a = Buffer.alloc(1);
const b = Buffer.alloc(1);
a.copy(b, 0, 0x100000000, 0x100000001);
}, common.expectsError(
{ code: undefined, type: RangeError, message: 'Index out of range' }));
}, { code: undefined, type: RangeError, message: 'Index out of range' });

// Unpooled buffer (replaces SlowBuffer)
{
Expand Down
10 changes: 4 additions & 6 deletions test/parallel/test-buffer-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,21 +421,19 @@ common.expectsError(() => {

// Testing process.binding. Make sure "end" is properly checked for -1 wrap
// around.
assert.throws(() => {
common.expectsError(() => {
process.binding('buffer').fill(Buffer.alloc(1), 1, 1, -2, 1);
}, common.expectsError(
{ code: undefined, type: RangeError, message: 'Index out of range' }));
}, { code: undefined, type: RangeError, message: 'Index out of range' });

// Test that bypassing 'length' won't cause an abort.
assert.throws(() => {
common.expectsError(() => {
const buf = new Buffer('w00t');
Object.defineProperty(buf, 'length', {
value: 1337,
enumerable: true
});
buf.fill('');
}, common.expectsError(
{ code: undefined, type: RangeError, message: 'Index out of range' }));
}, { code: undefined, type: RangeError, message: 'Index out of range' });

assert.deepStrictEqual(
Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'),
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-console-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ common.expectsError(
);

// Console constructor should throw if stderr exists but is not writable
assert.throws(
common.expectsError(
() => {
out.write = () => {};
err.write = undefined;
new Console(out, err);
},
common.expectsError({
{
code: 'ERR_CONSOLE_WRITABLE_STREAM',
type: TypeError,
message: /stderr/
})
}
);

out.write = err.write = (d) => {};
Expand Down
16 changes: 8 additions & 8 deletions test/parallel/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,20 @@ assert.doesNotThrow(() => {
}, common.mustCall());
});

assert.throws(() => dns.lookupService('0.0.0.0'), common.expectsError({
common.expectsError(() => dns.lookupService('0.0.0.0'), {
code: 'ERR_MISSING_ARGS',
type: TypeError,
message: 'The "host", "port", and "callback" arguments must be specified'
}));
});

const invalidHost = 'fasdfdsaf';
assert.throws(() => {
common.expectsError(() => {
dns.lookupService(invalidHost, 0, common.mustNotCall());
}, common.expectsError({
}, {
code: 'ERR_INVALID_OPT_VALUE',
type: TypeError,
message: `The value "${invalidHost}" is invalid for option "host"`
}));
});

const portErr = (port) => {
common.expectsError(
Expand All @@ -238,9 +238,9 @@ portErr(undefined);
portErr(65538);
portErr('test');

assert.throws(() => {
common.expectsError(() => {
dns.lookupService('0.0.0.0', 80, null);
}, common.expectsError({
}, {
code: 'ERR_INVALID_CALLBACK',
type: TypeError
}));
});
60 changes: 30 additions & 30 deletions test/parallel/test-http-outgoing-proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,80 +21,80 @@ assert.strictEqual(
typeof ServerResponse.prototype._implicitHeader, 'function');

// validateHeader
assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader();
}, common.expectsError({
}, {
code: 'ERR_INVALID_HTTP_TOKEN',
type: TypeError,
message: 'Header name must be a valid HTTP token ["undefined"]'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader('test');
}, common.expectsError({
}, {
code: 'ERR_MISSING_ARGS',
type: TypeError,
message: 'The "value" argument must be specified'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader(404);
}, common.expectsError({
}, {
code: 'ERR_INVALID_HTTP_TOKEN',
type: TypeError,
message: 'Header name must be a valid HTTP token ["404"]'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader.call({ _header: 'test' }, 'test', 'value');
}, common.expectsError({
}, {
code: 'ERR_HTTP_HEADERS_SENT',
type: Error,
message: 'Cannot set headers after they are sent to the client'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader('200', 'あ');
}, common.expectsError({
}, {
code: 'ERR_INVALID_CHAR',
type: TypeError,
message: 'Invalid character in header content ["200"]'
}));
});

// write
assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write();
}, common.expectsError({
}, {
code: 'ERR_METHOD_NOT_IMPLEMENTED',
type: Error,
message: 'The _implicitHeader() method is not implemented'
}));
});

assert(OutgoingMessage.prototype.write.call({ _header: 'test' }));

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' });
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The first argument must be one of type string or Buffer'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' }, 1);
}, common.expectsError({
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The first argument must be one of type string or Buffer'
}));
});

// addTrailers()
// The `Error` comes from the JavaScript engine so confirm that it is a
Expand All @@ -105,20 +105,20 @@ assert.throws(() => {
outgoingMessage.addTrailers();
}, TypeError);

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.addTrailers({ 'あ': 'value' });
}, common.expectsError({
}, {
code: 'ERR_INVALID_HTTP_TOKEN',
type: TypeError,
message: 'Trailer name must be a valid HTTP token ["あ"]'
}));
});

assert.throws(() => {
common.expectsError(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.addTrailers({ 404: 'あ' });
}, common.expectsError({
}, {
code: 'ERR_INVALID_CHAR',
type: TypeError,
message: 'Invalid character in trailer content ["404"]'
}));
});
7 changes: 3 additions & 4 deletions test/parallel/test-http2-client-request-options-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');

// Check if correct errors are emitted when wrong type of data is passed
Expand Down Expand Up @@ -40,19 +39,19 @@ server.listen(0, common.mustCall(() => {
return;
}

assert.throws(
common.expectsError(
() => client.request({
':method': 'CONNECT',
':authority': `localhost:${port}`
}, {
[option]: types[type]
}),
common.expectsError({
{
type: TypeError,
code: 'ERR_INVALID_OPT_VALUE',
message: `The value "${String(types[type])}" is invalid ` +
`for option "${option}"`
})
}
);
});
});
Expand Down
18 changes: 9 additions & 9 deletions test/parallel/test-http2-connect-method.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,36 @@ server.listen(0, common.mustCall(() => {
const client = http2.connect(`http://localhost:${proxy.address().port}`);

// confirm that :authority is required and :scheme & :path are forbidden
assert.throws(
common.expectsError(
() => client.request({
[HTTP2_HEADER_METHOD]: 'CONNECT'
}),
common.expectsError({
{
code: 'ERR_HTTP2_CONNECT_AUTHORITY',
message: ':authority header is required for CONNECT requests'
})
}
);
assert.throws(
common.expectsError(
() => client.request({
[HTTP2_HEADER_METHOD]: 'CONNECT',
[HTTP2_HEADER_AUTHORITY]: `localhost:${port}`,
[HTTP2_HEADER_SCHEME]: 'http'
}),
common.expectsError({
{
code: 'ERR_HTTP2_CONNECT_SCHEME',
message: 'The :scheme header is forbidden for CONNECT requests'
})
}
);
assert.throws(
common.expectsError(
() => client.request({
[HTTP2_HEADER_METHOD]: 'CONNECT',
[HTTP2_HEADER_AUTHORITY]: `localhost:${port}`,
[HTTP2_HEADER_PATH]: '/'
}),
common.expectsError({
{
code: 'ERR_HTTP2_CONNECT_PATH',
message: 'The :path header is forbidden for CONNECT requests'
})
}
);

// valid CONNECT request
Expand Down
7 changes: 3 additions & 4 deletions test/parallel/test-http2-respond-file-204.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ if (!common.hasCrypto)
common.skip('missing crypto');
const fixtures = require('../common/fixtures');
const http2 = require('http2');
const assert = require('assert');

const {
HTTP2_HEADER_CONTENT_TYPE,
Expand All @@ -16,16 +15,16 @@ const fname = fixtures.path('elipses.txt');

const server = http2.createServer();
server.on('stream', (stream) => {
assert.throws(() => {
common.expectsError(() => {
stream.respondWithFile(fname, {
[HTTP2_HEADER_STATUS]: 204,
[HTTP2_HEADER_CONTENT_TYPE]: 'text/plain'
});
}, common.expectsError({
}, {
code: 'ERR_HTTP2_PAYLOAD_FORBIDDEN',
type: Error,
message: 'Responses with 204 status must not have a payload'
}));
});
stream.respond({});
stream.end();
});
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-http2-server-push-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ server.on('stream', common.mustCall((stream) => {
// and pushStream() must throw.
assert.strictEqual(stream.pushAllowed, false);

assert.throws(() => {
common.expectsError(() => {
stream.pushStream({
':scheme': 'http',
':path': '/foobar',
':authority': `localhost:${server.address().port}`,
}, common.mustNotCall());
}, common.expectsError({
}, {
code: 'ERR_HTTP2_PUSH_DISABLED',
type: Error
}));
});

stream.respond({ ':status': 200 });
stream.end('test');
Expand Down
Loading

0 comments on commit 49d6628

Please sign in to comment.