Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poa tls changes #1406

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/sdk-utils/src/request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import percy from './percy-info.js';
import logger from './logger.js';

// Helper to send a request to the local CLI API
export async function request(path, options = {}) {
Expand Down Expand Up @@ -53,6 +54,9 @@ if (process.env.__PERCY_BROWSERIFIED__) {
// rollup throws error for -> await import(protocol === 'https:' ? 'https' : 'http')
let { default: http } = protocol === 'https:' ? await import('https') : await import('http');

const log = logger('request');
log.debug(`Request object for url: ${url},\nRequest object: ${JSON.stringify(options)}`);

return new Promise((resolve, reject) => {
http.request(url, options)
.on('response', response => {
Expand Down
23 changes: 19 additions & 4 deletions packages/webdriver-utils/src/driver.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import utils from '@percy/sdk-utils';
import Cache from './util/cache.js';
import httpsAgent from './util/utils.js';
const { request } = utils;
const log = utils.logger('webdriver-utils:driver');

Expand All @@ -13,8 +14,11 @@ export default class Driver {
async getCapabilites() {
return await Cache.withCache(Cache.caps, this.sessionId, async () => {
try {
const options = {
agent: httpsAgent()
};
const baseUrl = `${this.executorUrl}/session/${this.sessionId}`;
const caps = JSON.parse((await request(baseUrl)).body);
const caps = JSON.parse((await request(baseUrl, options)).body);
return caps.value;
} catch (err) {
log.warn(`Falling back to legacy protocol, Error: ${err.message}`);
Expand All @@ -24,8 +28,11 @@ export default class Driver {
}

async getWindowSize() {
const options = {
agent: httpsAgent()
};
const baseUrl = `${this.executorUrl}/session/${this.sessionId}/window/current/size`;
const windowSize = JSON.parse((await request(baseUrl)).body);
const windowSize = JSON.parse((await request(baseUrl, options)).body);
return windowSize;
}

Expand All @@ -48,6 +55,7 @@ export default class Driver {
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
agent: httpsAgent(),
body: JSON.stringify(command)
};
const baseUrl = `${this.executorUrl}/session/${this.sessionId}/execute/sync`;
Expand All @@ -56,14 +64,20 @@ export default class Driver {
}

async takeScreenshot() {
const options = {
agent: httpsAgent()
};
const baseUrl = `${this.executorUrl}/session/${this.sessionId}/screenshot`;
const screenShot = JSON.parse((await request(baseUrl)).body);
const screenShot = JSON.parse((await request(baseUrl, options)).body);
return screenShot.value;
}

async rect(elementId) {
const options = {
agent: httpsAgent()
};
const baseUrl = `${this.executorUrl}/session/${this.sessionId}/element/${elementId}/rect`;
const response = JSON.parse((await request(baseUrl)).body);
const response = JSON.parse((await request(baseUrl, options)).body);
return response.value;
}

Expand All @@ -73,6 +87,7 @@ export default class Driver {
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
agent: httpsAgent(),
body: JSON.stringify({ using, value })
};
const baseUrl = `${this.executorUrl}/session/${this.sessionId}/element`;
Expand Down
3 changes: 2 additions & 1 deletion packages/webdriver-utils/src/providers/genericProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import MetaDataResolver from '../metadata/metaDataResolver.js';
import Tile from '../util/tile.js';
import Driver from '../driver.js';
import Cache from '../util/cache.js';
import httpsAgent from '../util/utils.js';
const { request } = utils;

const DEVICES_CONFIG_URL = 'https://storage.googleapis.com/percy-utils/devices.json';
Expand Down Expand Up @@ -272,7 +273,7 @@ export default class GenericProvider {
async getHeaderFooter(deviceName, osVersion, browserName) {
// passing 0 as key, since across different pages and tests, this config will remain same
const devicesConfig = await Cache.withCache(Cache.devicesConfig, 0, async () => {
return (await request(DEVICES_CONFIG_URL)).body;
return (await request(DEVICES_CONFIG_URL, httpsAgent())).body;
});
let deviceKey = `${deviceName}-${osVersion}`;
return devicesConfig[deviceKey]
Expand Down
10 changes: 10 additions & 0 deletions packages/webdriver-utils/src/util/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import https from 'https';

export function httpsAgent() {
return new https.Agent({
minVersion: 'TLSv1.2',
maxVersion: 'TLSv1.2'
});
}

export default httpsAgent;
24 changes: 16 additions & 8 deletions packages/webdriver-utils/test/driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ describe('Driver', () => {
describe('getCapabilities', () => {
it('calls requests', async () => {
let res = await driver.getCapabilites();
expect(requestSpy).toHaveBeenCalledOnceWith(`${executorUrl}/session/${sessionId}`, Object({}));
expect(requestSpy).toHaveBeenCalledWith(
`${executorUrl}/session/${sessionId}`,
{ agent: jasmine.any(Object) }
);
expect(res).toBe('mockVal');
});
});
Expand All @@ -56,7 +59,9 @@ describe('Driver', () => {

it('falls back to passed capabilites', async () => {
let res = await newDriver.getCapabilites();
expect(requestFailedSpy).toHaveBeenCalledOnceWith(`${executorUrl}/session/${sessionId}`, Object({}));
expect(requestFailedSpy).toHaveBeenCalledOnceWith(`${executorUrl}/session/${sessionId}`,
{ agent: jasmine.any(Object) }
);
expect(res).toBe(passedCapabilities);
});
});
Expand All @@ -66,7 +71,7 @@ describe('Driver', () => {
let res = await driver.getWindowSize();
expect(requestSpy).toHaveBeenCalledOnceWith(
`${executorUrl}/session/${sessionId}/window/current/size`,
Object({}));
{ agent: jasmine.any(Object) });
expect(res).toEqual({ value: 'mockVal' });
});
});
Expand All @@ -83,7 +88,8 @@ describe('Driver', () => {
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(command)
body: JSON.stringify(command),
agent: jasmine.any(Object)
}
);
expect(res).toEqual({ value: 'mockVal' });
Expand All @@ -100,7 +106,8 @@ describe('Driver', () => {
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify(command)
body: JSON.stringify(command),
agent: jasmine.any(Object)
}
);
expect(res).toEqual({ value: 'mockVal' });
Expand All @@ -119,7 +126,7 @@ describe('Driver', () => {
let res = await driver.takeScreenshot();
expect(requestSpy).toHaveBeenCalledOnceWith(
`${executorUrl}/session/${sessionId}/screenshot`,
Object({}));
{ agent: jasmine.any(Object) });
expect(res).toEqual('mockVal');
});
});
Expand All @@ -136,7 +143,8 @@ describe('Driver', () => {
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify({ using, value: value })
body: JSON.stringify({ using, value: value }),
agent: jasmine.any(Object)
}
);
expect(res).toEqual('mockVal');
Expand All @@ -157,7 +165,7 @@ describe('Driver', () => {
let res = await driver.rect(elementId);
expect(requestSpy).toHaveBeenCalledOnceWith(
`${executorUrl}/session/${sessionId}/element/${elementId}/rect`,
Object({}));
{ agent: jasmine.any(Object) });
expect(res).toEqual('mockVal');
});
});
Expand Down
Loading