-
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance use http proxy with http factory (#3899)
* 2890 started to implement * Add test for new functionality --------- Co-authored-by: Rockford Lhotka <rocky@lhotka.net>
- Loading branch information
1 parent
8c8108a
commit bba0987
Showing
6 changed files
with
225 additions
and
11 deletions.
There are no files selected for viewing
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,37 @@ | ||
using Csla.Channels.Http; | ||
using Csla.Configuration; | ||
using Csla.TestHelpers; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Csla.Test.HttpProxy; | ||
|
||
[TestClass] | ||
public class HttpProxyExtensionsTests | ||
{ | ||
[TestMethod] | ||
public void UseHttpProxy_WhenConfiguringACustomHttpClientFactoryItShouldBeUsedWhenHttpProxyIsCreated() | ||
{ | ||
bool hasBeenCalled = false; | ||
HttpClient CustomFactory(IServiceProvider sp) | ||
{ | ||
hasBeenCalled = true; | ||
return new HttpClient(); | ||
} | ||
|
||
var diContext = TestDIContextFactory.CreateContext( | ||
o => o.DataPortal( | ||
dp => dp.ClientSideDataPortal( | ||
cdp => cdp.UseHttpProxy( | ||
hp => hp.WithHttpClientFactory(CustomFactory) | ||
) | ||
) | ||
) | ||
); | ||
|
||
_ = diContext.ServiceProvider.GetRequiredService<Csla.DataPortalClient.IDataPortalProxy>(); | ||
|
||
hasBeenCalled.Should().BeTrue(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
Source/Csla.test/HttpProxy/HttpProxyOptionsExtensionsTests.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,67 @@ | ||
using Csla.Channels.Http; | ||
using FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Csla.Test.HttpProxy; | ||
|
||
[TestClass] | ||
public class HttpProxyOptionsExtensionsTests | ||
{ | ||
[TestMethod] | ||
public void WithDataPortalUrl_ShouldSetTheDataPortalUrlAsExpected() | ||
{ | ||
var options = new HttpProxyOptions(); | ||
options.WithDataPortalUrl("http://localhost:1337"); | ||
|
||
var expectedOptions = new HttpProxyOptions | ||
{ | ||
DataPortalUrl = "http://localhost:1337" | ||
}; | ||
|
||
options.Should().BeEquivalentTo(expectedOptions); | ||
} | ||
|
||
[TestMethod] | ||
public void UseTextForSerialization_ShouldSetTheUseTextSerializationToTrue() | ||
{ | ||
var options = new HttpProxyOptions(); | ||
options.UseTextForSerialization(); | ||
|
||
var expectedOptions = new HttpProxyOptions | ||
{ | ||
UseTextSerialization = true | ||
}; | ||
|
||
options.Should().BeEquivalentTo(expectedOptions); | ||
} | ||
|
||
[TestMethod] | ||
public void UseDefaultForSerialization_ShouldSetTheUseTextSerializationToFalse() | ||
{ | ||
var options = new HttpProxyOptions(); | ||
options.UseDefaultForSerialization(); | ||
|
||
var expectedOptions = new HttpProxyOptions | ||
{ | ||
UseTextSerialization = false | ||
}; | ||
|
||
options.Should().BeEquivalentTo(expectedOptions); | ||
} | ||
|
||
[TestMethod] | ||
public void WithHttpClientFactory_ShouldSetTheFactoryToTheProvidedFactory() | ||
{ | ||
var options = new HttpProxyOptions(); | ||
options.WithHttpClientFactory(httpClientFactory); | ||
|
||
var expectedOptions = new HttpProxyOptions | ||
{ | ||
HttpClientFactory = httpClientFactory | ||
}; | ||
|
||
options.Should().BeEquivalentTo(expectedOptions); | ||
|
||
static HttpClient httpClientFactory(IServiceProvider sp) => new(); | ||
} | ||
} |
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
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
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
86 changes: 86 additions & 0 deletions
86
Source/Csla/DataPortalClient/HttpProxyOptionsExtensions.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,86 @@ | ||
#nullable enable | ||
//----------------------------------------------------------------------- | ||
// <copyright file="HttpProxyOptions.cs" company="Marimer LLC"> | ||
// Copyright (c) Marimer LLC. All rights reserved. | ||
// Website: https://cslanet.com | ||
// </copyright> | ||
// <summary>Extensions for HttpProxyOptions</summary> | ||
//----------------------------------------------------------------------- | ||
|
||
#if !NETSTANDARD2_0 && !NET6_0_OR_GREATER | ||
using System.Net.Http; | ||
#endif | ||
|
||
namespace Csla.Channels.Http | ||
{ | ||
/// <summary> | ||
/// Provides extension methods for <see cref="HttpProxyOptions"/>. | ||
/// </summary> | ||
public static class HttpProxyOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the <see cref="HttpProxyOptions.DataPortalUrl"/>. | ||
/// </summary> | ||
/// <param name="source">The options object.</param> | ||
/// <param name="dataPortalUrl">The data portal server endpoint url to use.</param> | ||
/// <returns>The options object.</returns> | ||
public static HttpProxyOptions WithDataPortalUrl(this HttpProxyOptions source, string dataPortalUrl) | ||
{ | ||
if (source is null) | ||
throw new ArgumentNullException(nameof(source)); | ||
if (string.IsNullOrEmpty(dataPortalUrl)) | ||
throw new ArgumentException($"'{nameof(dataPortalUrl)}' cannot be null or empty.", nameof(dataPortalUrl)); | ||
|
||
source.DataPortalUrl = dataPortalUrl; | ||
return source; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the <see cref="HttpProxyOptions.UseTextSerialization"/> to <see langword="true"/>. | ||
/// </summary> | ||
/// <param name="source">The options object.</param> | ||
/// <returns>The options object.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> | ||
public static HttpProxyOptions UseTextForSerialization(this HttpProxyOptions source) | ||
{ | ||
if (source is null) | ||
throw new ArgumentNullException(nameof(source)); | ||
|
||
source.UseTextSerialization = true; | ||
return source; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the <see cref="HttpProxyOptions.UseTextSerialization"/> to <see langword="false"/> to use the default serialization. | ||
/// </summary> | ||
/// <param name="source">The options object.</param> | ||
/// <returns>The options object.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception> | ||
public static HttpProxyOptions UseDefaultForSerialization(this HttpProxyOptions source) | ||
{ | ||
if (source is null) | ||
throw new ArgumentNullException(nameof(source)); | ||
|
||
source.UseTextSerialization = false; | ||
return source; | ||
} | ||
|
||
/// <summary> | ||
/// Configures the <see cref="HttpProxyOptions.HttpClientFactory"/> to use the provided factory instead of the default. | ||
/// </summary> | ||
/// <param name="source">The options object.</param> | ||
/// <param name="factory">The factory to obtain an <see cref="HttpClient"/> for use.</param> | ||
/// <returns>The options object.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="factory"/> is <see langword="null"/>.</exception> | ||
public static HttpProxyOptions WithHttpClientFactory(this HttpProxyOptions source, Func<IServiceProvider, HttpClient> factory) | ||
{ | ||
if (source is null) | ||
throw new ArgumentNullException(nameof(source)); | ||
if (factory is null) | ||
throw new ArgumentNullException(nameof(factory)); | ||
|
||
source.HttpClientFactory = factory; | ||
return source; | ||
} | ||
} | ||
} |