1+ /*
2+ * Copyright 2002-2016 the original author or authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
17+ package org .springframework .http .client .support ;
18+
19+ import static org .junit .Assert .*;
20+
21+ import java .io .IOException ;
22+ import java .util .Arrays ;
23+ import java .util .List ;
24+
25+ import org .hamcrest .Matchers ;
26+ import org .junit .Test ;
27+
28+ import org .springframework .core .Ordered ;
29+ import org .springframework .core .annotation .Order ;
30+ import org .springframework .http .HttpRequest ;
31+ import org .springframework .http .client .ClientHttpRequestExecution ;
32+ import org .springframework .http .client .ClientHttpRequestInterceptor ;
33+ import org .springframework .http .client .ClientHttpResponse ;
34+
35+ /**
36+ * Tests for {@link InterceptingHttpAccessor}.
37+ *
38+ * @author Brian Clozel
39+ */
40+ public class InterceptingHttpAccessorTests {
41+
42+ @ Test
43+ public void getInterceptors () throws Exception {
44+ TestInterceptingHttpAccessor accessor = new TestInterceptingHttpAccessor ();
45+ List <ClientHttpRequestInterceptor > interceptors = Arrays .asList (
46+ new SecondClientHttpRequestInterceptor (),
47+ new ThirdClientHttpRequestInterceptor (),
48+ new FirstClientHttpRequestInterceptor ()
49+
50+ );
51+ accessor .setInterceptors (interceptors );
52+
53+ assertThat (accessor .getInterceptors ().get (0 ), Matchers .instanceOf (FirstClientHttpRequestInterceptor .class ));
54+ assertThat (accessor .getInterceptors ().get (1 ), Matchers .instanceOf (SecondClientHttpRequestInterceptor .class ));
55+ assertThat (accessor .getInterceptors ().get (2 ), Matchers .instanceOf (ThirdClientHttpRequestInterceptor .class ));
56+ }
57+
58+ private class TestInterceptingHttpAccessor extends InterceptingHttpAccessor {
59+ }
60+
61+ @ Order (1 )
62+ private class FirstClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
63+ @ Override
64+ public ClientHttpResponse intercept (HttpRequest request , byte [] body ,
65+ ClientHttpRequestExecution execution ) throws IOException {
66+ return null ;
67+ }
68+ }
69+
70+ private class SecondClientHttpRequestInterceptor implements ClientHttpRequestInterceptor , Ordered {
71+ @ Override
72+ public ClientHttpResponse intercept (HttpRequest request , byte [] body ,
73+ ClientHttpRequestExecution execution ) throws IOException {
74+ return null ;
75+ }
76+
77+ @ Override
78+ public int getOrder () {
79+ return 2 ;
80+ }
81+ }
82+
83+ private class ThirdClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
84+ @ Override
85+ public ClientHttpResponse intercept (HttpRequest request , byte [] body ,
86+ ClientHttpRequestExecution execution ) throws IOException {
87+ return null ;
88+ }
89+ }
90+
91+ }
0 commit comments