Skip to content

Commit d146f2d

Browse files
committed
core: Apply code review suggestions
1 parent a1eafcf commit d146f2d

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/Testcontainers.Mosquitto/MosquittoContainer.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,59 @@ public MosquittoContainer(MosquittoConfiguration configuration)
1717
}
1818

1919
/// <summary>
20-
/// Gets the MQTT endpoint.
20+
/// Gets the MQTT port.
2121
/// </summary>
22-
/// <returns>An MQTT address in the format: <c>mqtt://hostname:port</c>.</returns>
23-
public string GetEndpoint()
22+
public ushort MqttPort => GetMappedPublicPort(MosquittoBuilder.MqttPort);
23+
24+
/// <summary>
25+
/// Gets the secure MQTT port.
26+
/// </summary>
27+
public ushort MqttTlsPort => GetMappedPublicPort(MosquittoBuilder.MqttTlsPort);
28+
29+
/// <summary>
30+
/// Gets the MQTT connection string.
31+
/// </summary>
32+
/// <returns>An MQTT connection string in the format: <c>mqtt://hostname:port</c>.</returns>
33+
public string GetConnectionString()
2434
{
25-
return new UriBuilder("mqtt", Hostname, GetMappedPublicPort(MosquittoBuilder.MqttPort)).ToString();
35+
return new UriBuilder("mqtt", Hostname, MqttPort).ToString();
2636
}
2737

2838
/// <summary>
29-
/// Gets the secure MQTT endpoint.
39+
/// Gets the secure MQTT connection string.
3040
/// </summary>
31-
/// <returns>A secure MQTT address in the format: <c>mqtts://hostname:port</c>.</returns>
32-
public string GetSecureEndpoint()
41+
/// <returns>A secure MQTT connection string in the format: <c>mqtts://hostname:port</c>.</returns>
42+
/// <exception cref="InvalidOperationException">TLS/SSL support is not enabled in the container configuration.</exception>
43+
public string GetSecureConnectionString()
3344
{
3445
ThrowIfTlsNotEnabled();
35-
return new UriBuilder("mqtts", Hostname, GetMappedPublicPort(MosquittoBuilder.MqttTlsPort)).ToString();
46+
return new UriBuilder("mqtts", Hostname, MqttTlsPort).ToString();
3647
}
3748

3849
/// <summary>
39-
/// Gets the WebSocket endpoint.
50+
/// Gets the MQTT over WebSocket connection string.
4051
/// </summary>
41-
/// <returns>A WS address in the format: <c>ws://hostname:port</c>.</returns>
42-
public string GetWsEndpoint()
52+
/// <returns>A WebSocket connection string in the format: <c>ws://hostname:port</c>.</returns>
53+
public string GetWsConnectionString()
4354
{
4455
return new UriBuilder("ws", Hostname, GetMappedPublicPort(MosquittoBuilder.MqttWsPort)).ToString();
4556
}
4657

4758
/// <summary>
48-
/// Gets the secure WebSocket endpoint.
59+
/// Gets the secure MQTT over WebSocket connection string.
4960
/// </summary>
50-
/// <returns>A secure WS address in the format: <c>wss://hostname:port</c>.</returns>
51-
public string GetWssEndpoint()
61+
/// <returns>A secure WebSocket connection string in the format: <c>wss://hostname:port</c>.</returns>
62+
/// <exception cref="InvalidOperationException">TLS/SSL support is not enabled in the container configuration.</exception>
63+
public string GetWssConnectionString()
5264
{
5365
ThrowIfTlsNotEnabled();
5466
return new UriBuilder("wss", Hostname, GetMappedPublicPort(MosquittoBuilder.MqttWssPort)).ToString();
5567
}
5668

5769
/// <summary>
58-
/// Throws <see cref="InvalidOperationException" /> when TLS/SSL is not enabled in the configuration.
70+
/// Throws <see cref="InvalidOperationException" /> when TLS/SSL support is not enabled in the container configuration.
5971
/// </summary>
60-
/// <exception cref="InvalidOperationException">TLS/SSL is not enabled in the configuration.</exception>
72+
/// <exception cref="InvalidOperationException">TLS/SSL support is not enabled in the container configuration.</exception>
6173
private void ThrowIfTlsNotEnabled()
6274
{
6375
if (!_configuration.TlsEnabled)

tests/Testcontainers.Mosquitto.Tests/MosquittoContainerTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public sealed class TcpUnencryptedUnauthenticatedConfiguration(ITestOutputHelper
7878
protected override MqttClientOptions GetClientOptions()
7979
{
8080
return new MqttClientOptionsBuilder()
81-
.WithTcpServer(Container.Hostname, Container.GetMappedPublicPort(MosquittoBuilder.MqttPort))
81+
.WithTcpServer(Container.Hostname, Container.MqttPort)
8282
.Build();
8383
}
8484
}
@@ -95,7 +95,7 @@ protected override MosquittoBuilder Configure(MosquittoBuilder builder)
9595
protected override MqttClientOptions GetClientOptions()
9696
{
9797
return new MqttClientOptionsBuilder()
98-
.WithTcpServer(Container.Hostname, Container.GetMappedPublicPort(MosquittoBuilder.MqttTlsPort))
98+
.WithTcpServer(Container.Hostname, Container.MqttTlsPort)
9999
.WithTlsOptions(options => options.WithCertificateValidationHandler(e =>
100100
"CN=Test CA".Equals(e.Certificate.Issuer, StringComparison.Ordinal)))
101101
.Build();
@@ -109,7 +109,7 @@ public sealed class WebSocketUnencryptedUnauthenticatedConfiguration(ITestOutput
109109
protected override MqttClientOptions GetClientOptions()
110110
{
111111
return new MqttClientOptionsBuilder()
112-
.WithWebSocketServer(options => options.WithUri(Container.GetWsEndpoint()))
112+
.WithWebSocketServer(options => options.WithUri(Container.GetWsConnectionString()))
113113
.Build();
114114
}
115115
}
@@ -126,7 +126,7 @@ protected override MosquittoBuilder Configure(MosquittoBuilder builder)
126126
protected override MqttClientOptions GetClientOptions()
127127
{
128128
return new MqttClientOptionsBuilder()
129-
.WithWebSocketServer(options => options.WithUri(Container.GetWssEndpoint()))
129+
.WithWebSocketServer(options => options.WithUri(Container.GetWssConnectionString()))
130130
.WithTlsOptions(options => options.WithCertificateValidationHandler(e =>
131131
"CN=Test CA".Equals(e.Certificate.Issuer, StringComparison.Ordinal)))
132132
.Build();

0 commit comments

Comments
 (0)