Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit e4004f5

Browse files
authored
Avoid exceptions in command-specific telemetry events (#421)
1 parent a2a40ed commit e4004f5

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

src/Microsoft.HttpRepl/Telemetry/Events/PreferenceEvent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public PreferenceEvent(string getOrSet, string preferenceName) : base(TelemetryE
1717

1818
private static string SanitizePreferenceName(string preferenceName)
1919
{
20-
if (WellKnownPreference.Catalog.Names.Contains(preferenceName, StringComparer.OrdinalIgnoreCase))
20+
if (string.IsNullOrEmpty(preferenceName) ||
21+
WellKnownPreference.Catalog.Names.Contains(preferenceName, StringComparer.OrdinalIgnoreCase))
2122
{
2223
return preferenceName;
2324
}

src/Microsoft.HttpRepl/Telemetry/Events/SetHeaderEvent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public SetHeaderEvent(string headerName, bool isValueEmpty) : base(TelemetryEven
1616

1717
private static string SanitizeHeaderName(string headerName)
1818
{
19-
if (WellKnownHeaders.CommonHeaders.Contains(headerName, StringComparer.OrdinalIgnoreCase))
19+
if (string.IsNullOrEmpty(headerName) ||
20+
WellKnownHeaders.CommonHeaders.Contains(headerName, StringComparer.OrdinalIgnoreCase))
2021
{
2122
return headerName;
2223
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.HttpRepl.Telemetry.Events;
5+
using Xunit;
6+
7+
namespace Microsoft.HttpRepl.Tests.Telemetry.Events
8+
{
9+
public class PreferenceEventTests
10+
{
11+
[Theory]
12+
[InlineData("get", null)]
13+
[InlineData("get", "")]
14+
[InlineData("get", " ")]
15+
[InlineData("get", "editor.command.default")]
16+
[InlineData("get", "not.really.a.preference")]
17+
[InlineData("set", null)]
18+
[InlineData("set", "")]
19+
[InlineData("set", " ")]
20+
[InlineData("set", "editor.command.default")]
21+
[InlineData("set", "not.really.a.preference")]
22+
public void Constructor_DoesNotThrow(string getOrSet, string preferenceName)
23+
{
24+
PreferenceEvent preferenceEvent = new PreferenceEvent(getOrSet, preferenceName);
25+
}
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.HttpRepl.Telemetry.Events;
5+
using Xunit;
6+
7+
namespace Microsoft.HttpRepl.Tests.Telemetry.Events
8+
{
9+
public class SetHeaderEventTests
10+
{
11+
[Theory]
12+
[InlineData(null, false)]
13+
[InlineData("", false)]
14+
[InlineData(" ", false)]
15+
[InlineData("Content-Type", false)]
16+
[InlineData("X-Custom-Header", false)]
17+
[InlineData(null, true)]
18+
[InlineData("", true)]
19+
[InlineData(" ", true)]
20+
[InlineData("Content-Type", true)]
21+
[InlineData("X-Custom-Header", true)]
22+
public void Constructor_DoesNotThrow(string headerName, bool isValueEmpty)
23+
{
24+
SetHeaderEvent preferenceEvent = new SetHeaderEvent(headerName, isValueEmpty);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)