use of org.apache.commons.httpclient.Header in project sling by apache.
the class SelectorAuthenticationResponseCodeTest method testWithAcceptHeaderIncorrectCredentials.
// this method assumes the use of the selector auth bundle
@Test
public void testWithAcceptHeaderIncorrectCredentials() throws Exception {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new NameValuePair("j_username", "garbage"));
params.add(new NameValuePair("j_password", "garbage"));
// simulate a browser request
List<Header> headers = new ArrayList<Header>();
headers.add(new Header("User-Agent", "Mozilla/5.0 Sling Integration Test"));
HttpMethod post = assertPostStatus(HttpTest.HTTP_BASE_URL + "/j_security_check", HttpServletResponse.SC_MOVED_TEMPORARILY, params, headers, null);
final String location = post.getResponseHeader("Location").getValue();
assertNotNull(location);
assertTrue(location.startsWith(HttpTest.HTTP_BASE_URL + "/system/sling/selector/login?"));
assertTrue(location.contains("resource=%2F"));
assertTrue(location.contains("j_reason=INVALID_CREDENTIALS"));
}
use of org.apache.commons.httpclient.Header in project sling by apache.
the class AuthenticatedTestUtil method getAuthenticatedPostContent.
/** retrieve the contents of given URL and assert its content type
* @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
* @throws IOException
* @throws HttpException */
public String getAuthenticatedPostContent(Credentials creds, String url, String expectedContentType, List<NameValuePair> postParams, int expectedStatusCode) throws IOException {
final PostMethod post = new PostMethod(url);
URL baseUrl = new URL(HTTP_BASE_URL);
AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
post.setDoAuthentication(true);
Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
try {
httpClient.getState().setCredentials(authScope, creds);
if (postParams != null) {
final NameValuePair[] nvp = {};
post.setRequestBody(postParams.toArray(nvp));
}
final int status = httpClient.executeMethod(post);
final InputStream is = post.getResponseBodyAsStream();
final StringBuffer content = new StringBuffer();
final String charset = post.getResponseCharSet();
final byte[] buffer = new byte[16384];
int n = 0;
while ((n = is.read(buffer, 0, buffer.length)) > 0) {
content.append(new String(buffer, 0, n, charset));
}
assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
final Header h = post.getResponseHeader("Content-Type");
if (expectedContentType == null) {
if (h != null) {
fail("Expected null Content-Type, got " + h.getValue());
}
} else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
// no check
} else if (h == null) {
fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
} else {
assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
}
return content.toString();
} finally {
httpClient.getState().setCredentials(authScope, oldCredentials);
}
}
use of org.apache.commons.httpclient.Header in project sling by apache.
the class AuthenticatedTestUtil method getAuthenticatedContent.
/** retrieve the contents of given URL and assert its content type
* @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
* @throws IOException
* @throws HttpException */
public String getAuthenticatedContent(Credentials creds, String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode) throws IOException {
final GetMethod get = new GetMethod(url);
URL baseUrl = new URL(HTTP_BASE_URL);
AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
get.setDoAuthentication(true);
Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
try {
httpClient.getState().setCredentials(authScope, creds);
if (params != null) {
final NameValuePair[] nvp = new NameValuePair[0];
get.setQueryString(params.toArray(nvp));
}
final int status = httpClient.executeMethod(get);
final InputStream is = get.getResponseBodyAsStream();
final StringBuffer content = new StringBuffer();
final String charset = get.getResponseCharSet();
final byte[] buffer = new byte[16384];
int n = 0;
while ((n = is.read(buffer, 0, buffer.length)) > 0) {
content.append(new String(buffer, 0, n, charset));
}
assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
final Header h = get.getResponseHeader("Content-Type");
if (expectedContentType == null) {
if (h != null) {
fail("Expected null Content-Type, got " + h.getValue());
}
} else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
// no check
} else if (h == null) {
fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
} else {
assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
}
return content.toString();
} finally {
httpClient.getState().setCredentials(authScope, oldCredentials);
}
}
use of org.apache.commons.httpclient.Header in project sling by apache.
the class WebdavOptionsTest method doTestOnPath.
/** OPTIONS request at given path must return a WWW-Authenticate header */
private void doTestOnPath(String path) throws Exception {
final HttpClient noCredentialsClient = new HttpClient();
final HttpAnyMethod opt = new HttpAnyMethod("OPTIONS", HTTP_BASE_URL + path);
final int status = noCredentialsClient.executeMethod(opt);
assertEquals("Expecting matching status on OPTIONS request at " + path, 401, status);
final Header h = opt.getResponseHeader(WWW_Authenticate);
assertNotNull("Expecting " + WWW_Authenticate + " header in response at " + path, h);
}
use of org.apache.commons.httpclient.Header in project sling by apache.
the class AuthenticationResponseCodeTest method assertXReason.
private void assertXReason(final HttpMethod method) throws IOException {
// expected the X-Reason header
final Header reason = method.getResponseHeader("X-Reason");
assertNotNull(reason);
// expect the response to be the same as the reason (SLING-1831)
assertEquals(reason.getValue(), method.getResponseBodyAsString().trim());
}
Aggregations