Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,35 @@ namespace Microsoft.CommonDataModel.ObjectModel.Tests.Utilities.Network
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.CommonDataModel.ObjectModel.Utilities;
using Microsoft.CommonDataModel.ObjectModel.Cdm;

[TestClass]
public class CdmHttpClientTest
{
private int requestsExecutionCounter = 0;

/// <summary>
/// The path between TestDataPath and TestName.
/// </summary>
private readonly string testsSubpath = "Utilities";

/// <summary>
/// Dummy value used for correlation ID testing.
/// </summary>
private readonly string DummyCorrelationId = "12345";

/// <summary>
/// Declare a blackhole callback. We're focusing on event recorder, don't care about output going to the standard log stream.
/// </summary>
private readonly EventCallback eventCallback = new EventCallback
{
Invoke = (level, message) =>
{
// NOOP
}
};


private async Task<HttpResponseMessage> CompleteResponseMethod(HttpRequestMessage request, CancellationToken token)
{
Expand Down Expand Up @@ -77,6 +101,31 @@ public async Task TestResultReturnedFirstTime()
}
}

/// <summary>
/// Testing for a result returned immediatelly when a corpus is defined
/// </summary>
[TestMethod]
public async Task TestResultReturnedFirstTimeWithCorpus()
{
CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(testsSubpath, "TestHttpClient");
corpus.SetEventCallback(eventCallback, CdmStatusLevel.Warning, DummyCorrelationId);

using (var cdmHttpClient = new CdmHttpClient("https://www.example.com", new CdmHttpMessageHandlerStub(CompleteResponseMethod)))
{
var cdmHttpRequest = new CdmHttpRequest("/folder1")
{
Timeout = TimeSpan.FromSeconds(5),
MaximumTimeout = TimeSpan.FromSeconds(9)
};

var result = await cdmHttpClient.SendAsync(cdmHttpRequest, null, corpus.Ctx);

var content = await result.Content.ReadAsStringAsync();

Assert.AreEqual("REPLY1", content);
}
}

/// <summary>
/// Testing for an HTTP exception when a wrong path is provided.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ private async Task<CdmHttpResponse> SendAsyncHelper(CdmHttpRequest cdmRequest, C
{
contentLength = response.Content.Headers.ContentLength.ToString();
}
string adlsRequestId = response?.Headers.GetValues("x-ms-request-id").FirstOrDefault();

string adlsRequestId = string.Empty;
if (response?.Content.Headers.Contains("x-ms-request-id") == true)
{
adlsRequestId = response?.Headers.GetValues("x-ms-request-id").FirstOrDefault();
}

Logger.Info(ctx, Tag, nameof(SendAsyncHelper), null, $"Response for request id: {adlsRequestId}, elapsed time: {endTime.Subtract(startTime).TotalMilliseconds} ms, content length: {contentLength}, status code: {response.StatusCode}.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private CdmHttpResponse sendAsyncHelper(final CdmHttpRequest cdmHttpRequest,
final Instant endTime = java.time.Instant.now();
Logger.info(ctx, TAG, "sendAsyncHelper",
null, Logger.format("Response for request id: {0}, elapsed time: {1} ms, content length: {2}, status code: {3}.",
response.getFirstHeader("x-ms-request-id").getValue(),
response.getFirstHeader("x-ms-request-id") != null ? response.getFirstHeader("x-ms-request-id").getValue() : "",
Duration.between(startTime, endTime).toMillis(),
response.getEntity() != null ? response.getEntity().getContentLength() : "",
response.getStatusLine() != null ? response.getStatusLine().getStatusCode() : ""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"manifestName": "default.manifest.cdm.json",
"entities": [],
"jsonSchemaSemanticVersion": "1.0.0",
"imports": [
{
"corpusPath": "cdm:/foundations.cdm.json",
"moniker": ""
}
]
}