-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2e2911
commit 4a55556
Showing
5 changed files
with
179 additions
and
8 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
function-app/adb-to-purview/tests/unit-tests/Function.Domain/Helpers/HttpHelperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using Moq; | ||
using Function.Domain.Helpers; | ||
using Microsoft.Azure.Functions.Worker; | ||
using System.IO; | ||
using System.Net; | ||
using System.Security.Claims; | ||
using UnitTests.Mocks; | ||
|
||
namespace UnitTests.Function.Domain.Helpers | ||
{ | ||
public class HttpHelperTests | ||
{ | ||
private HttpHelper _util = new(); | ||
|
||
[Fact] | ||
public void ValidateRequestHeaders_Should_Return_True_When_SourceHeader_Matches() | ||
{ | ||
// Arrange | ||
Mock<FakeHttpRequestData> mockRequest = new(); | ||
mockRequest.Setup(x => x.Headers).Returns([]); // Set up the headers | ||
mockRequest.Object.Headers.Add("x-teladoc-udf-ol-source", "ExpectedValue"); // Set the expected header value | ||
|
||
// Act | ||
var result = _util.ValidateRequestHeaders(mockRequest.Object, "ExpectedValue"); | ||
|
||
// Assert | ||
Assert.True(result, "Expected header value should match."); | ||
} | ||
|
||
[Fact] | ||
public void ValidateRequestHeaders_Should_Return_False_When_SourceHeader_Does_Not_Match() | ||
{ | ||
// Arrange | ||
Mock<FakeHttpRequestData> mockRequest = new(); | ||
mockRequest.Setup(x => x.Headers).Returns([]); // Set up the headers | ||
mockRequest.Object.Headers.Add("x-teladoc-udf-ol-source", "DifferentValue"); // Set a different header value | ||
|
||
// Act | ||
var result = _util.ValidateRequestHeaders(mockRequest.Object, "ExpectedValue"); | ||
|
||
// Assert | ||
Assert.False(result, "Expected header value should not match."); | ||
} | ||
|
||
[Fact] | ||
public void ValidateRequestHeaders_Should_Return_False_When_SourceHeader_Is_Not_Present() | ||
{ | ||
// Arrange | ||
Mock<FakeHttpRequestData> mockRequest = new(); | ||
mockRequest.Setup(x => x.Headers).Returns([]); // Set up the headers | ||
|
||
// Act | ||
var result = _util.ValidateRequestHeaders(mockRequest.Object, "ExpectedValue"); | ||
|
||
// Assert | ||
Assert.False(result, "Header should not be present."); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
function-app/adb-to-purview/tests/unit-tests/Mocks/FakeFunctionContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.Azure.Functions.Worker; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace UnitTests.Mocks | ||
{ | ||
public class FakeFunctionContext : FunctionContext | ||
{ | ||
public override string InvocationId => Guid.NewGuid().ToString(); | ||
|
||
public override string FunctionId => Guid.NewGuid().ToString(); | ||
|
||
public override TraceContext TraceContext => throw new NotImplementedException(); | ||
|
||
public override BindingContext BindingContext => throw new NotImplementedException(); | ||
|
||
public override RetryContext RetryContext => throw new NotImplementedException(); | ||
|
||
public override IServiceProvider InstanceServices { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
|
||
public override FunctionDefinition FunctionDefinition => throw new NotImplementedException(); | ||
|
||
public override IDictionary<object, object> Items { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } | ||
|
||
public override IInvocationFeatures Features => throw new NotImplementedException(); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
function-app/adb-to-purview/tests/unit-tests/Mocks/FakeHttpRequestData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using Microsoft.Azure.Functions.Worker; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Security.Claims; | ||
|
||
namespace UnitTests.Mocks | ||
{ | ||
public class FakeHttpRequestData : HttpRequestData | ||
{ | ||
public FakeHttpRequestData() : this(new FakeFunctionContext(), new Uri("http://fakeurl.test")) | ||
{ | ||
|
||
} | ||
|
||
public FakeHttpRequestData(FunctionContext functionContext, Uri url, Stream body = null) : base(functionContext) | ||
Check warning on line 20 in function-app/adb-to-purview/tests/unit-tests/Mocks/FakeHttpRequestData.cs
|
||
{ | ||
Url = url; | ||
Body = body ?? new MemoryStream(); | ||
} | ||
|
||
public override Stream Body { get; } = new MemoryStream(); | ||
|
||
public override HttpHeadersCollection Headers { get; } = []; | ||
|
||
public override IReadOnlyCollection<IHttpCookie> Cookies { get; } | ||
|
||
public override Uri Url { get; } | ||
|
||
public override IEnumerable<ClaimsIdentity> Identities { get; } | ||
|
||
public override string Method { get; } | ||
|
||
public override HttpResponseData CreateResponse() | ||
{ | ||
return new FakeHttpResponseData(FunctionContext); | ||
} | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
function-app/adb-to-purview/tests/unit-tests/Mocks/FakeHttpResponseData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Microsoft.Azure.Functions.Worker.Http; | ||
using Microsoft.Azure.Functions.Worker; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace UnitTests.Mocks | ||
{ | ||
public class FakeHttpResponseData : HttpResponseData | ||
{ | ||
public FakeHttpResponseData(FunctionContext functionContext) : base(functionContext) | ||
{ | ||
} | ||
|
||
public override HttpStatusCode StatusCode { get; set; } | ||
public override HttpHeadersCollection Headers { get; set; } = []; | ||
public override Stream Body { get; set; } = new MemoryStream(); | ||
public override HttpCookies Cookies { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters