25
25
using Grpc . AspNetCore . Server . Internal ;
26
26
using Grpc . AspNetCore . Server . Internal . CallHandlers ;
27
27
using Grpc . AspNetCore . Server . Tests . Infrastructure ;
28
+ using Grpc . AspNetCore . Server . Tests . TestObjects ;
28
29
using Grpc . Core ;
29
30
using Grpc . Tests . Shared ;
30
31
using Microsoft . AspNetCore . Http ;
@@ -50,7 +51,7 @@ public class CallHandlerTests
50
51
public async Task MinRequestBodyDataRateFeature_MethodType_HasRequestBodyDataRate ( MethodType methodType , bool hasRequestBodyDataRate )
51
52
{
52
53
// Arrange
53
- var httpContext = CreateContext ( ) ;
54
+ var httpContext = HttpContextHelpers . CreateContext ( ) ;
54
55
var call = CreateHandler ( methodType ) ;
55
56
56
57
// Act
@@ -67,7 +68,7 @@ public async Task MinRequestBodyDataRateFeature_MethodType_HasRequestBodyDataRat
67
68
public async Task MaxRequestBodySizeFeature_MethodType_HasMaxRequestBodySize ( MethodType methodType , bool hasMaxRequestBodySize )
68
69
{
69
70
// Arrange
70
- var httpContext = CreateContext ( ) ;
71
+ var httpContext = HttpContextHelpers . CreateContext ( ) ;
71
72
var call = CreateHandler ( methodType ) ;
72
73
73
74
// Act
@@ -84,7 +85,7 @@ public async Task MaxRequestBodySizeFeature_FeatureIsReadOnly_FailureLogged()
84
85
var testSink = new TestSink ( ) ;
85
86
var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
86
87
87
- var httpContext = CreateContext ( isMaxRequestBodySizeFeatureReadOnly : true ) ;
88
+ var httpContext = HttpContextHelpers . CreateContext ( isMaxRequestBodySizeFeatureReadOnly : true ) ;
88
89
var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
89
90
90
91
// Act
@@ -102,7 +103,7 @@ public async Task ContentTypeValidation_InvalidContentType_FailureLogged()
102
103
var testSink = new TestSink ( ) ;
103
104
var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
104
105
105
- var httpContext = CreateContext ( contentType : "text/plain" ) ;
106
+ var httpContext = HttpContextHelpers . CreateContext ( contentType : "text/plain" ) ;
106
107
var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
107
108
108
109
// Act
@@ -121,7 +122,7 @@ public async Task SetResponseTrailers_FeatureMissing_ThrowError()
121
122
var testSink = new TestSink ( ) ;
122
123
var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
123
124
124
- var httpContext = CreateContext ( skipTrailerFeatureSet : true ) ;
125
+ var httpContext = HttpContextHelpers . CreateContext ( skipTrailerFeatureSet : true ) ;
125
126
var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
126
127
127
128
// Act
@@ -138,7 +139,7 @@ public async Task ProtocolValidation_InvalidProtocol_FailureLogged()
138
139
var testSink = new TestSink ( ) ;
139
140
var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
140
141
141
- var httpContext = CreateContext ( protocol : "HTTP/1.1" ) ;
142
+ var httpContext = HttpContextHelpers . CreateContext ( protocol : "HTTP/1.1" ) ;
142
143
var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
143
144
144
145
// Act
@@ -157,7 +158,7 @@ public async Task ProtocolValidation_IISHttp2Protocol_Success()
157
158
var testSink = new TestSink ( ) ;
158
159
var testLoggerFactory = new TestLoggerFactory ( testSink , true ) ;
159
160
160
- var httpContext = CreateContext ( protocol : GrpcProtocolConstants . Http20Protocol ) ;
161
+ var httpContext = HttpContextHelpers . CreateContext ( protocol : GrpcProtocolConstants . Http20Protocol ) ;
161
162
var call = CreateHandler ( MethodType . ClientStreaming , testLoggerFactory ) ;
162
163
163
164
// Act
@@ -210,125 +211,6 @@ private static ServerCallHandlerBase<TestService, TestMessage, TestMessage> Crea
210
211
throw new ArgumentException ( ) ;
211
212
}
212
213
}
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
- }
332
214
}
333
215
334
216
public class TestServiceProvider : IServiceProvider
@@ -340,4 +222,8 @@ public object GetService(Type serviceType)
340
222
throw new NotImplementedException ( ) ;
341
223
}
342
224
}
225
+
226
+ public class TestService { }
227
+
228
+ public class TestMessage { }
343
229
}
0 commit comments