22
22
import java .util .concurrent .Future ;
23
23
24
24
import javax .servlet .ServletException ;
25
- import javax .servlet .ServletRequest ;
26
- import javax .servlet .ServletResponse ;
27
25
import javax .servlet .http .HttpServletRequest ;
28
26
import javax .servlet .http .HttpServletResponse ;
29
27
@@ -57,29 +55,26 @@ public class BasicHttpProxyToHttpTest {
57
55
public static class BasicAuthProxyServlet extends ProxyServlet {
58
56
59
57
@ Override
60
- public void service (ServletRequest req , ServletResponse res ) throws ServletException , IOException {
58
+ protected void service (final HttpServletRequest request , final HttpServletResponse response ) throws ServletException , IOException {
61
59
LOGGER .debug (">>> got a request !" );
62
60
63
- HttpServletRequest httpReq = (HttpServletRequest ) req ;
64
- HttpServletResponse httpRes = (HttpServletResponse ) res ;
65
-
66
- String authorization = httpReq .getHeader (PROXY_AUTHORIZATION );
61
+ String authorization = request .getHeader (PROXY_AUTHORIZATION );
67
62
if (authorization == null ) {
68
- httpRes .setStatus (HttpServletResponse .SC_PROXY_AUTHENTICATION_REQUIRED );
69
- httpRes .setHeader (PROXY_AUTHENTICATE , "Basic realm=\" Fake Realm\" " );
70
- httpRes .getOutputStream ().flush ();
63
+ response .setStatus (HttpServletResponse .SC_PROXY_AUTHENTICATION_REQUIRED );
64
+ response .setHeader (PROXY_AUTHENTICATE , "Basic realm=\" Fake Realm\" " );
65
+ response .getOutputStream ().flush ();
71
66
72
67
} else if (authorization .equals ("Basic am9obmRvZTpwYXNz" )) {
73
- super .service (req , res );
68
+ super .service (request , response );
74
69
75
70
} else {
76
- httpRes .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
77
- httpRes .getOutputStream ().flush ();
71
+ response .setStatus (HttpServletResponse .SC_UNAUTHORIZED );
72
+ response .getOutputStream ().flush ();
78
73
}
79
74
}
80
75
}
81
76
82
- @ BeforeClass ( alwaysRun = true )
77
+ @ BeforeClass
83
78
public void setUpGlobal () throws Exception {
84
79
85
80
httpPort = findFreePort ();
@@ -92,7 +87,7 @@ public void setUpGlobal() throws Exception {
92
87
proxy = new Server (proxyPort );
93
88
ServletHandler servletHandler = new ServletHandler ();
94
89
ServletHolder servletHolder = servletHandler .addServletWithMapping (BasicAuthProxyServlet .class , "/*" );
95
- servletHolder .setInitParameter ("maxThreads" , "5 " );
90
+ servletHolder .setInitParameter ("maxThreads" , "20 " );
96
91
proxy .setHandler (servletHandler );
97
92
proxy .start ();
98
93
@@ -101,8 +96,20 @@ public void setUpGlobal() throws Exception {
101
96
102
97
@ AfterClass (alwaysRun = true )
103
98
public void tearDownGlobal () throws Exception {
104
- httpServer .stop ();
105
- proxy .stop ();
99
+ if (proxy != null ) {
100
+ try {
101
+ proxy .stop ();
102
+ } catch (Exception e ) {
103
+ LOGGER .error ("Failed to properly close proxy" , e );
104
+ }
105
+ }
106
+ if (httpServer != null ) {
107
+ try {
108
+ httpServer .stop ();
109
+ } catch (Exception e ) {
110
+ LOGGER .error ("Failed to properly close server" , e );
111
+ }
112
+ }
106
113
}
107
114
108
115
@ Test
@@ -111,7 +118,7 @@ public void nonPreemptyProxyAuthWithPlainHttpTarget() throws IOException, Interr
111
118
String targetUrl = "http://localhost:" + httpPort + "/foo/bar" ;
112
119
Request request = get (targetUrl )//
113
120
.setProxyServer (proxyServer ("127.0.0.1" , proxyPort ).setRealm (realm (AuthScheme .BASIC , "johndoe" , "pass" )))//
114
- //.setRealm(realm(AuthScheme.BASIC, "user", "passwd"))//
121
+ // .setRealm(realm(AuthScheme.BASIC, "user", "passwd"))//
115
122
.build ();
116
123
Future <Response > responseFuture = client .executeRequest (request );
117
124
Response response = responseFuture .get ();
0 commit comments