use of org.apache.http.StatusLine in project wildfly by wildfly.
the class AbstractWebSecurityFORMTestCase method makeCall.
// Protected methods -----------------------------------------------------
/**
* Makes a HTTP request to the protected web application.
*
* @param user
* @param pass
* @param expectedStatusCode
* @throws Exception
* @see WebSecurityPasswordBasedBase#makeCall(java.lang.String, java.lang.String,
* int)
*/
@Override
protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
String req = url.toExternalForm() + "secured/";
HttpGet httpget = new HttpGet(req);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
EntityUtils.consume(entity);
}
// We should get the Login Page
StatusLine statusLine = response.getStatusLine();
LOGGER.trace("Login form get: " + statusLine);
assertEquals(200, statusLine.getStatusCode());
LOGGER.trace("Initial set of cookies:");
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
LOGGER.trace("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
LOGGER.trace("- " + cookies.get(i).toString());
}
}
req = url.toExternalForm() + "secured/j_security_check";
// We should now login with the user name and password
HttpPost httpPost = new HttpPost(req);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("j_username", user));
nvps.add(new BasicNameValuePair("j_password", pass));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
response = httpclient.execute(httpPost);
entity = response.getEntity();
if (entity != null) {
EntityUtils.consume(entity);
}
statusLine = response.getStatusLine();
// Post authentication - we have a 302
assertEquals(302, statusLine.getStatusCode());
Header locationHeader = response.getFirstHeader("Location");
String location = locationHeader.getValue();
HttpGet httpGet = new HttpGet(location);
response = httpclient.execute(httpGet);
entity = response.getEntity();
if (entity != null) {
EntityUtils.consume(entity);
}
LOGGER.trace("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
LOGGER.trace("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
LOGGER.trace("- " + cookies.get(i).toString());
}
}
// Either the authentication passed or failed based on the expected status code
statusLine = response.getStatusLine();
assertEquals(expectedStatusCode, statusLine.getStatusCode());
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
use of org.apache.http.StatusLine in project wildfly by wildfly.
the class WebModuleDeploymentTestCase method testSimpleBeanInjected.
@Test
public void testSimpleBeanInjected() throws Exception {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpGet httpget = new HttpGet(url.toExternalForm() + "/servlet");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
assertEquals(200, statusLine.getStatusCode());
String result = EntityUtils.toString(entity);
Assert.assertEquals(ModuleServlet.MODULE_SERVLET, result);
}
}
use of org.apache.http.StatusLine in project wildfly by wildfly.
the class DefaultServletMultipartConfigTestCase method testMultipartRequestToDefaultServlet.
@Test
public void testMultipartRequestToDefaultServlet() throws Exception {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost post = new HttpPost(url.toExternalForm() + "/servlet");
post.setEntity(MultipartEntityBuilder.create().addTextBody("file", MESSAGE).build());
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
assertEquals(200, statusLine.getStatusCode());
String result = EntityUtils.toString(entity);
Assert.assertEquals(MESSAGE, result);
}
}
use of org.apache.http.StatusLine in project wildfly by wildfly.
the class WebSecurityBASICTestCase method makeCall.
@Override
protected void makeCall(String user, String pass, int expectedStatusCode) throws Exception {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass));
try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build()) {
HttpGet httpget = new HttpGet(url.toExternalForm() + "secured/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
if (entity != null) {
log.trace("Response content length: " + entity.getContentLength());
}
assertEquals(expectedStatusCode, statusLine.getStatusCode());
EntityUtils.consume(entity);
}
}
use of org.apache.http.StatusLine in project wildfly by wildfly.
the class UndertowNonBlockingHandlerTestCase method testNonBlockingHandler.
@Test
public void testNonBlockingHandler() throws Exception {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpget = new HttpGet(url.toExternalForm());
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
StatusLine statusLine = response.getStatusLine();
assertEquals(200, statusLine.getStatusCode());
String result = EntityUtils.toString(entity);
Assert.assertEquals(SimpleUndertowExtension.THIS_IS_NOT_A_SERVLET, result);
}
}
Aggregations