13
13
*/
14
14
package org .asynchttpclient .request .body .multipart ;
15
15
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 .* ;
18
18
import static java .nio .charset .StandardCharsets .UTF_8 ;
19
19
import static org .asynchttpclient .Dsl .*;
20
20
import static org .asynchttpclient .test .TestUtils .*;
21
- import static org .testng .Assert .assertEquals ;
21
+ import static org .testng .Assert .* ;
22
22
23
23
import java .io .File ;
24
+ import java .io .IOException ;
25
+ import java .util .concurrent .ExecutionException ;
26
+ import java .util .function .Function ;
24
27
25
28
import org .asynchttpclient .AbstractBasicTest ;
26
29
import org .asynchttpclient .AsyncHttpClient ;
27
30
import org .asynchttpclient .BasicAuthTest ;
31
+ import org .asynchttpclient .BoundRequestBuilder ;
28
32
import org .asynchttpclient .Response ;
29
33
import org .eclipse .jetty .server .Server ;
30
34
import org .eclipse .jetty .server .ServerConnector ;
@@ -37,7 +41,6 @@ public class MultipartBasicAuthTest extends AbstractBasicTest {
37
41
@ BeforeClass (alwaysRun = true )
38
42
@ Override
39
43
public void setUpGlobal () throws Exception {
40
-
41
44
server = new Server ();
42
45
ServerConnector connector1 = addHttpConnector (server );
43
46
addBasicAuthHandler (server , configureHandler ());
@@ -51,31 +54,57 @@ public AbstractHandler configureHandler() throws Exception {
51
54
return new BasicAuthTest .SimpleHandler ();
52
55
}
53
56
54
- @ Test (groups = "standalone" , enabled = false )
55
- public void testNoRealm () throws Exception {
57
+ private void expectBrokenPipe (Function <BoundRequestBuilder , BoundRequestBuilder > f ) throws Exception {
56
58
File file = createTempFile (1024 * 1024 );
57
59
60
+ Throwable cause = null ;
58
61
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 ();
63
70
}
64
71
}
72
+
73
+ assertTrue (cause instanceof IOException , "Expected an IOException" );
74
+ assertEquals (cause .getMessage (), "Broken pipe" );
65
75
}
66
76
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 {
69
88
File file = createTempFile (1024 * 1024 );
70
89
71
90
try (AsyncHttpClient client = asyncHttpClient ()) {
72
91
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 ();
76
95
assertEquals (response .getStatusCode (), 200 );
77
96
assertEquals (response .getResponseBodyAsBytes ().length , Integer .valueOf (response .getHeader ("X-" + CONTENT_LENGTH )).intValue ());
78
97
}
79
98
}
80
99
}
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
+ }
81
110
}
0 commit comments