Fix issue with URL normalization, add tests

This commit is contained in:
Jackson Harper 2022-06-08 11:22:26 -07:00
parent 2e3ff15b51
commit c86c18507e
2 changed files with 19 additions and 2 deletions

View file

@ -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: "")
}

View file

@ -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)
]
}