未验证 提交 d1b5a34d 编写于 作者: M Miha Zupan 提交者: GitHub

Throw ArgumentNull instead of NullReference for null requests (#53742)

* Throw ArgumentNull instead of NullReference for null requests

* Don't use Send on browser
上级 61587f4f
......@@ -144,6 +144,11 @@ protected internal override HttpResponseMessage Send(HttpRequestMessage request,
protected internal override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request), SR.net_http_handler_norequest);
}
try
{
var requestObject = new JSObject();
......
......@@ -552,8 +552,9 @@ private void CheckRequestBeforeSend(HttpRequestMessage request)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
throw new ArgumentNullException(nameof(request), SR.net_http_handler_norequest);
}
CheckDisposed();
CheckRequestMessage(request);
......
......@@ -500,6 +500,11 @@ private HttpMessageHandlerStage SetupHandlerChain()
protected internal override HttpResponseMessage Send(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request), SR.net_http_handler_norequest);
}
if (request.Version.Major >= 2)
{
throw new NotSupportedException(SR.Format(SR.net_http_http2_sync_not_supported, GetType()));
......@@ -528,6 +533,11 @@ private HttpMessageHandlerStage SetupHandlerChain()
protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request), SR.net_http_handler_norequest);
}
CheckDisposed();
if (cancellationToken.IsCancellationRequested)
......
......@@ -1101,37 +1101,6 @@ public void SendAsync_ExpectedDiagnosticCancelledActivityLogging()
}, UseVersion.ToString()).Dispose();
}
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void SendAsync_NullRequest_ThrowsArgumentNullException()
{
RemoteExecutor.Invoke(async () =>
{
var diagnosticListenerObserver = new FakeDiagnosticListenerObserver(null);
using (DiagnosticListener.AllListeners.Subscribe(diagnosticListenerObserver))
{
diagnosticListenerObserver.Enable();
using (MyHandler handler = new MyHandler())
{
// Getting the Task first from the .SendAsync() call also tests
// that the exception comes from the async Task path.
Task t = handler.SendAsync(null);
await Assert.ThrowsAsync<ArgumentNullException>(() => t);
}
}
diagnosticListenerObserver.Disable();
}).Dispose();
}
private class MyHandler : HttpClientHandler
{
internal Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
{
return SendAsync(request, CancellationToken.None);
}
}
private static T GetPropertyValueFromAnonymousTypeInstance<T>(object obj, string propertyName)
{
Type t = obj.GetType();
......
......@@ -1428,4 +1428,30 @@ public sealed class HttpClientSendTest_Sync : HttpClientTest.HttpClientSendTest
public HttpClientSendTest_Sync(ITestOutputHelper output) : base(output) { }
protected override bool TestAsync => false;
}
public sealed class CustomHttpClientTest
{
private sealed class CustomHttpClient : HttpClientHandler
{
public Task<HttpResponseMessage> PublicSendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default) =>
SendAsync(request, cancellationToken);
public HttpResponseMessage PublicSend(HttpRequestMessage request, CancellationToken cancellationToken = default) =>
Send(request, cancellationToken);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
public void Send_NullRequest_ThrowsException()
{
using var client = new CustomHttpClient();
AssertExtensions.Throws<ArgumentNullException>("request", () => client.PublicSend(null));
}
[Fact]
public async Task SendAsync_NullRequest_ThrowsException()
{
using var client = new CustomHttpClient();
await AssertExtensions.ThrowsAsync<ArgumentNullException>("request", () => client.PublicSendAsync(null));
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册