From c86c18507e9da3d7451e6888f2342c8b0993491f Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Wed, 8 Jun 2022 11:22:26 -0700 Subject: [PATCH] Fix issue with URL normalization, add tests --- .../OmnivoreKit/Sources/Utils/NormalizeURL.swift | 6 +++++- .../OmnivoreKit/Tests/UtilsTests/UtilsTests.swift | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift b/apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift index 3d31dbe52..ea417ad68 100644 --- a/apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift +++ b/apple/OmnivoreKit/Sources/Utils/NormalizeURL.swift @@ -26,9 +26,13 @@ public func normalizeURL(_ dirtyURL: String) -> String { } urlObject.queryItems = urlObject.queryItems?.filter { item in - item.name.starts(with: "utm_") + !item.name.starts(with: "utm_") } + urlObject.queryItems = urlObject.queryItems?.sorted(by: { first, second in + first.name <= second.name + }) + if /* options.removeTrailingSlash */ true { urlObject.path = urlObject.path.replacingRegex(pattern: "/$", replaceWith: "") } diff --git a/apple/OmnivoreKit/Tests/UtilsTests/UtilsTests.swift b/apple/OmnivoreKit/Tests/UtilsTests/UtilsTests.swift index 2fd256f9f..8439564aa 100644 --- a/apple/OmnivoreKit/Tests/UtilsTests/UtilsTests.swift +++ b/apple/OmnivoreKit/Tests/UtilsTests/UtilsTests.swift @@ -9,7 +9,20 @@ final class UtilsTests: XCTestCase { XCTAssertEqual("Hello", "Hello") } + func testNormalizeUrl() { + // trailing slash removed + XCTAssertEqual(normalizeURL("https://omnivore.app/"), "https://omnivore.app") + + // utm_ removed + XCTAssertEqual(normalizeURL("https://omnivore.app/?aa=a&bb=b&utm_track=track&cc=c"), "https://omnivore.app?aa=a&bb=b&cc=c") + + // query params sorted + XCTAssertEqual(normalizeURL("https://omnivore.app/?aa=a&cc=c&bb=b"), "https://omnivore.app?aa=a&bb=b&cc=c") + XCTAssertEqual(normalizeURL("https://omnivore.app/?cc=c&bb=b&aa=a"), "https://omnivore.app?aa=a&bb=b&cc=c") + } + static var allTests = [ - ("testExample", testExample) + ("testExample", testExample), + ("testNormalizeUrl", testNormalizeUrl) ] }