Skip to content

Commit 6171c81

Browse files
committed
CrossPlatformSocket_Tests: create a localhost web server for remote http server.
1 parent b035659 commit 6171c81

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

Source/MQTTnet.Tests/Internal/CrossPlatformSocket_Tests.cs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
using MQTTnet.Implementations;
510
using System;
11+
using System.Linq;
612
using System.Net;
13+
using System.Net.NetworkInformation;
714
using System.Net.Sockets;
815
using System.Text;
916
using System.Threading;
1017
using System.Threading.Tasks;
11-
using Microsoft.VisualStudio.TestTools.UnitTesting;
12-
using MQTTnet.Implementations;
1318

1419
namespace MQTTnet.Tests.Internal
1520
{
@@ -19,19 +24,47 @@ public class CrossPlatformSocket_Tests
1924
[TestMethod]
2025
public async Task Connect_Send_Receive()
2126
{
27+
var serverPort = GetServerPort();
28+
var responseContent = "Connect_Send_Receive";
29+
30+
// create a localhost web server.
31+
var builder = WebApplication.CreateSlimBuilder();
32+
builder.WebHost.UseKestrel(k => k.ListenLocalhost(serverPort));
33+
34+
await using var webApp = builder.Build();
35+
var webAppStartedSource = new TaskCompletionSource();
36+
webApp.Lifetime.ApplicationStarted.Register(() => webAppStartedSource.TrySetResult());
37+
webApp.Use(next => context => context.Response.WriteAsync(responseContent));
38+
await webApp.StartAsync();
39+
await webAppStartedSource.Task;
40+
41+
2242
var crossPlatformSocket = new CrossPlatformSocket(ProtocolType.Tcp);
23-
await crossPlatformSocket.ConnectAsync(new DnsEndPoint("www.microsoft.com", 80), CancellationToken.None);
43+
await crossPlatformSocket.ConnectAsync(new DnsEndPoint("localhost", serverPort), CancellationToken.None);
2444

25-
var requestBuffer = Encoding.UTF8.GetBytes("GET / HTTP/1.1\r\nHost: www.microsoft.com\r\n\r\n");
26-
await crossPlatformSocket.SendAsync(new ArraySegment<byte>(requestBuffer), System.Net.Sockets.SocketFlags.None);
45+
var requestBuffer = Encoding.UTF8.GetBytes($"GET /test/path HTTP/1.1\r\nHost: localhost:{serverPort}\r\n\r\n");
46+
await crossPlatformSocket.SendAsync(new ArraySegment<byte>(requestBuffer), SocketFlags.None);
2747

2848
var buffer = new byte[1024];
29-
var length = await crossPlatformSocket.ReceiveAsync(new ArraySegment<byte>(buffer), System.Net.Sockets.SocketFlags.None);
49+
var length = await crossPlatformSocket.ReceiveAsync(new ArraySegment<byte>(buffer), SocketFlags.None);
3050
crossPlatformSocket.Dispose();
3151

3252
var responseText = Encoding.UTF8.GetString(buffer, 0, length);
3353

34-
Assert.IsTrue(responseText.Contains("HTTP/1.1 200")|| responseText.Contains("HTTP/1.1 302"));
54+
Assert.IsTrue(responseText.Contains(responseContent));
55+
56+
57+
static int GetServerPort(int defaultPort = 9999)
58+
{
59+
var listeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
60+
var portSet = listeners.Select(i => i.Port).ToHashSet();
61+
62+
while (!portSet.Add(defaultPort))
63+
{
64+
defaultPort += 1;
65+
}
66+
return defaultPort;
67+
}
3568
}
3669

3770
[TestMethod]

0 commit comments

Comments
 (0)