From 109d1d6b985c0612401d7a6cccebe03770867637 Mon Sep 17 00:00:00 2001 From: Mike Kalinin Date: Wed, 18 Apr 2018 19:06:03 +0300 Subject: [PATCH] run testcafe with https protocol (#1985) --- src/client/utils/url.js | 5 ++++- src/proxy/index.js | 26 +++++++++++++++++++------- src/request-pipeline/context.js | 2 ++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/client/utils/url.js b/src/client/utils/url.js index c9e2ea01ea..b25ba7cace 100644 --- a/src/client/utils/url.js +++ b/src/client/utils/url.js @@ -25,9 +25,12 @@ export function getProxyUrl (url, opts) { /*eslint-disable no-restricted-properties*/ const proxyHostname = opts && opts.proxyHostname || location.hostname; const proxyPort = opts && opts.proxyPort || location.port.toString(); + const protocol = opts && opts.proxyProtocol || location.protocol; /*eslint-enable no-restricted-properties*/ - const proxyProtocol = parsedResourceType.isWebSocket ? 'ws:' : void 0; + const webSocketProtocol = protocol === 'https:' ? 'wss:' : 'ws:'; + const proxyProtocol = parsedResourceType.isWebSocket ? webSocketProtocol : protocol; + const sessionId = opts && opts.sessionId || settings.get().sessionId; let charset = opts && opts.charset; let reqOrigin = opts && opts.reqOrigin; diff --git a/src/proxy/index.js b/src/proxy/index.js index 5e4bf80961..77309c096d 100644 --- a/src/proxy/index.js +++ b/src/proxy/index.js @@ -1,5 +1,6 @@ import Router from './router'; import http from 'http'; +import https from 'https'; import * as urlUtils from '../utils/url'; import { readSync as read } from 'read-file-relative'; import { respond204, respond500, respondWithJSON, fetchBody, preventCaching } from '../utils/http'; @@ -22,26 +23,36 @@ function parseAsJson (msg) { } } -function createServerInfo (hostname, port, crossDomainPort) { +function createServerInfo (hostname, port, crossDomainPort, protocol) { return { hostname: hostname, port: port, crossDomainPort: crossDomainPort, - domain: `http://${hostname}:${port}` + protocol: protocol, + domain: `${protocol}//${hostname}:${port}` }; } // Proxy export default class Proxy extends Router { - constructor (hostname, port1, port2) { + constructor (hostname, port1, port2, options) { super(); this.openSessions = {}; - this.server1Info = createServerInfo(hostname, port1, port2); - this.server2Info = createServerInfo(hostname, port2, port1); - this.server1 = http.createServer((req, res) => this._onRequest(req, res, this.server1Info)); - this.server2 = http.createServer((req, res) => this._onRequest(req, res, this.server2Info)); + options = options || {}; + + const serverProvider = options.ssl ? https : http; + const protocol = options.ssl ? 'https:' : 'http:'; + const serverOptions = { + key: options.key || '', + cert: options.cert || '' + }; + + this.server1Info = createServerInfo(hostname, port1, port2, protocol); + this.server2Info = createServerInfo(hostname, port2, port1, protocol); + this.server1 = serverProvider.createServer(serverOptions, (req, res) => this._onRequest(req, res, this.server1Info)); + this.server2 = serverProvider.createServer(serverOptions, (req, res) => this._onRequest(req, res, this.server2Info)); this.server1.on('upgrade', (req, socket, head) => this._onUpgradeRequest(req, socket, head, this.server1Info)); this.server2.on('upgrade', (req, socket, head) => this._onUpgradeRequest(req, socket, head, this.server2Info)); @@ -171,6 +182,7 @@ export default class Proxy extends Router { return urlUtils.getProxyUrl(url, { proxyHostname: this.server1Info.hostname, proxyPort: this.server1Info.port, + proxyProtocol: this.server1Info.protocol, sessionId: session.id }); } diff --git a/src/request-pipeline/context.js b/src/request-pipeline/context.js index b096b126a9..43f2ded38f 100644 --- a/src/request-pipeline/context.js +++ b/src/request-pipeline/context.js @@ -249,11 +249,13 @@ export default class RequestPipelineContext { toProxyUrl (url, isCrossDomain, resourceType, charset) { const proxyHostname = this.serverInfo.hostname; + const proxyProtocol = this.serverInfo.protocol; const proxyPort = isCrossDomain ? this.serverInfo.crossDomainPort : this.serverInfo.port; const sessionId = this.session.id; return urlUtils.getProxyUrl(url, { proxyHostname, + proxyProtocol, proxyPort, sessionId, resourceType,