damus

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

commit d4faacb99f81c639b3341712d8c102da11d4c13b
parent a73271e3d4d34b8a853e335573fc165f171648a8
Author: William Casarin <jb55@jb55.com>
Date:   Sun,  6 Aug 2023 09:02:18 -0700

relays: strip trailing / from relay urls

Fixes: https://github.com/damus-io/damus/issues/1443
Changelog-Fixed: Fix issue with slashes on relay urls causing relay connection problems

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 4++++
Mdamus/Nostr/RelayConnection.swift | 9++++++++-
AdamusTests/UrlTests.swift | 28++++++++++++++++++++++++++++
3 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -171,6 +171,7 @@ 4C64987E286D082C00EAE2B3 /* DirectMessagesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C64987D286D082C00EAE2B3 /* DirectMessagesModel.swift */; }; 4C649881286E0EE300EAE2B3 /* secp256k1 in Frameworks */ = {isa = PBXBuildFile; productRef = 4C649880286E0EE300EAE2B3 /* secp256k1 */; }; 4C684A552A7E91FE005E6031 /* LongPostTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C684A542A7E91FE005E6031 /* LongPostTests.swift */; }; + 4C684A572A7FFAE6005E6031 /* UrlTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C684A562A7FFAE6005E6031 /* UrlTests.swift */; }; 4C687C212A5F7ED00092C550 /* DamusBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C687C202A5F7ED00092C550 /* DamusBackground.swift */; }; 4C687C242A5FA86D0092C550 /* SearchHeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C687C232A5FA86D0092C550 /* SearchHeaderView.swift */; }; 4C687C272A6039500092C550 /* TestData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C687C262A6039500092C550 /* TestData.swift */; }; @@ -699,6 +700,7 @@ 4C64987B286D03E000EAE2B3 /* DirectMessagesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DirectMessagesView.swift; sourceTree = "<group>"; }; 4C64987D286D082C00EAE2B3 /* DirectMessagesModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DirectMessagesModel.swift; sourceTree = "<group>"; }; 4C684A542A7E91FE005E6031 /* LongPostTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LongPostTests.swift; sourceTree = "<group>"; }; + 4C684A562A7FFAE6005E6031 /* UrlTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UrlTests.swift; sourceTree = "<group>"; }; 4C687C202A5F7ED00092C550 /* DamusBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DamusBackground.swift; sourceTree = "<group>"; }; 4C687C232A5FA86D0092C550 /* SearchHeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchHeaderView.swift; sourceTree = "<group>"; }; 4C687C262A6039500092C550 /* TestData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestData.swift; sourceTree = "<group>"; }; @@ -1742,6 +1744,7 @@ 3AAC7A012A60FE72002B50DF /* LocalizationUtilTests.swift */, D78525242A7B2EA4002FA637 /* NoteContentViewTests.swift */, 4C684A542A7E91FE005E6031 /* LongPostTests.swift */, + 4C684A562A7FFAE6005E6031 /* UrlTests.swift */, ); path = damusTests; sourceTree = "<group>"; @@ -2441,6 +2444,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 4C684A572A7FFAE6005E6031 /* UrlTests.swift in Sources */, 5019CADD2A0FB0A9000069E1 /* ProfileDatabaseTests.swift in Sources */, 3A90B1832A4EA3C600000D94 /* UserSearchCacheTests.swift in Sources */, 4C9B0DEE2A65A75F00CBDA21 /* AttrStringTestExtensions.swift in Sources */, diff --git a/damus/Nostr/RelayConnection.swift b/damus/Nostr/RelayConnection.swift @@ -21,7 +21,14 @@ public struct RelayURL: Hashable { } init?(_ str: String) { - guard let url = URL(string: str) else { + guard let last = str.last else { return nil } + + var urlstr = str + if last == "/" { + urlstr = String(str.dropLast(1)) + } + + guard let url = URL(string: urlstr) else { return nil } diff --git a/damusTests/UrlTests.swift b/damusTests/UrlTests.swift @@ -0,0 +1,28 @@ +// +// UrlTests.swift +// damusTests +// +// Created by William Casarin on 2023-08-06. +// + +import XCTest +@testable import damus + +final class UrlTests: XCTestCase { + + override func setUpWithError() throws { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDownWithError() throws { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func testRelayUrlStripsEndingSlash() throws { + let url1 = RelayURL("wss://jb55.com/")! + let url2 = RelayURL("wss://jb55.com")! + XCTAssertEqual(url1, url2) + XCTAssertEqual(url1.url.absoluteString, "wss://jb55.com") + } + +}