Commit 522e424b1a2bfab831e6727b02d8facda84212db
1 parent
181e85cd
Exists in
master
IN PROGRESS - issue FWK-119: Testes da extensão Servlet
https://demoiselle.atlassian.net/browse/FWK-119
Showing
15 changed files
with
177 additions
and
105 deletions
Show diff stats
impl/extension/servlet/pom.xml
impl/extension/servlet/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/InternalProcessorFilterImpl.java
... | ... | @@ -24,9 +24,9 @@ public class InternalProcessorFilterImpl implements InternalProcessorFilter { |
24 | 24 | |
25 | 25 | filters.add(new HttpServletRequestProducerFilter()); |
26 | 26 | filters.add(new HttpServletResponseProducerFilter()); |
27 | - | |
27 | + | |
28 | 28 | // TODO Analizar o uso do BasicAuthenticationFilter |
29 | - // filters.add(new BasicAuthenticationFilter()); | |
29 | + filters.add(new BasicAuthenticationFilter()); | |
30 | 30 | } |
31 | 31 | |
32 | 32 | @Override | ... | ... |
impl/extension/servlet/src/test/java/producer/request/HelperServlet.java
0 → 100644
... | ... | @@ -0,0 +1,30 @@ |
1 | +package producer.request; | |
2 | + | |
3 | +import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR; | |
4 | +import static org.apache.http.HttpStatus.SC_OK; | |
5 | + | |
6 | +import java.io.IOException; | |
7 | + | |
8 | +import javax.servlet.ServletException; | |
9 | +import javax.servlet.http.HttpServlet; | |
10 | +import javax.servlet.http.HttpServletRequest; | |
11 | +import javax.servlet.http.HttpServletResponse; | |
12 | + | |
13 | +import br.gov.frameworkdemoiselle.util.Beans; | |
14 | + | |
15 | +public class HelperServlet extends HttpServlet { | |
16 | + | |
17 | + private static final long serialVersionUID = 1L; | |
18 | + | |
19 | + @Override | |
20 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
21 | + | |
22 | + HttpServletRequest httpRequest = Beans.getReference(HttpServletRequest.class); | |
23 | + | |
24 | + if (httpRequest != null) { | |
25 | + response.setStatus(SC_OK); | |
26 | + } else { | |
27 | + response.setStatus(SC_INTERNAL_SERVER_ERROR); | |
28 | + } | |
29 | + } | |
30 | +} | ... | ... |
impl/extension/servlet/src/test/java/producer/request/HttpServletRequestProducerTest.java
1 | 1 | package producer.request; |
2 | 2 | |
3 | 3 | import static junit.framework.Assert.assertEquals; |
4 | +import static org.apache.http.HttpStatus.SC_OK; | |
4 | 5 | |
5 | 6 | import java.io.IOException; |
6 | 7 | import java.net.URL; |
7 | 8 | |
8 | 9 | import org.apache.http.HttpResponse; |
9 | -import org.apache.http.HttpStatus; | |
10 | 10 | import org.apache.http.client.ClientProtocolException; |
11 | 11 | import org.apache.http.client.methods.HttpGet; |
12 | -import org.apache.http.impl.client.DefaultHttpClient; | |
12 | +import org.apache.http.impl.client.HttpClientBuilder; | |
13 | 13 | import org.jboss.arquillian.container.test.api.Deployment; |
14 | 14 | import org.jboss.arquillian.junit.Arquillian; |
15 | 15 | import org.jboss.arquillian.test.api.ArquillianResource; |
... | ... | @@ -29,16 +29,16 @@ public class HttpServletRequestProducerTest { |
29 | 29 | |
30 | 30 | @Deployment(testable = false) |
31 | 31 | public static WebArchive createDeployment() { |
32 | - return Tests.createDeployment().addClass(RequestServlet.class) | |
32 | + return Tests.createDeployment().addClass(HelperServlet.class) | |
33 | 33 | .addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml"); |
34 | 34 | } |
35 | 35 | |
36 | 36 | @Test |
37 | - public void createR() throws ClientProtocolException, IOException { | |
38 | - HttpGet httpGet = new HttpGet(deploymentUrl + "/servlet"); | |
39 | - HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet); | |
37 | + public void producedSuccessfully() throws ClientProtocolException, IOException { | |
38 | + HttpGet httpGet = new HttpGet(deploymentUrl + "/helper"); | |
39 | + HttpResponse httpResponse = HttpClientBuilder.create().build().execute(httpGet); | |
40 | 40 | |
41 | 41 | int status = httpResponse.getStatusLine().getStatusCode(); |
42 | - assertEquals(HttpStatus.SC_OK, status); | |
42 | + assertEquals(SC_OK, status); | |
43 | 43 | } |
44 | 44 | } | ... | ... |
impl/extension/servlet/src/test/java/producer/request/RequestServlet.java
... | ... | @@ -1,30 +0,0 @@ |
1 | -package producer.request; | |
2 | - | |
3 | -import java.io.IOException; | |
4 | - | |
5 | -import javax.servlet.ServletException; | |
6 | -import javax.servlet.http.HttpServlet; | |
7 | -import javax.servlet.http.HttpServletRequest; | |
8 | -import javax.servlet.http.HttpServletResponse; | |
9 | - | |
10 | -import org.apache.http.HttpStatus; | |
11 | - | |
12 | -import br.gov.frameworkdemoiselle.util.Beans; | |
13 | - | |
14 | -public class RequestServlet extends HttpServlet { | |
15 | - | |
16 | - private static final long serialVersionUID = 1L; | |
17 | - | |
18 | - @Override | |
19 | - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
20 | - | |
21 | - HttpServletRequest httpRequest = Beans.getReference(HttpServletRequest.class); | |
22 | - | |
23 | - if (httpRequest != null) { | |
24 | - response.setStatus(HttpStatus.SC_OK); | |
25 | - } else { | |
26 | - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); | |
27 | - } | |
28 | - } | |
29 | - | |
30 | -} |
impl/extension/servlet/src/test/java/producer/response/HelperServlet.java
0 → 100644
... | ... | @@ -0,0 +1,30 @@ |
1 | +package producer.response; | |
2 | + | |
3 | +import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR; | |
4 | +import static org.apache.http.HttpStatus.SC_OK; | |
5 | + | |
6 | +import java.io.IOException; | |
7 | + | |
8 | +import javax.servlet.ServletException; | |
9 | +import javax.servlet.http.HttpServlet; | |
10 | +import javax.servlet.http.HttpServletRequest; | |
11 | +import javax.servlet.http.HttpServletResponse; | |
12 | + | |
13 | +import br.gov.frameworkdemoiselle.util.Beans; | |
14 | + | |
15 | +public class HelperServlet extends HttpServlet { | |
16 | + | |
17 | + private static final long serialVersionUID = 1L; | |
18 | + | |
19 | + @Override | |
20 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
21 | + | |
22 | + HttpServletResponse httpResponse = Beans.getReference(HttpServletResponse.class); | |
23 | + | |
24 | + if (httpResponse != null) { | |
25 | + response.setStatus(SC_OK); | |
26 | + } else { | |
27 | + response.setStatus(SC_INTERNAL_SERVER_ERROR); | |
28 | + } | |
29 | + } | |
30 | +} | ... | ... |
impl/extension/servlet/src/test/java/producer/response/HttpServletResponseProducerTest.java
1 | 1 | package producer.response; |
2 | 2 | |
3 | 3 | import static junit.framework.Assert.assertEquals; |
4 | +import static org.apache.http.HttpStatus.SC_OK; | |
4 | 5 | |
5 | 6 | import java.io.IOException; |
6 | 7 | import java.net.URL; |
7 | 8 | |
8 | 9 | import org.apache.http.HttpResponse; |
9 | -import org.apache.http.HttpStatus; | |
10 | 10 | import org.apache.http.client.ClientProtocolException; |
11 | 11 | import org.apache.http.client.methods.HttpGet; |
12 | -import org.apache.http.impl.client.DefaultHttpClient; | |
12 | +import org.apache.http.impl.client.HttpClientBuilder; | |
13 | 13 | import org.jboss.arquillian.container.test.api.Deployment; |
14 | 14 | import org.jboss.arquillian.junit.Arquillian; |
15 | 15 | import org.jboss.arquillian.test.api.ArquillianResource; |
... | ... | @@ -17,7 +17,7 @@ import org.jboss.shrinkwrap.api.spec.WebArchive; |
17 | 17 | import org.junit.Test; |
18 | 18 | import org.junit.runner.RunWith; |
19 | 19 | |
20 | -import producer.request.RequestServlet; | |
20 | +import producer.request.HelperServlet; | |
21 | 21 | import test.Tests; |
22 | 22 | |
23 | 23 | @RunWith(Arquillian.class) |
... | ... | @@ -30,17 +30,16 @@ public class HttpServletResponseProducerTest { |
30 | 30 | |
31 | 31 | @Deployment(testable = false) |
32 | 32 | public static WebArchive createDeployment() { |
33 | - return Tests.createDeployment().addClass(RequestServlet.class) | |
33 | + return Tests.createDeployment().addClass(HelperServlet.class) | |
34 | 34 | .addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml"); |
35 | 35 | } |
36 | 36 | |
37 | 37 | @Test |
38 | - public void createResponse() throws ClientProtocolException, IOException { | |
39 | - HttpGet httpGet = new HttpGet(deploymentUrl + "/servlet"); | |
40 | - HttpResponse httpResponse = new DefaultHttpClient().execute(httpGet); | |
38 | + public void producedSuccessfully() throws ClientProtocolException, IOException { | |
39 | + HttpGet httpGet = new HttpGet(deploymentUrl + "/helper"); | |
40 | + HttpResponse httpResponse = HttpClientBuilder.create().build().execute(httpGet); | |
41 | 41 | |
42 | 42 | int status = httpResponse.getStatusLine().getStatusCode(); |
43 | - assertEquals(HttpStatus.SC_OK, status); | |
43 | + assertEquals(SC_OK, status); | |
44 | 44 | } |
45 | - | |
46 | 45 | } | ... | ... |
impl/extension/servlet/src/test/java/producer/response/ResponseServlet.java
... | ... | @@ -1,30 +0,0 @@ |
1 | -package producer.response; | |
2 | - | |
3 | -import java.io.IOException; | |
4 | - | |
5 | -import javax.servlet.ServletException; | |
6 | -import javax.servlet.http.HttpServlet; | |
7 | -import javax.servlet.http.HttpServletRequest; | |
8 | -import javax.servlet.http.HttpServletResponse; | |
9 | - | |
10 | -import org.apache.http.HttpStatus; | |
11 | - | |
12 | -import br.gov.frameworkdemoiselle.util.Beans; | |
13 | - | |
14 | -public class ResponseServlet extends HttpServlet { | |
15 | - | |
16 | - private static final long serialVersionUID = 1L; | |
17 | - | |
18 | - @Override | |
19 | - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
20 | - | |
21 | - HttpServletResponse httpResponse = Beans.getReference(HttpServletResponse.class); | |
22 | - | |
23 | - if (httpResponse != null) { | |
24 | - response.setStatus(HttpStatus.SC_OK); | |
25 | - } else { | |
26 | - response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR); | |
27 | - } | |
28 | - } | |
29 | - | |
30 | -} |
impl/extension/servlet/src/test/java/security/SecurityServlet.java
1 | 1 | package security; |
2 | 2 | |
3 | +import static org.apache.http.HttpStatus.SC_FORBIDDEN; | |
4 | +import static org.apache.http.HttpStatus.SC_OK; | |
5 | + | |
3 | 6 | import java.io.IOException; |
4 | 7 | |
5 | 8 | import javax.servlet.ServletException; |
... | ... | @@ -8,8 +11,6 @@ import javax.servlet.http.HttpServlet; |
8 | 11 | import javax.servlet.http.HttpServletRequest; |
9 | 12 | import javax.servlet.http.HttpServletResponse; |
10 | 13 | |
11 | -import org.apache.http.HttpStatus; | |
12 | - | |
13 | 14 | import br.gov.frameworkdemoiselle.security.AuthenticationException; |
14 | 15 | import br.gov.frameworkdemoiselle.security.Credentials; |
15 | 16 | import br.gov.frameworkdemoiselle.security.SecurityContext; |
... | ... | @@ -31,12 +32,10 @@ public class SecurityServlet extends HttpServlet { |
31 | 32 | |
32 | 33 | try { |
33 | 34 | Beans.getReference(SecurityContext.class).login(); |
34 | - response.setStatus(HttpStatus.SC_OK); | |
35 | + response.setStatus(SC_OK); | |
36 | + | |
35 | 37 | } catch (AuthenticationException e) { |
36 | - response.setStatus(HttpStatus.SC_UNAUTHORIZED); | |
38 | + response.setStatus(SC_FORBIDDEN); | |
37 | 39 | } |
38 | - | |
39 | - | |
40 | - | |
41 | 40 | } |
42 | 41 | } | ... | ... |
impl/extension/servlet/src/test/java/security/SecurityTest.java
... | ... | @@ -57,6 +57,6 @@ public class SecurityTest { |
57 | 57 | HttpResponse response = client.execute(get); |
58 | 58 | |
59 | 59 | int status = response.getStatusLine().getStatusCode(); |
60 | - assertEquals(HttpStatus.SC_UNAUTHORIZED, status); | |
60 | + assertEquals(HttpStatus.SC_FORBIDDEN, status); | |
61 | 61 | } |
62 | 62 | } | ... | ... |
impl/extension/servlet/src/test/java/security/authentication/basic/BasicAuthenticationFilterTest.java
1 | 1 | package security.authentication.basic; |
2 | 2 | |
3 | +import static org.apache.http.HttpStatus.SC_OK; | |
4 | +import static org.apache.http.HttpStatus.SC_FORBIDDEN; | |
5 | +import static org.junit.Assert.assertEquals; | |
6 | + | |
7 | +import java.io.IOException; | |
3 | 8 | import java.net.URL; |
4 | 9 | |
10 | +import org.apache.commons.codec.binary.Base64; | |
11 | +import org.apache.http.HttpResponse; | |
12 | +import org.apache.http.auth.AuthScope; | |
13 | +import org.apache.http.auth.UsernamePasswordCredentials; | |
14 | +import org.apache.http.client.ClientProtocolException; | |
15 | +import org.apache.http.client.CredentialsProvider; | |
16 | +import org.apache.http.client.methods.HttpGet; | |
17 | +import org.apache.http.impl.client.BasicCredentialsProvider; | |
18 | +import org.apache.http.impl.client.CloseableHttpClient; | |
19 | +import org.apache.http.impl.client.HttpClients; | |
5 | 20 | import org.jboss.arquillian.container.test.api.Deployment; |
6 | 21 | import org.jboss.arquillian.junit.Arquillian; |
7 | 22 | import org.jboss.arquillian.test.api.ArquillianResource; |
... | ... | @@ -26,10 +41,40 @@ public class BasicAuthenticationFilterTest { |
26 | 41 | } |
27 | 42 | |
28 | 43 | @Test |
29 | - public void loginSucessfull() { | |
44 | + public void loginSucessfull() throws ClientProtocolException, IOException { | |
45 | + CredentialsProvider credsProvider = new BasicCredentialsProvider(); | |
46 | + credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("demoiselle", "changeit")); | |
47 | + CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); | |
48 | + | |
49 | + String username = "demoiselle"; | |
50 | + String password = "changeit"; | |
51 | + | |
52 | + HttpGet httpGet = new HttpGet(deploymentUrl + "/helper"); | |
53 | + byte[] encoded = Base64.encodeBase64((username + ":" + password).getBytes()); | |
54 | + httpGet.setHeader("Authorization", "Basic " + new String(encoded)); | |
55 | + | |
56 | + HttpResponse httpResponse = httpClient.execute(httpGet); | |
57 | + | |
58 | + int status = httpResponse.getStatusLine().getStatusCode(); | |
59 | + assertEquals(SC_OK, status); | |
30 | 60 | } |
31 | 61 | |
32 | 62 | @Test |
33 | - public void loginfailed() { | |
63 | + public void loginFailed() throws ClientProtocolException, IOException { | |
64 | + CredentialsProvider credsProvider = new BasicCredentialsProvider(); | |
65 | + credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("demoiselle", "changeit")); | |
66 | + CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); | |
67 | + | |
68 | + String username = "invalid"; | |
69 | + String password = "changeit"; | |
70 | + | |
71 | + HttpGet httpGet = new HttpGet(deploymentUrl + "/helper"); | |
72 | + byte[] encoded = Base64.encodeBase64((username + ":" + password).getBytes()); | |
73 | + httpGet.setHeader("Authorization", "Basic " + new String(encoded)); | |
74 | + | |
75 | + HttpResponse httpResponse = httpClient.execute(httpGet); | |
76 | + | |
77 | + int status = httpResponse.getStatusLine().getStatusCode(); | |
78 | + assertEquals(SC_FORBIDDEN, status); | |
34 | 79 | } |
35 | 80 | } | ... | ... |
impl/extension/servlet/src/test/java/security/authentication/basic/HelperServlet.java
0 → 100644
... | ... | @@ -0,0 +1,30 @@ |
1 | +package security.authentication.basic; | |
2 | + | |
3 | +import static org.apache.http.HttpStatus.SC_FORBIDDEN; | |
4 | +import static org.apache.http.HttpStatus.SC_OK; | |
5 | + | |
6 | +import java.io.IOException; | |
7 | + | |
8 | +import javax.servlet.ServletException; | |
9 | +import javax.servlet.http.HttpServlet; | |
10 | +import javax.servlet.http.HttpServletRequest; | |
11 | +import javax.servlet.http.HttpServletResponse; | |
12 | + | |
13 | +import br.gov.frameworkdemoiselle.security.SecurityContext; | |
14 | +import br.gov.frameworkdemoiselle.util.Beans; | |
15 | + | |
16 | +public class HelperServlet extends HttpServlet { | |
17 | + | |
18 | + private static final long serialVersionUID = 1L; | |
19 | + | |
20 | + @Override | |
21 | + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
22 | + boolean loggedIn = Beans.getReference(SecurityContext.class).isLoggedIn(); | |
23 | + | |
24 | + if (loggedIn) { | |
25 | + response.setStatus(SC_OK); | |
26 | + } else { | |
27 | + response.setStatus(SC_FORBIDDEN); | |
28 | + } | |
29 | + } | |
30 | +} | ... | ... |
impl/extension/servlet/src/test/resources/producer/request/web.xml
... | ... | @@ -51,12 +51,12 @@ |
51 | 51 | </filter-mapping> |
52 | 52 | |
53 | 53 | <servlet> |
54 | - <servlet-name>Request Class</servlet-name> | |
55 | - <servlet-class>producer.request.RequestServlet</servlet-class> | |
54 | + <servlet-name>Helper Servlet</servlet-name> | |
55 | + <servlet-class>producer.request.HelperServlet</servlet-class> | |
56 | 56 | </servlet> |
57 | 57 | |
58 | 58 | <servlet-mapping> |
59 | - <servlet-name>Request Class</servlet-name> | |
60 | - <url-pattern>/servlet</url-pattern> | |
59 | + <servlet-name>Helper Servlet</servlet-name> | |
60 | + <url-pattern>/helper</url-pattern> | |
61 | 61 | </servlet-mapping> |
62 | 62 | </web-app> |
63 | 63 | \ No newline at end of file | ... | ... |
impl/extension/servlet/src/test/resources/producer/response/web.xml
... | ... | @@ -51,12 +51,12 @@ |
51 | 51 | </filter-mapping> |
52 | 52 | |
53 | 53 | <servlet> |
54 | - <servlet-name>Response Servlet</servlet-name> | |
55 | - <servlet-class>producer.response.ResponseServlet</servlet-class> | |
54 | + <servlet-name>Helper Servlet</servlet-name> | |
55 | + <servlet-class>producer.response.HelperServlet</servlet-class> | |
56 | 56 | </servlet> |
57 | 57 | |
58 | 58 | <servlet-mapping> |
59 | - <servlet-name>Response Servlet</servlet-name> | |
60 | - <url-pattern>/servlet</url-pattern> | |
59 | + <servlet-name>Helper Servlet</servlet-name> | |
60 | + <url-pattern>/helper</url-pattern> | |
61 | 61 | </servlet-mapping> |
62 | 62 | </web-app> |
63 | 63 | \ No newline at end of file | ... | ... |
impl/extension/servlet/src/test/resources/security/authentication/basic/web.xml
... | ... | @@ -51,19 +51,19 @@ |
51 | 51 | </filter-mapping> |
52 | 52 | |
53 | 53 | <servlet> |
54 | - <servlet-name>Security Class</servlet-name> | |
55 | - <servlet-class>security.SecurityServlet</servlet-class> | |
54 | + <servlet-name>Helper Servlet</servlet-name> | |
55 | + <servlet-class>security.authentication.basic.HelperServlet</servlet-class> | |
56 | 56 | </servlet> |
57 | 57 | <servlet-mapping> |
58 | - <servlet-name>Security Class</servlet-name> | |
59 | - <url-pattern>/login</url-pattern> | |
60 | - </servlet-mapping> | |
61 | - | |
58 | + <servlet-name>Helper Servlet</servlet-name> | |
59 | + <url-pattern>/helper</url-pattern> | |
60 | + </servlet-mapping> | |
61 | + | |
62 | 62 | <!-- |
63 | 63 | <security-constraint> |
64 | 64 | <web-resource-collection> |
65 | 65 | <web-resource-name>Private URL</web-resource-name> |
66 | - <url-pattern>/login</url-pattern> | |
66 | + <url-pattern>/helper</url-pattern> | |
67 | 67 | </web-resource-collection> |
68 | 68 | <auth-constraint> |
69 | 69 | <role-name>*</role-name> |
... | ... | @@ -72,7 +72,6 @@ |
72 | 72 | |
73 | 73 | <login-config> |
74 | 74 | <auth-method>BASIC</auth-method> |
75 | - <realm-name>fileRealm</realm-name> | |
76 | 75 | </login-config> |
77 | 76 | |
78 | 77 | <security-role> | ... | ... |