From a4304687caf340f1109f848d0fd4b85e5382520c Mon Sep 17 00:00:00 2001 From: Minh Nguyen Nhat Date: Sun, 2 Apr 2023 20:32:31 +0100 Subject: [PATCH 1/3] Creating InjectionHelper and ExtractorHelper functions for Http header transport --- .../Src/Transport/ZipkinHttpHelper.cs | 29 +++++++++++++++ .../Tests/Transport/T_ZipkinHttpHelper.cs | 37 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Src/zipkin4net/Src/Transport/ZipkinHttpHelper.cs create mode 100644 Src/zipkin4net/Tests/Transport/T_ZipkinHttpHelper.cs diff --git a/Src/zipkin4net/Src/Transport/ZipkinHttpHelper.cs b/Src/zipkin4net/Src/Transport/ZipkinHttpHelper.cs new file mode 100644 index 0000000..097a32c --- /dev/null +++ b/Src/zipkin4net/Src/Transport/ZipkinHttpHelper.cs @@ -0,0 +1,29 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Collections.Generic; +using System.Linq; + +namespace zipkin4net.Transport +{ + public static class ZipkinHttpHelper + { + public static void InjectionHelper(HttpClient carrier, string key, string value) + { + carrier.DefaultRequestHeaders.Add(key, value); + } + + public static string ExtractorHelper(HttpRequestHeaders carrier, string key) + { + IEnumerable headerValues; + string header = string.Empty; + if (carrier.TryGetValues(key, out headerValues)) + { + header = headerValues.FirstOrDefault(); + } + + return header; + } + } +} + diff --git a/Src/zipkin4net/Tests/Transport/T_ZipkinHttpHelper.cs b/Src/zipkin4net/Tests/Transport/T_ZipkinHttpHelper.cs new file mode 100644 index 0000000..e0a5fd8 --- /dev/null +++ b/Src/zipkin4net/Tests/Transport/T_ZipkinHttpHelper.cs @@ -0,0 +1,37 @@ +using zipkin4net.Transport; +using NUnit.Framework; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Collections.Generic; +using Newtonsoft.Json.Linq; + + +namespace zipkin4net.UTests.Transport +{ + [TestFixture] + internal class T_ZipkinHttpHelper + { + [Test] + [TestCase("X-B3-TraceId", "Test_TraceID_123")] + [TestCase("", "Test_TraceID_123")] + [TestCase("", "")] + public void ExtractorCorrectlyExtractsExistingHeader(string key, string value) + { + var carrier = new HttpRequestMessage().Headers; + carrier.Add(key, value); + + Assert.AreEqual(ZipkinHttpHelper.ExtractorHelper(carrier, key), value); + } + + [Test] + [TestCase("X-B3-TraceId")] + [TestCase("")] + public void ExtractorCorrectlyExtractsNonExistentHeader(string key) + { + var carrier = new HttpRequestMessage().Headers; + Assert.AreEqual(ZipkinHttpHelper.ExtractorHelper(carrier, key), string.Empty); + } + + } +} + From 2a087559e4769d490dbeffa53f87317d216d62d7 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Nhat Date: Sun, 2 Apr 2023 20:33:07 +0100 Subject: [PATCH 2/3] Make naming consistent --- Src/zipkin4net/Src/Transport/ZipkinHttpHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/zipkin4net/Src/Transport/ZipkinHttpHelper.cs b/Src/zipkin4net/Src/Transport/ZipkinHttpHelper.cs index 097a32c..7c95b30 100644 --- a/Src/zipkin4net/Src/Transport/ZipkinHttpHelper.cs +++ b/Src/zipkin4net/Src/Transport/ZipkinHttpHelper.cs @@ -8,7 +8,7 @@ namespace zipkin4net.Transport { public static class ZipkinHttpHelper { - public static void InjectionHelper(HttpClient carrier, string key, string value) + public static void InjectorHelper(HttpClient carrier, string key, string value) { carrier.DefaultRequestHeaders.Add(key, value); } From 93bb61089d4fd460411e8ce9dd391a2fa168dad1 Mon Sep 17 00:00:00 2001 From: Minh Nguyen Nhat Date: Sun, 2 Apr 2023 20:39:21 +0100 Subject: [PATCH 3/3] Comments --- Src/zipkin4net/Tests/Transport/T_ZipkinHttpHelper.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Src/zipkin4net/Tests/Transport/T_ZipkinHttpHelper.cs b/Src/zipkin4net/Tests/Transport/T_ZipkinHttpHelper.cs index e0a5fd8..b38515b 100644 --- a/Src/zipkin4net/Tests/Transport/T_ZipkinHttpHelper.cs +++ b/Src/zipkin4net/Tests/Transport/T_ZipkinHttpHelper.cs @@ -32,6 +32,8 @@ public void ExtractorCorrectlyExtractsNonExistentHeader(string key) Assert.AreEqual(ZipkinHttpHelper.ExtractorHelper(carrier, key), string.Empty); } + // No test needed for Injector + } }