parent
ab18cf3658
commit
5be9f28ff3
@ -0,0 +1,32 @@ |
||||
// Copyright SIX DAY LLC. All rights reserved. |
||||
|
||||
import Foundation |
||||
|
||||
class BrowserURLParser { |
||||
|
||||
private let defaultURL = "google.com" |
||||
let searchURL = "https://www.google.com/search?q=" |
||||
let schemes = ["http", "https"] |
||||
|
||||
init() { |
||||
|
||||
} |
||||
|
||||
func url(from string: String) -> URL? { |
||||
let urlString = appendScheme(for: string) |
||||
let component = NSURLComponents(string: urlString) |
||||
guard let componentURL = component?.url, let _ = component?.host else { |
||||
return searchURL(for: string) |
||||
} |
||||
return componentURL |
||||
} |
||||
|
||||
func appendScheme(for string: String) -> String { |
||||
let values = schemes.filter { string.hasPrefix($0) } |
||||
return values.isEmpty ? "http://" + string : string |
||||
} |
||||
|
||||
private func searchURL(for query: String) -> URL? { |
||||
return URL(string: searchURL + query)! |
||||
} |
||||
} |
@ -0,0 +1,49 @@ |
||||
// Copyright SIX DAY LLC. All rights reserved. |
||||
|
||||
import XCTest |
||||
@testable import Trust |
||||
|
||||
class BrowserURLParserTests: XCTestCase { |
||||
|
||||
// func testQueryReturnsSearchEngineURL() { |
||||
// let parser = BrowserURLParser() |
||||
// let query = "1" |
||||
// let result = parser.url(from: query) |
||||
// let expetected = parser.searchURL + query |
||||
// |
||||
// XCTAssertEqual(expetected, result?.absoluteString) |
||||
// } |
||||
|
||||
func testParseDomain() { |
||||
let parser = BrowserURLParser() |
||||
let query = "trustwalletapp.com" |
||||
let result = parser.url(from: query) |
||||
|
||||
XCTAssertEqual("http://" + query, result?.absoluteString) |
||||
} |
||||
|
||||
func testParseHttp() { |
||||
let parser = BrowserURLParser() |
||||
let query = "http://trustwalletapp.com" |
||||
let result = parser.url(from: query) |
||||
|
||||
XCTAssertEqual(query, result?.absoluteString) |
||||
} |
||||
|
||||
func testParseHttps() { |
||||
let parser = BrowserURLParser() |
||||
let query = "https://trustwalletapp.com" |
||||
let result = parser.url(from: query) |
||||
|
||||
XCTAssertEqual(query, result?.absoluteString) |
||||
} |
||||
|
||||
func testSearchURL() { |
||||
let parser = BrowserURLParser() |
||||
let query = "test" |
||||
let result = parser.searchURL(for: query) |
||||
let expeted = parser.searchURL + query |
||||
|
||||
XCTAssertEqual(expeted, result?.absoluteString) |
||||
} |
||||
} |
Loading…
Reference in new issue