@@ -152,17 +152,39 @@ class CoreNatsTests: XCTestCase {
152
152
}
153
153
154
154
func testUsernameAndPassword( ) async throws {
155
- natsServer. start ( )
156
155
logger. logLevel = . debug
156
+ let currentFile = URL ( fileURLWithPath: #file)
157
+ // Navigate up to the Tests directory
158
+ let testsDir = currentFile. deletingLastPathComponent ( ) . deletingLastPathComponent ( )
159
+ // Construct the path to the resource
160
+ let resourceURL = testsDir
161
+ . appendingPathComponent ( " Integration/Resources/creds.conf " , isDirectory: false )
162
+ natsServer. start ( cfg: resourceURL. path)
157
163
let client = ClientOptions ( )
158
- . url ( URL ( string: " nats://localhost:4222 " ) !)
159
- . username_and_password ( " user " , " pass " )
164
+ . url ( URL ( string: natsServer . clientURL ) !)
165
+ . username_and_password ( " derek " , " s3cr3t " )
160
166
. maxReconnects ( 5 )
161
167
. build ( )
162
168
try await client. connect ( )
163
169
try client. publish ( " msg " . data ( using: . utf8) !, subject: " test " )
164
170
try await client. flush ( )
165
171
try await client. subscribe ( to: " test " )
166
172
XCTAssertNotNil ( client, " Client should not be nil " )
173
+
174
+
175
+ // Test if client with bad credentials throws an error
176
+ let bad_creds_client = ClientOptions ( )
177
+ . url ( URL ( string: natsServer. clientURL) !)
178
+ . username_and_password ( " derek " , " badpassword " )
179
+ . maxReconnects ( 5 )
180
+ . build ( )
181
+
182
+ do {
183
+ try await bad_creds_client. connect ( )
184
+ XCTFail ( " Should have thrown an error " )
185
+ } catch {
186
+ XCTAssertNotNil ( error, " Error should not be nil " )
187
+ }
188
+
167
189
}
168
190
}
0 commit comments