|
|
@@ -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] |
|
|
|
public async Task Subscribe_Unsubscribe() |
|
|
|
{ |
|
|
@@ -156,7 +195,7 @@ namespace MQTTnet.Tests |
|
|
|
Assert.AreEqual(1, receivedMessagesCount); |
|
|
|
|
|
|
|
var unsubscribeEventCalled = false; |
|
|
|
server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(e => |
|
|
|
server.ClientUnsubscribedTopicHandler = new MqttServerClientUnsubscribedTopicHandlerDelegate(e => |
|
|
|
{ |
|
|
|
unsubscribeEventCalled = e.TopicFilter == "a" && e.ClientId == "c1"; |
|
|
|
}); |
|
|
@@ -376,7 +415,7 @@ namespace MQTTnet.Tests |
|
|
|
using (var testEnvironment = new TestEnvironment()) |
|
|
|
{ |
|
|
|
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 receivedMessages = new List<MqttApplicationMessage>(); |
|
|
@@ -689,7 +728,7 @@ namespace MQTTnet.Tests |
|
|
|
{ |
|
|
|
await testEnvironment.StartServerAsync( |
|
|
|
new MqttServerOptionsBuilder().WithApplicationMessageInterceptor( |
|
|
|
c => { c.ApplicationMessage = new MqttApplicationMessage {Topic = "new_topic" }; })); |
|
|
|
c => { c.ApplicationMessage = new MqttApplicationMessage { Topic = "new_topic" }; })); |
|
|
|
|
|
|
|
string receivedTopic = null; |
|
|
|
var c1 = await testEnvironment.ConnectClientAsync(); |
|
|
@@ -866,7 +905,7 @@ namespace MQTTnet.Tests |
|
|
|
|
|
|
|
var events = new List<string>(); |
|
|
|
|
|
|
|
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(_ => |
|
|
|
server.ClientConnectedHandler = new MqttServerClientConnectedHandlerDelegate(_ => |
|
|
|
{ |
|
|
|
lock (events) |
|
|
|
{ |
|
|
@@ -1050,7 +1089,7 @@ namespace MQTTnet.Tests |
|
|
|
var client2 = await testEnvironment.ConnectClientAsync(); |
|
|
|
|
|
|
|
var buffer = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
client2.UseApplicationMessageReceivedHandler(c => |
|
|
|
{ |
|
|
|
lock (buffer) |
|
|
@@ -1082,7 +1121,7 @@ namespace MQTTnet.Tests |
|
|
|
|
|
|
|
var clientStatus = await server.GetClientStatusAsync(); |
|
|
|
Assert.AreEqual(0, clientStatus.Count); |
|
|
|
|
|
|
|
|
|
|
|
var client2 = await testEnvironment.ConnectClientAsync(new MqttClientOptionsBuilder().WithClientId("b")); |
|
|
|
await client2.PublishAsync("x", "1"); |
|
|
|
await client2.PublishAsync("x", "2"); |
|
|
|