Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update external link interceptor with optional param to override defa… #14

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Sources/ShopifyCheckout/CheckoutView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ extension CheckoutView: WKNavigationDelegate {
}

if isExternalLink(action) || isMailOrTelLink(url) {
viewDelegate?.checkoutViewDidClickLink(url: url)
viewDelegate?.checkoutViewDidClickLink(url: removeExternalParam(url))
decisionHandler(.cancel)
return
}
Expand Down Expand Up @@ -170,9 +170,24 @@ extension CheckoutView: WKNavigationDelegate {
}

private func isExternalLink(_ action: WKNavigationAction) -> Bool {
return action.navigationType == .linkActivated && action.targetFrame == nil
if action.navigationType == .linkActivated && action.targetFrame == nil { return true }

guard let url = action.request.url else { return false }
guard let url = URLComponents(url: url, resolvingAgainstBaseURL: false) else { return false }

guard let openExternally = url.queryItems?.first(where: { $0.name == "open_externally" })?.value else { return false }

return openExternally.lowercased() == "true" || openExternally == "1"
}

private func removeExternalParam(_ url: URL) -> URL {
guard var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
return url
}
urlComponents.queryItems = urlComponents.queryItems?.filter { !($0.name == "open_externally") }
return urlComponents.url ?? url
}

private func isMailOrTelLink(_ url: URL) -> Bool {
return ["mailto", "tel"].contains(url.scheme)
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/ShopifyCheckoutTests/CheckoutViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ class CheckoutViewTests: XCTestCase {
wait(for: [didClickLinkExpectation], timeout: 1)
}

func testURLLinkDelegationWithExternalParam() {
let link = URL(string: "https://www.shopify.com/legal/privacy/app-users?open_externally=true")!

let delegate = MockCheckoutViewDelegate()
let didClickLinkExpectation = expectation(
description: "checkoutViewDidClickLink was called"
)
delegate.didClickLinkExpectation = didClickLinkExpectation
view.viewDelegate = delegate

view.webView(view, decidePolicyFor: MockExternalNavigationAction(url: link, navigationType: .other)) { policy in
XCTAssertEqual(policy, .cancel)
}

wait(for: [didClickLinkExpectation], timeout: 1)
}

func test410responseOnCheckoutURLCodeDelegation() {
view.load(checkout: URL(string: "http://shopify1.shopify.com/checkouts/cn/123")!)
let link = view.url!
Expand Down
6 changes: 4 additions & 2 deletions Tests/ShopifyCheckoutTests/Mocks/MockNavigationAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,23 @@ class MockNavigationAction: WKNavigationAction {

class MockExternalNavigationAction: WKNavigationAction {
private let mockRequest: URLRequest
private let navType: WKNavigationType

override var request: URLRequest {
return mockRequest
}

override var navigationType: WKNavigationType {
return .linkActivated
return self.navType
}

override var targetFrame: WKFrameInfo? {
return nil
}

init(url: URL) {
init(url: URL, navigationType: WKNavigationType = .linkActivated) {
self.mockRequest = URLRequest(url: url)
self.navType = navigationType
super.init()
}
}