mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-09 18:03:21 +00:00
AAE-30882 Remove superagent for fetch (#11856)
* [AAE-30882] - Remove superagent for fetch * [AAE-30882] - Fixing comments * [AAE-30882] - fixde failing unit tests * [AAE-30882] - fixde failing unit tests * [AAE-30882] - fixd sonarcloud comment
This commit is contained in:
@@ -15,7 +15,96 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import nock from 'nock';
|
||||
/* eslint-disable no-underscore-dangle, jsdoc/require-jsdoc */
|
||||
import { MockAgent, Interceptable, fetch as undiciFetch } from 'undici';
|
||||
|
||||
export function getGlobalMockAgent(): MockAgent {
|
||||
if (!(global as any).__mockAgent__) {
|
||||
const agent = new MockAgent();
|
||||
agent.disableNetConnect();
|
||||
(global as any).__mockAgent__ = agent;
|
||||
}
|
||||
const agent: MockAgent = (global as any).__mockAgent__;
|
||||
(process as any).__test_fetch__ = (input: any, init?: any) => undiciFetch(input, { ...init, dispatcher: agent });
|
||||
return agent;
|
||||
}
|
||||
|
||||
export function resetGlobalMockAgent(): void {
|
||||
const agent = (global as any).__mockAgent__;
|
||||
if (agent) {
|
||||
agent.close();
|
||||
(global as any).__mockAgent__ = undefined;
|
||||
}
|
||||
delete (process as any).__test_fetch__;
|
||||
}
|
||||
|
||||
interface MockReplyChain {
|
||||
reply(statusCode: number, body?: any, headers?: Record<string, string>): void;
|
||||
}
|
||||
|
||||
interface MockQueryable {
|
||||
query(params: Record<string, string>): MockReplyChain;
|
||||
reply(statusCode: number, body?: any, headers?: Record<string, string>): void;
|
||||
}
|
||||
|
||||
interface MockInterceptor {
|
||||
get(path: string, body?: any): MockQueryable;
|
||||
post(path: string, body?: any): MockQueryable;
|
||||
put(path: string, body?: any): MockQueryable;
|
||||
delete(path: string, body?: any): MockQueryable;
|
||||
}
|
||||
|
||||
function buildQueryString(params: Record<string, string>): string {
|
||||
const sp = new URLSearchParams();
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
sp.append(key, value);
|
||||
}
|
||||
return sp.toString();
|
||||
}
|
||||
|
||||
// cspell:ignore Interceptable
|
||||
function createInterceptor(pool: Interceptable): MockInterceptor {
|
||||
const doIntercept = (method: string, path: string, body?: any): MockReplyChain => ({
|
||||
reply(statusCode: number, responseBody?: any, headers?: Record<string, string>) {
|
||||
const interceptOpts: any = { path, method };
|
||||
if (body && method !== 'GET' && method !== 'DELETE') {
|
||||
interceptOpts.body = typeof body === 'string' ? body : JSON.stringify(body);
|
||||
}
|
||||
const responseHeaders = { 'content-type': 'application/json', ...headers };
|
||||
const replyBody =
|
||||
responseBody === undefined || responseBody === ''
|
||||
? ''
|
||||
: typeof responseBody === 'string'
|
||||
? responseBody
|
||||
: JSON.stringify(responseBody);
|
||||
pool.intercept(interceptOpts).reply(statusCode, replyBody, { headers: responseHeaders });
|
||||
}
|
||||
});
|
||||
|
||||
const makeChain = (method: string, path: string, body?: any): MockQueryable => ({
|
||||
query(params: Record<string, string>): MockReplyChain {
|
||||
const qs = buildQueryString(params);
|
||||
const separator = path.includes('?') ? '&' : '?';
|
||||
return doIntercept(method, `${path}${separator}${qs}`, body);
|
||||
},
|
||||
reply(statusCode: number, responseBody?: any, headers?: Record<string, string>) {
|
||||
doIntercept(method, path, body).reply(statusCode, responseBody, headers);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
get: (path: string) => makeChain('GET', path),
|
||||
post: (path: string, body?: any) => makeChain('POST', path, body),
|
||||
put: (path: string, body?: any) => makeChain('PUT', path, body),
|
||||
delete: (path: string, body?: any) => makeChain('DELETE', path, body)
|
||||
};
|
||||
}
|
||||
|
||||
export function mockHost(host: string): MockInterceptor {
|
||||
const agent = getGlobalMockAgent();
|
||||
const pool = agent.get(host);
|
||||
return createInterceptor(pool);
|
||||
}
|
||||
|
||||
export class BaseMock {
|
||||
host: string;
|
||||
@@ -24,15 +113,17 @@ export class BaseMock {
|
||||
this.host = host || 'https://127.0.0.1:8080';
|
||||
}
|
||||
|
||||
put200GenericResponse(scriptSlug: string): void {
|
||||
nock(this.host, { encodedQueryParams: true }).put(scriptSlug).reply(200);
|
||||
protected mock(): MockInterceptor {
|
||||
return mockHost(this.host);
|
||||
}
|
||||
|
||||
play(): void {
|
||||
nock.recorder.play();
|
||||
put200GenericResponse(scriptSlug: string): void {
|
||||
this.mock().put(scriptSlug).reply(200);
|
||||
}
|
||||
|
||||
cleanAll(): void {
|
||||
nock.cleanAll();
|
||||
const agent = getGlobalMockAgent();
|
||||
const pool = agent.get(this.host) as Interceptable;
|
||||
pool.cleanMocks();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user