From 5f1ec17c1e53073733f985b294b02f4cfd28aa10 Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Wed, 4 Sep 2024 12:45:53 -0700 Subject: [PATCH] Fixed lack of url in openURL options output; Added test. (#358) --- Sources/Segment/Analytics.swift | 7 ++- Tests/Segment-Tests/Analytics_Tests.swift | 53 +++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Sources/Segment/Analytics.swift b/Sources/Segment/Analytics.swift index 49c6779..500d138 100644 --- a/Sources/Segment/Analytics.swift +++ b/Sources/Segment/Analytics.swift @@ -386,8 +386,11 @@ extension Analytics { var jsonProperties: JSON? = nil if let json = try? JSON(options) { - jsonProperties = json - _ = try? jsonProperties?.add(value: url.absoluteString, forKey: "url") + do { + jsonProperties = try json.add(value: url.absoluteString, forKey: "url") + } catch { + jsonProperties = json + } } else { if let json = try? JSON(["url": url.absoluteString]) { jsonProperties = json diff --git a/Tests/Segment-Tests/Analytics_Tests.swift b/Tests/Segment-Tests/Analytics_Tests.swift index 711f8db..22ca5f5 100644 --- a/Tests/Segment-Tests/Analytics_Tests.swift +++ b/Tests/Segment-Tests/Analytics_Tests.swift @@ -275,6 +275,59 @@ final class Analytics_Tests: XCTestCase { XCTAssertTrue(token?.count == 32) // it's a uuid w/o the dashes. 36 becomes 32. } #endif + + func testOpenURL() { + let analytics = Analytics(configuration: Configuration(writeKey: "test")) + let outputReader = OutputReaderPlugin() + analytics.add(plugin: outputReader) + + waitUntilStarted(analytics: analytics) + + let url = URL(string: "https://blah.com")! + + // you ain't got no options Lt. Dan! + analytics.openURL(url) + let urlEvent: TrackEvent? = outputReader.lastEvent as? TrackEvent + XCTAssertEqual(urlEvent?.properties?.dictionaryValue!["url"] as! String, "https://blah.com") + + // Anyway, like I was sayin' ... + let options = [ + "Shrimp": [ + "Description": "Fruit of the sea", + "CookingMethods": [ + "barbecue", + "boil", + "broil", + "bake", + "saute", + "fried (implied)" + ], + "Types": [ + "shrimp kabobs", + "shrimp gumbo", + "pan fried", + "deep fried", + "stir fried", + "pineapple shrimp", + "lemon shrimp", + "coconut shrimp", + "pepper shrimp", + "shrimp soup", + "shrimp stew", + "shrimp salad", + "shrimp and potatoes", + "shrimp burger", + "shrimp sandwich", + "That- that's about it" + ] + ] + ] + + analytics.openURL(url, options: options) + let urlOptionEvent: TrackEvent? = outputReader.lastEvent as? TrackEvent + XCTAssertEqual(urlOptionEvent?.properties?.dictionaryValue!["url"] as! String, "https://blah.com") + XCTAssertNotNil(urlOptionEvent?.properties?.dictionaryValue!["Shrimp"]) + } func testTrack() { let analytics = Analytics(configuration: Configuration(writeKey: "test"))