diff --git a/device/index.js b/device/index.js index 95913ae..314a57e 100644 --- a/device/index.js +++ b/device/index.js @@ -512,7 +512,8 @@ function DeviceClient(options) { } if (!isUndefined(options.host) && isUndefined(options.region)) { - var pattern = /[a-zA-Z0-9]+\.iot\.([a-z]+[-[a-z]+]*-[a-z]+-[0-9]+)\.amazonaws\..+/; + // extract anything in between "iot" and "amazonaws" as region + var pattern = /[a-zA-Z0-9]+\.iot\.(.*)\.amazonaws\..+/; var region = pattern.exec(options.host); if (region === null) { console.log('Host endpoint is not valid'); diff --git a/test/device-unit-tests.js b/test/device-unit-tests.js index 65a92d5..9bab280 100644 --- a/test/device-unit-tests.js +++ b/test/device-unit-tests.js @@ -616,6 +616,18 @@ describe( "device class unit tests", function() { ); }); }); + describe("device extract the region from host value", function () { + it("does not throw an exception", function () { + assert.doesNotThrow(function (err) { + var device = deviceModule({ + host: 'XXXX.iot.us-east-1.amazonaws.com', + protocol: 'wss' + }); + }, function (err) { console.log('\t[' + err + ']'); return true; } + ); + assert.equal(mqttSave.firstCall.args[1].region, "us-east-1"); + }); + }); describe( "device doesn't accept invalid timing parameters: baseReconnectTimeMs<1", function() { it("throws an exception", function() { assert.throws( function( err ) {