1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-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.
35
35
import static org .assertj .core .api .Assertions .assertThat ;
36
36
37
37
/**
38
+ * Tests for {@link InterceptingClientHttpRequestFactory}
38
39
* @author Arjen Poutsma
39
40
* @author Juergen Hoeller
41
+ * @author Brian Clozel
40
42
*/
41
43
class InterceptingClientHttpRequestFactoryTests {
42
44
@@ -54,7 +56,7 @@ void beforeEach() {
54
56
}
55
57
56
58
@ Test
57
- void basic () throws Exception {
59
+ void shouldInvokeInterceptors () throws Exception {
58
60
List <ClientHttpRequestInterceptor > interceptors = new ArrayList <>();
59
61
interceptors .add (new NoOpInterceptor ());
60
62
interceptors .add (new NoOpInterceptor ());
@@ -64,31 +66,30 @@ void basic() throws Exception {
64
66
ClientHttpRequest request = requestFactory .createRequest (URI .create ("https://example.com" ), HttpMethod .GET );
65
67
ClientHttpResponse response = request .execute ();
66
68
67
- assertThat (((NoOpInterceptor ) interceptors .get (0 )).invoked ). isTrue ( );
68
- assertThat (((NoOpInterceptor ) interceptors .get (1 )).invoked ). isTrue ( );
69
- assertThat (((NoOpInterceptor ) interceptors .get (2 )).invoked ). isTrue ( );
69
+ assertThat (((NoOpInterceptor ) interceptors .get (0 )).invocationCount ). isEqualTo ( 1 );
70
+ assertThat (((NoOpInterceptor ) interceptors .get (1 )).invocationCount ). isEqualTo ( 1 );
71
+ assertThat (((NoOpInterceptor ) interceptors .get (2 )).invocationCount ). isEqualTo ( 1 );
70
72
assertThat (requestMock .isExecuted ()).isTrue ();
71
73
assertThat (response ).isSameAs (responseMock );
72
74
}
73
75
74
76
@ Test
75
- void noExecution () throws Exception {
77
+ void shouldSkipIntercetor () throws Exception {
76
78
List <ClientHttpRequestInterceptor > interceptors = new ArrayList <>();
77
79
interceptors .add ((request , body , execution ) -> responseMock );
78
-
79
80
interceptors .add (new NoOpInterceptor ());
80
81
requestFactory = new InterceptingClientHttpRequestFactory (requestFactoryMock , interceptors );
81
82
82
83
ClientHttpRequest request = requestFactory .createRequest (URI .create ("https://example.com" ), HttpMethod .GET );
83
84
ClientHttpResponse response = request .execute ();
84
85
85
- assertThat (((NoOpInterceptor ) interceptors .get (1 )).invoked ). isFalse ();
86
+ assertThat (((NoOpInterceptor ) interceptors .get (1 )).invocationCount ). isZero ();
86
87
assertThat (requestMock .isExecuted ()).isFalse ();
87
88
assertThat (response ).isSameAs (responseMock );
88
89
}
89
90
90
91
@ Test
91
- void changeHeaders () throws Exception {
92
+ void interceptorShouldUpdateRequestHeader () throws Exception {
92
93
final String headerName = "Foo" ;
93
94
final String headerValue = "Bar" ;
94
95
final String otherValue = "Baz" ;
@@ -98,7 +99,6 @@ void changeHeaders() throws Exception {
98
99
wrapper .getHeaders ().add (headerName , otherValue );
99
100
return execution .execute (wrapper , body );
100
101
};
101
-
102
102
requestMock = new MockClientHttpRequest () {
103
103
@ Override
104
104
protected ClientHttpResponse executeInternal () {
@@ -110,39 +110,34 @@ protected ClientHttpResponse executeInternal() {
110
110
requestMock .getHeaders ().add (headerName , headerValue );
111
111
112
112
requestFactory = new InterceptingClientHttpRequestFactory (requestFactoryMock , Collections .singletonList (interceptor ));
113
-
114
113
ClientHttpRequest request = requestFactory .createRequest (URI .create ("https://example.com" ), HttpMethod .GET );
115
114
request .execute ();
116
115
}
117
116
118
117
@ Test
119
- void changeAttribute () throws Exception {
118
+ void interceptorShouldUpdateRequestAttribute () throws Exception {
120
119
final String attrName = "Foo" ;
121
120
final String attrValue = "Bar" ;
122
121
123
122
ClientHttpRequestInterceptor interceptor = (request , body , execution ) -> {
124
- System .out .println ("interceptor" );
125
123
request .getAttributes ().put (attrName , attrValue );
126
124
return execution .execute (request , body );
127
125
};
128
-
129
126
requestMock = new MockClientHttpRequest () {
130
127
@ Override
131
128
protected ClientHttpResponse executeInternal () {
132
- System .out .println ("execute" );
133
129
assertThat (getAttributes ()).containsEntry (attrName , attrValue );
134
130
return responseMock ;
135
131
}
136
132
};
137
-
138
133
requestFactory = new InterceptingClientHttpRequestFactory (requestFactoryMock , Collections .singletonList (interceptor ));
139
134
140
135
ClientHttpRequest request = requestFactory .createRequest (URI .create ("https://example.com" ), HttpMethod .GET );
141
136
request .execute ();
142
137
}
143
138
144
139
@ Test
145
- void changeURI () throws Exception {
140
+ void interceptorShouldUpdateRequestURI () throws Exception {
146
141
final URI changedUri = URI .create ("https://example.com/2" );
147
142
148
143
ClientHttpRequestInterceptor interceptor = (request , body , execution ) -> execution .execute (new HttpRequestWrapper (request ) {
@@ -152,23 +147,21 @@ public URI getURI() {
152
147
}
153
148
154
149
}, body );
155
-
156
150
requestFactoryMock = new RequestFactoryMock () {
157
151
@ Override
158
152
public ClientHttpRequest createRequest (URI uri , HttpMethod httpMethod ) throws IOException {
159
153
assertThat (uri ).isEqualTo (changedUri );
160
154
return super .createRequest (uri , httpMethod );
161
155
}
162
156
};
163
-
164
157
requestFactory = new InterceptingClientHttpRequestFactory (requestFactoryMock , Collections .singletonList (interceptor ));
165
158
166
159
ClientHttpRequest request = requestFactory .createRequest (URI .create ("https://example.com" ), HttpMethod .GET );
167
160
request .execute ();
168
161
}
169
162
170
163
@ Test
171
- void changeMethod () throws Exception {
164
+ void interceptorShouldUpdateRequestMethod () throws Exception {
172
165
final HttpMethod changedMethod = HttpMethod .POST ;
173
166
174
167
ClientHttpRequestInterceptor interceptor = (request , body , execution ) -> execution .execute (new HttpRequestWrapper (request ) {
@@ -178,44 +171,66 @@ public HttpMethod getMethod() {
178
171
}
179
172
180
173
}, body );
181
-
182
174
requestFactoryMock = new RequestFactoryMock () {
183
175
@ Override
184
176
public ClientHttpRequest createRequest (URI uri , HttpMethod httpMethod ) throws IOException {
185
177
assertThat (httpMethod ).isEqualTo (changedMethod );
186
178
return super .createRequest (uri , httpMethod );
187
179
}
188
180
};
189
-
190
181
requestFactory = new InterceptingClientHttpRequestFactory (requestFactoryMock , Collections .singletonList (interceptor ));
191
182
192
183
ClientHttpRequest request = requestFactory .createRequest (URI .create ("https://example.com" ), HttpMethod .GET );
193
184
request .execute ();
194
185
}
195
186
196
187
@ Test
197
- void changeBody () throws Exception {
188
+ void interceptorShouldUpdateRequestBody () throws Exception {
198
189
final byte [] changedBody = "Foo" .getBytes ();
199
-
200
190
ClientHttpRequestInterceptor interceptor = (request , body , execution ) -> execution .execute (request , changedBody );
201
-
202
191
requestFactory = new InterceptingClientHttpRequestFactory (requestFactoryMock , Collections .singletonList (interceptor ));
203
-
204
192
ClientHttpRequest request = requestFactory .createRequest (URI .create ("https://example.com" ), HttpMethod .GET );
205
193
request .execute ();
194
+
206
195
assertThat (Arrays .equals (changedBody , requestMock .getBodyAsBytes ())).isTrue ();
207
196
assertThat (requestMock .getHeaders ().getContentLength ()).isEqualTo (changedBody .length );
208
197
}
209
198
199
+ @ Test
200
+ void interceptorShouldAlwaysExecuteNextInterceptor () throws Exception {
201
+ List <ClientHttpRequestInterceptor > interceptors = new ArrayList <>();
202
+ interceptors .add (new MultipleExecutionInterceptor ());
203
+ interceptors .add (new NoOpInterceptor ());
204
+ requestFactory = new InterceptingClientHttpRequestFactory (requestFactoryMock , interceptors );
205
+
206
+ ClientHttpRequest request = requestFactory .createRequest (URI .create ("https://example.com" ), HttpMethod .GET );
207
+ ClientHttpResponse response = request .execute ();
208
+
209
+ assertThat (((NoOpInterceptor ) interceptors .get (1 )).invocationCount ).isEqualTo (2 );
210
+ assertThat (requestMock .isExecuted ()).isTrue ();
211
+ assertThat (response ).isSameAs (responseMock );
212
+ }
213
+
210
214
211
215
private static class NoOpInterceptor implements ClientHttpRequestInterceptor {
212
216
213
- private boolean invoked = false ;
217
+ private int invocationCount = 0 ;
218
+
219
+ @ Override
220
+ public ClientHttpResponse intercept (HttpRequest request , byte [] body , ClientHttpRequestExecution execution )
221
+ throws IOException {
222
+ invocationCount ++;
223
+ return execution .execute (request , body );
224
+ }
225
+ }
226
+
227
+ private static class MultipleExecutionInterceptor implements ClientHttpRequestInterceptor {
214
228
215
229
@ Override
216
230
public ClientHttpResponse intercept (HttpRequest request , byte [] body , ClientHttpRequestExecution execution )
217
231
throws IOException {
218
- invoked = true ;
232
+ // execute another request first
233
+ execution .execute (new MockClientHttpRequest (), body );
219
234
return execution .execute (request , body );
220
235
}
221
236
}
0 commit comments