11package org .javaee7 .servlet .security .basicauth ;
22
3- import com .meterware . httpunit . AuthorizationRequiredException ;
4- import com .meterware . httpunit . GetMethodWebRequest ;
5- import com .meterware . httpunit . WebConversation ;
6- import com .meterware . httpunit . WebResponse ;
3+ import com .gargoylesoftware . htmlunit . DefaultCredentialsProvider ;
4+ import com .gargoylesoftware . htmlunit . FailingHttpStatusCodeException ;
5+ import com .gargoylesoftware . htmlunit . WebClient ;
6+ import com .gargoylesoftware . htmlunit . html . HtmlPage ;
77import java .io .File ;
88import java .net .URL ;
99import org .jboss .arquillian .container .test .api .Deployment ;
1313import org .jboss .shrinkwrap .api .spec .WebArchive ;
1414import org .junit .Test ;
1515import static org .junit .Assert .*;
16+ import org .junit .Before ;
1617import org .junit .runner .RunWith ;
1718
1819/**
@@ -26,6 +27,9 @@ public class SecureServletTest {
2627 @ ArquillianResource
2728 private URL base ;
2829
30+ DefaultCredentialsProvider correctCreds = new DefaultCredentialsProvider ();
31+ DefaultCredentialsProvider incorrectCreds = new DefaultCredentialsProvider ();
32+
2933 @ Deployment (testable = false )
3034 public static WebArchive createDeployment () {
3135 WebArchive war = ShrinkWrap .create (WebArchive .class ).
@@ -34,31 +38,29 @@ public static WebArchive createDeployment() {
3438 return war ;
3539 }
3640
41+ @ Before
42+ public void setup () {
43+ correctCreds .addCredentials ("u1" , "p1" );
44+ incorrectCreds .addCredentials ("random" , "random" );
45+ }
46+
3747 @ Test
3848 public void testWithCorrectCredentials () throws Exception {
39- WebConversation conv = new WebConversation ();
40- conv .setAuthentication ("file" , "u1" , "p1" );
41- GetMethodWebRequest getRequest = new GetMethodWebRequest (base + "/SecureServlet" );
42- WebResponse response = null ;
43- try {
44- response = conv .getResponse (getRequest );
45- } catch (AuthorizationRequiredException e ) {
46- fail (e .getMessage ());
47- }
48- assertNotNull (response );
49- assertTrue (response .getText ().contains ("<title>Servlet Security - Basic Auth with File-base Realm</title>" ));
49+ WebClient webClient = new WebClient ();
50+ webClient .setCredentialsProvider (correctCreds );
51+ HtmlPage page = webClient .getPage (base + "/SecureServlet" );
52+ assertEquals ("Servlet Security - Basic Auth with File-base Realm" , page .getTitleText ());
5053 }
5154
5255 @ Test
5356 public void testWithIncorrectCredentials () throws Exception {
54- WebConversation conv = new WebConversation ();
55- conv .setAuthentication ("file" , "u" , "p1" );
56- GetMethodWebRequest getRequest = new GetMethodWebRequest (base + "/SecureServlet" );
57+ WebClient webClient = new WebClient ();
58+ webClient .setCredentialsProvider (incorrectCreds );
5759 try {
58- conv . getResponse ( getRequest );
59- } catch ( AuthorizationRequiredException e ) {
60+ HtmlPage page = webClient . getPage ( base + "/SecureServlet" );
61+ } catch ( FailingHttpStatusCodeException e ) {
6062 assertNotNull (e );
61- assertEquals ("Basic" , e .getAuthenticationScheme ());
63+ assertEquals (401 , e .getStatusCode ());
6264 return ;
6365 }
6466 fail ("/SecureServlet could be accessed without proper security credentials" );
0 commit comments