added all files to project

This commit is contained in:
2022-03-10 10:36:59 +01:00
parent 09dd957b33
commit 46a936d7de
25351 changed files with 3883356 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
/// <reference types="webdriverio/webdriverio-core" />
/// <reference types="node" />
/// <reference types="webdriverio" />
/// <reference types="@wdio/cucumber-framework" />
import Interception from '.';
declare type RequestOptions = {
requestId: string;
responseCode?: number;
responseHeaders?: Record<string, string>[];
body?: string | WebdriverIO.JsonCompatible;
errorReason?: string;
};
declare type ClientResponse = {
body: string;
base64Encoded?: boolean;
};
declare type Client = {
send: (requestName: string, requestOptions: RequestOptions) => Promise<ClientResponse>;
};
declare type Event = {
requestId: string;
request: WebdriverIO.Matches & {
mockedResponse: string | Buffer;
};
responseStatusCode?: number;
responseHeaders: Record<string, string>[];
};
export default class DevtoolsInterception extends Interception {
static handleRequestInterception(client: Client, mocks: Set<Interception>): (event: Event) => Promise<void | ClientResponse>;
get calls(): import("webdriverio").Matches[];
clear(): void;
restore(): void;
respond(overwrite: WebdriverIO.MockOverwrite, params?: WebdriverIO.MockResponseParams): void;
respondOnce(overwrite: WebdriverIO.MockOverwrite, params?: WebdriverIO.MockResponseParams): void;
abort(errorReason: string, sticky?: boolean): void;
abortOnce(errorReason: string): void;
}
export {};
//# sourceMappingURL=devtools.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"devtools.d.ts","sourceRoot":"","sources":["../../../src/utils/interception/devtools.ts"],"names":[],"mappings":";;;;AAMA,OAAO,YAAY,MAAM,GAAG,CAAA;AAM5B,aAAK,cAAc,GAAG;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;IAC3C,IAAI,CAAC,EAAE,MAAM,GAAG,WAAW,CAAC,cAAc,CAAC;IAC3C,WAAW,CAAC,EAAE,MAAM,CAAC;CACxB,CAAA;AAED,aAAK,cAAc,GAAG;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,CAAC,EAAE,OAAO,CAAA;CAC1B,CAAA;AAED,aAAK,MAAM,GAAG;IACV,IAAI,EAAE,CAAC,WAAW,EAAE,MAAM,EAAE,cAAc,EAAE,cAAc,KAAK,OAAO,CAAC,cAAc,CAAC,CAAC;CAC1F,CAAA;AAED,aAAK,KAAK,GAAG;IACT,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,WAAW,CAAC,OAAO,GAAG;QAAE,cAAc,EAAE,MAAM,GAAG,MAAM,CAAC;KAAE,CAAC;IACpE,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC;CAC7C,CAAA;AAID,MAAM,CAAC,OAAO,OAAO,oBAAqB,SAAQ,YAAY;IAC1D,MAAM,CAAC,yBAAyB,CAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC,IAAI,GAAG,cAAc,CAAC;IAiJ7H,IAAI,KAAK,oCAER;IAKD,KAAK;IAQL,OAAO;IAUP,OAAO,CAAE,SAAS,EAAE,WAAW,CAAC,aAAa,EAAE,MAAM,GAAE,WAAW,CAAC,kBAAuB;IAS1F,WAAW,CAAE,SAAS,EAAE,WAAW,CAAC,aAAa,EAAE,MAAM,GAAE,WAAW,CAAC,kBAAuB;IAQ9F,KAAK,CAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAE,OAAc;IAWlD,SAAS,CAAE,WAAW,EAAE,MAAM;CAGjC"}
+184
View File
@@ -0,0 +1,184 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const atob_1 = __importDefault(require("atob"));
const minimatch_1 = __importDefault(require("minimatch"));
const logger_1 = __importDefault(require("@wdio/logger"));
const _1 = __importDefault(require("."));
const __1 = require("..");
const constants_1 = require("../../constants");
const log = logger_1.default('webdriverio');
class DevtoolsInterception extends _1.default {
static handleRequestInterception(client, mocks) {
return async (event) => {
const isRequest = !event.responseHeaders;
const eventResponseHeaders = event.responseHeaders || [];
const responseHeaders = eventResponseHeaders.reduce((headers, { name, value }) => {
headers[name] = value;
return headers;
}, {});
const { requestId, request, responseStatusCode = 200 } = event;
for (const mock of mocks) {
if (isRequest && (mock.respondOverwrites.length === 0 ||
(!mock.respondOverwrites[0].errorReason &&
mock.respondOverwrites[0].params &&
mock.respondOverwrites[0].params.fetchResponse !== false))) {
continue;
}
if (!minimatch_1.default(request.url, mock.url)) {
continue;
}
request.statusCode = responseStatusCode;
request.responseHeaders = { ...responseHeaders };
if (filterMethod(request.method, mock.filterOptions.method) ||
filterHeaders(request.headers, mock.filterOptions.requestHeaders) ||
filterHeaders(responseHeaders, mock.filterOptions.headers) ||
filterRequest(request.postData, mock.filterOptions.postData) ||
filterStatusCode(responseStatusCode, mock.filterOptions.statusCode)) {
continue;
}
const { body, base64Encoded = undefined } = isRequest ? { body: '' } : await client.send('Fetch.getResponseBody', { requestId }).catch(() => ({}));
request.body = base64Encoded ? atob_1.default(body) : body;
const contentTypeHeader = Object.keys(responseHeaders).find(h => h.toLowerCase() === 'content-type') || '';
const responseContentType = responseHeaders[contentTypeHeader];
request.body = responseContentType && responseContentType.includes('application/json')
? tryParseJson(request.body)
: request.body;
mock.matches.push(request);
if (mock.respondOverwrites.length === 0) {
continue;
}
const { errorReason, overwrite, params = {} } = mock.respondOverwrites[0].sticky
? mock.respondOverwrites[0]
: mock.respondOverwrites.shift() || {};
if (overwrite !== undefined) {
let newBody = overwrite;
if (typeof overwrite === 'function') {
newBody = await overwrite(request, client);
}
const isBodyUndefined = typeof newBody === 'undefined';
if (isBodyUndefined) {
newBody = '';
}
if (typeof newBody !== 'string') {
newBody = JSON.stringify(newBody);
}
let responseCode = typeof params.statusCode === 'function' ? params.statusCode(request) : params.statusCode || responseStatusCode;
let responseHeaders = [
...eventResponseHeaders,
...Object.entries(typeof params.headers === 'function' ? params.headers(request) : params.headers || {}).map(([name, value]) => ({ name, value }))
];
const responseFilePath = path_1.default.isAbsolute(newBody) ? newBody : path_1.default.join(process.cwd(), newBody);
if (newBody.length > 0 && await fs_extra_1.default.pathExists(responseFilePath) && await canAccess(responseFilePath)) {
newBody = await fs_extra_1.default.readFile(responseFilePath);
}
else if (newBody.startsWith('http')) {
responseCode = 301;
responseHeaders = responseHeaders.filter(({ name }) => name.toLowerCase() !== 'location');
responseHeaders.push({ name: 'Location', value: newBody });
}
request.mockedResponse = newBody;
return client.send('Fetch.fulfillRequest', {
requestId,
responseCode,
responseHeaders,
body: isBodyUndefined ? undefined : (newBody instanceof Buffer ? newBody : Buffer.from(newBody, 'utf8')).toString('base64')
}).catch(logFetchError);
}
if (errorReason) {
return client.send('Fetch.failRequest', {
requestId,
errorReason
}).catch(logFetchError);
}
}
return client.send('Fetch.continueRequest', { requestId }).catch(logFetchError);
};
}
get calls() {
return this.matches;
}
clear() {
this.matches = [];
}
restore() {
this.clear();
this.respondOverwrites = [];
}
respond(overwrite, params = {}) {
this.respondOverwrites.push({ overwrite, params, sticky: true });
}
respondOnce(overwrite, params = {}) {
this.respondOverwrites.push({ overwrite, params });
}
abort(errorReason, sticky = true) {
if (typeof errorReason !== 'string' || !constants_1.ERROR_REASON.includes(errorReason)) {
throw new Error(`Invalid value for errorReason, allowed are: ${constants_1.ERROR_REASON.join(', ')}`);
}
this.respondOverwrites.push({ errorReason, sticky });
}
abortOnce(errorReason) {
this.abort(errorReason, false);
}
}
exports.default = DevtoolsInterception;
const filterMethod = (method, expected) => {
if (typeof expected === 'undefined') {
return false;
}
if (typeof expected === 'function') {
return expected(method) !== true;
}
return expected.toLowerCase() !== method.toLowerCase();
};
const filterHeaders = (responseHeaders, expected) => {
if (typeof expected === 'undefined') {
return false;
}
if (typeof expected === 'function') {
return expected(responseHeaders) !== true;
}
return !__1.containsHeaderObject(responseHeaders, expected);
};
const filterRequest = (postData, expected) => {
if (typeof expected === 'undefined') {
return false;
}
if (typeof expected === 'function') {
return expected(postData) !== true;
}
return postData !== expected;
};
const filterStatusCode = (statusCode, expected) => {
if (typeof expected === 'undefined') {
return false;
}
if (typeof expected === 'function') {
return expected(statusCode) !== true;
}
return statusCode !== expected;
};
const canAccess = async (filepath) => {
try {
await fs_extra_1.default.access(filepath);
return true;
}
catch (_a) {
return false;
}
};
const tryParseJson = (body) => {
try {
return JSON.parse(body) || body;
}
catch (_a) {
return body;
}
};
const logFetchError = (err) => {
log.debug(err === null || err === void 0 ? void 0 : err.message);
};
+19
View File
@@ -0,0 +1,19 @@
/// <reference types="webdriverio/webdriverio-core" />
/// <reference types="webdriverio" />
/// <reference types="@wdio/cucumber-framework" />
export default class Interception {
url: string;
filterOptions: WebdriverIO.MockFilterOptions;
browser: WebdriverIO.BrowserObject;
respondOverwrites: {
overwrite?: WebdriverIO.MockOverwrite;
params?: WebdriverIO.MockResponseParams;
sticky?: boolean;
errorReason?: string;
}[];
matches: WebdriverIO.Matches[];
constructor(url: string, filterOptions: import("webdriverio").MockFilterOptions | undefined, browser: WebdriverIO.BrowserObject);
get calls(): WebdriverIO.Matches[] | Promise<WebdriverIO.Matches[]>;
waitForResponse({ timeout, interval, timeoutMsg, }?: WebdriverIO.WaitForOptions): Promise<boolean>;
}
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/interception/index.ts"],"names":[],"mappings":";;;AAEA,MAAM,CAAC,OAAO,OAAO,YAAY;IAC7B,GAAG,EAAE,MAAM,CAAA;IACX,aAAa,EAAE,WAAW,CAAC,iBAAiB,CAAA;IAC5C,OAAO,EAAE,WAAW,CAAC,aAAa,CAAA;IAClC,iBAAiB,EAAE;QACf,SAAS,CAAC,EAAE,WAAW,CAAC,aAAa,CAAA;QACrC,MAAM,CAAC,EAAE,WAAW,CAAC,kBAAkB,CAAA;QACvC,MAAM,CAAC,EAAE,OAAO,CAAA;QAChB,WAAW,CAAC,EAAE,MAAM,CAAA;KACvB,EAAE,CAAK;IACR,OAAO,EAAE,WAAW,CAAC,OAAO,EAAE,CAAK;gBAEtB,GAAG,EAAE,MAAM,EAAE,aAAa,qDAAoC,EAAE,OAAO,EAAE,WAAW,CAAC,aAAa;IAM/G,IAAI,KAAK,IAAK,WAAW,CAAC,OAAO,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC,CAEnE;IAED,eAAe,CAAE,EACb,OAA6C,EAC7C,QAA+C,EAC/C,UAAU,GACb,GAAE,WAAW,CAAC,cAAmB;CA2BrC"}
+38
View File
@@ -0,0 +1,38 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Timer_1 = __importDefault(require("../Timer"));
class Interception {
constructor(url, filterOptions = {}, browser) {
this.respondOverwrites = [];
this.matches = [];
this.url = url;
this.filterOptions = filterOptions;
this.browser = browser;
}
get calls() {
throw new Error('Implement me');
}
waitForResponse({ timeout = this.browser.options.waitforTimeout, interval = this.browser.options.waitforInterval, timeoutMsg, } = {}) {
if (typeof timeout !== 'number') {
timeout = this.browser.options.waitforTimeout;
}
if (typeof interval !== 'number') {
interval = this.browser.options.waitforInterval;
}
const fn = async () => this.calls && (await this.calls).length > 0;
const timer = new Timer_1.default(interval, timeout, fn, true);
return this.browser.call(() => timer.catch((e) => {
if (e.message === 'timeout') {
if (typeof timeoutMsg === 'string') {
throw new Error(timeoutMsg);
}
throw new Error(`waitForResponse timed out after ${timeout}ms`);
}
throw new Error(`waitForResponse failed with the following reason: ${(e && e.message) || e}`);
}));
}
}
exports.default = Interception;
+16
View File
@@ -0,0 +1,16 @@
/// <reference types="webdriverio/webdriverio-core" />
/// <reference types="webdriverio" />
/// <reference types="@wdio/cucumber-framework" />
import Interception from '.';
export default class WebDriverInterception extends Interception {
mockId?: string;
init(): Promise<void>;
get calls(): Promise<import("webdriverio").Matches[]>;
clear(): Promise<void>;
restore(): Promise<void>;
respond(overwrite: WebdriverIO.MockOverwrite, params?: WebdriverIO.MockResponseParams): Promise<void>;
respondOnce(overwrite: WebdriverIO.MockOverwrite, params?: WebdriverIO.MockResponseParams): Promise<void>;
abort(errorReason: string, sticky?: boolean): Promise<void>;
abortOnce(errorReason: string): Promise<void>;
}
//# sourceMappingURL=webdriver.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"webdriver.d.ts","sourceRoot":"","sources":["../../../src/utils/interception/webdriver.ts"],"names":[],"mappings":";;;AAAA,OAAO,YAAY,MAAM,GAAG,CAAA;AAS5B,MAAM,CAAC,OAAO,OAAO,qBAAsB,SAAQ,YAAY;IAC3D,MAAM,CAAC,EAAE,MAAM,CAAC;IAEV,IAAI;IAQV,IAAI,KAAK,6CAIR;IAKD,KAAK;IASL,OAAO;IAUP,OAAO,CAAE,SAAS,EAAE,WAAW,CAAC,aAAa,EAAE,MAAM,GAAE,WAAW,CAAC,kBAAuB;IAc1F,WAAW,CAAE,SAAS,EAAE,WAAW,CAAC,aAAa,EAAE,MAAM,GAAE,WAAW,CAAC,kBAAuB;IAa9F,KAAK,CAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAE,OAAc;IAgBlD,SAAS,CAAE,WAAW,EAAE,MAAM;CAGjC"}
+38
View File
@@ -0,0 +1,38 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = __importDefault(require("."));
const constants_1 = require("../../constants");
class WebDriverInterception extends _1.default {
async init() {
const { mockId } = await this.browser.mockRequest(this.url, this.filterOptions);
this.mockId = mockId;
}
get calls() {
return this.browser.call(async () => this.browser.getMockCalls(this.mockId));
}
clear() {
return this.browser.call(async () => this.browser.clearMockCalls(this.mockId));
}
restore() {
return this.browser.call(async () => this.browser.clearMockCalls(this.mockId, true));
}
respond(overwrite, params = {}) {
return this.browser.call(async () => this.browser.respondMock(this.mockId, { overwrite, params, sticky: true }));
}
respondOnce(overwrite, params = {}) {
return this.browser.call(async () => this.browser.respondMock(this.mockId, { overwrite, params }));
}
abort(errorReason, sticky = true) {
if (typeof errorReason !== 'string' || !constants_1.ERROR_REASON.includes(errorReason)) {
throw new Error(`Invalid value for errorReason, allowed are: ${constants_1.ERROR_REASON.join(', ')}`);
}
return this.browser.call(async () => this.browser.respondMock(this.mockId, { errorReason, sticky }));
}
abortOnce(errorReason) {
return this.abort(errorReason, false);
}
}
exports.default = WebDriverInterception;