Skip to content

Commit

Permalink
fix: Only set incoming user agent if not already present (#1366)
Browse files Browse the repository at this point in the history
Following up to the following PR, we should only set the incoming user
agent if the key does not already exist. This is useful in certain
scenarios when two Refinery's are connected together.

- #1358

- Only set incoming user agent meta field if the event doesn't already
have a value for it
- Add unit test to verify behaviour
- Update existing tests to set custom user-agent and verify it's set
correctly
  • Loading branch information
MikeGoldsmith authored and TylerHelmuth committed Oct 16, 2024
1 parent 57d2243 commit 37c4c8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ func getUserAgentFromRequest(req *http.Request) string {
}

func addIncomingUserAgent(ev *types.Event, userAgent string) {
if userAgent != "" {
if userAgent != "" && ev.Data["meta.refinery.incoming_user_agent"] == nil {
ev.Data["meta.refinery.incoming_user_agent"] = userAgent
}
}
22 changes: 22 additions & 0 deletions route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,25 @@ func TestAddIncomingUserAgent(t *testing.T) {
require.Equal(t, "test-agent", event.Data["meta.refinery.incoming_user_agent"])
})
}

func TestAddIncomingUserAgent(t *testing.T) {
t.Run("no incoming user agent", func(t *testing.T) {
event := &types.Event{
Data: map[string]interface{}{},
}

addIncomingUserAgent(event, "test-agent")
require.Equal(t, "test-agent", event.Data["meta.refinery.incoming_user_agent"])
})

t.Run("existing incoming user agent", func(t *testing.T) {
event := &types.Event{
Data: map[string]interface{}{
"meta.refinery.incoming_user_agent": "test-agent",
},
}

addIncomingUserAgent(event, "another-test-agent")
require.Equal(t, "test-agent", event.Data["meta.refinery.incoming_user_agent"])
})
}

0 comments on commit 37c4c8b

Please sign in to comment.