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>
This commit is contained in:
committed by
William Casarin
parent
dfcef0ba95
commit
34e32bc930
@@ -109,5 +109,13 @@ enum RelayError: Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func get_relay_id(_ url: RelayURL) -> String {
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
82
damusTests/RelayPoolTests.swift
Normal file
82
damusTests/RelayPoolTests.swift
Normal file
@@ -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)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user