From f1db8fff419adfc564dfc78b63ea88285d13930d Mon Sep 17 00:00:00 2001 From: Timothy Qiu Date: Tue, 1 Aug 2017 19:31:12 +0800 Subject: [PATCH] Cleanup: Don't accept optional in init --- ShadowsocksX-NG/ServerProfile.swift | 10 ++++---- ShadowsocksX-NGTests/ServerProfileTests.swift | 24 +++++++------------ 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/ShadowsocksX-NG/ServerProfile.swift b/ShadowsocksX-NG/ServerProfile.swift index dfb1fa0..f5f01f5 100644 --- a/ShadowsocksX-NG/ServerProfile.swift +++ b/ShadowsocksX-NG/ServerProfile.swift @@ -31,7 +31,7 @@ class ServerProfile: NSObject, NSCopying { self.uuid = uuid } - convenience init?(url: URL?) { + convenience init?(url: URL) { self.init() func padBase64(string: String) -> String { @@ -44,14 +44,12 @@ class ServerProfile: NSObject, NSCopying { } } - func decodeUrl(url: URL?) -> String? { - guard let urlStr = url?.absoluteString else { - return nil - } + func decodeUrl(url: URL) -> String? { + let urlStr = url.absoluteString let index = urlStr.index(urlStr.startIndex, offsetBy: 5) let encodedStr = urlStr.substring(from: index) guard let data = Data(base64Encoded: padBase64(string: encodedStr)) else { - return url?.absoluteString + return url.absoluteString } guard let decoded = String(data: data, encoding: String.Encoding.utf8) else { return nil diff --git a/ShadowsocksX-NGTests/ServerProfileTests.swift b/ShadowsocksX-NGTests/ServerProfileTests.swift index d942895..8204f42 100644 --- a/ShadowsocksX-NGTests/ServerProfileTests.swift +++ b/ShadowsocksX-NGTests/ServerProfileTests.swift @@ -31,7 +31,7 @@ class ServerProfileTests: XCTestCase { } func testInitWithSelfGeneratedURL() { - let newProfile = ServerProfile.init(url: profile.URL()) + let newProfile = ServerProfile.init(url: profile.URL()!) XCTAssertEqual(newProfile?.serverHost, profile.serverHost) XCTAssertEqual(newProfile?.serverPort, profile.serverPort) @@ -42,7 +42,7 @@ class ServerProfileTests: XCTestCase { } func testInitWithPlainURL() { - let url = URL(string: "ss://aes-256-cfb:password@example.com:8388") + let url = URL(string: "ss://aes-256-cfb:password@example.com:8388")! let profile = ServerProfile(url: url) @@ -57,7 +57,7 @@ class ServerProfileTests: XCTestCase { } func testInitWithPlainURLandQuery() { - let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true") + let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true")! let profile = ServerProfile(url: url) @@ -72,7 +72,7 @@ class ServerProfileTests: XCTestCase { } func testInitWithPlainURLandAnotherQuery() { - let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=0") + let url = URL(string: "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=0")! let profile = ServerProfile(url: url) @@ -88,7 +88,7 @@ class ServerProfileTests: XCTestCase { func testInitWithBase64EncodedURL() { // "ss://aes-256-cfb:password@example.com:8388" - let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA") + let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OA")! let profile = ServerProfile(url: url) @@ -104,7 +104,7 @@ class ServerProfileTests: XCTestCase { func testInitWithBase64EncodedURLandQuery() { // "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true" - let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU") + let url = URL(string: "ss://YWVzLTI1Ni1jZmI6cGFzc3dvcmRAZXhhbXBsZS5jb206ODM4OD9SZW1hcms9UHJpc20mT1RBPXRydWU")! let profile = ServerProfile(url: url) @@ -119,15 +119,7 @@ class ServerProfileTests: XCTestCase { } func testInitWithEmptyURL() { - let url = URL(string: "ss://") - - let profile = ServerProfile(url: url) - - XCTAssertNil(profile) - } - - func testInitWithInvalidURL() { - let url = URL(string: "ss://invalid url") + let url = URL(string: "ss://")! let profile = ServerProfile(url: url) @@ -136,7 +128,7 @@ class ServerProfileTests: XCTestCase { func testInitWithBase64EncodedInvalidURL() { // "ss://invalid url" - let url = URL(string: "ss://aW52YWxpZCB1cmw") + let url = URL(string: "ss://aW52YWxpZCB1cmw")! let profile = ServerProfile(url: url)