Skip to content

Commit

Permalink
Fix for iothub-explorer#7: Client.fromSharedAccessSignature does not …
Browse files Browse the repository at this point in the history
…initialize the REST API wrapper
  • Loading branch information
Pierre Cauchois committed Jan 23, 2017
1 parent a46c4a1 commit 672bd8c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
25 changes: 18 additions & 7 deletions e2etests/test/device_method.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 = {
Expand All @@ -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) {
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion service/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions service/test/_client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 672bd8c

Please sign in to comment.