Skip to content

Commit cfa82e0

Browse files
authored
Seal metadata type and cleanup InterceptorCollection (#462)
1 parent 3c271ec commit cfa82e0

File tree

3 files changed

+147
-3
lines changed

3 files changed

+147
-3
lines changed

src/Grpc.AspNetCore.Server/GrpcMethodMetadata.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Grpc.AspNetCore.Server
2424
/// <summary>
2525
/// Metadata for a gRPC method endpoint.
2626
/// </summary>
27-
public class GrpcMethodMetadata
27+
public sealed class GrpcMethodMetadata
2828
{
2929
/// <summary>
3030
/// Creates a new instance of <see cref="GrpcMethodMetadata"/> with the provided service type and method.

src/Grpc.AspNetCore.Server/InterceptorCollection.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,44 @@ namespace Grpc.AspNetCore.Server
2828
/// </summary>
2929
public class InterceptorCollection : Collection<InterceptorRegistration>
3030
{
31+
internal InterceptorCollection()
32+
{
33+
}
34+
3135
/// <summary>
3236
/// Add an interceptor to the end of the pipeline.
3337
/// </summary>
3438
/// <typeparam name="TInterceptor">The interceptor type.</typeparam>
3539
/// <param name="args">The list of arguments to pass to the interceptor constructor when creating an instance.</param>
3640
public void Add<TInterceptor>(params object[] args) where TInterceptor : Interceptor
3741
{
38-
Add(new InterceptorRegistration(typeof(TInterceptor), args));
42+
Add(typeof(TInterceptor), args);
43+
}
44+
45+
/// <summary>
46+
/// Add an interceptor to the end of the pipeline.
47+
/// </summary>
48+
/// <param name="interceptorType">The interceptor type.</param>
49+
/// <param name="args">The list of arguments to pass to the interceptor constructor when creating an instance.</param>
50+
public void Add(Type interceptorType, params object[] args)
51+
{
52+
if (interceptorType == null)
53+
{
54+
throw new ArgumentNullException(nameof(interceptorType));
55+
}
56+
if (!interceptorType.IsSubclassOf(typeof(Interceptor)))
57+
{
58+
throw new ArgumentException($"Type must inherit from {typeof(Interceptor).FullName}.", nameof(interceptorType));
59+
}
60+
61+
Add(new InterceptorRegistration(interceptorType, args));
3962
}
4063

4164
/// <summary>
4265
/// Append a set of interceptor registrations to the end of the pipeline.
4366
/// </summary>
4467
/// <param name="registrations">The set of interceptor registrations to add.</param>
45-
public void AddRange(IEnumerable<InterceptorRegistration> registrations)
68+
internal void AddRange(IEnumerable<InterceptorRegistration> registrations)
4669
{
4770
if (registrations == null)
4871
{
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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;
20+
using Grpc.AspNetCore.Server;
21+
using Grpc.Core.Interceptors;
22+
using NUnit.Framework;
23+
24+
namespace Grpc.AspNetCore.FunctionalTests.Server
25+
{
26+
[TestFixture]
27+
public class InterceptorCollectionTests
28+
{
29+
[Test]
30+
public void Add_NonGeneric_AddedToCollection()
31+
{
32+
// Arrange
33+
var interceptors = new InterceptorCollection();
34+
35+
// Act
36+
interceptors.Add(typeof(TestInterceptor));
37+
38+
// Assert
39+
Assert.AreEqual(1, interceptors.Count);
40+
Assert.AreEqual(typeof(TestInterceptor), interceptors[0].Type);
41+
Assert.AreEqual(0, interceptors[0].Arguments.Count);
42+
}
43+
44+
[Test]
45+
public void Add_NonGenericInterceptorBaseType_ThrowError()
46+
{
47+
// Arrange
48+
var interceptors = new InterceptorCollection();
49+
50+
// Act
51+
var ex = Assert.Throws<ArgumentException>(() => interceptors.Add(typeof(Interceptor)));
52+
53+
// Assert
54+
Assert.AreEqual("Type must inherit from Grpc.Core.Interceptors.Interceptor. (Parameter 'interceptorType')", ex.Message);
55+
}
56+
57+
[Test]
58+
public void Add_Generic_AddedToCollection()
59+
{
60+
// Arrange
61+
var interceptors = new InterceptorCollection();
62+
63+
// Act
64+
interceptors.Add<TestInterceptor>();
65+
66+
// Assert
67+
Assert.AreEqual(1, interceptors.Count);
68+
Assert.AreEqual(typeof(TestInterceptor), interceptors[0].Type);
69+
Assert.AreEqual(0, interceptors[0].Arguments.Count);
70+
}
71+
72+
[Test]
73+
public void Add_GenericInterceptorBaseType_AddedToCollection()
74+
{
75+
// Arrange
76+
var interceptors = new InterceptorCollection();
77+
78+
// Act
79+
var ex = Assert.Throws<ArgumentException>(() => interceptors.Add<Interceptor>());
80+
81+
// Assert
82+
Assert.AreEqual("Type must inherit from Grpc.Core.Interceptors.Interceptor. (Parameter 'interceptorType')", ex.Message);
83+
}
84+
85+
[Test]
86+
public void Add_NonGenericWithArgs_AddedToCollection()
87+
{
88+
// Arrange
89+
var interceptors = new InterceptorCollection();
90+
91+
// Act
92+
interceptors.Add(typeof(TestInterceptor), "Arg");
93+
94+
// Assert
95+
Assert.AreEqual(1, interceptors.Count);
96+
Assert.AreEqual(typeof(TestInterceptor), interceptors[0].Type);
97+
Assert.AreEqual(1, interceptors[0].Arguments.Count);
98+
Assert.AreEqual("Arg", interceptors[0].Arguments[0]);
99+
}
100+
101+
[Test]
102+
public void Add_GenericWithArgs_AddedToCollection()
103+
{
104+
// Arrange
105+
var interceptors = new InterceptorCollection();
106+
107+
// Act
108+
interceptors.Add<TestInterceptor>("Arg");
109+
110+
// Assert
111+
Assert.AreEqual(1, interceptors.Count);
112+
Assert.AreEqual(typeof(TestInterceptor), interceptors[0].Type);
113+
Assert.AreEqual(1, interceptors[0].Arguments.Count);
114+
Assert.AreEqual("Arg", interceptors[0].Arguments[0]);
115+
}
116+
117+
private class TestInterceptor : Interceptor
118+
{
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)