@@ -45,6 +45,8 @@ class CoreNatsTests: XCTestCase {
45
45
( " testRequest " , testRequest) ,
46
46
( " testRequest_noResponders " , testRequest_noResponders) ,
47
47
( " testRequest_timeout " , testRequest_timeout) ,
48
+ ( " testNkeyAuth " , testNkeyAuth) ,
49
+ ( " testNkeyAuthFile " , testNkeyAuthFile) ,
48
50
]
49
51
var natsServer = NatsServer ( )
50
52
@@ -463,6 +465,58 @@ class CoreNatsTests: XCTestCase {
463
465
_ = await subscribe. next ( )
464
466
}
465
467
468
+ func testNkeyAuth( ) async throws {
469
+ logger. logLevel = . debug
470
+ let bundle = Bundle . module
471
+ natsServer. start ( cfg: bundle. url ( forResource: " nkey " , withExtension: " conf " ) !. relativePath)
472
+
473
+ let client = NatsClientOptions ( )
474
+ . url ( URL ( string: natsServer. clientURL) !)
475
+ . nkey ( " SUACH75SWCM5D2JMJM6EKLR2WDARVGZT4QC6LX3AGHSWOMVAKERABBBRWM " )
476
+ . build ( )
477
+ try await client. connect ( )
478
+ let subscribe = try await client. subscribe ( subject: " foo " ) . makeAsyncIterator ( )
479
+ try await client. publish ( " data " . data ( using: . utf8) !, subject: " foo " )
480
+ _ = await subscribe. next ( )
481
+ }
482
+
483
+ func testNkeyAuthFile( ) async throws {
484
+ logger. logLevel = . debug
485
+ let bundle = Bundle . module
486
+ natsServer. start ( cfg: bundle. url ( forResource: " nkey " , withExtension: " conf " ) !. relativePath)
487
+
488
+ let client = NatsClientOptions ( )
489
+ . url ( URL ( string: natsServer. clientURL) !)
490
+ . nkeyFile ( bundle. url ( forResource: " nkey " , withExtension: " " ) !)
491
+ . build ( )
492
+ try await client. connect ( )
493
+ let subscribe = try await client. subscribe ( subject: " foo " ) . makeAsyncIterator ( )
494
+ try await client. publish ( " data " . data ( using: . utf8) !, subject: " foo " )
495
+ _ = await subscribe. next ( )
496
+
497
+ // Test if passing both nkey and nkeyPath throws an error
498
+ let badClient = NatsClientOptions ( )
499
+ . url ( URL ( string: natsServer. clientURL) !)
500
+ . nkeyFile ( bundle. url ( forResource: " nkey " , withExtension: " " ) !)
501
+ . nkey ( " SUACH75SWCM5D2JMJM6EKLR2WDARVGZT4QC6LX3AGHSWOMVAKERABBBRWM " )
502
+ . build ( )
503
+
504
+ var thrownError : Error ?
505
+ do {
506
+ try await badClient. connect ( )
507
+ } catch {
508
+ thrownError = error
509
+ }
510
+
511
+ // Now assert that an error was thrown and check its type and properties
512
+ XCTAssertNotNil ( thrownError, " Expected method to throw an error but it did not. " )
513
+ if let natsError = thrownError as? NatsConfigError {
514
+ XCTAssertEqual ( natsError. description, " cannot use both nkey and nkeyPath " )
515
+ } else {
516
+ XCTFail ( " Unexpected error type: \( String ( describing: thrownError) ) " )
517
+ }
518
+ }
519
+
466
520
func testMutualTls( ) async throws {
467
521
let bundle = Bundle . module
468
522
logger. logLevel = . debug
0 commit comments