1
1
/*
2
- * Copyright 2023-2024 the original author or authors.
2
+ * Copyright 2023-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
43
43
* Tests for {@link ClientInterceptorsConfigurer}.
44
44
*
45
45
* @author Chris Bono
46
+ * @author Andrey Litvitski
46
47
*/
47
48
class ClientInterceptorsConfigurerTests {
48
49
@@ -67,8 +68,9 @@ private void customizeContextAndRunConfigurer(
67
68
List <ClientInterceptor > clientSpecificInterceptors , List <ClientInterceptor > expectedInterceptors ) {
68
69
ManagedChannelBuilder <?> builder = Mockito .mock ();
69
70
this .contextRunner ().with (contextCustomizer ).run ((context ) -> {
71
+ var factory = Mockito .mock (GrpcChannelFactory .class );
70
72
var configurer = context .getBean (ClientInterceptorsConfigurer .class );
71
- configurer .configureInterceptors (builder , clientSpecificInterceptors , true );
73
+ configurer .configureInterceptors (builder , clientSpecificInterceptors , true , factory );
72
74
// NOTE: the interceptors are called in reverse order per builder contract
73
75
var expectedInterceptorsReversed = new ArrayList <>(expectedInterceptors );
74
76
Collections .reverse (expectedInterceptorsReversed );
@@ -129,13 +131,14 @@ void whenBlendInterceptorsFalseThenGlobalInterceptorsAddedFirst() {
129
131
ClientInterceptorsConfigurerTests .this .contextRunner ()
130
132
.withUserConfiguration (GlobalClientInterceptorsConfig .class , ClientSpecificInterceptorsConfig .class )
131
133
.run ((context ) -> {
134
+ var factory = Mockito .mock (GrpcChannelFactory .class );
132
135
var interceptorA = context .getBean ("interceptorA" , ClientInterceptor .class );
133
136
var interceptorB = context .getBean ("interceptorB" , ClientInterceptor .class );
134
137
var clientSpecificInterceptors = List .of (interceptorB , interceptorA );
135
138
var expectedInterceptors = List .of (GlobalClientInterceptorsConfig .GLOBAL_INTERCEPTOR_BAR ,
136
139
GlobalClientInterceptorsConfig .GLOBAL_INTERCEPTOR_FOO , interceptorB , interceptorA );
137
140
var configurer = context .getBean (ClientInterceptorsConfigurer .class );
138
- configurer .configureInterceptors (builder , clientSpecificInterceptors , false );
141
+ configurer .configureInterceptors (builder , clientSpecificInterceptors , false , factory );
139
142
// NOTE: the interceptors are called in reverse order per builder
140
143
// contract
141
144
var expectedInterceptorsReversed = new ArrayList <>(expectedInterceptors );
@@ -151,13 +154,55 @@ void whenBlendInterceptorsTrueThenGlobalInterceptorsBlended() {
151
154
ClientInterceptorsConfigurerTests .this .contextRunner ()
152
155
.withUserConfiguration (GlobalClientInterceptorsConfig .class , ClientSpecificInterceptorsConfig .class )
153
156
.run ((context ) -> {
157
+ var factory = Mockito .mock (GrpcChannelFactory .class );
154
158
var interceptorA = context .getBean ("interceptorA" , ClientInterceptor .class );
155
159
var interceptorB = context .getBean ("interceptorB" , ClientInterceptor .class );
156
160
var clientSpecificInterceptors = List .of (interceptorB , interceptorA );
157
161
var expectedInterceptors = List .of (GlobalClientInterceptorsConfig .GLOBAL_INTERCEPTOR_BAR ,
158
162
interceptorB , GlobalClientInterceptorsConfig .GLOBAL_INTERCEPTOR_FOO , interceptorA );
159
163
var configurer = context .getBean (ClientInterceptorsConfigurer .class );
160
- configurer .configureInterceptors (builder , clientSpecificInterceptors , true );
164
+ configurer .configureInterceptors (builder , clientSpecificInterceptors , true , factory );
165
+ // NOTE: the interceptors are called in reverse order per builder
166
+ // contract
167
+ var expectedInterceptorsReversed = new ArrayList <>(expectedInterceptors );
168
+ Collections .reverse (expectedInterceptorsReversed );
169
+ verify (builder ).intercept (expectedInterceptorsReversed );
170
+ });
171
+ }
172
+
173
+ }
174
+
175
+ @ Nested
176
+ class WithInterceptorFilters {
177
+
178
+ @ Test
179
+ void whenFilterExcludesOneGlobalInterceptor_thenBuilderGetsOnlyAllowedOnes () {
180
+ ManagedChannelBuilder <?> builder = Mockito .mock ();
181
+ ClientInterceptorsConfigurerTests .this .contextRunner ()
182
+ .withUserConfiguration (GlobalClientInterceptorsConfig .class )
183
+ .withBean (ClientInterceptorFilter .class ,
184
+ () -> (interceptor , __ ) -> interceptor == GlobalClientInterceptorsConfig .GLOBAL_INTERCEPTOR_BAR )
185
+ .run (context -> {
186
+ var factory = Mockito .mock (GrpcChannelFactory .class );
187
+ var configurer = context .getBean (ClientInterceptorsConfigurer .class );
188
+ configurer .configureInterceptors (builder , List .of (), true , factory );
189
+ var expectedInterceptors = List .of (GlobalClientInterceptorsConfig .GLOBAL_INTERCEPTOR_BAR );
190
+ verify (builder ).intercept (expectedInterceptors );
191
+ });
192
+ }
193
+
194
+ @ Test
195
+ void whenFilterIncludesAllGlobalInterceptors_thenBuilderGetsOnlyAllowedOnes () {
196
+ ManagedChannelBuilder <?> builder = Mockito .mock ();
197
+ ClientInterceptorsConfigurerTests .this .contextRunner ()
198
+ .withUserConfiguration (GlobalClientInterceptorsConfig .class )
199
+ .withBean (ClientInterceptorFilter .class , () -> (interceptor , __ ) -> true )
200
+ .run (context -> {
201
+ var factory = Mockito .mock (GrpcChannelFactory .class );
202
+ var configurer = context .getBean (ClientInterceptorsConfigurer .class );
203
+ configurer .configureInterceptors (builder , List .of (), true , factory );
204
+ var expectedInterceptors = List .of (GlobalClientInterceptorsConfig .GLOBAL_INTERCEPTOR_BAR ,
205
+ GlobalClientInterceptorsConfig .GLOBAL_INTERCEPTOR_FOO );
161
206
// NOTE: the interceptors are called in reverse order per builder
162
207
// contract
163
208
var expectedInterceptorsReversed = new ArrayList <>(expectedInterceptors );
0 commit comments