Skip to content

Commit

Permalink
Fixed lack of url in openURL options output; Added test. (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
bsneed authored Sep 4, 2024
1 parent 9f6cd28 commit 5f1ec17
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Sources/Segment/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions Tests/Segment-Tests/Analytics_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit 5f1ec17

Please sign in to comment.