1
1
package org .javaee7 .servlet .security .annotated ;
2
2
3
- import com .gargoylesoftware .htmlunit .DefaultCredentialsProvider ;
4
- import com .gargoylesoftware .htmlunit .FailingHttpStatusCodeException ;
5
- import com .gargoylesoftware .htmlunit .WebClient ;
6
- import com .gargoylesoftware .htmlunit .WebRequest ;
7
- import com .gargoylesoftware .htmlunit .html .HtmlPage ;
3
+ import static com .gargoylesoftware .htmlunit .HttpMethod .POST ;
4
+ import static org .javaee7 .ServerOperations .addUsersToContainerIdentityStore ;
5
+ import static org .jboss .shrinkwrap .api .ShrinkWrap .create ;
6
+ import static org .junit .Assert .assertEquals ;
7
+ import static org .junit .Assert .assertNotNull ;
8
+ import static org .junit .Assert .fail ;
9
+
8
10
import java .net .URL ;
9
- import javax . ws . rs . HttpMethod ;
11
+
10
12
import org .jboss .arquillian .container .test .api .Deployment ;
11
13
import org .jboss .arquillian .junit .Arquillian ;
12
14
import org .jboss .arquillian .test .api .ArquillianResource ;
13
- import org .jboss .shrinkwrap .api .ShrinkWrap ;
14
15
import org .jboss .shrinkwrap .api .spec .WebArchive ;
15
- import org .junit .Test ;
16
- import static org .junit .Assert .*;
16
+ import org .junit .After ;
17
17
import org .junit .Before ;
18
+ import org .junit .Test ;
18
19
import org .junit .runner .RunWith ;
19
20
21
+ import com .gargoylesoftware .htmlunit .DefaultCredentialsProvider ;
22
+ import com .gargoylesoftware .htmlunit .FailingHttpStatusCodeException ;
23
+ import com .gargoylesoftware .htmlunit .WebClient ;
24
+ import com .gargoylesoftware .htmlunit .WebRequest ;
25
+ import com .gargoylesoftware .htmlunit .html .HtmlPage ;
26
+
20
27
/**
21
28
* @author Arun Gupta
22
29
*/
@@ -26,15 +33,17 @@ public class SecureServletTest {
26
33
@ ArquillianResource
27
34
private URL base ;
28
35
29
- DefaultCredentialsProvider correctCreds = new DefaultCredentialsProvider ();
30
- DefaultCredentialsProvider incorrectCreds = new DefaultCredentialsProvider ();
31
- WebClient webClient ;
36
+ private DefaultCredentialsProvider correctCreds = new DefaultCredentialsProvider ();
37
+ private DefaultCredentialsProvider incorrectCreds = new DefaultCredentialsProvider ();
38
+ private WebClient webClient ;
32
39
33
40
@ Deployment (testable = false )
34
41
public static WebArchive createDeployment () {
35
- WebArchive war = ShrinkWrap .create (WebArchive .class ).
36
- addClass (SecureServlet .class );
37
- return war ;
42
+
43
+ addUsersToContainerIdentityStore ();
44
+
45
+ return create (WebArchive .class )
46
+ .addClass (SecureServlet .class );
38
47
}
39
48
40
49
@ Before
@@ -43,45 +52,58 @@ public void setup() {
43
52
incorrectCreds .addCredentials ("random" , "random" );
44
53
webClient = new WebClient ();
45
54
}
55
+
56
+ @ After
57
+ public void tearDown () {
58
+ webClient .getCookieManager ().clearCookies ();
59
+ webClient .closeAllWindows ();
60
+ }
46
61
47
62
@ Test
48
63
public void testGetWithCorrectCredentials () throws Exception {
49
64
webClient .setCredentialsProvider (correctCreds );
50
65
HtmlPage page = webClient .getPage (base + "/SecureServlet" );
66
+
51
67
assertEquals ("Servlet Security Annotated - Basic Auth with File-base Realm" , page .getTitleText ());
52
68
}
53
69
54
70
@ Test
55
71
public void testGetWithIncorrectCredentials () throws Exception {
56
72
webClient .setCredentialsProvider (incorrectCreds );
73
+
57
74
try {
58
75
webClient .getPage (base + "/SecureServlet" );
59
76
} catch (FailingHttpStatusCodeException e ) {
60
77
assertNotNull (e );
61
78
assertEquals (401 , e .getStatusCode ());
62
79
return ;
63
80
}
81
+
64
82
fail ("/SecureServlet could be accessed without proper security credentials" );
65
83
}
66
84
67
85
@ Test
68
86
public void testPostWithCorrectCredentials () throws Exception {
69
87
webClient .setCredentialsProvider (correctCreds );
70
- WebRequest request = new WebRequest (new URL (base + "SecureServlet" ), HttpMethod . POST );
88
+ WebRequest request = new WebRequest (new URL (base + "/ SecureServlet" ), POST );
71
89
HtmlPage page = webClient .getPage (request );
90
+
72
91
assertEquals ("Servlet Security Annotated - Basic Auth with File-base Realm" , page .getTitleText ());
73
92
}
74
93
75
94
@ Test
76
95
public void testPostWithIncorrectCredentials () throws Exception {
77
- webClient .setCredentialsProvider (correctCreds );
78
- WebRequest request = new WebRequest (new URL (base + "SecureServlet" ), HttpMethod .POST );
96
+ webClient .setCredentialsProvider (incorrectCreds );
97
+ WebRequest request = new WebRequest (new URL (base + "/SecureServlet" ), POST );
98
+
79
99
try {
80
100
webClient .getPage (request );
81
101
} catch (FailingHttpStatusCodeException e ) {
82
102
assertNotNull (e );
83
- assertEquals (403 , e .getStatusCode ());
103
+ assertEquals (401 , e .getStatusCode ());
104
+ return ;
84
105
}
106
+
85
107
fail ("/SecureServlet could be accessed without proper security credentials" );
86
108
}
87
109
0 commit comments