Skip to content

Commit

Permalink
Consolidate .netcoreapp.cs test files in System.Net.* (dotnet#664)
Browse files Browse the repository at this point in the history
* Consolidate .netcoreapp.cs files because System.Net.* projects is no longer cross-compiled

* Fix build

* Add end-of-line
  • Loading branch information
Marusyk authored and davidsh committed Dec 9, 2019
1 parent 7d49d20 commit 3d9a603
Show file tree
Hide file tree
Showing 43 changed files with 2,313 additions and 2,573 deletions.
1 change: 0 additions & 1 deletion src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<Compile Include="System\Net\Http\HttpClient.cs" />
<Compile Include="System\Net\Http\HttpClientHandler.cs" />
<Compile Include="System\Net\Http\HttpClientHandler.Core.cs" />
<Compile Include="System\Net\Http\HttpClientHandler.netcoreapp.cs" />
<Compile Include="System\Net\Http\HttpCompletionOption.cs" />
<Compile Include="System\Net\Http\HttpContent.cs" />
<Compile Include="System\Net\Http\HttpMessageHandler.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Net.Security;
using System.Collections.Generic;
using System.Security.Authentication;
Expand Down Expand Up @@ -222,6 +221,14 @@ protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessa
_diagnosticsHandler.SendAsync(request, cancellationToken) :
_socketsHttpHandler.SendAsync(request, cancellationToken);
}

public static Func<HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors, bool> DangerousAcceptAnyServerCertificateValidator { get; } = delegate { return true; };

private void ThrowForModifiedManagedSslOptionsIfStarted()
{
// Hack to trigger an InvalidOperationException if a property that's stored on
// SslOptions is changed, since SslOptions itself does not do any such checks.
_socketsHttpHandler.SslOptions = _socketsHttpHandler.SslOptions;
}
}
}

This file was deleted.

158 changes: 157 additions & 1 deletion src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.DotNet.RemoteExecutor;
using System.IO;
using System.Linq;
using System.Threading;
Expand All @@ -11,7 +12,7 @@

namespace System.Net.Http.Functional.Tests
{
public sealed partial class HttpClientTest
public sealed class HttpClientTest
{
[Fact]
public void Dispose_MultipleTimes_Success()
Expand Down Expand Up @@ -410,6 +411,161 @@ public async Task Timeout_SetTo30AndGetResponseQuickly_Success()
}
}

[Fact]
public void DefaultProxy_SetNull_Throws()
{
Assert.Throws<ArgumentNullException>(() => HttpClient.DefaultProxy = null );
}

[Fact]
public void DefaultProxy_Get_ReturnsNotNull()
{
IWebProxy proxy = HttpClient.DefaultProxy;
Assert.NotNull(proxy);
}

[Fact]
public void DefaultProxy_SetGet_Roundtrips()
{
RemoteExecutor.Invoke(() =>
{
IWebProxy proxy = new WebProxy("http://localhost:3128/");
HttpClient.DefaultProxy = proxy;
Assert.True(Object.ReferenceEquals(proxy, HttpClient.DefaultProxy));
}).Dispose();
}

[Fact]
public void DefaultProxy_Credentials_SetGet_Roundtrips()
{
RemoteExecutor.Invoke(() =>
{
IWebProxy proxy = HttpClient.DefaultProxy;
ICredentials nc = proxy.Credentials;

proxy.Credentials = null;
Assert.Null(proxy.Credentials);

proxy.Credentials = nc;
Assert.Same(nc, proxy.Credentials);

return RemoteExecutor.SuccessExitCode;
}).Dispose();
}

[Fact]
public async Task PatchAsync_Canceled_Throws()
{
using (var client = new HttpClient(new CustomResponseHandler((r, c) => WhenCanceled<HttpResponseMessage>(c))))
{
var content = new ByteArrayContent(new byte[1]);
var cts = new CancellationTokenSource();

Task t1 = client.PatchAsync(CreateFakeUri(), content, cts.Token);

cts.Cancel();

await Assert.ThrowsAsync<TaskCanceledException>(() => t1);
}
}

[Fact]
public async Task PatchAsync_Success()
{
static void Verify(HttpResponseMessage message)
{
using (message)
{
Assert.Equal(HttpStatusCode.OK, message.StatusCode);
}
}

using (var client = new HttpClient(new CustomResponseHandler((r, c) => Task.FromResult(new HttpResponseMessage()))))
{
Verify(await client.PatchAsync(CreateFakeUri(), new ByteArrayContent(new byte[1])));
Verify(await client.PatchAsync(CreateFakeUri(), new ByteArrayContent(new byte[1]), CancellationToken.None));
}
}

[Fact]
public void Dispose_UsePatchAfterDispose_Throws()
{
var client = new HttpClient(new CustomResponseHandler((r, c) => Task.FromResult(new HttpResponseMessage())));
client.Dispose();

Assert.Throws<ObjectDisposedException>(() => { client.PatchAsync(CreateFakeUri(), new ByteArrayContent(new byte[1])); });
}

[Fact]
public void DefaultRequestVersion_InitialValueExpected()
{
using (var client = new HttpClient())
{
Assert.Equal(HttpVersion.Version11, client.DefaultRequestVersion);
Assert.Same(client.DefaultRequestVersion, client.DefaultRequestVersion);
}
}

[Fact]
public void DefaultRequestVersion_Roundtrips()
{
using (var client = new HttpClient())
{
for (int i = 3; i < 5; i++)
{
var newVersion = new Version(i, i, i, i);
client.DefaultRequestVersion = newVersion;
Assert.Same(newVersion, client.DefaultRequestVersion);
}
}
}

[Fact]
public void DefaultRequestVersion_InvalidArgument_Throws()
{
using (var client = new HttpClient())
{
AssertExtensions.Throws<ArgumentNullException>("value", () => client.DefaultRequestVersion = null);
client.DefaultRequestVersion = new Version(1, 0); // still usable after
Assert.Equal(new Version(1, 0), client.DefaultRequestVersion);
}
}

[Fact]
public async Task DefaultRequestVersion_SetAfterUse_Throws()
{
var handler = new StoreMessageHttpMessageInvoker();
using (var client = new HttpClient(handler))
{
await client.GetAsync("http://doesntmatter", HttpCompletionOption.ResponseHeadersRead);
Assert.Throws<InvalidOperationException>(() => client.DefaultRequestVersion = new Version(1, 1));
}
}

[Fact]
public async Task DefaultRequestVersion_UsedInCreatedMessages()
{
var handler = new StoreMessageHttpMessageInvoker();
using (var client = new HttpClient(handler))
{
var version = new Version(1, 2, 3, 4);
client.DefaultRequestVersion = version;
await client.GetAsync("http://doesntmatter", HttpCompletionOption.ResponseHeadersRead);
Assert.Same(version, handler.Message.Version);
}
}

private sealed class StoreMessageHttpMessageInvoker : HttpMessageHandler
{
public HttpRequestMessage Message;

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Message = request;
return Task.FromResult(new HttpResponseMessage());
}
}

private static string CreateFakeUri() => $"http://{Guid.NewGuid().ToString("N")}";

private static async Task<T> WhenCanceled<T>(CancellationToken cancellationToken)
Expand Down
Loading

0 comments on commit 3d9a603

Please sign in to comment.