|
@@ -121,6 +121,45 @@ namespace MQTTnet.Tests |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[TestMethod] |
|
|
|
|
|
public async Task Intercept_Subscription() |
|
|
|
|
|
{ |
|
|
|
|
|
using (var testEnvironment = new TestEnvironment()) |
|
|
|
|
|
{ |
|
|
|
|
|
await testEnvironment.StartServerAsync(new MqttServerOptionsBuilder().WithSubscriptionInterceptor( |
|
|
|
|
|
c => |
|
|
|
|
|
{ |
|
|
|
|
|
// Set the topic to "a" regards what the client wants to subscribe. |
|
|
|
|
|
c.TopicFilter.Topic = "a"; |
|
|
|
|
|
})); |
|
|
|
|
|
|
|
|
|
|
|
var topicAReceived = false; |
|
|
|
|
|
var topicBReceived = false; |
|
|
|
|
|
|
|
|
|
|
|
var client = await testEnvironment.ConnectClientAsync(); |
|
|
|
|
|
client.UseApplicationMessageReceivedHandler(c => |
|
|
|
|
|
{ |
|
|
|
|
|
if (c.ApplicationMessage.Topic == "a") |
|
|
|
|
|
{ |
|
|
|
|
|
topicAReceived = true; |
|
|
|
|
|
} |
|
|
|
|
|
else if (c.ApplicationMessage.Topic == "b") |
|
|
|
|
|
{ |
|
|
|
|
|
topicBReceived = true; |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
await client.SubscribeAsync("b"); |
|
|
|
|
|
|
|
|
|
|
|
await client.PublishAsync("a"); |
|
|
|
|
|
|
|
|
|
|
|
await Task.Delay(500); |
|
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(topicAReceived); |
|
|
|
|
|
Assert.IsFalse(topicBReceived); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
[TestMethod] |
|
|
[TestMethod] |
|
|
public async Task Subscribe_Unsubscribe() |
|
|
public async Task Subscribe_Unsubscribe() |
|
|
{ |
|
|
{ |
|
@@ -156,7 +195,7 @@ namespace MQTTnet.Tests |
|
|
Assert.AreEqual(1, receivedMessagesCount); |
|
|
Assert.AreEqual(1, receivedMessagesCount); |
|
|
|
|
|
|
|
|
var unsubscribeEventCalled = false; |
|
|
var unsubscribeEventCalled = false; |
|
|
server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(e => |
|
|
|
|
|
|
|
|
server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(e => |
|
|
{ |
|
|
{ |
|
|
unsubscribeEventCalled = e.TopicFilter == "a" && e.ClientId == "c1"; |
|
|
unsubscribeEventCalled = e.TopicFilter == "a" && e.ClientId == "c1"; |
|
|
}); |
|
|
}); |
|
@@ -376,7 +415,7 @@ namespace MQTTnet.Tests |
|
|
using (var testEnvironment = new TestEnvironment()) |
|
|
using (var testEnvironment = new TestEnvironment()) |
|
|
{ |
|
|
{ |
|
|
var server = await testEnvironment.StartServerAsync(); |
|
|
var server = await testEnvironment.StartServerAsync(); |
|
|
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(e => server.SubscribeAsync(e.ClientId, "topic1")); |
|
|
|
|
|
|
|
|
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(e => server.SubscribeAsync(e.ClientId, "topic1")); |
|
|
|
|
|
|
|
|
var client = await testEnvironment.ConnectClientAsync(); |
|
|
var client = await testEnvironment.ConnectClientAsync(); |
|
|
var receivedMessages = new List<MqttApplicationMessage>(); |
|
|
var receivedMessages = new List<MqttApplicationMessage>(); |
|
@@ -689,7 +728,7 @@ namespace MQTTnet.Tests |
|
|
{ |
|
|
{ |
|
|
await testEnvironment.StartServerAsync( |
|
|
await testEnvironment.StartServerAsync( |
|
|
new MqttServerOptionsBuilder().WithApplicationMessageInterceptor( |
|
|
new MqttServerOptionsBuilder().WithApplicationMessageInterceptor( |
|
|
c => { c.ApplicationMessage = new MqttApplicationMessage {Topic = "new_topic" }; })); |
|
|
|
|
|
|
|
|
c => { c.ApplicationMessage = new MqttApplicationMessage { Topic = "new_topic" }; })); |
|
|
|
|
|
|
|
|
string receivedTopic = null; |
|
|
string receivedTopic = null; |
|
|
var c1 = await testEnvironment.ConnectClientAsync(); |
|
|
var c1 = await testEnvironment.ConnectClientAsync(); |
|
@@ -866,7 +905,7 @@ namespace MQTTnet.Tests |
|
|
|
|
|
|
|
|
var events = new List<string>(); |
|
|
var events = new List<string>(); |
|
|
|
|
|
|
|
|
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(_ => |
|
|
|
|
|
|
|
|
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(_ => |
|
|
{ |
|
|
{ |
|
|
lock (events) |
|
|
lock (events) |
|
|
{ |
|
|
{ |
|
@@ -1050,7 +1089,7 @@ namespace MQTTnet.Tests |
|
|
var client2 = await testEnvironment.ConnectClientAsync(); |
|
|
var client2 = await testEnvironment.ConnectClientAsync(); |
|
|
|
|
|
|
|
|
var buffer = new StringBuilder(); |
|
|
var buffer = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client2.UseApplicationMessageReceivedHandler(c => |
|
|
client2.UseApplicationMessageReceivedHandler(c => |
|
|
{ |
|
|
{ |
|
|
lock (buffer) |
|
|
lock (buffer) |
|
@@ -1082,7 +1121,7 @@ namespace MQTTnet.Tests |
|
|
|
|
|
|
|
|
var clientStatus = await server.GetClientStatusAsync(); |
|
|
var clientStatus = await server.GetClientStatusAsync(); |
|
|
Assert.AreEqual(0, clientStatus.Count); |
|
|
Assert.AreEqual(0, clientStatus.Count); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var client2 = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithClientId("b")); |
|
|
var client2 = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithClientId("b")); |
|
|
await client2.PublishAsync("x", "1"); |
|
|
await client2.PublishAsync("x", "1"); |
|
|
await client2.PublishAsync("x", "2"); |
|
|
await client2.PublishAsync("x", "2"); |
|
|