mirror of
https://github.com/mas-cli/mas
synced 2025-02-18 05:28:24 +00:00
⛔️ Remove impossible error case
This commit is contained in:
parent
f7dd98811f
commit
844131ff2e
4 changed files with 18 additions and 33 deletions
|
@ -30,9 +30,9 @@ class MasStoreSearch: StoreSearch {
|
||||||
///
|
///
|
||||||
/// - Parameter appName: MAS app identifier.
|
/// - Parameter appName: MAS app identifier.
|
||||||
/// - Returns: URL for the search service or nil if appName can't be encoded.
|
/// - Returns: URL for the search service or nil if appName can't be encoded.
|
||||||
static func searchURL(for appName: String) -> URL? {
|
static func searchURL(for appName: String) -> URL {
|
||||||
guard var components = URLComponents(string: "https://itunes.apple.com/search") else {
|
guard var components = URLComponents(string: "https://itunes.apple.com/search") else {
|
||||||
return nil
|
fatalError("URLComponents failed to parse URL.")
|
||||||
}
|
}
|
||||||
|
|
||||||
components.queryItems = [
|
components.queryItems = [
|
||||||
|
@ -40,20 +40,28 @@ class MasStoreSearch: StoreSearch {
|
||||||
URLQueryItem(name: "entity", value: "macSoftware"),
|
URLQueryItem(name: "entity", value: "macSoftware"),
|
||||||
URLQueryItem(name: "term", value: appName),
|
URLQueryItem(name: "term", value: appName),
|
||||||
]
|
]
|
||||||
return components.url
|
guard let url = components.url else {
|
||||||
|
fatalError("URLComponents failed to generate URL.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds the lookup URL for an app.
|
/// Builds the lookup URL for an app.
|
||||||
///
|
///
|
||||||
/// - Parameter appId: MAS app identifier.
|
/// - Parameter appId: MAS app identifier.
|
||||||
/// - Returns: URL for the lookup service or nil if appId can't be encoded.
|
/// - Returns: URL for the lookup service or nil if appId can't be encoded.
|
||||||
static func lookupURL(forApp appId: Int) -> URL? {
|
static func lookupURL(forApp appId: Int) -> URL {
|
||||||
guard var components = URLComponents(string: "https://itunes.apple.com/lookup") else {
|
guard var components = URLComponents(string: "https://itunes.apple.com/lookup") else {
|
||||||
return nil
|
fatalError("URLComponents failed to parse URL.")
|
||||||
}
|
}
|
||||||
|
|
||||||
components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")]
|
components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")]
|
||||||
return components.url
|
guard let url = components.url else {
|
||||||
|
fatalError("URLComponents failed to generate URL.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Searches for an app.
|
/// Searches for an app.
|
||||||
|
@ -62,11 +70,7 @@ class MasStoreSearch: StoreSearch {
|
||||||
/// - Parameter completion: A closure that receives the search results or an Error if there is a
|
/// - Parameter completion: A closure that receives the search results or an Error if there is a
|
||||||
/// problem with the network request. Results array will be empty if there were no matches.
|
/// problem with the network request. Results array will be empty if there were no matches.
|
||||||
func search(for appName: String) -> Promise<[SearchResult]> {
|
func search(for appName: String) -> Promise<[SearchResult]> {
|
||||||
guard let url = MasStoreSearch.searchURL(for: appName)
|
let url = MasStoreSearch.searchURL(for: appName)
|
||||||
else {
|
|
||||||
return Promise(error: MASError.urlEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
return loadSearchResults(url)
|
return loadSearchResults(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,11 +80,7 @@ class MasStoreSearch: StoreSearch {
|
||||||
/// - Returns: A Promise for the search result record of app, or nil if no apps match the ID,
|
/// - Returns: A Promise for the search result record of app, or nil if no apps match the ID,
|
||||||
/// or an Error if there is a problem with the network request.
|
/// or an Error if there is a problem with the network request.
|
||||||
func lookup(app appId: Int) -> Promise<SearchResult?> {
|
func lookup(app appId: Int) -> Promise<SearchResult?> {
|
||||||
guard let url = MasStoreSearch.lookupURL(forApp: appId)
|
let url = MasStoreSearch.lookupURL(forApp: appId)
|
||||||
else {
|
|
||||||
return Promise(error: MASError.urlEncoding)
|
|
||||||
}
|
|
||||||
|
|
||||||
return loadSearchResults(url).map { results in results.first }
|
return loadSearchResults(url).map { results in results.first }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,6 @@ public enum MASError: Error, Equatable {
|
||||||
case notInstalled
|
case notInstalled
|
||||||
case uninstallFailed
|
case uninstallFailed
|
||||||
|
|
||||||
case urlEncoding
|
|
||||||
case noData
|
case noData
|
||||||
case jsonParsing(error: NSError?)
|
case jsonParsing(error: NSError?)
|
||||||
}
|
}
|
||||||
|
@ -91,9 +90,6 @@ extension MASError: CustomStringConvertible {
|
||||||
case .uninstallFailed:
|
case .uninstallFailed:
|
||||||
return "Uninstall failed"
|
return "Uninstall failed"
|
||||||
|
|
||||||
case .urlEncoding:
|
|
||||||
return "Unable to encode service URL"
|
|
||||||
|
|
||||||
case .noData:
|
case .noData:
|
||||||
return "Service did not return data"
|
return "Service did not return data"
|
||||||
|
|
||||||
|
|
|
@ -19,22 +19,16 @@ public class MasStoreSearchSpec: QuickSpec {
|
||||||
describe("url string") {
|
describe("url string") {
|
||||||
it("contains the app name") {
|
it("contains the app name") {
|
||||||
let appName = "myapp"
|
let appName = "myapp"
|
||||||
let urlString = MasStoreSearch.searchURL(for: appName)?.absoluteString
|
let urlString = MasStoreSearch.searchURL(for: appName).absoluteString
|
||||||
expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)"
|
expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)"
|
||||||
}
|
}
|
||||||
it("contains the encoded app name") {
|
it("contains the encoded app name") {
|
||||||
let appName = "My App"
|
let appName = "My App"
|
||||||
let appNameEncoded = "My%20App"
|
let appNameEncoded = "My%20App"
|
||||||
let urlString = MasStoreSearch.searchURL(for: appName)?.absoluteString
|
let urlString = MasStoreSearch.searchURL(for: appName).absoluteString
|
||||||
expect(urlString)
|
expect(urlString)
|
||||||
== "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appNameEncoded)"
|
== "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appNameEncoded)"
|
||||||
}
|
}
|
||||||
// Find a character that causes addingPercentEncoding(withAllowedCharacters to return nil
|
|
||||||
xit("is nil when app name cannot be url encoded") {
|
|
||||||
let appName = "`~!@#$%^&*()_+ 💩"
|
|
||||||
let urlString = MasStoreSearch.searchURL(for: appName)?.absoluteString
|
|
||||||
expect(urlString).to(beNil())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
describe("store") {
|
describe("store") {
|
||||||
context("when searched") {
|
context("when searched") {
|
||||||
|
|
|
@ -116,11 +116,6 @@ class MASErrorTestCase: XCTestCase {
|
||||||
XCTAssertEqual(error.description, "Uninstall failed")
|
XCTAssertEqual(error.description, "Uninstall failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testUrlEncoding() {
|
|
||||||
error = .urlEncoding
|
|
||||||
XCTAssertEqual(error.description, "Unable to encode service URL")
|
|
||||||
}
|
|
||||||
|
|
||||||
func testNoData() {
|
func testNoData() {
|
||||||
error = .noData
|
error = .noData
|
||||||
XCTAssertEqual(error.description, "Service did not return data")
|
XCTAssertEqual(error.description, "Service did not return data")
|
||||||
|
|
Loading…
Add table
Reference in a new issue