Skip to content

Commit

Permalink
chore: Fix Clickable Image bug for invalid url (RMCCX-7351)
Browse files Browse the repository at this point in the history
  • Loading branch information
SoumenRautray committed Sep 20, 2024
1 parent bb8ca90 commit 9323072
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Sources/RInAppMessaging/Utilities/CommonUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ internal struct CommonUtility {
Logger.debug("Failed converting value \(attribute.value) to \(attribute.type)")
return nil
}

static func isValidURL(_ urlString: String) -> Bool {
let pattern = "^(https://.*|.*://.*)$"
guard let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
return false
}
let range = NSRange(location: 0, length: urlString.utf16.count)
return regex.firstMatch(in: urlString, options: [], range: range) != nil
}
}

internal enum Logger {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,17 @@ internal class FullViewPresenter: BaseViewPresenter, FullViewPresenterType, Erro
func didClickCampaignImage() {
guard !campaign.isPushPrimer else { return }
guard let clickImageData = campaign.data.customJson?.clickableImage,
CommonUtility.isValidURL(clickImageData.url ?? ""),
let uriToOpen = URL(string: clickImageData.url ?? "") else {
return
}
logImpression(type: .clickContent)
sendImpressions()
UIApplication.shared.open(uriToOpen)
UIApplication.shared.open(uriToOpen, options: [:], completionHandler: { [view] success in
if !success, let view = view {
self.showURLError(view: view)
}
})
view?.dismiss()
}

Expand Down
11 changes: 11 additions & 0 deletions Tests/Tests/ViewPresenterSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,17 @@ class ViewPresenterSpec: QuickSpec {
expect(impressionService.sentImpressions).to(beNil())
}

it("will do nothing if redirect URL is invalid") {
let campaignClickableImage = TestHelpers.generateCampaign(id: "ClickableImage",
hasImage: true,
customJson: CustomJson(clickableImage:
ClickableImage(url: "invalid.url")))
presenter.campaign = campaignClickableImage
presenter.didClickCampaignImage()
expect(view.wasDismissCalled).to(beFalse())
expect(impressionService.sentImpressions).to(beNil())
}

it("will do nothing if clickableImage is empty") {
let campaignClickableImage = TestHelpers.generateCampaign(id: "ClickableImage",
hasImage: true,
Expand Down

0 comments on commit 9323072

Please sign in to comment.