Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Grpc.Net.Client.Web/GrpcWebHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ private async Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request

var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);

if (_httpVersion != null)
{
// If a HTTP version has been specified then we need to reset it back to 2.0.
// The gRPC client validates HTTP version 2.0.
response.Version = HttpVersion.Version20;
}

if (IsMatchingResponseContentType(_mode, response.Content.Headers.ContentType?.MediaType))
{
response.Content = new GrpcWebResponseContent(response.Content, _mode, response.TrailingHeaders);
// The gRPC client validates HTTP version 2.0
response.Version = HttpVersion.Version20;
}

return response;
Expand Down
65 changes: 65 additions & 0 deletions test/FunctionalTests/Web/Client/AuthTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#endregion

using System.Threading.Tasks;
using Authorize;
using Grpc.AspNetCore.FunctionalTests.Infrastructure;
using Grpc.Core;
using Grpc.Gateway.Testing;
using Grpc.Net.Client;
using Grpc.Tests.Shared;
using Microsoft.Extensions.Logging;
using NUnit.Framework;

namespace Grpc.AspNetCore.FunctionalTests.Web.Client
{
[TestFixture(GrpcTestMode.GrpcWeb, TestServerEndpointName.Http1)]
[TestFixture(GrpcTestMode.GrpcWeb, TestServerEndpointName.Http2)]
[TestFixture(GrpcTestMode.GrpcWebText, TestServerEndpointName.Http1)]
[TestFixture(GrpcTestMode.GrpcWebText, TestServerEndpointName.Http2)]
[TestFixture(GrpcTestMode.Grpc, TestServerEndpointName.Http2)]
public class AuthTests : GrpcWebFunctionalTestBase
{
public AuthTests(GrpcTestMode grpcTestMode, TestServerEndpointName endpointName)
: base(grpcTestMode, endpointName)
{
}

[Test]
public async Task SendValidRequest_SuccessResponse()
{
// Arrage
var httpClient = CreateGrpcWebClient();
var channel = GrpcChannel.ForAddress(httpClient.BaseAddress, new GrpcChannelOptions
{
HttpClient = httpClient,
LoggerFactory = LoggerFactory
});

var client = new AuthorizedGreeter.AuthorizedGreeterClient(channel);

// Act
var ex = await ExceptionAssert.ThrowsAsync<RpcException>(() => client.SayHelloAsync(new HelloRequest { Name = "test" }).ResponseAsync).DefaultTimeout();

// Assert
Assert.AreEqual(StatusCode.Unauthenticated, ex.StatusCode);

AssertHasLog(LogLevel.Information, "GrpcStatusError", "Call failed with gRPC error status. Status code: 'Unauthenticated', Message: 'Bad gRPC response. HTTP status code: 401'.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public async Task SendValidRequest_ServerAbort_ClientThrowsAbortException()

Assert.AreEqual(StatusCode.Aborted, call.GetStatus().StatusCode);

AssertHasLog(LogLevel.Information, "GrpcStatusError", "Call failed with gRPC error status. Status code: 'Aborted', Message: 'Aborted from server side.'.");
AssertHasLogRpcConnectionError(StatusCode.Aborted, "Aborted from server side.");
}

Expand Down