♻️ Move URL builders to MasStoreSearch

This commit is contained in:
Ben Chatelain 2022-10-08 17:32:26 -06:00
parent 85a31ed5ba
commit f7dd98811f
4 changed files with 53 additions and 57 deletions

View file

@ -26,13 +26,43 @@ class MasStoreSearch: StoreSearch {
self.networkManager = networkManager
}
/// Builds the search URL for an app.
///
/// - Parameter appName: MAS app identifier.
/// - Returns: URL for the search service or nil if appName can't be encoded.
static func searchURL(for appName: String) -> URL? {
guard var components = URLComponents(string: "https://itunes.apple.com/search") else {
return nil
}
components.queryItems = [
URLQueryItem(name: "media", value: "software"),
URLQueryItem(name: "entity", value: "macSoftware"),
URLQueryItem(name: "term", value: appName),
]
return components.url
}
/// Builds the lookup URL for an app.
///
/// - Parameter appId: MAS app identifier.
/// - Returns: URL for the lookup service or nil if appId can't be encoded.
static func lookupURL(forApp appId: Int) -> URL? {
guard var components = URLComponents(string: "https://itunes.apple.com/lookup") else {
return nil
}
components.queryItems = [URLQueryItem(name: "id", value: "\(appId)")]
return components.url
}
/// Searches for an app.
///
/// - Parameter appName: MAS ID of app
/// - 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.
func search(for appName: String) -> Promise<[SearchResult]> {
guard let url = searchURL(for: appName)
guard let url = MasStoreSearch.searchURL(for: appName)
else {
return Promise(error: MASError.urlEncoding)
}
@ -46,7 +76,7 @@ class MasStoreSearch: StoreSearch {
/// - 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.
func lookup(app appId: Int) -> Promise<SearchResult?> {
guard let url = lookupURL(forApp: appId)
guard let url = MasStoreSearch.lookupURL(forApp: appId)
else {
return Promise(error: MASError.urlEncoding)
}

View file

@ -6,8 +6,8 @@
// Copyright © 2018 mas-cli. All rights reserved.
//
import Foundation
import PromiseKit
import Foundation
/// Protocol for searching the MAS catalog.
protocol StoreSearch {

View file

@ -16,6 +16,26 @@ public class MasStoreSearchSpec: QuickSpec {
beforeSuite {
MasKit.initialize()
}
describe("url string") {
it("contains the app name") {
let appName = "myapp"
let urlString = MasStoreSearch.searchURL(for: appName)?.absoluteString
expect(urlString) == "https://itunes.apple.com/search?media=software&entity=macSoftware&term=\(appName)"
}
it("contains the encoded app name") {
let appName = "My App"
let appNameEncoded = "My%20App"
let urlString = MasStoreSearch.searchURL(for: appName)?.absoluteString
expect(urlString)
== "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") {
context("when searched") {
it("can find slack") {

View file

@ -1,54 +0,0 @@
//
// StoreSearchSpec.swift
// MasKitTests
//
// Created by Ben Chatelain on 1/11/19.
// Copyright © 2019 mas-cli. All rights reserved.
//
import Foundation
import Nimble
import PromiseKit
import Quick
@testable import MasKit
/// Protocol minimal implementation
struct StoreSearchForTesting: StoreSearch {
func lookup(app _: Int) -> Promise<SearchResult?> {
.value(nil)
}
func search(for _: String) -> Promise<[SearchResult]> {
.value([])
}
}
public class StoreSearchSpec: QuickSpec {
override public func spec() {
let storeSearch = StoreSearchForTesting()
let region = Locale.autoupdatingCurrent.regionCode!
describe("url string") {
it("contains the app name") {
let appName = "myapp"
let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString) == "https://itunes.apple.com/search?"
+ "media=software&entity=macSoftware&term=\(appName)&country=\(region)"
}
it("contains the encoded app name") {
let appName = "My App"
let appNameEncoded = "My%20App"
let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString) == "https://itunes.apple.com/search?"
+ "media=software&entity=macSoftware&term=\(appNameEncoded)&country=\(region)"
}
// Find a character that causes addingPercentEncoding(withAllowedCharacters to return nil
xit("is nil when app name cannot be url encoded") {
let appName = "`~!@#$%^&*()_+ 💩"
let urlString = storeSearch.searchURL(for: appName)?.absoluteString
expect(urlString).to(beNil())
}
}
}
}