1
+ /**
2
+ * The MIT License
3
+ *
4
+ * Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining
7
+ * a copy of this software and associated documentation files (the
8
+ * "Software"), to deal in the Software without restriction, including
9
+ * without limitation the rights to use, copy, modify, merge, publish,
10
+ * distribute, sublicense, and/or sell copies of the Software, and to
11
+ * permit persons to whom the Software is furnished to do so, subject to
12
+ * the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be
15
+ * included in all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ */
25
+
26
+ package kong .unirest ;
27
+
28
+ import org .junit .jupiter .api .Test ;
29
+ import org .junit .jupiter .api .extension .ExtendWith ;
30
+ import org .mockito .Mock ;
31
+ import org .mockito .junit .jupiter .MockitoExtension ;
32
+
33
+ import java .util .Arrays ;
34
+ import java .util .Collections ;
35
+
36
+ import static org .junit .jupiter .api .Assertions .*;
37
+ import static org .mockito .Mockito .*;
38
+
39
+ @ ExtendWith (MockitoExtension .class )
40
+ class CompoundInterceptorTest {
41
+ @ Mock
42
+ Interceptor t1 ;
43
+ @ Mock
44
+ Interceptor t2 ;
45
+ @ Mock
46
+ HttpRequest <?> request ;
47
+ @ Mock
48
+ Config config ;
49
+ MockResponse <?> response = new MockResponse <>();
50
+ @ Mock
51
+ HttpRequestSummary summary ;
52
+
53
+
54
+ @ Test
55
+ void willExecuteAllOnBefore () {
56
+ CompoundInterceptor compound = new CompoundInterceptor (
57
+ Arrays .asList (t1 , t2 )
58
+ );
59
+
60
+ compound .onRequest (request , config );
61
+
62
+ verify (t1 ).onRequest (request , config );
63
+ verify (t2 ).onRequest (request , config );
64
+ verifyNoMoreInteractions (t1 );
65
+ verifyNoMoreInteractions (t2 );
66
+ }
67
+
68
+ @ Test
69
+ void willExecuteAllOnAfter () {
70
+ CompoundInterceptor compound = new CompoundInterceptor (
71
+ Arrays .asList (t1 , t2 )
72
+ );
73
+
74
+ compound .onResponse (response , summary , config );
75
+
76
+ verify (t1 ).onResponse (response , summary , config );
77
+ verify (t2 ).onResponse (response , summary , config );
78
+ verifyNoMoreInteractions (t1 );
79
+ verifyNoMoreInteractions (t2 );
80
+ }
81
+
82
+ @ Test
83
+ void exceptionsStopsArFirstOne () {
84
+ CompoundInterceptor compound = new CompoundInterceptor (
85
+ Arrays .asList (t1 , t2 )
86
+ );
87
+ RuntimeException e = new RuntimeException ();
88
+ when (t2 .onFail (e , summary , config )).thenReturn (new MockResponse <>());
89
+ HttpResponse r = compound .onFail (e , summary , config );
90
+ assertTrue (r instanceof MockResponse );
91
+
92
+ verify (t1 ).onFail (e , summary , config );
93
+ verify (t2 ).onFail (e , summary , config );
94
+ verifyNoMoreInteractions (t1 );
95
+ verifyNoMoreInteractions (t2 );
96
+ }
97
+
98
+ @ Test
99
+ void willThrowIfNothingElse () {
100
+ CompoundInterceptor compound = new CompoundInterceptor (
101
+ Arrays .asList ()
102
+ );
103
+
104
+ RuntimeException e = new RuntimeException ();
105
+ UnirestException u = assertThrows (UnirestException .class ,
106
+ () -> compound .onFail (e , summary , config ));
107
+
108
+ assertSame (e , u .getCause ());
109
+ }
110
+
111
+ @ Test
112
+ void theDefaultInterceptorIsTheDefault () {
113
+ CompoundInterceptor compound = new CompoundInterceptor ();
114
+ assertEquals (1 , compound .size ());
115
+ assertTrue (compound .getInterceptors ().get (0 ) instanceof DefaultInterceptor );
116
+ }
117
+
118
+ @ Test
119
+ void buildingWithCollectionDoesNotIncludeDefault () {
120
+ CompoundInterceptor compound = new CompoundInterceptor (Collections .emptyList ());
121
+ assertEquals (0 , compound .size ());
122
+ }
123
+
124
+ @ Test
125
+ void canReplaceDefault () {
126
+ CompoundInterceptor compound = new CompoundInterceptor ();
127
+ compound .register (t1 );
128
+ compound .register (t2 );
129
+ assertEquals (2 , compound .size ());
130
+ assertSame (t1 , compound .getInterceptors ().get (0 ));
131
+ assertSame (t2 , compound .getInterceptors ().get (1 ));
132
+ }
133
+
134
+ @ Test
135
+ void cantAddTheSameOneTwice () {
136
+ CompoundInterceptor compound = new CompoundInterceptor ();
137
+ compound .register (t1 );
138
+ compound .register (t1 );
139
+
140
+ assertEquals (1 , compound .size ());
141
+ }
142
+ }
0 commit comments