UNPKG

cumulocity-cypress

Version:
163 lines (159 loc) 6.07 kB
'use strict'; var httpcontroller = require('./httpcontroller-CSB0Sadq.js'); var _ = require('lodash'); require('util'); require('express'); require('raw-body'); require('cookie-parser'); require('winston'); require('morgan'); require('date-fns'); require('set-cookie-parser'); require('cookie'); require('cross-fetch'); require('buffer'); require('http-proxy-middleware'); require('fs'); require('path'); require('semver'); require('swagger-ui-express'); require('yaml'); require('debug'); /** * Converts the given URL to a string. * @param url The URL or RequestInfo to convert. * @returns The URL as a string. */ function toUrlString(url) { if (_.isString(url)) { return url; } else if (url instanceof URL) { return url.toString(); } else if (url instanceof Request) { return url.url; } else { throw new Error(`Type for URL not supported. Expected URL, string or Request, but found $'{typeof url}}'.`); } } /** * Converts the given object to a Cypress.Response. * @param obj The object to convert. * @param duration The duration of the request. * @param fetchOptions The fetch options used for the request. * @param url The URL of the request. * @param schema The schema of the response. */ function toCypressResponse(obj, duration = 0, fetchOptions = {}, url, schema) { if (!obj) return undefined; if (typeof httpcontroller.isPactRecord === "function" && httpcontroller.isPactRecord(obj)) { return obj.toCypressResponse(); } let fetchResponse; if (isIResult(obj)) { fetchResponse = obj.res; } else if (isWindowFetchResponse(obj)) { fetchResponse = obj; } else { fetchResponse = obj; } if ("responseObj" in fetchResponse) { return _.get(fetchResponse, "responseObj"); } return { status: fetchResponse.status, isOkStatusCode: fetchResponse.ok || (fetchResponse.status > 199 && fetchResponse.status < 300), statusText: fetchResponse.statusText, headers: Object.fromEntries(fetchResponse.headers || []), requestHeaders: fetchOptions.headers, duration: duration, ...(url && { url: toUrlString(url) }), allRequestResponses: [], body: fetchResponse.data, requestBody: fetchResponse.requestBody, method: fetchResponse.method || "GET", ...(schema && { $body: schema }), }; } /** * Checks if the given object is a window.Response. * @param obj The object to check. */ function isWindowFetchResponse(obj) { return (obj != null && _.isObjectLike(obj) && "status" in obj && "statusText" in obj && "headers" in obj && "body" in obj && "url" in obj && _.isFunction(_.get(obj, "json")) && _.isFunction(_.get(obj, "arrayBuffer"))); } /** * Checks if the given object is an IResult. * @param obj The object to check. */ function isIResult(obj) { return (obj != null && _.isObjectLike(obj) && "data" in obj && "res" in obj && isWindowFetchResponse(obj.res)); } /** * Checks if the given object is a CypressError. * @param error The object to check. * @returns True if the object is a CypressError, false otherwise. */ function isCypressError(error) { return _.isError(error) && _.get(error, "name") === "CypressError"; } exports.C8yDefaultPact = httpcontroller.C8yDefaultPact; exports.C8yDefaultPactMatcher = httpcontroller.C8yDefaultPactMatcher; exports.C8yDefaultPactPreprocessor = httpcontroller.C8yDefaultPactPreprocessor; exports.C8yDefaultPactRecord = httpcontroller.C8yDefaultPactRecord; exports.C8yISODateStringMatcher = httpcontroller.C8yISODateStringMatcher; exports.C8yIdentifierMatcher = httpcontroller.C8yIdentifierMatcher; exports.C8yIgnoreMatcher = httpcontroller.C8yIgnoreMatcher; exports.C8yNumberMatcher = httpcontroller.C8yNumberMatcher; exports.C8yPactBodyMatcher = httpcontroller.C8yPactBodyMatcher; exports.C8yPactHttpController = httpcontroller.C8yPactHttpController; exports.C8yPactHttpControllerDefaultMode = httpcontroller.C8yPactHttpControllerDefaultMode; exports.C8yPactHttpControllerDefaultRecordingMode = httpcontroller.C8yPactHttpControllerDefaultRecordingMode; exports.C8yPactHttpControllerLogLevel = httpcontroller.C8yPactHttpControllerLogLevel; exports.C8yPactModeValues = httpcontroller.C8yPactModeValues; exports.C8yPactObjectKeys = httpcontroller.C8yPactObjectKeys; exports.C8yPactPreprocessorDefaultOptions = httpcontroller.C8yPactPreprocessorDefaultOptions; exports.C8yPactRecordingModeValues = httpcontroller.C8yPactRecordingModeValues; exports.C8ySameTypeMatcher = httpcontroller.C8ySameTypeMatcher; exports.C8yStringMatcher = httpcontroller.C8yStringMatcher; exports.createPactRecord = httpcontroller.createPactRecord; exports.getCreatedObjectId = httpcontroller.getCreatedObjectId; exports.getEnvVar = httpcontroller.getEnvVar; exports.isCypressResponse = httpcontroller.isCypressResponse; exports.isOneOfStrings = httpcontroller.isOneOfStrings; exports.isPact = httpcontroller.isPact; exports.isPactError = httpcontroller.isPactError; exports.isPactRecord = httpcontroller.isPactRecord; exports.isValidPactId = httpcontroller.isValidPactId; exports.oauthLogin = httpcontroller.oauthLogin; exports.pactId = httpcontroller.pactId; exports.parseRegexReplace = httpcontroller.parseRegexReplace; exports.performRegexReplace = httpcontroller.performRegexReplace; exports.toPactRequest = httpcontroller.toPactRequest; exports.toPactResponse = httpcontroller.toPactResponse; exports.toPactSerializableObject = httpcontroller.toPactSerializableObject; exports.toSerializablePactRecord = httpcontroller.toSerializablePactRecord; exports.validatePactMode = httpcontroller.validatePactMode; exports.validatePactRecordingMode = httpcontroller.validatePactRecordingMode; exports.isCypressError = isCypressError; exports.isIResult = isIResult; exports.isWindowFetchResponse = isWindowFetchResponse; exports.toCypressResponse = toCypressResponse;