|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- import {IncomingHttpHeaders, IncomingMessage, OutgoingHttpHeaders, request, RequestOptions} from 'http';
- import {Method, DataSource} from '../src/backend';
- import {FALLBACK_LANGUAGE, Language} from '../src/common';
-
- interface ClientParams {
- method: Method;
- path: string;
- headers?: IncomingHttpHeaders;
- body?: unknown;
- }
-
- type ResponseBody = Buffer | string | object;
-
- export interface TestClient {
- (params: ClientParams): Promise<[IncomingMessage, ResponseBody?]>;
- acceptMediaType(mediaType: string): this;
- acceptLanguage(language: string): this;
- acceptCharset(charset: string): this;
- contentType(mediaType: string): this;
- contentCharset(charset: string): this;
- }
-
- export const createTestClient = (options: Omit<RequestOptions, 'method' | 'path'>): TestClient => {
- const additionalHeaders: OutgoingHttpHeaders = {};
- const client = (params: ClientParams) => new Promise<[IncomingMessage, ResponseBody?]>((resolve, reject) => {
- const {
- ...etcAdditionalHeaders
- } = additionalHeaders;
-
- // odd that request() uses OutgoingHttpHeaders instead of IncomingHttpHeaders...
- const headers: OutgoingHttpHeaders = {
- ...(options.headers ?? {}),
- ...etcAdditionalHeaders,
- };
-
- let contentTypeHeader: string | undefined;
- if (typeof params.body !== 'undefined') {
- contentTypeHeader = headers['content-type'] = params.headers?.['content-type'] ?? 'application/json';
- }
-
- const req = request({
- ...options,
- method: params.method,
- path: params.path,
- headers,
- });
-
- req.on('response', (res) => {
- res.on('error', (err) => {
- reject(err);
- });
-
- let resBuffer: Buffer | undefined;
- res.on('data', (c) => {
- resBuffer = (
- typeof resBuffer === 'undefined'
- ? Buffer.from(c)
- : Buffer.concat([resBuffer, c])
- );
- });
-
- res.on('close', () => {
- const acceptHeader = Array.isArray(headers['accept']) ? headers['accept'].join('; ') : headers['accept'];
- const contentTypeBase = acceptHeader ?? 'application/octet-stream';
- const [type, subtype] = contentTypeBase.split('/');
- const allSubtypes = subtype.split('+');
- if (typeof resBuffer !== 'undefined') {
- if (allSubtypes.includes('json')) {
- const acceptCharset = (
- Array.isArray(headers['accept-charset'])
- ? headers['accept-charset'].join('; ')
- : headers['accept-charset']
- ) as BufferEncoding | undefined;
- resolve([res, JSON.parse(resBuffer.toString(acceptCharset ?? 'utf-8'))]);
- return;
- }
-
- if (type === 'text') {
- const acceptCharset = (
- Array.isArray(headers['accept-charset'])
- ? headers['accept-charset'].join('; ')
- : headers['accept-charset']
- ) as BufferEncoding | undefined;
- resolve([res, resBuffer.toString(acceptCharset ?? 'utf-8')]);
- return;
- }
-
- resolve([res, resBuffer]);
- return;
- }
-
- resolve([res]);
- });
- });
-
- req.on('error', (err) => {
- reject(err);
- })
-
- if (typeof params.body !== 'undefined') {
- const theContentTypeHeader = Array.isArray(contentTypeHeader) ? contentTypeHeader.join('; ') : contentTypeHeader?.toString();
- const contentTypeAll = theContentTypeHeader ?? 'application/octet-stream';
- const [contentTypeBase, ...contentTypeParams] = contentTypeAll.split(';').map((s) => s.replace(/\s+/g, '').trim());
- const charsetParam = contentTypeParams.find((s) => s.startsWith('charset='));
- const charset = charsetParam?.split('=')?.[1] as BufferEncoding | undefined;
- const [, subtype] = contentTypeBase.split('/');
- const allSubtypes = subtype.split('+');
- req.write(
- allSubtypes.includes('json')
- ? JSON.stringify(params.body)
- : Buffer.from(params.body?.toString() ?? '', contentTypeBase === 'text' ? charset : undefined)
- );
- }
-
- req.end();
- });
-
- client.acceptMediaType = function acceptMediaType(mediaType: string) {
- additionalHeaders['accept'] = mediaType;
- return this;
- };
-
- client.acceptLanguage = function acceptLanguage(language: string) {
- additionalHeaders['accept-language'] = language;
- return this;
- };
-
- client.acceptCharset = function acceptCharset(charset: string) {
- additionalHeaders['accept-charset'] = charset;
- return this;
- };
-
- client.contentType = function contentType(mediaType: string) {
- additionalHeaders['content-type'] = mediaType;
- return this;
- };
-
- client.contentCharset = function contentCharset(charset: string) {
- additionalHeaders['content-type'] = `${additionalHeaders['content-type']}; charset="${charset}"`;
- return this;
- };
-
- return client;
- };
-
- export const dummyGenerationStrategy = () => Promise.resolve();
-
- export class DummyError extends Error {}
-
- export class DummyDataSource implements DataSource {
- private resource?: { dataSource?: unknown };
-
- async create(): Promise<object> {
- return {};
- }
-
- async delete(): Promise<void> {}
-
- async emplace(): Promise<[object, boolean]> {
- return [{}, false];
- }
-
- async getById(): Promise<object> {
- return {};
- }
-
- async newId(): Promise<string> {
- return '';
- }
-
- async getMultiple(): Promise<object[]> {
- return [];
- }
-
- async getSingle(): Promise<object> {
- return {};
- }
-
- async getTotalCount(): Promise<number> {
- return 0;
- }
-
- async initialize(): Promise<void> {}
-
- async patch(): Promise<object> {
- return {};
- }
-
- prepareResource(rr: unknown) {
- this.resource = rr as unknown as { dataSource: DummyDataSource };
- this.resource.dataSource = this;
- }
- }
-
- export const TEST_LANGUAGE: Language = {
- name: FALLBACK_LANGUAGE.name,
- statusMessages: {
- unableToInitializeResourceDataSource: 'unableToInitializeResourceDataSource',
- unableToFetchResourceCollection: 'unableToFetchResourceCollection',
- unableToFetchResource: 'unableToFetchResource',
- resourceIdNotGiven: 'resourceIdNotGiven',
- languageNotAcceptable: 'languageNotAcceptable',
- encodingNotAcceptable: 'encodingNotAcceptable',
- mediaTypeNotAcceptable: 'mediaTypeNotAcceptable',
- methodNotAllowed: 'methodNotAllowed',
- urlNotFound: 'urlNotFound',
- badRequest: 'badRequest',
- ok: 'ok',
- resourceCollectionFetched: 'resourceCollectionFetched',
- resourceFetched: 'resourceFetched',
- resourceNotFound: 'resourceNotFound',
- deleteNonExistingResource: 'deleteNonExistingResource',
- unableToCreateResource: 'unableToCreateResource',
- unableToBindResourceDataSource: 'unableToBindResourceDataSource',
- unableToGenerateIdFromResourceDataSource: 'unableToGenerateIdFromResourceDataSource',
- unableToAssignIdFromResourceDataSource: 'unableToAssignIdFromResourceDataSource',
- unableToEmplaceResource: 'unableToEmplaceResource',
- unableToSerializeResponse: 'unableToSerializeResponse',
- unableToEncodeResponse: 'unableToEncodeResponse',
- unableToDeleteResource: 'unableToDeleteResource',
- unableToDeserializeResource: 'unableToDeserializeResource',
- unableToDecodeResource: 'unableToDecodeResource',
- resourceDeleted: 'resourceDeleted',
- unableToDeserializeRequest: 'unableToDeserializeRequest',
- patchNonExistingResource: 'patchNonExistingResource',
- unableToPatchResource: 'unableToPatchResource',
- invalidResourcePatch: 'invalidResourcePatch',
- invalidResourcePatchType: 'invalidResourcePatchType',
- invalidResource: 'invalidResource',
- resourcePatched: 'resourcePatched',
- resourceCreated: 'resourceCreated',
- resourceReplaced: 'resourceReplaced',
- notImplemented: 'notImplemented',
- provideOptions: 'provideOptions',
- internalServerError: 'internalServerError',
- },
- bodies: {
- languageNotAcceptable: [],
- encodingNotAcceptable: [],
- mediaTypeNotAcceptable: [],
- },
- };
|