node/test/parallel/test-http-outgoing-proto.js
Ruben Bridgewater e038d6a1cd
test: refactor common.expectsError
This completely refactors the `expectsError` behavior: so far it's
almost identical to `assert.throws(fn, object)` in case it was used
with a function as first argument. It had a magical property check
that allowed to verify a functions `type` in case `type` was passed
used in the validation object. This pattern is now completely removed
and `assert.throws()` should be used instead.

The main intent for `common.expectsError()` is to verify error cases
for callback based APIs. This is now more flexible by accepting all
validation possibilites that `assert.throws()` accepts as well. No
magical properties exist anymore. This reduces surprising behavior
for developers who are not used to the Node.js core code base.

This has the side effect that `common` is used significantly less
frequent.

PR-URL: https://github.com/nodejs/node/pull/31092
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2019-12-31 15:54:20 +01:00

122 lines
3.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const OutgoingMessage = http.OutgoingMessage;
const ClientRequest = http.ClientRequest;
const ServerResponse = http.ServerResponse;
assert.strictEqual(
typeof ClientRequest.prototype._implicitHeader, 'function');
assert.strictEqual(
typeof ServerResponse.prototype._implicitHeader, 'function');
// validateHeader
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader();
}, {
code: 'ERR_INVALID_HTTP_TOKEN',
name: 'TypeError',
message: 'Header name must be a valid HTTP token ["undefined"]'
});
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader('test');
}, {
code: 'ERR_HTTP_INVALID_HEADER_VALUE',
name: 'TypeError',
message: 'Invalid value "undefined" for header "test"'
});
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader(404);
}, {
code: 'ERR_INVALID_HTTP_TOKEN',
name: 'TypeError',
message: 'Header name must be a valid HTTP token ["404"]'
});
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader.call({ _header: 'test' }, 'test', 'value');
}, {
code: 'ERR_HTTP_HEADERS_SENT',
name: 'Error',
message: 'Cannot set headers after they are sent to the client'
});
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.setHeader('200', 'あ');
}, {
code: 'ERR_INVALID_CHAR',
name: 'TypeError',
message: 'Invalid character in header content ["200"]'
});
// write
{
const outgoingMessage = new OutgoingMessage();
outgoingMessage.on('error', common.expectsError({
code: 'ERR_METHOD_NOT_IMPLEMENTED',
name: 'Error',
message: 'The _implicitHeader() method is not implemented'
}));
outgoingMessage.write('');
}
assert(OutgoingMessage.prototype.write.call({ _header: 'test' }));
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' });
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer. Received undefined'
});
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.write.call({ _header: 'test', _hasBody: 'test' }, 1);
}, {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The first argument must be of type string or an instance of ' +
'Buffer. Received type number (1)'
});
// addTrailers()
// The `Error` comes from the JavaScript engine so confirm that it is a
// `TypeError` but do not check the message. It will be different in different
// JavaScript engines.
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.addTrailers();
}, TypeError);
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.addTrailers({ 'あ': 'value' });
}, {
code: 'ERR_INVALID_HTTP_TOKEN',
name: 'TypeError',
message: 'Trailer name must be a valid HTTP token ["あ"]'
});
assert.throws(() => {
const outgoingMessage = new OutgoingMessage();
outgoingMessage.addTrailers({ 404: 'あ' });
}, {
code: 'ERR_INVALID_CHAR',
name: 'TypeError',
message: 'Invalid character in trailer content ["404"]'
});