// Tests_SRS_DEVICECLIENT_10_013: [** If the given method does not have an associated delegate, failed silently **]** public async Task DeviceClient_OnMethodCalled_NoMethodHandler() { DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(fakeConnectionString); var innerHandler = Substitute.For <IDelegatingHandler>(); deviceClient.InnerHandler = innerHandler; var methodRequestInternal = new MethodRequestInternal("TestMethodName", "4B810AFC-CF5B-4AE8-91EB-245F7C7751F9", new MemoryStream(Encoding.UTF8.GetBytes("{\"grade\":\"good\"}"))); await deviceClient.OnMethodCalled(methodRequestInternal); await innerHandler.DidNotReceive().SendMethodResponseAsync(Arg.Any <MethodResponseInternal>(), Arg.Any <CancellationToken>()); }
private void OnMethodReceived(MethodRequestInternal methodRequestInternal) { Logging.Enter(this, methodRequestInternal, nameof(OnMethodReceived)); try { _onMethodCallback?.Invoke(methodRequestInternal); } finally { Logging.Exit(this, methodRequestInternal, nameof(OnMethodReceived)); } }
async Task <ReceivingAmqpLink> CreateMethodReceivingLinkAsync(TimeSpan timeout, CancellationToken cancellationToken) { string path = string.Format(CultureInfo.InvariantCulture, CommonConstants.DeviceMethodPathTemplate, System.Net.WebUtility.UrlEncode(this.deviceId)); return(await this.IotHubConnection.CreateMethodReceivingLinkAsync( path, this.iotHubConnectionString, timeout, this.prefetchCount, cancellationToken, this.deviceId, (amqpMessage, methodReceivingLink) => { MethodRequestInternal methodRequestInternal = MethodConverter.ConstructMethodRequestFromAmqpMessage(amqpMessage); methodReceivingLink.DisposeDelivery(amqpMessage, true, AmqpConstants.AcceptedOutcome); this.messageListener(methodRequestInternal); })); }
private void OnMethodReceived(AmqpMessage amqpMessage) { if (Logging.IsEnabled) Logging.Enter(this, amqpMessage, $"{nameof(OnMethodReceived)}"); try { MethodRequestInternal methodRequestInternal = MethodConverter.ConstructMethodRequestFromAmqpMessage(amqpMessage, new CancellationToken(false)); _methodReceivingLink?.DisposeDelivery(amqpMessage, true, AmqpConstants.AcceptedOutcome); _methodHandler?.Invoke(methodRequestInternal); } finally { if (Logging.IsEnabled) Logging.Exit(this, amqpMessage, $"{nameof(OnMethodReceived)}"); } }
async Task <ReceivingAmqpLink> CreateMethodReceivingLinkAsync(TimeSpan timeout, CancellationToken cancellationToken) { string path = this.BuildPath(CommonConstants.DeviceMethodPathTemplate, CommonConstants.ModuleMethodPathTemplate); ReceivingAmqpLink methodReceivingLink = await IotHubConnection.CreateReceivingLinkAsync(path, iotHubConnectionString, methodConnectionCorrelationId, IotHubConnection.ReceivingLinkType.Methods, prefetchCount, timeout, productInfo, cancellationToken).ConfigureAwait(false); methodReceivingLink.RegisterMessageListener(amqpMessage => { MethodRequestInternal methodRequestInternal = MethodConverter.ConstructMethodRequestFromAmqpMessage(amqpMessage, cancellationToken); methodReceivingLink.DisposeDelivery(amqpMessage, true, AmqpConstants.AcceptedOutcome); this.methodReceivedListener(methodRequestInternal); }); MyStringCopy(methodReceivingLink.Name, out methodReceivingLinkName); methodReceivingLink.SafeAddClosed(OnAmqpConnectionClose); return(methodReceivingLink); }
// Tests_SRS_DEVICECLIENT_28_021: [** If the MethodResponse from the MethodHandler is not valid json, JsonReaderException shall be throw **]** public async Task DeviceClient_OnMethodCalled_MethodResponseHasInvalidJson() { DeviceClient deviceClient = DeviceClient.CreateFromConnectionString(fakeConnectionString); bool isMethodHandlerCalled = false; deviceClient.SetMethodHandler("TestMethodName", (payload, context) => { isMethodHandlerCalled = true; return(Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes("{\"name\"\"ABC\"}"), 200))); }, "custom data"); var innerHandler = Substitute.For <IDelegatingHandler>(); deviceClient.InnerHandler = innerHandler; var methodRequestInternal = new MethodRequestInternal("TestMethodName", "4B810AFC-CF5B-4AE8-91EB-245F7C7751F9", new MemoryStream(Encoding.UTF8.GetBytes("{\"grade\":\"good\"}"))); await deviceClient.OnMethodCalled(methodRequestInternal); Assert.IsTrue(isMethodHandlerCalled); await innerHandler.DidNotReceive().SendMethodResponseAsync(Arg.Any <MethodResponseInternal>(), Arg.Any <CancellationToken>()); }
private void OnMethodReceived(AmqpMessage amqpMessage) { if (Logging.IsEnabled) { Logging.Enter(this, amqpMessage, nameof(OnMethodReceived)); } try { MethodRequestInternal methodRequestInternal = AmqpIotMessageConverter.ConstructMethodRequestFromAmqpMessage( amqpMessage, new CancellationToken(false)); DisposeDelivery(amqpMessage, true, AmqpConstants.AcceptedOutcome); _onMethodReceived?.Invoke(methodRequestInternal); } finally { if (Logging.IsEnabled) { Logging.Exit(this, amqpMessage, nameof(OnMethodReceived)); } } }