UNPKG

@serenity-js/rest

Version:

Serenity/JS Screenplay Pattern library for interacting with REST and other HTTP-based services, supporting comprehensive API testing and blended testing scenarios

66 lines 1.84 kB
import { createUrl } from './createUrl.js'; export class ProxyBypass { static create(value) { if (value === undefined || value === '') { return new BypassNone(); } if (value === '*') { return new BypassAll(); } return new BypassMatching(value); } } class BypassNone extends ProxyBypass { matches(url_) { return false; } } class BypassAll extends ProxyBypass { matches(url_) { return true; } } class BypassHostnamePattern extends ProxyBypass { hostname; port; static template = /^((?<hostname>[^:]+)(:(?<port>\d+))?)$/; static defaultPorts = { 'ftp:': '21', 'gopher:': '70', 'http:': '80', 'https:': '443', 'ws:': '80', 'wss:': '443', }; static create(patternConfig) { const { hostname, port } = this.template.exec(patternConfig)?.groups || {}; return new BypassHostnamePattern(hostname, port); } constructor(hostname, port) { super(); this.hostname = hostname; this.port = port; } matches(url) { const urlPort = url.port || BypassHostnamePattern.defaultPorts[url.protocol]; return url.hostname.endsWith(this.hostname) && (this.port ? urlPort === this.port : true); } } class BypassMatching extends ProxyBypass { patterns; constructor(value) { super(); this.patterns = value.split(',').map(patternConfig => BypassHostnamePattern.create(patternConfig.trim())); } matches(url) { const normalisedUrl = createUrl(url); for (const pattern of this.patterns) { if (pattern.matches(normalisedUrl)) { return true; } } return false; } } //# sourceMappingURL=ProxyBypass.js.map