Skip to content

Commit 9b87775

Browse files
committed
Cleaned up tests, added POST with correct/incorrect credentials
1 parent 17f92b3 commit 9b87775

File tree

3 files changed

+32
-59
lines changed

3 files changed

+32
-59
lines changed

servlet/security-basicauth/src/main/java/org/javaee7/servlet/security/basicauth/SecureServlet.java

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -52,68 +52,15 @@
5252
*/
5353
@WebServlet(urlPatterns = {"/SecureServlet"})
5454
public class SecureServlet extends HttpServlet {
55-
56-
/**
57-
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
58-
* methods.
59-
*
60-
* @param request servlet request
61-
* @param response servlet response
62-
* @throws ServletException if a servlet-specific error occurs
63-
* @throws IOException if an I/O error occurs
64-
*/
65-
protected void processRequest(HttpServletRequest request, HttpServletResponse response, String method)
66-
throws ServletException, IOException {
67-
response.setContentType("text/html;charset=UTF-8");
68-
PrintWriter out = response.getWriter();
69-
out.println("<!DOCTYPE html>");
70-
out.println("<html>");
71-
out.println("<head>");
72-
out.println("<title>Servlet Security - Basic Auth with File-base Realm</title>");
73-
out.println("</head>");
74-
out.println("<body>");
75-
out.println("<h1>Basic Auth with File-base Realm (" + method + ")</h1>");
76-
out.println("<h2>Were you prompted for username/password ?</h2>");
77-
out.println("</body>");
78-
out.println("</html>");
79-
}
80-
81-
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
82-
/**
83-
* Handles the HTTP <code>GET</code> method.
84-
*
85-
* @param request servlet request
86-
* @param response servlet response
87-
* @throws ServletException if a servlet-specific error occurs
88-
* @throws IOException if an I/O error occurs
89-
*/
9055
@Override
9156
protected void doGet(HttpServletRequest request, HttpServletResponse response)
9257
throws ServletException, IOException {
93-
processRequest(request, response, "GET");
58+
response.getWriter().print("my GET");
9459
}
9560

96-
/**
97-
* Handles the HTTP <code>POST</code> method.
98-
*
99-
* @param request servlet request
100-
* @param response servlet response
101-
* @throws ServletException if a servlet-specific error occurs
102-
* @throws IOException if an I/O error occurs
103-
*/
10461
@Override
10562
protected void doPost(HttpServletRequest request, HttpServletResponse response)
10663
throws ServletException, IOException {
107-
processRequest(request, response, "POST");
64+
response.getWriter().print("my POST");
10865
}
109-
110-
/**
111-
* Returns a short description of the servlet.
112-
*
113-
* @return a String containing servlet description
114-
*/
115-
@Override
116-
public String getServletInfo() {
117-
return "Short description";
118-
}// </editor-fold>
11966
}

servlet/security-basicauth/src/main/webapp/WEB-INF/web.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<web-resource-name>SecureServlet</web-resource-name>
5151
<url-pattern>/SecureServlet</url-pattern>
5252
<http-method>GET</http-method>
53+
<http-method>POST</http-method>
5354
</web-resource-collection>
5455
<auth-constraint>
5556
<role-name>g1</role-name>

servlet/security-basicauth/src/test/java/org/javaee7/servlet/security/basicauth/SecureServletTest.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
44
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
5+
import com.gargoylesoftware.htmlunit.HttpMethod;
6+
import com.gargoylesoftware.htmlunit.TextPage;
57
import com.gargoylesoftware.htmlunit.WebClient;
8+
import com.gargoylesoftware.htmlunit.WebRequest;
69
import com.gargoylesoftware.htmlunit.html.HtmlPage;
710
import java.io.File;
811
import java.net.URL;
@@ -27,6 +30,7 @@ public class SecureServletTest {
2730
@ArquillianResource
2831
private URL base;
2932

33+
WebClient webClient;
3034
DefaultCredentialsProvider correctCreds = new DefaultCredentialsProvider();
3135
DefaultCredentialsProvider incorrectCreds = new DefaultCredentialsProvider();
3236

@@ -40,21 +44,20 @@ public static WebArchive createDeployment() {
4044

4145
@Before
4246
public void setup() {
47+
webClient = new WebClient();
4348
correctCreds.addCredentials("u1", "p1");
4449
incorrectCreds.addCredentials("random", "random");
4550
}
4651

4752
@Test
4853
public void testGetWithCorrectCredentials() throws Exception {
49-
WebClient webClient = new WebClient();
5054
webClient.setCredentialsProvider(correctCreds);
51-
HtmlPage page = webClient.getPage(base + "/SecureServlet");
52-
assertEquals("Servlet Security - Basic Auth with File-base Realm", page.getTitleText());
55+
TextPage page = webClient.getPage(base + "/SecureServlet");
56+
assertEquals("my GET", page.getContent());
5357
}
5458

5559
@Test
5660
public void testGetWithIncorrectCredentials() throws Exception {
57-
WebClient webClient = new WebClient();
5861
webClient.setCredentialsProvider(incorrectCreds);
5962
try {
6063
webClient.getPage(base + "/SecureServlet");
@@ -65,4 +68,26 @@ public void testGetWithIncorrectCredentials() throws Exception {
6568
}
6669
fail("/SecureServlet could be accessed without proper security credentials");
6770
}
71+
72+
@Test
73+
public void testPostWithCorrectCredentials() throws Exception {
74+
webClient.setCredentialsProvider(correctCreds);
75+
WebRequest request = new WebRequest(new URL(base + "/SecureServlet"), HttpMethod.POST);
76+
TextPage page = webClient.getPage(request);
77+
assertEquals("my POST", page.getContent());
78+
}
79+
80+
@Test
81+
public void testPostWithIncorrectCredentials() throws Exception {
82+
webClient.setCredentialsProvider(incorrectCreds);
83+
WebRequest request = new WebRequest(new URL(base + "/SecureServlet"), HttpMethod.POST);
84+
try {
85+
webClient.getPage(request);
86+
} catch(FailingHttpStatusCodeException e) {
87+
assertNotNull(e);
88+
assertEquals(401, e.getStatusCode());
89+
return;
90+
}
91+
fail("/SecureServlet could be accessed without proper security credentials");
92+
}
6893
}

0 commit comments

Comments
 (0)