File tree Expand file tree Collapse file tree 5 files changed +49
-9
lines changed
BitwardenShared/Core/Platform Expand file tree Collapse file tree 5 files changed +49
-9
lines changed Original file line number Diff line number Diff line change @@ -93,12 +93,18 @@ extension URL {
93
93
}
94
94
95
95
/// Returns a sanitized version of the URL. This will add a https scheme to the URL if the
96
- /// scheme is missing.
96
+ /// scheme is missing and remove a trailing slash .
97
97
var sanitized : URL {
98
- guard absoluteString. starts ( with: " https:// " ) || absoluteString. starts ( with: " http:// " ) else {
99
- return URL ( string: " https:// " + absoluteString) ?? self
98
+ let stringUrl = if absoluteString. hasSuffix ( " / " ) {
99
+ String ( absoluteString. dropLast ( ) )
100
+ } else {
101
+ absoluteString
102
+ }
103
+
104
+ guard stringUrl. starts ( with: " https:// " ) || stringUrl. starts ( with: " http:// " ) else {
105
+ return URL ( string: " https:// " + stringUrl) ?? self
100
106
}
101
- return self
107
+ return URL ( string : stringUrl ) ?? self
102
108
}
103
109
104
110
// MARK: Methods
Original file line number Diff line number Diff line change @@ -82,6 +82,18 @@ class URLTests: BitwardenTestCase {
82
82
)
83
83
}
84
84
85
+ /// `sanitized` removes a trailing slash from the URL.
86
+ func test_sanitized_trailingSlash( ) {
87
+ XCTAssertEqual (
88
+ URL ( string: " https://bitwarden.com/ " ) ? . sanitized,
89
+ URL ( string: " https://bitwarden.com " )
90
+ )
91
+ XCTAssertEqual (
92
+ URL ( string: " example.com/ " ) ? . sanitized,
93
+ URL ( string: " https://example.com " )
94
+ )
95
+ }
96
+
85
97
/// `sanitized` returns the URL unchanged if it's valid and contains a scheme.
86
98
func test_sanitized_validURL( ) {
87
99
XCTAssertEqual (
Original file line number Diff line number Diff line change @@ -109,7 +109,7 @@ extension EnvironmentUrlData {
109
109
// which includes the `#` symbol. Since the `#` character is a critical portion of these urls, we use String
110
110
// concatenation to get around this limitation.
111
111
if let baseUrl = webVault ?? base,
112
- let url = URL ( string: baseUrl. absoluteString. appending ( " /#/ \( additionalPath) " ) ) {
112
+ let url = URL ( string: baseUrl. sanitized . absoluteString. appending ( " /#/ \( additionalPath) " ) ) {
113
113
return url
114
114
}
115
115
return nil
Original file line number Diff line number Diff line change @@ -43,11 +43,11 @@ extension EnvironmentUrls {
43
43
///
44
44
init ( environmentUrlData: EnvironmentUrlData ) {
45
45
if let base = environmentUrlData. base {
46
- apiURL = base. appendingPathComponent ( " / api" )
46
+ apiURL = base. appendingPathComponent ( " api " )
47
47
baseURL = base
48
- eventsURL = base. appendingPathComponent ( " / events" )
49
- iconsURL = base. appendingPathComponent ( " / icons" )
50
- identityURL = base. appendingPathComponent ( " / identity" )
48
+ eventsURL = base. appendingPathComponent ( " events " )
49
+ iconsURL = base. appendingPathComponent ( " icons " )
50
+ identityURL = base. appendingPathComponent ( " identity " )
51
51
webVaultURL = base
52
52
} else {
53
53
apiURL = environmentUrlData. api ?? URL ( string: " https://api.bitwarden.com " ) !
Original file line number Diff line number Diff line change @@ -27,6 +27,28 @@ class EnvironmentUrlsTests: BitwardenTestCase {
27
27
)
28
28
}
29
29
30
+ /// `init(environmentUrlData:)` sets the URLs from the base URL which includes a trailing slash.
31
+ func test_init_environmentUrlData_baseUrlWithTrailingSlash( ) {
32
+ let subject = EnvironmentUrls (
33
+ environmentUrlData: EnvironmentUrlData ( base: URL ( string: " https://example.com/ " ) !)
34
+ )
35
+ XCTAssertEqual (
36
+ subject,
37
+ EnvironmentUrls (
38
+ apiURL: URL ( string: " https://example.com/api " ) !,
39
+ baseURL: URL ( string: " https://example.com/ " ) !,
40
+ eventsURL: URL ( string: " https://example.com/events " ) !,
41
+ iconsURL: URL ( string: " https://example.com/icons " ) !,
42
+ identityURL: URL ( string: " https://example.com/identity " ) !,
43
+ importItemsURL: URL ( string: " https://example.com/#/tools/import " ) !,
44
+ recoveryCodeURL: URL ( string: " https://example.com/#/recover-2fa " ) !,
45
+ sendShareURL: URL ( string: " https://example.com/#/send " ) !,
46
+ settingsURL: URL ( string: " https://example.com/#/settings " ) !,
47
+ webVaultURL: URL ( string: " https://example.com/ " ) !
48
+ )
49
+ )
50
+ }
51
+
30
52
/// `init(environmentUrlData:)` sets the URLs based on the corresponding URL if there isn't a base URL.
31
53
func test_init_environmentUrlData_custom( ) {
32
54
let subject = EnvironmentUrls (
You can’t perform that action at this time.
0 commit comments