Skip to content

Commit 9e63dc0

Browse files
authored
Add functional tests for using HttpContext (#106)
1 parent 847bbe0 commit 9e63dc0

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System.IO;
20+
using System.IO.Pipelines;
21+
using System.Linq;
22+
using System.Net;
23+
using System.Net.Http;
24+
using System.Threading.Tasks;
25+
using Greet;
26+
using Grpc.AspNetCore.FunctionalTests.Infrastructure;
27+
using Grpc.AspNetCore.Server.Internal;
28+
using Grpc.Core;
29+
using NUnit.Framework;
30+
31+
namespace Grpc.AspNetCore.FunctionalTests
32+
{
33+
[TestFixture]
34+
public class HttpContextTests : FunctionalTestBase
35+
{
36+
[Test]
37+
public async Task HttpContextAccessor_ReturnContextInTrailer()
38+
{
39+
// Arrange
40+
var requestMessage = new HelloRequest
41+
{
42+
Name = "World"
43+
};
44+
45+
var requestStream = new MemoryStream();
46+
MessageHelpers.WriteMessage(requestStream, requestMessage);
47+
48+
var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Greet.Greeter/SayHelloWithHttpContextAccessor?query=extra");
49+
httpRequest.Content = new StreamContent(requestStream);
50+
51+
// Act
52+
var response = await Fixture.Client.SendAsync(httpRequest).DefaultTimeout();
53+
54+
// Assert
55+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
56+
57+
Assert.AreEqual(StatusCode.OK.ToTrailerString(), Fixture.TrailersContainer.Trailers[GrpcProtocolConstants.StatusTrailer].ToString());
58+
Assert.AreEqual("/Greet.Greeter/SayHelloWithHttpContextAccessor?query=extra", Fixture.TrailersContainer.Trailers["Test-HttpContext-PathAndQueryString"].ToString());
59+
}
60+
61+
[Test]
62+
public async Task HttpContextExtensionMethod_ReturnContextInTrailer()
63+
{
64+
// Arrange
65+
var requestMessage = new HelloRequest
66+
{
67+
Name = "World"
68+
};
69+
70+
var requestStream = new MemoryStream();
71+
MessageHelpers.WriteMessage(requestStream, requestMessage);
72+
73+
var httpRequest = new HttpRequestMessage(HttpMethod.Post, "Greet.Greeter/SayHelloWithHttpContextExtensionMethod?query=extra");
74+
httpRequest.Content = new StreamContent(requestStream);
75+
76+
// Act
77+
var response = await Fixture.Client.SendAsync(httpRequest).DefaultTimeout();
78+
79+
// Assert
80+
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
81+
82+
Assert.AreEqual(StatusCode.OK.ToTrailerString(), Fixture.TrailersContainer.Trailers[GrpcProtocolConstants.StatusTrailer].ToString());
83+
Assert.AreEqual("/Greet.Greeter/SayHelloWithHttpContextExtensionMethod?query=extra", Fixture.TrailersContainer.Trailers["Test-HttpContext-PathAndQueryString"].ToString());
84+
}
85+
}
86+
}

testassets/FunctionalTestsWebsite/Greeter.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
using System.Threading.Tasks;
2222
using Greet;
2323
using Grpc.Core;
24+
using Microsoft.AspNetCore.Http;
2425
using Microsoft.Extensions.Logging;
2526

2627
class GreeterService : Greeter.GreeterBase
2728
{
2829
private readonly ILogger _logger;
30+
private readonly IHttpContextAccessor _httpContextAccessor;
2931

30-
public GreeterService(ILoggerFactory loggerFactory)
32+
public GreeterService(ILoggerFactory loggerFactory, IHttpContextAccessor httpContextAccessor)
3133
{
3234
_logger = loggerFactory.CreateLogger<GreeterService>();
35+
_httpContextAccessor = httpContextAccessor;
3336
}
3437

3538
//Server side handler of the SayHello RPC
@@ -39,6 +42,22 @@ public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContex
3942
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
4043
}
4144

45+
public override Task<HelloReply> SayHelloWithHttpContextAccessor(HelloRequest request, ServerCallContext context)
46+
{
47+
var httpContext = _httpContextAccessor.HttpContext;
48+
context.ResponseTrailers.Add("Test-HttpContext-PathAndQueryString", httpContext.Request.Path + httpContext.Request.QueryString);
49+
50+
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
51+
}
52+
53+
public override Task<HelloReply> SayHelloWithHttpContextExtensionMethod(HelloRequest request, ServerCallContext context)
54+
{
55+
var httpContext = context.GetHttpContext();
56+
context.ResponseTrailers.Add("Test-HttpContext-PathAndQueryString", httpContext.Request.Path + httpContext.Request.QueryString);
57+
58+
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
59+
}
60+
4261
public override Task<HelloReply> SayHelloSendLargeReply(HelloRequest request, ServerCallContext context)
4362
{
4463
_logger.LogInformation($"Sending hello to {request.Name}");

testassets/FunctionalTestsWebsite/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public void ConfigureServices(IServiceCollection services)
3838
options.SendMaxMessageSize = 64 * 1024;
3939
options.ReceiveMaxMessageSize = 64 * 1024;
4040
});
41+
services.AddHttpContextAccessor();
42+
4143
services.AddScoped<IncrementingCounter>();
4244

4345
// When the site is run from the test project a signaler will already be registered

testassets/Proto/greet.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ package Greet;
2020
service Greeter {
2121
// Sends a greeting
2222
rpc SayHello (HelloRequest) returns (HelloReply) {}
23+
rpc SayHelloWithHttpContextAccessor (HelloRequest) returns (HelloReply) {}
24+
rpc SayHelloWithHttpContextExtensionMethod (HelloRequest) returns (HelloReply) {}
2325
rpc SayHelloSendLargeReply (HelloRequest) returns (HelloReply) {}
2426
rpc SayHelloSendHeadersTwice (HelloRequest) returns (HelloReply) {}
2527
rpc SayHellos (HelloRequest) returns (stream HelloReply) {}

0 commit comments

Comments
 (0)