Cleanup: Don't accept optional in init

This commit is contained in:
Timothy Qiu
2017-08-01 19:31:12 +08:00
parent fb46501ff4
commit f1db8fff41
2 changed files with 12 additions and 22 deletions

View File

@ -31,7 +31,7 @@ class ServerProfile: NSObject, NSCopying {
self.uuid = uuid self.uuid = uuid
} }
convenience init?(url: URL?) { convenience init?(url: URL) {
self.init() self.init()
func padBase64(string: String) -> String { func padBase64(string: String) -> String {
@ -44,14 +44,12 @@ class ServerProfile: NSObject, NSCopying {
} }
} }
func decodeUrl(url: URL?) -> String? { func decodeUrl(url: URL) -> String? {
guard let urlStr = url?.absoluteString else { let urlStr = url.absoluteString
return nil
}
let index = urlStr.index(urlStr.startIndex, offsetBy: 5) let index = urlStr.index(urlStr.startIndex, offsetBy: 5)
let encodedStr = urlStr.substring(from: index) let encodedStr = urlStr.substring(from: index)
guard let data = Data(base64Encoded: padBase64(string: encodedStr)) else { 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 { guard let decoded = String(data: data, encoding: String.Encoding.utf8) else {
return nil return nil

View File

@ -31,7 +31,7 @@ class ServerProfileTests: XCTestCase {
} }
func testInitWithSelfGeneratedURL() { func testInitWithSelfGeneratedURL() {
let newProfile = ServerProfile.init(url: profile.URL()) let newProfile = ServerProfile.init(url: profile.URL()!)
XCTAssertEqual(newProfile?.serverHost, profile.serverHost) XCTAssertEqual(newProfile?.serverHost, profile.serverHost)
XCTAssertEqual(newProfile?.serverPort, profile.serverPort) XCTAssertEqual(newProfile?.serverPort, profile.serverPort)
@ -42,7 +42,7 @@ class ServerProfileTests: XCTestCase {
} }
func testInitWithPlainURL() { 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) let profile = ServerProfile(url: url)
@ -57,7 +57,7 @@ class ServerProfileTests: XCTestCase {
} }
func testInitWithPlainURLandQuery() { 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) let profile = ServerProfile(url: url)
@ -72,7 +72,7 @@ class ServerProfileTests: XCTestCase {
} }
func testInitWithPlainURLandAnotherQuery() { 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) let profile = ServerProfile(url: url)
@ -88,7 +88,7 @@ class ServerProfileTests: XCTestCase {
func testInitWithBase64EncodedURL() { func testInitWithBase64EncodedURL() {
// "ss://aes-256-cfb:password@example.com:8388" // "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) let profile = ServerProfile(url: url)
@ -104,7 +104,7 @@ class ServerProfileTests: XCTestCase {
func testInitWithBase64EncodedURLandQuery() { func testInitWithBase64EncodedURLandQuery() {
// "ss://aes-256-cfb:password@example.com:8388?Remark=Prism&OTA=true" // "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) let profile = ServerProfile(url: url)
@ -119,15 +119,7 @@ class ServerProfileTests: XCTestCase {
} }
func testInitWithEmptyURL() { func testInitWithEmptyURL() {
let url = URL(string: "ss://") let url = URL(string: "ss://")!
let profile = ServerProfile(url: url)
XCTAssertNil(profile)
}
func testInitWithInvalidURL() {
let url = URL(string: "ss://invalid url")
let profile = ServerProfile(url: url) let profile = ServerProfile(url: url)
@ -136,7 +128,7 @@ class ServerProfileTests: XCTestCase {
func testInitWithBase64EncodedInvalidURL() { func testInitWithBase64EncodedInvalidURL() {
// "ss://invalid url" // "ss://invalid url"
let url = URL(string: "ss://aW52YWxpZCB1cmw") let url = URL(string: "ss://aW52YWxpZCB1cmw")!
let profile = ServerProfile(url: url) let profile = ServerProfile(url: url)