Skip to content

Commit

Permalink
chore: encode loadPage action URL (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 authored Jan 1, 2025
1 parent 9f5b69c commit 1f0ce7f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/MessagingInApp/Gist/Managers/MessageManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ class MessageManager: EngineWebDelegate {
logger.logWithModuleTag("Dismissing from action: \(action)", level: .info)
inAppMessageManager.dispatch(action: .dismissMessage(message: currentMessage, viaCloseAction: true))
case "loadPage":
if let page = url.queryParameters?["url"],
// Encode '#' in url action string and creates encoded URL to handle fragments
let encodedUrl = URL(string: action.percentEncode(character: "#")) ?? url
if let page = encodedUrl.queryParameters?["url"],
let pageUrl = URL(string: page),
UIApplication.shared.canOpenURL(pageUrl) {
UIApplication.shared.open(pageUrl)
Expand Down
8 changes: 8 additions & 0 deletions Sources/MessagingInApp/Gist/Utilities/String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

extension String {
/// Encodes a specific character in the string using percent encoding.
func percentEncode(character target: String, withAllowedCharacters allowedCharacters: CharacterSet = .urlPathAllowed) -> String {
replacingOccurrences(of: target, with: target.addingPercentEncoding(withAllowedCharacters: allowedCharacters) ?? target)
}
}
69 changes: 69 additions & 0 deletions Tests/MessagingInApp/Gist/Utilities/StringPercentEncodeTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
@testable import CioMessagingInApp
import XCTest

class StringPercentEncodeTests: XCTestCase {
func test_givenValidCharacter_expectEncodedURL() {
let input = "https://example.com/path#fragment"

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, "https://example.com/path%23fragment")
}

func test_givenMultipleOccurrences_expectAllEncoded() {
let input = "https://example.com/path#fragment#another"

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, "https://example.com/path%23fragment%23another")
}

func test_givenMultipleCharacter_expectEncodedGivenOnly() {
let input = "https://example.com/path#source=link&medium=email"

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, "https://example.com/path%23source=link&medium=email")
}

func test_givenCharacterInAllowedSet_expectUnchangedString() {
let input = "https://example.com/path/fragment"

// '/' is allowed in `.urlPathAllowed`
let encoded = input.percentEncode(character: "/")

XCTAssertEqual(encoded, input)
}

func test_givenCustomAllowedCharacterSet_expectEncodedSpaces() {
let input = "https://example.com/path with spaces"

let encoded = input.percentEncode(character: " ", withAllowedCharacters: .alphanumerics)

XCTAssertEqual(encoded, "https://example.com/path%20with%20spaces")
}

func test_givenNoTargetCharacter_expectUnchangedString() {
let input = "https://example.com/path/fragment"

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, input)
}

func test_givenEmptyTargetCharacter_expectUnchangedString() {
let input = "https://example.com/path/fragment"

let encoded = input.percentEncode(character: "")

XCTAssertEqual(encoded, input)
}

func test_givenEmptyInput_expectEmptyString() {
let input = ""

let encoded = input.percentEncode(character: "#")

XCTAssertEqual(encoded, input)
}
}

0 comments on commit 1f0ce7f

Please sign in to comment.