Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Pietrek <[email protected]>
  • Loading branch information
Jarema committed Apr 16, 2024
1 parent ed35b62 commit adcf514
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Sources/Nats/NatsConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ class ConnectionHandler: ChannelInboundHandler {
user: self.auth?.user ?? "", pass: self.auth?.password ?? "",
authToken: self.auth?.token ?? "", headers: true, noResponders: true)

if self.auth?.nkey != nil && self.auth?.nkeyPath != nil {
throw NatsConfigError("cannot use both nkey and nkeyPath")
}
if let auth = self.auth, let credentialsPath = auth.credentialsPath {
let credentials = try await URLSession.shared.data(from: credentialsPath).0
guard let jwt = JwtUtils.parseDecoratedJWT(contents: credentials) else {
Expand Down
24 changes: 24 additions & 0 deletions Tests/NatsTests/Integration/ConnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CoreNatsTests: XCTestCase {
("testRequest_noResponders", testRequest_noResponders),
("testRequest_timeout", testRequest_timeout),
("testNkeyAuth", testNkeyAuth),
("testNkeyAuthFile", testNkeyAuthFile),
]
var natsServer = NatsServer()

Expand Down Expand Up @@ -402,6 +403,29 @@ class CoreNatsTests: XCTestCase {
let subscribe = try await client.subscribe(subject: "foo").makeAsyncIterator()
try await client.publish("data".data(using: .utf8)!, subject: "foo")
_ = await subscribe.next()

// Test if passing both nkey and nkeyPath throws an error
let badClient = NatsClientOptions()
.url(URL(string: natsServer.clientURL)!)
.nkeyFile(bundle.url(forResource: "nkey", withExtension: "")!)
.nkey("SUACH75SWCM5D2JMJM6EKLR2WDARVGZT4QC6LX3AGHSWOMVAKERABBBRWM")
.build()


var thrownError: Error?
do {
try await badClient.connect()
} catch {
thrownError = error
}

// Now assert that an error was thrown and check its type and properties
XCTAssertNotNil(thrownError, "Expected method to throw an error but it did not.")
if let natsError = thrownError as? NatsConfigError {
XCTAssertEqual(natsError.description, "cannot use both nkey and nkeyPath")
} else {
XCTFail("Unexpected error type: \(String(describing: thrownError))")
}
}

func testMutualTls() async throws {
Expand Down

0 comments on commit adcf514

Please sign in to comment.