|
|
@@ -4,8 +4,8 @@ import {IncomingMessage} from 'http';
|
|
|
import queryString from 'querystring';
|
|
|
import {Errorf} from '@e22m4u/js-format';
|
|
|
import {isReadableStream} from './is-readable-stream.js';
|
|
|
-import {createCookieString} from './create-cookie-string.js';
|
|
|
-import {BUFFER_ENCODING_LIST} from './fetch-request-body.js';
|
|
|
+import {createCookiesString} from './create-cookies-string.js';
|
|
|
+import {CHARACTER_ENCODING_LIST} from './fetch-request-body.js';
|
|
|
|
|
|
/**
|
|
|
* @typedef {{
|
|
|
@@ -14,7 +14,7 @@ import {BUFFER_ENCODING_LIST} from './fetch-request-body.js';
|
|
|
* secure?: boolean;
|
|
|
* path?: string;
|
|
|
* query?: object;
|
|
|
- * cookie?: object;
|
|
|
+ * cookies?: object;
|
|
|
* headers?: object;
|
|
|
* body?: string;
|
|
|
* stream?: import('stream').Readable;
|
|
|
@@ -32,7 +32,7 @@ export function createRequestMock(patch) {
|
|
|
if ((patch != null && typeof patch !== 'object') || Array.isArray(patch)) {
|
|
|
throw new Errorf(
|
|
|
'The first parameter of "createRequestMock" ' +
|
|
|
- 'should be an Object, but %v given.',
|
|
|
+ 'should be an Object, but %v was given.',
|
|
|
patch,
|
|
|
);
|
|
|
}
|
|
|
@@ -40,25 +40,25 @@ export function createRequestMock(patch) {
|
|
|
if (patch.host != null && typeof patch.host !== 'string')
|
|
|
throw new Errorf(
|
|
|
'The parameter "host" of "createRequestMock" ' +
|
|
|
- 'should be a String, but %v given.',
|
|
|
+ 'should be a String, but %v was given.',
|
|
|
patch.host,
|
|
|
);
|
|
|
if (patch.method != null && typeof patch.method !== 'string')
|
|
|
throw new Errorf(
|
|
|
'The parameter "method" of "createRequestMock" ' +
|
|
|
- 'should be a String, but %v given.',
|
|
|
+ 'should be a String, but %v was given.',
|
|
|
patch.method,
|
|
|
);
|
|
|
if (patch.secure != null && typeof patch.secure !== 'boolean')
|
|
|
throw new Errorf(
|
|
|
'The parameter "secure" of "createRequestMock" ' +
|
|
|
- 'should be a Boolean, but %v given.',
|
|
|
+ 'should be a Boolean, but %v was given.',
|
|
|
patch.secure,
|
|
|
);
|
|
|
if (patch.path != null && typeof patch.path !== 'string')
|
|
|
throw new Errorf(
|
|
|
'The parameter "path" of "createRequestMock" ' +
|
|
|
- 'should be a String, but %v given.',
|
|
|
+ 'should be a String, but %v was given.',
|
|
|
patch.path,
|
|
|
);
|
|
|
if (
|
|
|
@@ -69,20 +69,20 @@ export function createRequestMock(patch) {
|
|
|
) {
|
|
|
throw new Errorf(
|
|
|
'The parameter "query" of "createRequestMock" ' +
|
|
|
- 'should be a String or Object, but %v given.',
|
|
|
+ 'should be a String or Object, but %v was given.',
|
|
|
patch.query,
|
|
|
);
|
|
|
}
|
|
|
if (
|
|
|
- (patch.cookie != null &&
|
|
|
- typeof patch.cookie !== 'string' &&
|
|
|
- typeof patch.cookie !== 'object') ||
|
|
|
- Array.isArray(patch.cookie)
|
|
|
+ (patch.cookies != null &&
|
|
|
+ typeof patch.cookies !== 'string' &&
|
|
|
+ typeof patch.cookies !== 'object') ||
|
|
|
+ Array.isArray(patch.cookies)
|
|
|
) {
|
|
|
throw new Errorf(
|
|
|
- 'The parameter "cookie" of "createRequestMock" ' +
|
|
|
- 'should be a String or Object, but %v given.',
|
|
|
- patch.cookie,
|
|
|
+ 'The parameter "cookies" of "createRequestMock" ' +
|
|
|
+ 'should be a String or Object, but %v was given.',
|
|
|
+ patch.cookies,
|
|
|
);
|
|
|
}
|
|
|
if (
|
|
|
@@ -91,25 +91,28 @@ export function createRequestMock(patch) {
|
|
|
) {
|
|
|
throw new Errorf(
|
|
|
'The parameter "headers" of "createRequestMock" ' +
|
|
|
- 'should be an Object, but %v given.',
|
|
|
+ 'should be an Object, but %v was given.',
|
|
|
patch.headers,
|
|
|
);
|
|
|
}
|
|
|
if (patch.stream != null && !isReadableStream(patch.stream))
|
|
|
throw new Errorf(
|
|
|
'The parameter "stream" of "createRequestMock" ' +
|
|
|
- 'should be a Stream, but %v given.',
|
|
|
+ 'should be a Stream, but %v was given.',
|
|
|
patch.stream,
|
|
|
);
|
|
|
if (patch.encoding != null) {
|
|
|
if (typeof patch.encoding !== 'string')
|
|
|
throw new Errorf(
|
|
|
'The parameter "encoding" of "createRequestMock" ' +
|
|
|
- 'should be a String, but %v given.',
|
|
|
+ 'should be a String, but %v was given.',
|
|
|
+ patch.encoding,
|
|
|
+ );
|
|
|
+ if (!CHARACTER_ENCODING_LIST.includes(patch.encoding))
|
|
|
+ throw new Errorf(
|
|
|
+ 'Character encoding %v is not supported.',
|
|
|
patch.encoding,
|
|
|
);
|
|
|
- if (!BUFFER_ENCODING_LIST.includes(patch.encoding))
|
|
|
- throw new Errorf('Buffer encoding %v is not supported.', patch.encoding);
|
|
|
}
|
|
|
// если передан поток, выполняется
|
|
|
// проверка на несовместимые опции
|
|
|
@@ -141,7 +144,7 @@ export function createRequestMock(patch) {
|
|
|
patch.host,
|
|
|
patch.secure,
|
|
|
patch.body,
|
|
|
- patch.cookie,
|
|
|
+ patch.cookies,
|
|
|
patch.encoding,
|
|
|
patch.headers,
|
|
|
);
|
|
|
@@ -161,7 +164,7 @@ function createRequestStream(secure, body, encoding) {
|
|
|
if (encoding != null && typeof encoding !== 'string')
|
|
|
throw new Errorf(
|
|
|
'The parameter "encoding" of "createRequestStream" ' +
|
|
|
- 'should be a String, but %v given.',
|
|
|
+ 'should be a String, but %v was given.',
|
|
|
encoding,
|
|
|
);
|
|
|
encoding = encoding || 'utf-8';
|
|
|
@@ -198,7 +201,7 @@ function createRequestUrl(path, query) {
|
|
|
if (typeof path !== 'string')
|
|
|
throw new Errorf(
|
|
|
'The parameter "path" of "createRequestUrl" ' +
|
|
|
- 'should be a String, but %v given.',
|
|
|
+ 'should be a String, but %v was given.',
|
|
|
path,
|
|
|
);
|
|
|
if (
|
|
|
@@ -207,7 +210,7 @@ function createRequestUrl(path, query) {
|
|
|
) {
|
|
|
throw new Errorf(
|
|
|
'The parameter "query" of "createRequestUrl" ' +
|
|
|
- 'should be a String or Object, but %v given.',
|
|
|
+ 'should be a String or Object, but %v was given.',
|
|
|
query,
|
|
|
);
|
|
|
}
|
|
|
@@ -227,36 +230,36 @@ function createRequestUrl(path, query) {
|
|
|
* @param {string|null|undefined} host
|
|
|
* @param {boolean|null|undefined} secure
|
|
|
* @param {*} body
|
|
|
- * @param {string|object|null|undefined} cookie
|
|
|
+ * @param {string|object|null|undefined} cookies
|
|
|
* @param {import('buffer').BufferEncoding|null|undefined} encoding
|
|
|
* @param {object|null|undefined} headers
|
|
|
* @returns {object}
|
|
|
*/
|
|
|
-function createRequestHeaders(host, secure, body, cookie, encoding, headers) {
|
|
|
+function createRequestHeaders(host, secure, body, cookies, encoding, headers) {
|
|
|
if (host != null && typeof host !== 'string')
|
|
|
throw new Errorf(
|
|
|
'The parameter "host" of "createRequestHeaders" ' +
|
|
|
- 'a non-empty String, but %v given.',
|
|
|
+ 'a non-empty String, but %v was given.',
|
|
|
host,
|
|
|
);
|
|
|
host = host || 'localhost';
|
|
|
if (secure != null && typeof secure !== 'boolean')
|
|
|
throw new Errorf(
|
|
|
'The parameter "secure" of "createRequestHeaders" ' +
|
|
|
- 'should be a String, but %v given.',
|
|
|
+ 'should be a String, but %v was given.',
|
|
|
secure,
|
|
|
);
|
|
|
secure = Boolean(secure);
|
|
|
if (
|
|
|
- (cookie != null &&
|
|
|
- typeof cookie !== 'object' &&
|
|
|
- typeof cookie !== 'string') ||
|
|
|
- Array.isArray(cookie)
|
|
|
+ (cookies != null &&
|
|
|
+ typeof cookies !== 'object' &&
|
|
|
+ typeof cookies !== 'string') ||
|
|
|
+ Array.isArray(cookies)
|
|
|
) {
|
|
|
throw new Errorf(
|
|
|
- 'The parameter "cookie" of "createRequestHeaders" ' +
|
|
|
- 'should be a String or Object, but %v given.',
|
|
|
- cookie,
|
|
|
+ 'The parameter "cookies" of "createRequestHeaders" ' +
|
|
|
+ 'should be a String or Object, but %v was given.',
|
|
|
+ cookies,
|
|
|
);
|
|
|
}
|
|
|
if (
|
|
|
@@ -265,7 +268,7 @@ function createRequestHeaders(host, secure, body, cookie, encoding, headers) {
|
|
|
) {
|
|
|
throw new Errorf(
|
|
|
'The parameter "headers" of "createRequestHeaders" ' +
|
|
|
- 'should be an Object, but %v given.',
|
|
|
+ 'should be an Object, but %v was given.',
|
|
|
headers,
|
|
|
);
|
|
|
}
|
|
|
@@ -273,7 +276,7 @@ function createRequestHeaders(host, secure, body, cookie, encoding, headers) {
|
|
|
if (encoding != null && typeof encoding !== 'string')
|
|
|
throw new Errorf(
|
|
|
'The parameter "encoding" of "createRequestHeaders" ' +
|
|
|
- 'should be a String, but %v given.',
|
|
|
+ 'should be a String, but %v was given.',
|
|
|
encoding,
|
|
|
);
|
|
|
encoding = encoding || 'utf-8';
|
|
|
@@ -282,13 +285,14 @@ function createRequestHeaders(host, secure, body, cookie, encoding, headers) {
|
|
|
if (secure) obj['x-forwarded-proto'] = 'https';
|
|
|
// формирование заголовка Cookie
|
|
|
// из строки или объекта
|
|
|
- if (cookie != null) {
|
|
|
- if (typeof cookie === 'string') {
|
|
|
+ if (cookies != null) {
|
|
|
+ if (typeof cookies === 'string') {
|
|
|
obj['cookie'] = obj['cookie'] ? obj['cookie'] : '';
|
|
|
- obj['cookie'] += cookie;
|
|
|
- } else if (typeof cookie === 'object') {
|
|
|
+ obj['cookie'] += obj['cookie'] ? `; ${cookies}` : cookies;
|
|
|
+ } else if (typeof cookies === 'object') {
|
|
|
obj['cookie'] = obj['cookie'] ? obj['cookie'] : '';
|
|
|
- obj['cookie'] += createCookieString(cookie);
|
|
|
+ const newCookies = createCookiesString(cookies);
|
|
|
+ obj['cookie'] += obj['cookie'] ? `; ${newCookies}` : newCookies;
|
|
|
}
|
|
|
}
|
|
|
// установка заголовка "content-type"
|