From 354d53f08c34d30c51ecfe6402a856b9ce60b1a2 Mon Sep 17 00:00:00 2001 From: Joao Vitor Date: Fri, 16 Aug 2024 20:46:00 -0300 Subject: [PATCH] fix panic when merging properties --- posthog.go | 3 +++ posthog_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/posthog.go b/posthog.go index f72a67c..d6ffcfd 100644 --- a/posthog.go +++ b/posthog.go @@ -230,6 +230,9 @@ func (c *client) Enqueue(msg Message) (err error) { } m.Properties["$active_feature_flags"] = featureKeys } + if m.Properties == nil { + m.Properties = NewProperties() + } m.Properties.Merge(c.DefaultEventProperties) c.setLastCapturedEvent(m) msg = m diff --git a/posthog_test.go b/posthog_test.go index 2daf1c6..b8d4c89 100644 --- a/posthog_test.go +++ b/posthog_test.go @@ -229,7 +229,33 @@ func ExampleCapture() { // } // ] // } +} + +func TestCaptureNoProperties(t *testing.T) { + defer func() { + // Ensure that the test doesn't panic. + if recover() != nil { + t.Error("shouldnt have panicked when merging properties into nil properties") + } + }() + + _, server := mockServer() + defer server.Close() + client, _ := NewWithConfig("Csyjlnlun3OzyNJAafdlv", Config{ + Endpoint: server.URL, + BatchSize: 1, + now: mockTime, + uid: mockId, + DefaultEventProperties: NewProperties().Set("service", "api"), + }) + defer client.Close() + + client.Enqueue(Capture{ + Event: "Download", + DistinctId: "123456", + SendFeatureFlags: false, + }) } func TestEnqueue(t *testing.T) {