Repository: saucelabs/node-saucelabs
Pull request #54 (2019-03-25 15:34:40)

Add strictSSL option to requests
See https://github.com/webdriverio/webdriverio/pull/3758


Patch:
diff --git a/src/index.js b/src/index.js
index 00317fc..b58beff 100644
--- a/src/index.js
+++ b/src/index.js
@@ -6,7 +6,7 @@ import changeCase from 'change-case'
 
 import {
     createHMAC, getSauceEndpoint, toString, getParameters,
-    isValidType, getErrorReason
+    isValidType, getErrorReason, getStrictSsl
 } from './utils'
 import {
     PROTOCOL_MAP, DEFAULT_OPTIONS, SYMBOL_INSPECT, SYMBOL_TOSTRING,
@@ -151,6 +151,7 @@ export default class SauceLabs {
                 [method === 'get' ? 'qs' : 'body']: body,
                 json: true,
                 auth: this._auth,
+                strictSSL: getStrictSsl(),
                 useQuerystring: true
             }, (err, response, body) => {
                 /* istanbul ignore if */
@@ -182,6 +183,7 @@ export default class SauceLabs {
         return new Promise((resolve, reject) => {
             const req = request({
                 method: 'GET',
+                strictSSL: getStrictSsl(),
                 uri: `${host}/jobs/${jobId}/${assetName}?ts=${Date.now()}&auth=${hmac}`
             }, (err, res, body) => {
                 /**
diff --git a/src/utils.js b/src/utils.js
index 0615f4a..ef50216 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -122,3 +122,11 @@ export function getErrorReason (body = {}) {
 
     return body.message || body.detail || 'unknown'
 }
+
+/**
+ * If the environment variable "STRICT_SSL" is defined as "false", it doesn't require SSL certificates to be valid.
+ * This is used in requests to define the value of the "strictSSL" option.
+ */
+export function getStrictSsl() {
+    return !(process.env.STRICT_SSL === 'false' || process.env.strict_ssl === 'false')
+}


Test patch:
diff --git a/tests/utils.test.js b/tests/utils.test.js
index 76c1168..3450589 100644
--- a/tests/utils.test.js
+++ b/tests/utils.test.js
@@ -1,4 +1,4 @@
-import { createHMAC, getSauceEndpoint, toString, isValidType, getErrorReason } from '../src/utils'
+import { createHMAC, getSauceEndpoint, toString, isValidType, getErrorReason, getStrictSsl } from '../src/utils'
 
 test('createHMAC', async () => {
     expect(await createHMAC('foo', 'bar', 'loo123'))
@@ -51,3 +51,44 @@ test('getErrorReason', () => {
     expect(getErrorReason('foobarloo')).toBe('foobarloo')
     expect(getErrorReason(['some reason', 'some other reason'])).toBe('some reason')
 })
+
+describe('getStrictSsl', () => {
+    beforeEach(function() {
+        delete process.env.STRICT_SSL
+        delete process.env.strict_ssl
+    })
+
+    it('should return "false" when environment variable "STRICT_SSL" is defined with value "false"', () => {
+        process.env['STRICT_SSL'] = 'false'
+        expect(getStrictSsl()).toEqual(false)
+    })
+
+    it('should return "false" when environment variable "strict_ssl" is defined with value "false"', () => {
+        process.env['strict_ssl'] = 'false'
+        expect(getStrictSsl()).toEqual(false)
+    })
+
+    it('should return "true" when environment variable "STRICT_SSL" is defined with value "true"', () => {
+        process.env['STRICT_SSL'] = 'true'
+        expect(getStrictSsl()).toEqual(true)
+    })
+
+    it('should return "true" when environment variable "strict_ssl" is defined with value "true"', () => {
+        process.env['strict_ssl'] = 'true'
+        expect(getStrictSsl()).toEqual(true)
+    })
+
+    it('should return "true" when environment variable "STRICT_SSL" / "strict_ssl" is not defined', () => {
+        expect(getStrictSsl()).toEqual(true)
+    })
+
+    it('should return "true" when environment variable "STRICT_SSL" is defined with any other value than "false"', () => {
+        process.env['STRICT_SSL'] = 'foo'
+        expect(getStrictSsl()).toEqual(true)
+    })
+
+    it('should return "true" when environment variable "strict_ssl" is defined with any other value than "false"', () => {
+        process.env['strict_ssl'] = 'foo'
+        expect(getStrictSsl()).toEqual(true)
+    })
+})