2525using Grpc . AspNetCore . Server . Internal ;
2626using Grpc . AspNetCore . Server . Internal . CallHandlers ;
2727using Grpc . AspNetCore . Server . Tests . Infrastructure ;
28+ using Grpc . AspNetCore . Server . Tests . TestObjects ;
2829using Grpc . Core ;
2930using Grpc . Tests . Shared ;
3031using Microsoft . AspNetCore . Http ;
@@ -50,7 +51,7 @@ public class CallHandlerTests
5051 public async Task MinRequestBodyDataRateFeature_MethodType_HasRequestBodyDataRate ( MethodType methodType , bool hasRequestBodyDataRate )
5152 {
5253 // Arrange
53- var httpContext = CreateContext ( ) ;
54+ var httpContext = HttpContextHelpers . CreateContext ( ) ;
5455 var call = CreateHandler ( methodType ) ;
5556
5657 // Act
@@ -67,7 +68,7 @@ public async Task MinRequestBodyDataRateFeature_MethodType_HasRequestBodyDataRat
6768 public async Task MaxRequestBodySizeFeature_MethodType_HasMaxRequestBodySize ( MethodType methodType , bool hasMaxRequestBodySize )
6869 {
6970 // Arrange
70- var httpContext = CreateContext ( ) ;
71+ var httpContext = HttpContextHelpers . CreateContext ( ) ;
7172 var call = CreateHandler ( methodType ) ;
7273
7374 // Act
@@ -84,7 +85,7 @@ public async Task MaxRequestBodySizeFeature_FeatureIsReadOnly_FailureLogged()
8485 var testSink = new TestSink ( ) ;
8586 var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
8687
87- var httpContext = CreateContext ( isMaxRequestBodySizeFeatureReadOnly : true ) ;
88+ var httpContext = HttpContextHelpers . CreateContext ( isMaxRequestBodySizeFeatureReadOnly : true ) ;
8889 var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
8990
9091 // Act
@@ -102,7 +103,7 @@ public async Task ContentTypeValidation_InvalidContentType_FailureLogged()
102103 var testSink = new TestSink ( ) ;
103104 var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
104105
105- var httpContext = CreateContext ( contentType : "text/plain" ) ;
106+ var httpContext = HttpContextHelpers . CreateContext ( contentType : "text/plain" ) ;
106107 var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
107108
108109 // Act
@@ -121,7 +122,7 @@ public async Task SetResponseTrailers_FeatureMissing_ThrowError()
121122 var testSink = new TestSink ( ) ;
122123 var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
123124
124- var httpContext = CreateContext ( skipTrailerFeatureSet : true ) ;
125+ var httpContext = HttpContextHelpers . CreateContext ( skipTrailerFeatureSet : true ) ;
125126 var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
126127
127128 // Act
@@ -138,7 +139,7 @@ public async Task ProtocolValidation_InvalidProtocol_FailureLogged()
138139 var testSink = new TestSink ( ) ;
139140 var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
140141
141- var httpContext = CreateContext ( protocol : "HTTP/1.1" ) ;
142+ var httpContext = HttpContextHelpers . CreateContext ( protocol : "HTTP/1.1" ) ;
142143 var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
143144
144145 // Act
@@ -157,7 +158,7 @@ public async Task ProtocolValidation_IISHttp2Protocol_Success()
157158 var testSink = new TestSink ( ) ;
158159 var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
159160
160- var httpContext = CreateContext ( protocol : GrpcProtocolConstants . Http20Protocol ) ;
161+ var httpContext = HttpContextHelpers . CreateContext ( protocol : GrpcProtocolConstants . Http20Protocol ) ;
161162 var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
162163
163164 // Act
@@ -210,125 +211,6 @@ private static ServerCallHandlerBase<TestService, TestMessage, TestMessage> Crea
210211 throw new ArgumentException ( ) ;
211212 }
212213 }
213-
214- private static HttpContext CreateContext (
215- bool isMaxRequestBodySizeFeatureReadOnly = false ,
216- bool skipTrailerFeatureSet = false ,
217- string ? protocol = null ,
218- string ? contentType = null )
219- {
220- var httpContext = new DefaultHttpContext ( ) ;
221- var responseFeature = new TestHttpResponseFeature ( ) ;
222- var responseBodyFeature = new TestHttpResponseBodyFeature ( httpContext . Features . Get < IHttpResponseBodyFeature > ( ) , responseFeature ) ;
223-
224- httpContext . Request . Protocol = protocol ?? GrpcProtocolConstants . Http2Protocol ;
225- httpContext . Request . ContentType = contentType ?? GrpcProtocolConstants . GrpcContentType ;
226- httpContext . Features . Set < IHttpMinRequestBodyDataRateFeature > ( new TestMinRequestBodyDataRateFeature ( ) ) ;
227- httpContext . Features . Set < IHttpMaxRequestBodySizeFeature > ( new TestMaxRequestBodySizeFeature ( isMaxRequestBodySizeFeatureReadOnly , 100 ) ) ;
228- httpContext . Features . Set < IHttpResponseFeature > ( responseFeature ) ;
229- httpContext . Features . Set < IHttpResponseBodyFeature > ( responseBodyFeature ) ;
230- if ( ! skipTrailerFeatureSet )
231- {
232- httpContext . Features . Set < IHttpResponseTrailersFeature > ( new TestHttpResponseTrailersFeature ( ) ) ;
233- }
234-
235- return httpContext ;
236- }
237- }
238-
239- public class TestService { }
240-
241- public class TestMessage { }
242-
243- public class TestHttpResponseBodyFeature : IHttpResponseBodyFeature
244- {
245- private readonly IHttpResponseBodyFeature _innerResponseBodyFeature ;
246- private readonly TestHttpResponseFeature _responseFeature ;
247-
248- public Stream Stream => _innerResponseBodyFeature . Stream ;
249- public PipeWriter Writer => _innerResponseBodyFeature . Writer ;
250-
251- public TestHttpResponseBodyFeature ( IHttpResponseBodyFeature innerResponseBodyFeature , TestHttpResponseFeature responseFeature )
252- {
253- _innerResponseBodyFeature = innerResponseBodyFeature ?? throw new ArgumentNullException ( nameof ( innerResponseBodyFeature ) ) ;
254- _responseFeature = responseFeature ?? throw new ArgumentNullException ( nameof ( responseFeature ) ) ;
255- }
256-
257- public Task CompleteAsync ( )
258- {
259- return _innerResponseBodyFeature . CompleteAsync ( ) ;
260- }
261-
262- public void DisableBuffering ( )
263- {
264- _innerResponseBodyFeature . DisableBuffering ( ) ;
265- }
266-
267- public Task SendFileAsync ( string path , long offset , long ? count , CancellationToken cancellationToken = default )
268- {
269- return _innerResponseBodyFeature . SendFileAsync ( path , offset , count , cancellationToken ) ;
270- }
271-
272- public Task StartAsync ( CancellationToken cancellationToken = default )
273- {
274- _responseFeature . HasStarted = true ;
275- return _innerResponseBodyFeature . StartAsync ( cancellationToken ) ;
276- }
277- }
278-
279- public class TestHttpResponseFeature : IHttpResponseFeature
280- {
281- public Stream Body { get ; set ; }
282- public bool HasStarted { get ; internal set ; }
283- public IHeaderDictionary Headers { get ; set ; }
284- public string ? ReasonPhrase { get ; set ; }
285- public int StatusCode { get ; set ; }
286-
287- public TestHttpResponseFeature ( )
288- {
289- StatusCode = 200 ;
290- Headers = new HeaderDictionary ( ) ;
291- Body = Stream . Null ;
292- }
293-
294- public void OnCompleted ( Func < object , Task > callback , object state )
295- {
296- }
297-
298- public void OnStarting ( Func < object , Task > callback , object state )
299- {
300- HasStarted = true ;
301- }
302- }
303-
304- public class TestMinRequestBodyDataRateFeature : IHttpMinRequestBodyDataRateFeature
305- {
306- public MinDataRate MinDataRate { get ; set ; } = new MinDataRate ( 1 , TimeSpan . FromSeconds ( 5 ) ) ;
307- }
308-
309- public class TestMaxRequestBodySizeFeature : IHttpMaxRequestBodySizeFeature
310- {
311- public TestMaxRequestBodySizeFeature ( bool isReadOnly , long ? maxRequestBodySize )
312- {
313- IsReadOnly = isReadOnly ;
314- MaxRequestBodySize = maxRequestBodySize ;
315- }
316-
317- public bool IsReadOnly { get ; }
318- public long ? MaxRequestBodySize { get ; set ; }
319- }
320-
321- internal class TestGrpcServiceActivator < TGrpcService > : IGrpcServiceActivator < TGrpcService > where TGrpcService : class , new ( )
322- {
323- public GrpcActivatorHandle < TGrpcService > Create ( IServiceProvider serviceProvider )
324- {
325- return new GrpcActivatorHandle < TGrpcService > ( new TGrpcService ( ) , false , null ) ;
326- }
327-
328- public ValueTask ReleaseAsync ( GrpcActivatorHandle < TGrpcService > service )
329- {
330- return default ;
331- }
332214 }
333215
334216 public class TestServiceProvider : IServiceProvider
@@ -340,4 +222,8 @@ public object GetService(Type serviceType)
340222 throw new NotImplementedException ( ) ;
341223 }
342224 }
225+
226+ public class TestService { }
227+
228+ public class TestMessage { }
343229}
0 commit comments