зеркало из
1
0
Форкнуть 0

fix(azure-iot-device): invalid body on received on a device method invoke throws (#856)

If somehow an invalid body is pushed through on a device method invocation, the device client side
will throw when it trys to json parse that body.
This commit is contained in:
Anthony V. Ercolano 2020-07-22 06:49:47 -07:00 коммит произвёл GitHub
Родитель 10284ea281
Коммит 8f78788bc5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 40 добавлений и 11 удалений

Просмотреть файл

@ -396,18 +396,25 @@ export abstract class InternalClient extends EventEmitter {
private _addMethodCallback(methodName: string, callback: (request: DeviceMethodRequest, response: DeviceMethodResponse) => void): void {
const self = this;
this._transport.onDeviceMethod(methodName, (message) => {
// build the request object
const request = new DeviceMethodRequest(
message.requestId,
message.methods.methodName,
message.body
);
// build the response object
const response = new DeviceMethodResponse(message.requestId, self._transport);
// Codes_SRS_NODE_INTERNAL_CLIENT_13_001: [ The onDeviceMethod method shall cause the callback function to be invoked when a cloud-to-device method invocation signal is received from the IoT Hub service. ]
callback(request, response);
// build the request object
try {
const request = new DeviceMethodRequest(
message.requestId,
message.methods.methodName,
message.body
);
// Codes_SRS_NODE_INTERNAL_CLIENT_13_001: [ The onDeviceMethod method shall cause the callback function to be invoked when a cloud-to-device method invocation signal is received from the IoT Hub service. ]
callback(request, response);
} catch (err) {
response.send(400, 'Invalid request format: ' + err.message, (err) => {
if (err) {
debug('Error sending invalid request response back to application');
}
});
}
});
}

Просмотреть файл

@ -832,12 +832,34 @@ describe('ModuleClient', function () {
done();
});
});
2
// test
transport.emitMethodCall('reboot');
});
it('calls response send with 400 status when method call contains invalid body', function (done) {
// setup
var transport = new FakeMethodTransport();
var client = new ModuleClient(transport);
client.open(function () {
client.onMethod('reboot', function () {
assert.fail('Should not reach here.');
});
});
var responseSpy = sinon.spy(transport, 'sendMethodResponse');
// test
transport.emit('method_' + 'reboot', {
methods: {
methodName: 'reboot'
},
body: '{',
requestId: '42'
});
transport.emitMethodCall('reboot');
assert.strictEqual(responseSpy.args[0][0].status, 400);
done();
});
// Tests_SRS_NODE_MODULE_CLIENT_13_003: [ The client shall start listening for method calls from the service whenever there is a listener subscribed for a method callback. ]
it('registers callback on transport when a method event is subscribed to', function () {
// setup