@@ -15,20 +15,21 @@ use testcontainers_modules::testcontainers::runners::AsyncRunner;
1515use trogon_nats:: auth:: { NatsAuth , NatsConfig } ;
1616use trogon_nats:: connect:: { ConnectError , connect} ;
1717
18- async fn start_nats ( ) -> Result < ( ContainerAsync < Nats > , u16 ) , Box < dyn std:: error:: Error > > {
18+ async fn start_nats ( ) -> Result < ( ContainerAsync < Nats > , String , u16 ) , Box < dyn std:: error:: Error > > {
1919 let container = Nats :: default ( ) . start ( ) . await ?;
20+ let host = container. get_host ( ) . await ?. to_string ( ) ;
2021 let port = container. get_host_port_ipv4 ( 4222 ) . await ?;
21- Ok ( ( container, port) )
22+ Ok ( ( container, host , port) )
2223}
2324
2425/// Covers the `NatsAuth::None` arm (lines 123-128) and the success branch (130-138).
2526/// Also exercises `apply_reconnect_options` (lines 69-74) indirectly.
2627#[ tokio:: test]
2728#[ ignore = "requires Docker" ]
2829async fn connect_with_no_auth_succeeds ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
29- let ( _container, port) = start_nats ( ) . await ?;
30+ let ( _container, host , port) = start_nats ( ) . await ?;
3031
31- let config = NatsConfig :: new ( vec ! [ format!( "nats://127.0.0.1 :{port}" ) ] , NatsAuth :: None ) ;
32+ let config = NatsConfig :: new ( vec ! [ format!( "nats://{host} :{port}" ) ] , NatsAuth :: None ) ;
3233
3334 connect ( & config, Duration :: from_secs ( 10 ) )
3435 . await
@@ -42,10 +43,10 @@ async fn connect_with_no_auth_succeeds() -> Result<(), Box<dyn std::error::Error
4243async fn connect_with_token_auth_succeeds_on_open_server ( ) -> Result < ( ) , Box < dyn std:: error:: Error > >
4344{
4445 // An open NATS server accepts any token — the token is just passed through.
45- let ( _container, port) = start_nats ( ) . await ?;
46+ let ( _container, host , port) = start_nats ( ) . await ?;
4647
4748 let config = NatsConfig :: new (
48- vec ! [ format!( "nats://127.0.0.1 :{port}" ) ] ,
49+ vec ! [ format!( "nats://{host} :{port}" ) ] ,
4950 NatsAuth :: Token ( "any-token" . to_string ( ) ) ,
5051 ) ;
5152
@@ -60,10 +61,10 @@ async fn connect_with_token_auth_succeeds_on_open_server() -> Result<(), Box<dyn
6061#[ ignore = "requires Docker" ]
6162async fn connect_with_user_password_succeeds_on_open_server ( )
6263-> Result < ( ) , Box < dyn std:: error:: Error > > {
63- let ( _container, port) = start_nats ( ) . await ?;
64+ let ( _container, host , port) = start_nats ( ) . await ?;
6465
6566 let config = NatsConfig :: new (
66- vec ! [ format!( "nats://127.0.0.1 :{port}" ) ] ,
67+ vec ! [ format!( "nats://{host} :{port}" ) ] ,
6768 NatsAuth :: UserPassword {
6869 user : "user" . to_string ( ) ,
6970 password : "pass" . to_string ( ) ,
@@ -84,17 +85,15 @@ async fn connect_with_user_password_succeeds_on_open_server()
8485#[ tokio:: test]
8586#[ ignore = "requires Docker" ]
8687async fn connect_with_nkey_auth_on_open_server ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
87- let ( _container, port) = start_nats ( ) . await ?;
88+ let ( _container, host , port) = start_nats ( ) . await ?;
8889
89- // A valid NKey user seed (base32-encoded, 58-char canonical format).
90- // On an open server the key is not validated — the test simply exercises
91- // the `NatsAuth::NKey` branch in `connect()`.
92- let seed = "SUACSSL3UAHUDXKFSNVUZRF5UHPMWZ6BFDTJ7M6USDRCRBZLYKI4LZPFZFR" . to_string ( ) ;
90+ // A valid NKey user seed (base32-encoded, 58-char canonical format,
91+ // starts with "SU"). On an open server the key is not validated against
92+ // a registered user — the test simply exercises the `NatsAuth::NKey`
93+ // branch in `connect()`.
94+ let seed = "SUANQDPB2RUOE4ETUA26CNX7FUKE5ZZKFCQIIW63OX225F2CO7UEXTM7ZY" . to_string ( ) ;
9395
94- let config = NatsConfig :: new (
95- vec ! [ format!( "nats://127.0.0.1:{port}" ) ] ,
96- NatsAuth :: NKey ( seed) ,
97- ) ;
96+ let config = NatsConfig :: new ( vec ! [ format!( "nats://{host}:{port}" ) ] , NatsAuth :: NKey ( seed) ) ;
9897
9998 let result = connect ( & config, Duration :: from_secs ( 10 ) ) . await ;
10099 assert ! (
@@ -134,10 +133,11 @@ async fn connect_with_wrong_token_returns_authorization_violation()
134133 . with_cmd ( [ "--auth" , "correct-token" ] )
135134 . start ( )
136135 . await ?;
136+ let host = container. get_host ( ) . await ?. to_string ( ) ;
137137 let port = container. get_host_port_ipv4 ( 4222 ) . await ?;
138138
139139 let config = NatsConfig :: new (
140- vec ! [ format!( "nats://127.0.0.1 :{port}" ) ] ,
140+ vec ! [ format!( "nats://{host} :{port}" ) ] ,
141141 NatsAuth :: Token ( "wrong-token" . to_string ( ) ) ,
142142 ) ;
143143
@@ -160,10 +160,11 @@ async fn connect_with_correct_token_succeeds() -> Result<(), Box<dyn std::error:
160160 . with_cmd ( [ "--auth" , "correct-token" ] )
161161 . start ( )
162162 . await ?;
163+ let host = container. get_host ( ) . await ?. to_string ( ) ;
163164 let port = container. get_host_port_ipv4 ( 4222 ) . await ?;
164165
165166 let config = NatsConfig :: new (
166- vec ! [ format!( "nats://127.0.0.1 :{port}" ) ] ,
167+ vec ! [ format!( "nats://{host} :{port}" ) ] ,
167168 NatsAuth :: Token ( "correct-token" . to_string ( ) ) ,
168169 ) ;
169170
@@ -190,7 +191,7 @@ async fn connect_to_unreachable_server_returns_ok_with_background_retry() {
190191 let port = listener. local_addr ( ) . unwrap ( ) . port ( ) ;
191192 drop ( listener) ;
192193
193- let config = NatsConfig :: new ( vec ! [ format!( "nats://127.0.0.1 :{port}" ) ] , NatsAuth :: None ) ;
194+ let config = NatsConfig :: new ( vec ! [ format!( "nats://{host} :{port}" ) ] , NatsAuth :: None ) ;
194195
195196 // connect() must return within a few seconds (INITIAL_CONNECT_CHECK_SECS + margin).
196197 let result = tokio:: time:: timeout (
0 commit comments