Skip to content

Commit 7bac3f2

Browse files
authored
test: Added unit tests for HttpCollectorWire. (#1565)
1 parent 343b580 commit 7bac3f2

File tree

4 files changed

+346
-17
lines changed

4 files changed

+346
-17
lines changed

src/Agent/NewRelic/Agent/Core/Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project>
1+
<Project>
22
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
33

44
<PropertyGroup>

src/Agent/NewRelic/Agent/Core/DataTransport/HttpCollectorWire.cs

+4-10
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ public string SendData(string method, ConnectionInfo connectionInfo, string seri
131131
{
132132
var responseContent = GetResponseContent(response, requestGuid);
133133

134-
_agentHealthReporter.ReportSupportabilityDataUsage("Collector", method, uncompressedByteCount, new UTF8Encoding().GetBytes(response.Content.ToString()).Length);
134+
_agentHealthReporter.ReportSupportabilityDataUsage("Collector", method, uncompressedByteCount, new UTF8Encoding().GetBytes(responseContent).Length);
135135

136136
// Possibly combine these logs? makes parsing harder in tests...
137137
Log.DebugFormat("Request({0}): Invoked \"{1}\" with : {2}", requestGuid, method, serializedData);
138138
Log.DebugFormat("Request({0}): Invocation of \"{1}\" yielded response : {2}", requestGuid, method, responseContent);
139139

140-
AuditLog(Direction.Received, Source.Collector, response.Content.ToString());
140+
AuditLog(Direction.Received, Source.Collector, responseContent);
141141
if (!response.IsSuccessStatusCode)
142142
{
143143
ThrowExceptionFromHttpResponseMessage(serializedData, response.StatusCode, responseContent, requestGuid);
@@ -199,16 +199,10 @@ private string GetResponseContent(HttpResponseMessage response, Guid requestGuid
199199
{
200200
try
201201
{
202-
var responseStream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
202+
var responseStream = response.Content?.ReadAsStreamAsync().GetAwaiter().GetResult();
203203

204204
if (responseStream == null)
205-
{
206-
throw new NullReferenceException("responseStream");
207-
}
208-
if (response.Headers == null)
209-
{
210-
throw new NullReferenceException("response.Headers");
211-
}
205+
return EmptyResponseBody;
212206

213207
var contentTypeEncoding = response.Content.Headers.ContentEncoding;
214208
if (contentTypeEncoding.Contains("gzip"))

tests/Agent/UnitTests/CompositeTests/CrossAgentTests/DistributedTracing/TraceContextCrossAgentTests.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2020 New Relic, Inc. All rights reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
using Castle.Core.Internal;
54
using NewRelic.Agent.Api;
65
using NewRelic.Agent.Core.DistributedTracing;
76
using NewRelic.Agent.Extensions.Providers.Wrapper;
@@ -137,7 +136,7 @@ void MakeTransaction(TraceContextTestData testData)
137136
carrier.Add(key, value);
138137
});
139138

140-
testData.OutboundPayloadsSettings?.ForEach(payloadSettings =>
139+
testData.OutboundPayloadsSettings?.ToList().ForEach(payloadSettings =>
141140
{
142141
_agent.CurrentTransaction.InsertDistributedTraceHeaders(insertedHeaders, setHeaders);
143142

@@ -148,6 +147,7 @@ void MakeTransaction(TraceContextTestData testData)
148147

149148
insertedHeaders.Clear();
150149
});
150+
151151
segment.End();
152152
transaction.End();
153153
}
@@ -161,8 +161,7 @@ List<IEnumerable<KeyValuePair<string, string>>> MakeHeaders(TraceContextTestData
161161
{
162162
var ingestHeaderSet = new List<KeyValuePair<string, string>>();
163163

164-
headerSet.Headers.ForEach
165-
(header =>
164+
headerSet.Headers?.ToList().ForEach(header =>
166165
{
167166

168167
if (header.Key.Equals("newrelic"))
@@ -295,7 +294,7 @@ private void ValidateOutboundHeaders(OutboundPayloadSettings payloadSettings, Di
295294
}
296295
}
297296

298-
payloadSettings.Expected?.ForEach(expected =>
297+
payloadSettings.Expected?.ToList().ForEach(expected =>
299298
{
300299
switch (expected.Substring(0, expected.IndexOf('.')))
301300
{
@@ -313,7 +312,7 @@ private void ValidateOutboundHeaders(OutboundPayloadSettings payloadSettings, Di
313312
}
314313
});
315314

316-
payloadSettings.Unexpected?.ForEach(unexpected =>
315+
payloadSettings.Unexpected?.ToList().ForEach(unexpected =>
317316
{
318317
switch (unexpected.Substring(0, unexpected.IndexOf('.')))
319318
{

0 commit comments

Comments
 (0)