Skip to content

Commit

Permalink
[tests] changes for AndroidClientHandlerTests to pass
Browse files Browse the repository at this point in the history
* MaxAutomaticRedirections should throw if <= 0
* UseCookies defaults to true
* CookieContainer creates a new instance on first call
* Catch TargetInvocationException, see: dotnet/runtime#56089
* Catch PlatformNotSupportedException for ClientCertificateOptions
  • Loading branch information
jonathanpeppers committed Jul 21, 2021
1 parent c7f8c29 commit c146362
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/Mono.Android/Xamarin.Android.Net/AndroidMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ sealed class RequestRedirectionState
internal const bool SupportsProxy = true;
internal const bool SupportsRedirectConfiguration = true;

public bool UseCookies { get; set; }

public DecompressionMethods AutomaticDecompression
{
get => _decompressionMethods;
Expand All @@ -84,13 +82,11 @@ public DecompressionMethods AutomaticDecompression

public CookieContainer CookieContainer
{
get => _cookieContainer;
get => _cookieContainer ?? (_cookieContainer = new CookieContainer ());
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

_cookieContainer = value;
}
Expand All @@ -99,6 +95,8 @@ public CookieContainer CookieContainer
// NOTE: defaults here are based on:
// https://github.com/dotnet/runtime/blob/ccfe21882e4a2206ce49cd5b32d3eb3cab3e530f/src/libraries/Common/src/System/Net/Http/HttpHandlerDefaults.cs

public bool UseCookies { get; set; } = true;

public bool PreAuthenticate { get; set; } = false;

public bool UseProxy { get; set; } = true;
Expand All @@ -109,7 +107,19 @@ public CookieContainer CookieContainer

public bool AllowAutoRedirect { get; set; } = true;

public int MaxAutomaticRedirections { get; set; } = 50;
int maxAutomaticRedirections = 50;

public int MaxAutomaticRedirections
{
get => maxAutomaticRedirections;
set {
// https://github.com/dotnet/runtime/blob/913facdca8b04cc674163e31a7650ef6868a7d5b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/SocketsHttpHandler.cs#L142-L145
if (value <= 0)
throw new ArgumentOutOfRangeException(nameof(value), value, "The specified value must be greater than 0");

maxAutomaticRedirections = value;
}
}

/// <summary>
/// <para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public void Properties_Defaults ()
Assert.IsTrue (h.UseCookies, "#12");
Assert.IsFalse (h.UseDefaultCredentials, "#13");
Assert.IsTrue (h.UseProxy, "#14");
Assert.AreEqual (ClientCertificateOption.Manual, h.ClientCertificateOptions, "#15");
try {
Assert.AreEqual (ClientCertificateOption.Manual, h.ClientCertificateOptions, "#15");
} catch (PlatformNotSupportedException) {
// https://github.com/dotnet/runtime/blob/07336810acf3b4e7bdd0fb7da87b54920ea9c382/src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs#L310-L314
}
}

[Test]
Expand All @@ -99,12 +103,16 @@ public void Properties_Invalid ()
h.MaxAutomaticRedirections = 0;
Assert.Fail ("#1");
} catch (ArgumentOutOfRangeException) {
} catch (TargetInvocationException) {
// See: https://github.com/dotnet/runtime/issues/56089
}

try {
h.MaxRequestContentBufferSize = -1;
Assert.Fail ("#2");
} catch (ArgumentOutOfRangeException) {
} catch (TargetInvocationException) {
// See: https://github.com/dotnet/runtime/issues/56089
}
}

Expand Down

0 comments on commit c146362

Please sign in to comment.