2222import org .glassfish .jersey .client .http .Expect100ContinueFeature ;
2323import org .glassfish .jersey .netty .connector .NettyClientProperties ;
2424import org .glassfish .jersey .netty .connector .NettyConnectorProvider ;
25+ import org .junit .jupiter .api .AfterEach ;
26+ import org .junit .jupiter .api .BeforeAll ;
2527import org .junit .jupiter .api .BeforeEach ;
2628import org .junit .jupiter .api .Test ;
2729
2830import javax .net .ServerSocketFactory ;
2931import javax .ws .rs .ProcessingException ;
32+ import javax .ws .rs .client .AsyncInvoker ;
3033import javax .ws .rs .client .Client ;
3134import javax .ws .rs .client .ClientBuilder ;
3235import javax .ws .rs .client .Entity ;
36+ import javax .ws .rs .client .Invocation ;
37+ import javax .ws .rs .client .InvocationCallback ;
3338import javax .ws .rs .client .WebTarget ;
3439import javax .ws .rs .core .HttpHeaders ;
3540import javax .ws .rs .core .Response ;
5257import static org .junit .jupiter .api .Assertions .assertEquals ;
5358import static org .junit .jupiter .api .Assertions .assertNotNull ;
5459import static org .junit .jupiter .api .Assertions .assertThrows ;
60+ import static org .junit .jupiter .api .Assertions .fail ;
5561
5662public class Expect100ContinueTest /*extends JerseyTest*/ {
5763
@@ -74,13 +80,24 @@ public class Expect100ContinueTest /*extends JerseyTest*/ {
7480
7581 private static Client client ;
7682
77- @ BeforeEach
78- public void beforeEach () {
83+ @ BeforeAll
84+ static void beforeAll () {
7985 final ClientConfig config = new ClientConfig ();
80- this . configureClient ( config );
86+ config . connectorProvider ( new NettyConnectorProvider () );
8187 client = ClientBuilder .newClient (config );
8288 }
8389
90+ @ BeforeEach
91+ void beforeEach () throws IOException {
92+ server = new TestSocketServer (portNumber );
93+ server .runServer ();
94+ }
95+
96+ @ AfterEach
97+ void afterEach () {
98+ server .stop ();
99+ }
100+
84101 private Client client () {
85102 return client ;
86103 }
@@ -94,147 +111,117 @@ protected void configureClient(ClientConfig config) {
94111 }
95112
96113 @ Test
97- public void testExpect100Continue () throws Exception {
98- final TestSocketServer server = new TestSocketServer (portNumber );
99- try {
100- server .runServer ();
101-
102- final Response response = target (RESOURCE_PATH ).request ().post (Entity .text (ENTITY_STRING ));
103- assertEquals (200 , response .getStatus (), "Expected 200" ); //no Expect header sent - response OK
104- } finally {
105- server .stop ();
106- }
114+ public void testExpect100Continue () {
115+ final Response response = target (RESOURCE_PATH ).request ().post (Entity .text (ENTITY_STRING ));
116+ assertEquals (200 , response .getStatus (), "Expected 200" ); //no Expect header sent - response OK
107117 }
108118
109119 @ Test
110- public void testExpect100ContinueChunked () throws Exception {
111- final TestSocketServer server = new TestSocketServer (portNumber );
112- try {
113- server .runServer ();
114-
120+ public void testExpect100ContinueChunked () {
115121 final Response response = target (RESOURCE_PATH ).register (Expect100ContinueFeature .basic ())
116122 .property (ClientProperties .REQUEST_ENTITY_PROCESSING ,
117123 RequestEntityProcessing .CHUNKED )
118124 .request ().post (Entity .text (ENTITY_STRING ));
119125 assertEquals (204 , response .getStatus (), "Expected 204" ); //Expect header sent - No Content response
120- } finally {
121- server .stop ();
122- }
123126 }
124127
125128 @ Test
126- public void testExpect100ContinueBuffered () throws Exception {
127- final TestSocketServer server = new TestSocketServer (portNumber );
128- try {
129- server .runServer ();
129+ public void testExpect100ContinueManyAsyncRequests () {
130+
131+ final Invocation .Builder requestBuilder = target (RESOURCE_PATH ).register (Expect100ContinueFeature .basic ())
132+ .property (ClientProperties .REQUEST_ENTITY_PROCESSING ,
133+ RequestEntityProcessing .CHUNKED )
134+ .request ();
135+ final AsyncInvoker invoker =
136+ requestBuilder .async ();
137+
138+ final InvocationCallback <Response > responseCallback = new InvocationCallback <Response >() {
139+ @ Override
140+ public void completed (Response response ) {
141+ assertEquals (204 , response .getStatus (), "Expected 204" ); //Expect header sent - No Content response
142+ }
130143
144+ @ Override
145+ public void failed (Throwable throwable ) {
146+ fail (throwable ); // should not fail
147+ }
148+ };
149+ invoker .post (Entity .text (ENTITY_STRING ), responseCallback );
150+ invoker .post (Entity .text (ENTITY_STRING ), responseCallback );
151+ invoker .post (Entity .text (ENTITY_STRING ), responseCallback );
152+ invoker .post (Entity .text (ENTITY_STRING ), responseCallback );
153+ invoker .post (Entity .text (ENTITY_STRING ), responseCallback );
154+ invoker .post (Entity .text (ENTITY_STRING ), responseCallback );
155+
156+ final Response response = requestBuilder .post (Entity .text (ENTITY_STRING ));
157+ assertEquals (204 , response .getStatus (), "Expected 204" ); //Expect header sent - No Content response
158+ }
159+
160+ @ Test
161+ public void testExpect100ContinueBuffered () {
131162 final Response response = target (RESOURCE_PATH ).register (Expect100ContinueFeature .basic ())
132163 .property (ClientProperties .REQUEST_ENTITY_PROCESSING ,
133164 RequestEntityProcessing .BUFFERED ).request ().header (HttpHeaders .CONTENT_LENGTH , 67000L )
134165 .post (Entity .text (generateStringByContentLength (67000 )));
135166 assertEquals (204 , response .getStatus (), "Expected 204" ); //Expect header sent - No Content response
136- } finally {
137- server .stop ();
138- }
139167 }
140168
141169 @ Test
142- public void testExpect100ContinueCustomLength () throws Exception {
143- final TestSocketServer server = new TestSocketServer (portNumber );
144- try {
145- server .runServer ();
146-
170+ public void testExpect100ContinueCustomLength () {
147171 final Response response = target (RESOURCE_PATH ).register (Expect100ContinueFeature .withCustomThreshold (100L ))
148172 .request ().header (HttpHeaders .CONTENT_LENGTH , 200 )
149173 .post (Entity .text (generateStringByContentLength (200 )));
150174 assertEquals (204 , response .getStatus (), "Expected 204" ); //Expect header sent - No Content response
151- } finally {
152- server .stop ();
153- }
154175 }
155176
156177 @ Test
157- public void testExpect100ContinueCustomLengthWrong () throws Exception {
158- final TestSocketServer server = new TestSocketServer (portNumber );
159- try {
160- server .runServer ();
161-
178+ public void testExpect100ContinueCustomLengthWrong () {
162179 final Response response = target (RESOURCE_PATH ).register (Expect100ContinueFeature .withCustomThreshold (100L ))
163180 .request ().header (HttpHeaders .CONTENT_LENGTH , 99L )
164181 .post (Entity .text (generateStringByContentLength (99 )));
165182 assertEquals (200 , response .getStatus (), "Expected 200" ); //Expect header NOT sent - low request size
166- } finally {
167- server .stop ();
168- }
169183 }
170184
171185 @ Test
172- public void testExpect100ContinueCustomLengthProperty () throws Exception {
173- final TestSocketServer server = new TestSocketServer (portNumber );
174- try {
175- server .runServer ();
176-
186+ public void testExpect100ContinueCustomLengthProperty () {
177187 final Response response = target (RESOURCE_PATH )
178188 .property (ClientProperties .EXPECT_100_CONTINUE_THRESHOLD_SIZE , 555L )
179189 .property (ClientProperties .EXPECT_100_CONTINUE , Boolean .TRUE )
180190 .register (Expect100ContinueFeature .withCustomThreshold (555L ))
181191 .request ().header (HttpHeaders .CONTENT_LENGTH , 666L )
182192 .post (Entity .text (generateStringByContentLength (666 )));
183193 assertNotNull (response .getStatus ()); //Expect header sent - No Content response
184- } finally {
185- server .stop ();
186- }
187194 }
188195
189196 @ Test
190- public void testExpect100ContinueRegisterViaCustomProperty () throws Exception {
191- final TestSocketServer server = new TestSocketServer (portNumber );
192- try {
193- server .runServer ();
197+ public void testExpect100ContinueRegisterViaCustomProperty () {
194198 final Response response = target (RESOURCE_PATH )
195199 .property (ClientProperties .EXPECT_100_CONTINUE_THRESHOLD_SIZE , 43L )
196200 .property (ClientProperties .EXPECT_100_CONTINUE , Boolean .TRUE )
197201 .request ().header (HttpHeaders .CONTENT_LENGTH , 44L )
198202 .post (Entity .text (generateStringByContentLength (44 )));
199203 assertEquals (204 , response .getStatus (), "Expected 204" ); //Expect header sent - No Content response
200- } finally {
201- server .stop ();
202- }
203204 }
204205
205206 @ Test
206- public void testExpect100ContinueNotSupported () throws Exception {
207- final TestSocketServer server = new TestSocketServer (portNumber );
208- try {
209- server .runServer ();
210-
207+ public void testExpect100ContinueNotSupported () {
211208 final Response response = target (RESOURCE_PATH_NOT_SUPPORTED )
212209 .property (ClientProperties .EXPECT_100_CONTINUE_THRESHOLD_SIZE , 43L )
213210 .property (ClientProperties .EXPECT_100_CONTINUE , Boolean .TRUE )
214211 .request ().header (HttpHeaders .CONTENT_LENGTH , 44L )
215212 .post (Entity .text (generateStringByContentLength (44 )));
216213 assertEquals (204 , response .getStatus (),
217214 "This should re-send request without expect and obtain the 204 response code" ); //Expectations not supported
218- } finally {
219- server .stop ();
220- }
221215 }
222216
223217 @ Test
224- public void testExpect100ContinueUnauthorized () throws Exception {
225- final TestSocketServer server = new TestSocketServer (portNumber );
226- try {
227- server .runServer ();
228-
218+ public void testExpect100ContinueUnauthorized () {
229219 assertThrows (ProcessingException .class , () -> target (RESOURCE_PATH_UNAUTHORIZED )
230220 .property (ClientProperties .EXPECT_100_CONTINUE_THRESHOLD_SIZE , 43L )
231221 .property (ClientProperties .EXPECT_100_CONTINUE , Boolean .TRUE )
232222 .property (NettyClientProperties .EXPECT_100_CONTINUE_TIMEOUT , 10000 )
233223 .request ().header (HttpHeaders .CONTENT_LENGTH , 44L )
234224 .post (Entity .text (generateStringByContentLength (44 ))));
235- } finally {
236- server .stop ();
237- }
238225 }
239226
240227 @ Test
@@ -248,21 +235,14 @@ public void testExpect100ContinuePayloadTooLarge() {
248235 }
249236
250237 @ Test
251- public void testExpect100ContinueMethodNotSupported () throws Exception {
252- final TestSocketServer server = new TestSocketServer (portNumber );
253- try {
254- server .runServer ();
255-
238+ public void testExpect100ContinueMethodNotSupported () {
256239 assertThrows (ProcessingException .class , () -> target (RESOURCE_PATH_METHOD_NOT_SUPPORTED )
257240 .property (ClientProperties .EXPECT_100_CONTINUE_THRESHOLD_SIZE , 43L )
258241 .property (ClientProperties .EXPECT_100_CONTINUE , Boolean .TRUE )
259242 .property (NettyClientProperties .EXPECT_100_CONTINUE_TIMEOUT , 10000 )
260243 .request ().header (HttpHeaders .CONTENT_LENGTH , 44L )
261244 .post (Entity .text (generateStringByContentLength (44 ))));
262245
263- } finally {
264- server .stop ();
265- }
266246 }
267247
268248 private String generateStringByContentLength (int length ) {
@@ -281,10 +261,12 @@ private static final class TestSocketServer {
281261 private static final String EXPECT_HEADER = "HTTP/1.1 100 Continue" ;
282262 private static final String UNAUTHORIZED_HEADER = "HTTP/1.1 401 Unauthorized" ;
283263 private static final String NOT_SUPPORTED_HEADER = "HTTP/1.1 405 Method Not Allowed" ;
264+ private static final String TOO_LARGE_HEADER = "HTTP/1.1 413 Request Entity Too Large" ;
284265
285266 private final ExecutorService executorService = Executors .newCachedThreadPool ();
286267 private AtomicBoolean unauthorized = new AtomicBoolean (false );
287268 private AtomicBoolean not_supported = new AtomicBoolean (false );
269+ private AtomicBoolean too_large = new AtomicBoolean (false );
288270
289271 private AtomicBoolean expect_processed = new AtomicBoolean (false );
290272
@@ -404,6 +386,12 @@ private boolean processExpect100Continue(Map<String, String> headers, BufferedWr
404386 failed = true ;
405387 }
406388
389+ if (too_large .get ()) {
390+ http_header = TOO_LARGE_HEADER ;
391+ too_large .set (false );
392+ failed = true ;
393+ }
394+
407395 expect_processed .set (http_header .equals (EXPECT_HEADER ));
408396
409397
@@ -438,6 +426,11 @@ private Map<String, String> mapHeaders(BufferedReader reader) throws IOException
438426 if (line .contains (RESOURCE_PATH_METHOD_NOT_SUPPORTED )) {
439427 not_supported .set (true );
440428 }
429+
430+ if (line .contains (RESOURCE_PATH_PAYLOAD_TOO_LARGE )) {
431+ too_large .set (true );
432+ }
433+
441434 int pos = line .indexOf (':' );
442435 if (pos > -1 ) {
443436 headers .put (
0 commit comments