SecurityTest.java
2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package security;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import test.Tests;
import com.sun.enterprise.security.auth.login.FileLoginModule;
@RunWith(Arquillian.class)
public class SecurityTest {
private static final String PATH = "src/test/resources/security";
@ArquillianResource
private URL deploymentUrl;
@Deployment(testable = false)
public static WebArchive createDeployment() {
return Tests.createDeployment().addClasses(SecurityServlet.class, FileLoginModule.class)
.addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml");
}
@Test
public void loginSucessfull() throws ClientProtocolException, IOException, URISyntaxException {
URIBuilder uriBuilder = new URIBuilder(deploymentUrl + "/login");
uriBuilder.setParameter("username", "demoiselle");
uriBuilder.setParameter("password", "changeit");
HttpGet httpGet = new HttpGet(uriBuilder.build());
HttpResponse response = new DefaultHttpClient().execute(httpGet);
int status = response.getStatusLine().getStatusCode();
assertEquals(HttpStatus.SC_OK, status);
}
@Test
public void loginFailed() throws ClientProtocolException, IOException {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(deploymentUrl + "/login?username=demoiselle&password=wrongpass");
HttpResponse response = client.execute(get);
int status = response.getStatusLine().getStatusCode();
assertEquals(HttpStatus.SC_UNAUTHORIZED, status);
}
}