diff --git a/e2etests/test/device_method.js b/e2etests/test/device_method.js index c8003e3ea..8c571a515 100644 --- a/e2etests/test/device_method.js +++ b/e2etests/test/device_method.js @@ -6,7 +6,9 @@ var Registry = require('azure-iothub').Registry; var ServiceClient = require('azure-iothub').Client; var ConnectionString = require('azure-iothub').ConnectionString; +var SharedAccessSignature = require('azure-iothub').SharedAccessSignature; var deviceSdk = require('azure-iot-device'); +var anHourFromNow = require('azure-iot-common').anHourFromNow; var util = require('util'); var deviceMqtt = require('azure-iot-device-mqtt').Mqtt; var uuid = require('uuid'); @@ -96,7 +98,7 @@ module.exports = function(hubConnectionString) { }); }; - var sendMethodCall = function(testPayload, done) { + var sendMethodCall = function(serviceClient, testPayload, done) { setTimeout(function() { // make the method call via the service var methodParams = { @@ -106,9 +108,7 @@ module.exports = function(hubConnectionString) { }; debug('service sending method call:'); debug(JSON.stringify(methodParams, null, 2)); - ServiceClient - .fromConnectionString(hubConnectionString) - .invokeDeviceMethod( + serviceClient.invokeDeviceMethod( deviceDescription.deviceId, methodParams, function(err, result) { @@ -125,18 +125,29 @@ module.exports = function(hubConnectionString) { }; [null, '', 'foo', { k1: 'v1' }, {}].forEach(function(testPayload) { - it('makes and receives a method call', function(done) { + it('makes and receives a method call with ' + JSON.stringify(testPayload), function(done) { setMethodHandler(testPayload); - sendMethodCall(testPayload,done); + var serviceClient = ServiceClient.fromConnectionString(hubConnectionString); + sendMethodCall(serviceClient, testPayload, done); }); }); + it('makes and receives a method call when the client is created with a Shared Access Signature', function(done) { + var testPayload = 'foo'; + var cn = ConnectionString.parse(hubConnectionString); + var sas = SharedAccessSignature.create(cn.HostName, cn.SharedAccessKeyName, cn.SharedAccessKey, anHourFromNow()); + var serviceClient = ServiceClient.fromSharedAccessSignature(sas); + setMethodHandler(testPayload); + sendMethodCall(serviceClient, testPayload, done); + }); + it('makes and receives a method call after renewing the SAS token', function(done) { var testPayload = {'k1' : 'v1'}; setMethodHandler(testPayload); deviceClient.on('_sharedAccessSignatureUpdated', function() { setTimeout(function() { - sendMethodCall(testPayload, done); + var serviceClient = ServiceClient.fromConnectionString(hubConnectionString); + sendMethodCall(serviceClient, testPayload, done); }, 1000); }); deviceClient._renewSharedAccessSignature(); diff --git a/service/lib/client.js b/service/lib/client.js index 590b8dc6e..115070d67 100644 --- a/service/lib/client.js +++ b/service/lib/client.js @@ -110,7 +110,7 @@ Client.fromSharedAccessSignature = function fromSharedAccessSignature(sharedAcce }; /*Codes_SRS_NODE_IOTHUB_CLIENT_05_007: [The fromSharedAccessSignature method shall return a new instance of the Client object, as by a call to new Client(transport).]*/ - return new Client(new Transport(config)); + return new Client(new Transport(config), new RestApiClient(config)); }; Client.prototype._disconnectHandler = function (reason) { diff --git a/service/test/_client_test.js b/service/test/_client_test.js index 04a9fe865..9544669e2 100644 --- a/service/test/_client_test.js +++ b/service/test/_client_test.js @@ -40,6 +40,7 @@ describe('Client', function () { it('returns an instance of Client', function () { var client = Client.fromConnectionString(connStr); assert.instanceOf(client, Client); + assert.isOk(client._restApiClient); }); }); @@ -71,6 +72,7 @@ describe('Client', function () { it('returns an instance of Client', function () { var client = Client.fromSharedAccessSignature(token); assert.instanceOf(client, Client); + assert.isOk(client._restApiClient); }); });