Переглянути джерело

Merge pull request #882 from xljiulang/master

Use IConnectionEndPointFeature to get IMqttChannelAdapter.Endpoint in aspnetcore31
release/3.x.x
Christian 4 роки тому
committed by GitHub
джерело
коміт
e9724c0202
Не вдалося знайти GPG ключ що відповідає даному підпису Ідентифікатор GPG ключа: 4AEE18F83AFDEB23
4 змінених файлів з 113 додано та 6 видалено
  1. +7
    -1
      Source/MQTTnet.AspnetCore/MqttConnectionContext.cs
  2. +7
    -1
      Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj
  3. +50
    -0
      Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionHandlerMockup.cs
  4. +49
    -4
      Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs

+ 7
- 1
Source/MQTTnet.AspnetCore/MqttConnectionContext.cs Переглянути файл

@@ -25,7 +25,7 @@ namespace MQTTnet.AspNetCore
_input = Connection.Transport.Input;
_output = Connection.Transport.Output;
}
_reader = new SpanBasedMqttPacketBodyReader();
}

@@ -37,6 +37,12 @@ namespace MQTTnet.AspNetCore
{
get
{
#if NETCOREAPP3_1
if (Connection?.RemoteEndPoint != null)
{
return Connection.RemoteEndPoint.ToString();
}
#endif
var connection = Http?.HttpContext?.Connection;
if (connection == null)
{


+ 7
- 1
Tests/MQTTnet.AspNetCore.Tests/MQTTnet.AspNetCore.Tests.csproj Переглянути файл

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netcoreapp3.1;net461</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

@@ -11,6 +11,12 @@
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
</ItemGroup>


<ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp3.1' ">
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Source\MQTTnet.AspnetCore\MQTTnet.AspNetCore.csproj" />
<ProjectReference Include="..\MQTTnet.Core.Tests\MQTTnet.Tests.csproj" />


+ 50
- 0
Tests/MQTTnet.AspNetCore.Tests/Mockups/ConnectionHandlerMockup.cs Переглянути файл

@@ -0,0 +1,50 @@
using Microsoft.AspNetCore.Connections;
using MQTTnet.Adapter;
using MQTTnet.Formatter;
using MQTTnet.Server;
using System;
using System.Threading.Tasks;

namespace MQTTnet.AspNetCore.Tests.Mockups
{
public class ConnectionHandlerMockup: IMqttServerAdapter
{
public TaskCompletionSource<MqttConnectionContext> Context { get; } = new TaskCompletionSource<MqttConnectionContext>();
public Func<IMqttChannelAdapter, Task> ClientHandler { get; set; }

public ConnectionHandlerMockup()
{
}

public async Task OnConnectedAsync(ConnectionContext connection)
{
try
{
var writer = new SpanBasedMqttPacketWriter();
var formatter = new MqttPacketFormatterAdapter(writer);
var context = new MqttConnectionContext(formatter, connection);
Context.TrySetResult(context);

await ClientHandler(context);
}
catch (Exception ex)
{
Context.TrySetException(ex);
}
}

public Task StartAsync(IMqttServerOptions options)
{
return Task.CompletedTask;
}

public Task StopAsync()
{
return Task.CompletedTask;
}

public void Dispose()
{
}
}
}

+ 49
- 4
Tests/MQTTnet.AspNetCore.Tests/MqttConnectionContextTest.cs Переглянути файл

@@ -1,13 +1,20 @@
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MQTTnet.AspNetCore.Tests.Mockups;
using MQTTnet.Exceptions;
using MQTTnet.Packets;

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Adapter;
using MQTTnet.AspNetCore.Tests.Mockups;
using MQTTnet.Client.Options;
using MQTTnet.Exceptions;
using MQTTnet.Formatter;
using MQTTnet.Packets;
using System.Net;

namespace MQTTnet.AspNetCore.Tests
{
@@ -63,5 +70,43 @@ namespace MQTTnet.AspNetCore.Tests
var readResult = await pipe.Send.Reader.ReadAsync();
Assert.IsTrue(readResult.Buffer.Length > 20000);
}

private class Startup
{
public void Configure(IApplicationBuilder app)
{
}
}

[TestMethod]
public async Task TestEndpoint()
{
var mockup = new ConnectionHandlerMockup();


using (var host = new WebHostBuilder()
.UseKestrel(kestrel => kestrel.ListenLocalhost(1883, listener => listener.Use((ctx, next) => mockup.OnConnectedAsync(ctx))))
.UseStartup<Startup>()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedMqttServer(o => o.WithoutDefaultEndpoint());
services.AddSingleton<IMqttServerAdapter>(mockup);
})
.Build())
using (var client = new MqttFactory().CreateMqttClient())
{
host.Start();
await client.ConnectAsync(new MqttClientOptionsBuilder()
.WithTcpServer("localhost")
.Build(), CancellationToken.None);

var ctx = await mockup.Context.Task;
#if NETCOREAPP3_1
var ep = IPEndPoint.Parse(ctx.Endpoint);
Assert.IsNotNull(ep);
#endif
Assert.IsNotNull(ctx);
}
}
}
}

Завантаження…
Відмінити
Зберегти