Skip to content

Commit

Permalink
Append sku_token to refresh route requests
Browse files Browse the repository at this point in the history
  • Loading branch information
S2Ler committed Aug 27, 2021
1 parent 2c0ddf3 commit 3a67542
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
24 changes: 15 additions & 9 deletions Sources/MapboxDirections/Directions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ open class Directions: NSObject {

- parameter credentials: A `DirectionsCredentials` object that, optionally, contains customized Token and Endpoint information. If no credentials object is supplied, then defaults are used.
*/

private var authenticationParams: [URLQueryItem] {
var params: [URLQueryItem] = [
URLQueryItem(name: "access_token", value: credentials.accessToken)
]

if let skuToken = credentials.skuToken {
params.append(URLQueryItem(name: "sku", value: skuToken))
}
return params
}

public init(credentials: DirectionsCredentials = .init()) {
self.credentials = credentials
Expand Down Expand Up @@ -479,9 +490,8 @@ open class Directions: NSObject {
}

open func urlRequest(forRefreshing responseIdentifier: String, routeIndex: Int, fromLegAtIndex startLegIndex: Int) -> URLRequest {
var params: [URLQueryItem] = []
params += [URLQueryItem(name: "access_token", value: credentials.accessToken)]

let params: [URLQueryItem] = authenticationParams

var unparameterizedURL = URL(string: "directions-refresh/v1/\(DirectionsProfileIdentifier.automobileAvoidingTraffic.rawValue)", relativeTo: credentials.host)!
unparameterizedURL.appendPathComponent(responseIdentifier)
unparameterizedURL.appendPathComponent(String(routeIndex))
Expand Down Expand Up @@ -520,12 +530,8 @@ open class Directions: NSObject {
open func url(forCalculating options: DirectionsOptions, httpMethod: String) -> URL {
let includesQuery = httpMethod != "POST"
var params = (includesQuery ? options.urlQueryItems : [])
params += [URLQueryItem(name: "access_token", value: credentials.accessToken)]

if let skuToken = credentials.skuToken {
params += [URLQueryItem(name: "sku", value: skuToken)]
}

params.append(contentsOf: authenticationParams)

let unparameterizedURL = URL(string: includesQuery ? options.path : options.abridgedPath, relativeTo: credentials.host)!
var components = URLComponents(url: unparameterizedURL, resolvingAgainstBaseURL: true)!
components.queryItems = params
Expand Down
2 changes: 1 addition & 1 deletion Sources/MapboxDirections/DirectionsCredentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct DirectionsCredentials: Equatable {
The SKU Token associated with the request. Used for billing.
*/
public var skuToken: String? {
#if os(iOS)
#if !os(Linux)
guard let mbx: AnyClass = NSClassFromString("MBXAccounts") else { return nil }
guard mbx.responds(to: Selector(("serviceSkuToken"))) else { return nil }
return mbx.value(forKeyPath: "serviceSkuToken") as? String
Expand Down
16 changes: 16 additions & 0 deletions Tests/MapboxDirectionsTests/DirectionsCredentialsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@ class DirectionsCredentialsTests: XCTestCase {
UserDefaults.standard.set(expected, forKey: "MBXAccessToken")
XCTAssertEqual(Directions.shared.credentials.accessToken, expected)
}

#if !os(Linux)
func testSkuToken() {
let expectedToken = "a token"
MBXAccounts.serviceSkuToken = expectedToken
XCTAssertEqual(Directions.shared.credentials.skuToken, expectedToken)
MBXAccounts.serviceSkuToken = nil
}
#endif
}

#if !os(Linux)
@objc(MBXAccounts)
final class MBXAccounts: NSObject {
@objc static var serviceSkuToken: String?
}
#endif
35 changes: 30 additions & 5 deletions Tests/MapboxDirectionsTests/DirectionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ Request ID: RAf2XH13mMVxQ96Z1cVQMPrd-hJoVA6LfaWVFDbdN2j-J1VkzaPvZg==

#if !os(Linux)
class DirectionsTests: XCTestCase {
private let skuToken: String = "1234567890"

override func setUp() {
// Make sure tests run in all time zones
NSTimeZone.default = TimeZone(secondsFromGMT: 0)!
MBXAccounts.serviceSkuToken = skuToken
}
override func tearDown() {
HTTPStubs.removeAllStubs()
super.tearDown()
MBXAccounts.serviceSkuToken = nil
}

func testConfiguration() {
Expand All @@ -61,10 +65,15 @@ class DirectionsTests: XCTestCase {
let url = directions.url(forCalculating: options, httpMethod: "GET")
XCTAssertLessThanOrEqual(url.absoluteString.count, MaximumURLLength, "maximumCoordinateCount is too high")

let components = URLComponents(string: url.absoluteString)
XCTAssertEqual(components?.queryItems?.count, 7)
XCTAssertTrue(components?.path.contains(coordinates.compactMap { $0.requestDescription }.joined(separator: ";")) ?? false)

guard let components = URLComponents(string: url.absoluteString),
let queryItems = components.queryItems else {
XCTFail("Invalid url"); return
}
XCTAssertEqual(queryItems.count, 8)
XCTAssertTrue(components.path.contains(coordinates.compactMap { $0.requestDescription }.joined(separator: ";")) )
XCTAssert(queryItems.contains(where: { $0.name == "sku" && $0.value == skuToken }) == true)
XCTAssert(queryItems.contains(where: { $0.name == "access_token" && $0.value == BogusToken }) == true)

let request = directions.urlRequest(forCalculating: options)
XCTAssertEqual(request.httpMethod, "GET")
XCTAssertEqual(request.url, url)
Expand All @@ -79,7 +88,7 @@ class DirectionsTests: XCTestCase {
let request = directions.urlRequest(forCalculating: options)

XCTAssertEqual(request.httpMethod, "POST")
XCTAssertEqual(request.url?.query, "access_token=\(BogusToken)")
XCTAssertEqual(request.url?.query, "access_token=\(BogusToken)&sku=\(skuToken)")
XCTAssertNotNil(request.httpBody)
var components = URLComponents()
components.query = String(data: request.httpBody ?? Data(), encoding: .utf8)
Expand Down Expand Up @@ -192,5 +201,21 @@ class DirectionsTests: XCTestCase {
})
wait(for: [expectation], timeout: 2.0)
}

func testRefreshRouteRequest() {
let directions = Directions(credentials: BogusCredentials)
guard let url = directions.urlRequest(forRefreshing: "any", routeIndex: 0, fromLegAtIndex: 0).url else {
XCTFail("Incorrect request"); return
}
XCTAssertLessThanOrEqual(url.absoluteString.count, MaximumURLLength, "maximumCoordinateCount is too high")

guard let queryItems = URLComponents(string: url.absoluteString)?.queryItems else {
XCTFail("Invalid url"); return
}
XCTAssertEqual(queryItems.count, 2)
XCTAssertTrue(queryItems.contains(where: { $0.name == "sku" && $0.value == skuToken }))
XCTAssertTrue(queryItems.contains(where: { $0.name == "access_token" && $0.value == BogusToken }))
}

}
#endif

0 comments on commit 3a67542

Please sign in to comment.