@@ -109,12 +109,12 @@ public void supportsResponseCode500() throws Exception {
109
109
110
110
@ Test
111
111
public void validatesHttpsCertificateIssuer () {
112
- SdkHttpClient client = createSdkHttpClient ();
112
+ try (SdkHttpClient client = createSdkHttpClient ()) {
113
+ SdkHttpFullRequest request = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), SdkHttpMethod .POST );
113
114
114
- SdkHttpFullRequest request = mockSdkRequest ("https://localhost:" + mockServer .httpsPort (), SdkHttpMethod .POST );
115
-
116
- assertThatThrownBy (client .prepareRequest (HttpExecuteRequest .builder ().request (request ).build ())::call )
115
+ assertThatThrownBy (client .prepareRequest (HttpExecuteRequest .builder ().request (request ).build ())::call )
117
116
.isInstanceOf (SSLHandshakeException .class );
117
+ }
118
118
}
119
119
120
120
@ Test
@@ -123,27 +123,26 @@ public void connectionPoolingWorks() throws Exception {
123
123
124
124
SdkHttpClientOptions httpClientOptions = new SdkHttpClientOptions ();
125
125
httpClientOptions .trustAll (true );
126
- SdkHttpClient client = createSdkHttpClient (httpClientOptions );
127
-
128
- stubForMockRequest (200 );
129
-
130
- for (int i = 0 ; i < 5 ; i ++) {
131
- SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), SdkHttpMethod .POST );
132
- HttpExecuteResponse response =
133
- client .prepareRequest (HttpExecuteRequest .builder ()
134
- .request (req )
135
- .contentStreamProvider (req .contentStreamProvider ().orElse (null ))
136
- .build ())
137
- .call ();
138
- response .responseBody ().ifPresent (IoUtils ::drainInputStream );
139
- }
140
-
141
- // connection pool growth strategies vary across client implementations. Some, such as the CRT grow connection counts
142
- // by a factor of 2, while some grow strictly as requested. Mainly we want to test that it kicks in at some point and
143
- // doesn't create a new connection for all 5 requests. This proves that while allowing variance in this behavior.
144
- assertThat (CONNECTION_COUNTER .openedConnections ()).isGreaterThanOrEqualTo (initialOpenedConnections + 1 );
145
- assertThat (CONNECTION_COUNTER .openedConnections ()).isLessThanOrEqualTo (initialOpenedConnections + 2 );
126
+ try (SdkHttpClient client = createSdkHttpClient (httpClientOptions )) {
127
+ stubForMockRequest (200 );
128
+
129
+ for (int i = 0 ; i < 5 ; i ++) {
130
+ SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), SdkHttpMethod .POST );
131
+ HttpExecuteResponse response =
132
+ client .prepareRequest (HttpExecuteRequest .builder ()
133
+ .request (req )
134
+ .contentStreamProvider (req .contentStreamProvider ().orElse (null ))
135
+ .build ())
136
+ .call ();
137
+ response .responseBody ().ifPresent (IoUtils ::drainInputStream );
138
+ }
146
139
140
+ // connection pool growth strategies vary across client implementations. Some, such as the CRT grow connection counts
141
+ // by a factor of 2, while some grow strictly as requested. Mainly we want to test that it kicks in at some point and
142
+ // doesn't create a new connection for all 5 requests. This proves that while allowing variance in this behavior.
143
+ assertThat (CONNECTION_COUNTER .openedConnections ()).isGreaterThanOrEqualTo (initialOpenedConnections + 1 );
144
+ assertThat (CONNECTION_COUNTER .openedConnections ()).isLessThanOrEqualTo (initialOpenedConnections + 2 );
145
+ }
147
146
}
148
147
149
148
@ Test
@@ -152,25 +151,25 @@ public void connectionsAreNotReusedOn5xxErrors() throws Exception {
152
151
153
152
SdkHttpClientOptions httpClientOptions = new SdkHttpClientOptions ();
154
153
httpClientOptions .trustAll (true );
155
- SdkHttpClient client = createSdkHttpClient (httpClientOptions );
156
-
157
- stubForMockRequest (503 );
158
-
159
- for (int i = 0 ; i < 5 ; i ++) {
160
- SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), SdkHttpMethod .POST );
161
- HttpExecuteResponse response =
162
- client .prepareRequest (HttpExecuteRequest .builder ()
163
- .request (req )
164
- .contentStreamProvider (req .contentStreamProvider ().orElse (null ))
165
- .build ())
166
- .call ();
167
- response .responseBody ().ifPresent (IoUtils ::drainInputStream );
168
- }
154
+ try (SdkHttpClient client = createSdkHttpClient (httpClientOptions )) {
155
+ stubForMockRequest (503 );
156
+
157
+ for (int i = 0 ; i < 5 ; i ++) {
158
+ SdkHttpFullRequest req = mockSdkRequest ("http://localhost:" + mockServer .port (), SdkHttpMethod .POST );
159
+ HttpExecuteResponse response =
160
+ client .prepareRequest (HttpExecuteRequest .builder ()
161
+ .request (req )
162
+ .contentStreamProvider (req .contentStreamProvider ().orElse (null ))
163
+ .build ())
164
+ .call ();
165
+ response .responseBody ().ifPresent (IoUtils ::drainInputStream );
166
+ }
169
167
170
- // don't couple this test to connection manager behaviors we don't have to. We want to make sure that the connection count
171
- // increased by at least as many connections as we got 5xx errors back on. But the connection manager also predictively
172
- // creates connections and we need to take those into account in a way that lets it remain a dynamic behavior.
173
- assertThat (CONNECTION_COUNTER .openedConnections ()).isGreaterThanOrEqualTo (initialOpenedConnections + 5 );
168
+ // don't couple this test to connection manager behaviors we don't have to. We want to make sure that the connection count
169
+ // increased by at least as many connections as we got 5xx errors back on. But the connection manager also predictively
170
+ // creates connections and we need to take those into account in a way that lets it remain a dynamic behavior.
171
+ assertThat (CONNECTION_COUNTER .openedConnections ()).isGreaterThanOrEqualTo (initialOpenedConnections + 5 );
172
+ }
174
173
}
175
174
176
175
@ Test
@@ -185,8 +184,7 @@ public void testCustomTlsTrustManager() throws Exception {
185
184
186
185
selfSignedServer .start ();
187
186
188
- try {
189
- SdkHttpClient client = createSdkHttpClient (httpClientOptions );
187
+ try (SdkHttpClient client = createSdkHttpClient (httpClientOptions )) {
190
188
SdkHttpFullRequest request = mockSdkRequest ("https://localhost:" + selfSignedServer .httpsPort (), SdkHttpMethod .POST );
191
189
192
190
client .prepareRequest (HttpExecuteRequest .builder ()
@@ -204,7 +202,10 @@ public void testTrustAllWorks() throws Exception {
204
202
SdkHttpClientOptions httpClientOptions = new SdkHttpClientOptions ();
205
203
httpClientOptions .trustAll (true );
206
204
207
- testForResponseCodeUsingHttps (createSdkHttpClient (httpClientOptions ), HttpURLConnection .HTTP_OK );
205
+ try (SdkHttpClient client = createSdkHttpClient (httpClientOptions )) {
206
+ testForResponseCodeUsingHttps (client , HttpURLConnection .HTTP_OK );
207
+ }
208
+
208
209
}
209
210
210
211
@ Test
@@ -221,19 +222,19 @@ protected void testForResponseCode(int returnCode) throws Exception {
221
222
}
222
223
223
224
private void testForResponseCode (int returnCode , SdkHttpMethod method ) throws Exception {
224
- SdkHttpClient client = createSdkHttpClient ();
225
-
226
- stubForMockRequest ( returnCode );
227
-
228
- SdkHttpFullRequest req = mockSdkRequest ( "http://localhost:" + mockServer . port (), method );
229
- HttpExecuteResponse rsp = client . prepareRequest ( HttpExecuteRequest . builder ( )
230
- . request (req )
231
- . contentStreamProvider ( req . contentStreamProvider ( )
232
- . orElse ( null ))
233
- . build ())
234
- . call ();
235
-
236
- validateResponse ( rsp , returnCode , method );
225
+ try ( SdkHttpClient client = createSdkHttpClient ()) {
226
+ stubForMockRequest ( returnCode );
227
+
228
+ SdkHttpFullRequest req = mockSdkRequest ( "http://localhost:" + mockServer . port (), method );
229
+ HttpExecuteResponse rsp = client . prepareRequest ( HttpExecuteRequest . builder ()
230
+ . request ( req )
231
+ . contentStreamProvider (req . contentStreamProvider ( )
232
+ . orElse ( null ) )
233
+ . build ( ))
234
+ . call ();
235
+
236
+ validateResponse ( rsp , returnCode , method );
237
+ }
237
238
}
238
239
239
240
protected void testForResponseCodeUsingHttps (SdkHttpClient client , int returnCode ) throws Exception {
0 commit comments