1313 */
1414package org .asynchttpclient .request .body .multipart ;
1515
16- import static io .netty .handler .codec .http .HttpHeaderNames .CONTENT_LENGTH ;
17- import static io .netty .handler .codec .http .HttpHeaderValues .APPLICATION_OCTET_STREAM ;
16+ import static io .netty .handler .codec .http .HttpHeaderNames .* ;
17+ import static io .netty .handler .codec .http .HttpHeaderValues .* ;
1818import static java .nio .charset .StandardCharsets .UTF_8 ;
1919import static org .asynchttpclient .Dsl .*;
2020import static org .asynchttpclient .test .TestUtils .*;
21- import static org .testng .Assert .assertEquals ;
21+ import static org .testng .Assert .* ;
2222
2323import java .io .File ;
24+ import java .io .IOException ;
25+ import java .util .concurrent .ExecutionException ;
26+ import java .util .function .Function ;
2427
2528import org .asynchttpclient .AbstractBasicTest ;
2629import org .asynchttpclient .AsyncHttpClient ;
2730import org .asynchttpclient .BasicAuthTest ;
31+ import org .asynchttpclient .BoundRequestBuilder ;
2832import org .asynchttpclient .Response ;
2933import org .eclipse .jetty .server .Server ;
3034import org .eclipse .jetty .server .ServerConnector ;
@@ -37,7 +41,6 @@ public class MultipartBasicAuthTest extends AbstractBasicTest {
3741 @ BeforeClass (alwaysRun = true )
3842 @ Override
3943 public void setUpGlobal () throws Exception {
40-
4144 server = new Server ();
4245 ServerConnector connector1 = addHttpConnector (server );
4346 addBasicAuthHandler (server , configureHandler ());
@@ -51,31 +54,57 @@ public AbstractHandler configureHandler() throws Exception {
5154 return new BasicAuthTest .SimpleHandler ();
5255 }
5356
54- @ Test (groups = "standalone" , enabled = false )
55- public void testNoRealm () throws Exception {
57+ private void expectBrokenPipe (Function <BoundRequestBuilder , BoundRequestBuilder > f ) throws Exception {
5658 File file = createTempFile (1024 * 1024 );
5759
60+ Throwable cause = null ;
5861 try (AsyncHttpClient client = asyncHttpClient ()) {
59- for (int i = 0 ; i < 20 ; i ++) {
60- Response response = client .preparePut (getTargetUrl ())//
61- .addBodyPart (new FilePart ("test" , file , APPLICATION_OCTET_STREAM .toString (), UTF_8 )).execute ().get ();
62- assertEquals (response .getStatusCode (), 401 );
62+ try {
63+ for (int i = 0 ; i < 20 && cause == null ; i ++) {
64+ f .apply (client .preparePut (getTargetUrl ())//
65+ .addBodyPart (new FilePart ("test" , file , APPLICATION_OCTET_STREAM .toString (), UTF_8 )))//
66+ .execute ().get ();
67+ }
68+ } catch (ExecutionException e ) {
69+ cause = e .getCause ();
6370 }
6471 }
72+
73+ assertTrue (cause instanceof IOException , "Expected an IOException" );
74+ assertEquals (cause .getMessage (), "Broken pipe" );
6575 }
6676
67- @ Test (groups = "standalone" , enabled = false )
68- public void testAuthorizedRealm () throws Exception {
77+ @ Test (groups = "standalone" )
78+ public void noRealmCausesServerToCloseSocket () throws Exception {
79+ expectBrokenPipe (rb -> rb );
80+ }
81+
82+ @ Test (groups = "standalone" )
83+ public void unauthorizedNonPreemptiveRealmCausesServerToCloseSocket () throws Exception {
84+ expectBrokenPipe (rb -> rb .setRealm (basicAuthRealm (USER , ADMIN )));
85+ }
86+
87+ private void expectSuccess (Function <BoundRequestBuilder , BoundRequestBuilder > f ) throws Exception {
6988 File file = createTempFile (1024 * 1024 );
7089
7190 try (AsyncHttpClient client = asyncHttpClient ()) {
7291 for (int i = 0 ; i < 20 ; i ++) {
73- Response response = client .preparePut (getTargetUrl ())//
74- .setRealm ( basicAuthRealm ( USER , ADMIN ). build ( ))//
75- .addBodyPart ( new FilePart ( "test" , file , APPLICATION_OCTET_STREAM . toString (), UTF_8 )). execute ().get ();
92+ Response response = f . apply ( client .preparePut (getTargetUrl ())//
93+ .addBodyPart ( new FilePart ( "test" , file , APPLICATION_OCTET_STREAM . toString (), UTF_8 ) ))//
94+ .execute ().get ();
7695 assertEquals (response .getStatusCode (), 200 );
7796 assertEquals (response .getResponseBodyAsBytes ().length , Integer .valueOf (response .getHeader ("X-" + CONTENT_LENGTH )).intValue ());
7897 }
7998 }
8099 }
100+
101+ @ Test (groups = "standalone" )
102+ public void authorizedPreemptiveRealmWorks () throws Exception {
103+ expectSuccess (rb -> rb .setRealm (basicAuthRealm (USER , ADMIN ).setUsePreemptiveAuth (true )));
104+ }
105+
106+ @ Test (groups = "standalone" )
107+ public void authorizedNonPreemptiveRealmWorksWithExpectContinue () throws Exception {
108+ expectSuccess (rb -> rb .setRealm (basicAuthRealm (USER , ADMIN )).setHeader (EXPECT , CONTINUE ));
109+ }
81110}
0 commit comments