in src/main/java/org/apache/sling/launchpad/webapp/integrationtest/AuthenticatedTestUtil.java [109:163]
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);
}
}