damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 34e32bc93038912d064c1c400dc248c5c88862e8
parent dfcef0ba958b7d5294e2dad49d57c078033af075
Author: kernelkind <kernelkind@gmail.com>
Date:   Sat, 16 Dec 2023 20:28:41 -0500

handle extra slashes for relay url

Closes: https://github.com/damus-io/damus/issues/1766

Signed-off-by: kernelkind <kernelkind@gmail.com>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Nostr/Relay.swift | 10+++++++++-
AdamusTests/RelayPoolTests.swift | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 91 insertions(+), 1 deletion(-)

diff --git a/damus/Nostr/Relay.swift b/damus/Nostr/Relay.swift @@ -109,5 +109,13 @@ enum RelayError: Error { } func get_relay_id(_ url: RelayURL) -> String { - return url.url.absoluteString + let trimTrailingSlashes: (String) -> String = { url in + var trimmedUrl = url + while trimmedUrl.hasSuffix("/") { + trimmedUrl.removeLast() + } + return trimmedUrl + } + + return trimTrailingSlashes(url.url.absoluteString) } diff --git a/damusTests/RelayPoolTests.swift b/damusTests/RelayPoolTests.swift @@ -0,0 +1,82 @@ +// +// RelayPoolTests.swift +// damusTests +// +// Created by kernelkind on 12/16/23. +// + +import Foundation + +import XCTest +@testable import damus + +final class RelayPoolTests: XCTestCase { + + override func setUpWithError() throws { + } + + override func tearDownWithError() throws { + } + + func testAddRelay_ValidRelayURL_NoErrors() { + testAddRelays(urls: [ + "wss://relay.damus.io" + ]) + } + + func testAddRelay_TwoSameURLs_ThrowsRelayAlreadyExists() { + testAddRelays(urls: [ + "wss://relay.damus.io", + "wss://relay.damus.io" + ], expectedError: .RelayAlreadyExists) + } + + func testAddRelay_OneExtraneousSlashURL_ThrowsRelayAlreadyExists() { + testAddRelays(urls: [ + "wss://relay.damus.io", + "wss://relay.damus.io/" + ], expectedError: .RelayAlreadyExists) + } + + func testAddRelay_MultipleExtraneousSlashURL_ThrowsRelayAlreadyExists() { + testAddRelays(urls: [ + "wss://relay.damus.io", + "wss://relay.damus.io///" + ], expectedError: .RelayAlreadyExists) + } + + func testAddRelay_ExtraSlashURLFirst_ThrowsRelayAlreadyExists() { + testAddRelays(urls: [ + "wss://relay.damus.io///", + "wss://relay.damus.io" + ], expectedError: .RelayAlreadyExists) + } +} + +func testAddRelays(urls: [String], expectedError: RelayError? = nil) { + let ndb = Ndb()! + let relayPool = RelayPool(ndb: ndb) + let info = RelayInfo(read: true, write: true) + + do { + for relay in urls { + guard let url = RelayURL(relay) else { + XCTFail("Invalid URL encountered: \(relay)") + return + } + + let descriptor = RelayDescriptor(url: url, info: info) + try relayPool.add_relay(descriptor) + } + + if expectedError != nil { + XCTFail("Expected \(expectedError!) error, but no error was thrown.") + } + } catch let error as RelayError where expectedError == .RelayAlreadyExists { + XCTAssertEqual(error, expectedError!, "Expected RelayAlreadyExists error, got \(error)") + } catch { + XCTFail("An unexpected error was thrown: \(error)") + } +} + +