use of org.apache.sling.testing.tools.http.Request in project sling by apache.
the class DistributionUtils method assertPostResource.
private static String assertPostResource(SlingInstance slingInstance, int status, String path, byte[] bytes) throws IOException {
Request request = slingInstance.getRequestBuilder().buildPostRequest(path);
if (bytes != null) {
ByteArrayEntity entity = new ByteArrayEntity(bytes);
request.withEntity(entity);
}
return slingInstance.getRequestExecutor().execute(request.withCredentials(slingInstance.getServerUsername(), slingInstance.getServerPassword())).assertStatus(status).getContent();
}
use of org.apache.sling.testing.tools.http.Request in project sling by apache.
the class DistributionUtils method getResource.
public static JsonObject getResource(SlingInstance slingInstance, String path) throws IOException, JsonException {
if (!path.endsWith(JSON_SELECTOR)) {
path += JSON_SELECTOR;
}
Request request = slingInstance.getRequestBuilder().buildGetRequest(path).withCredentials(slingInstance.getServerUsername(), slingInstance.getServerPassword());
// Get list of tests in JSON format
String content = slingInstance.getRequestExecutor().execute(request).assertStatus(200).assertContentType("application/json").getContent();
return Json.createReader(new StringReader(content)).readObject();
}
use of org.apache.sling.testing.tools.http.Request in project sling by apache.
the class RemoteTestHttpClient method runTests.
public RequestExecutor runTests(String testClassesSelector, String testMethodSelector, String extension, Map<String, String> requestOptions) throws ClientProtocolException, IOException {
final RequestBuilder builder = new RequestBuilder(junitServletUrl);
// Optionally let the client to consume the response entity
final RequestExecutor executor = new RequestExecutor(new DefaultHttpClient()) {
@Override
protected void consumeEntity() throws ParseException, IOException {
if (consumeContent) {
super.consumeEntity();
}
}
};
// Build path for POST request to execute the tests
// Test classes selector
subpath = new StringBuilder();
if (!junitServletUrl.endsWith(SLASH)) {
subpath.append(SLASH);
}
subpath.append(testClassesSelector);
// Test method selector
if (testMethodSelector != null && testMethodSelector.length() > 0) {
subpath.append("/");
subpath.append(testMethodSelector);
}
// Extension
if (!extension.startsWith(DOT)) {
subpath.append(DOT);
}
subpath.append(extension);
// Request options if any
final List<NameValuePair> opt = new ArrayList<NameValuePair>();
if (requestOptions != null) {
for (Map.Entry<String, String> e : requestOptions.entrySet()) {
opt.add(new BasicNameValuePair(e.getKey(), e.getValue()));
}
}
log.info("Executing test remotely, path={} JUnit servlet URL={}", subpath, junitServletUrl);
final Request r = builder.buildPostRequest(subpath.toString()).withCredentials(username, password).withCustomizer(requestCustomizer).withEntity(new UrlEncodedFormEntity(opt));
executor.execute(r).assertStatus(200);
return executor;
}
use of org.apache.sling.testing.tools.http.Request in project sling by apache.
the class DistributionUtils method assertPostResourceWithParameters.
public static String assertPostResourceWithParameters(SlingInstance slingInstance, int status, String path, String... parameters) throws IOException {
Request request = slingInstance.getRequestBuilder().buildPostRequest(path);
if (parameters != null) {
assertEquals(0, parameters.length % 2);
List<NameValuePair> valuePairList = new ArrayList<NameValuePair>();
for (int i = 0; i < parameters.length; i += 2) {
valuePairList.add(new BasicNameValuePair(parameters[i], parameters[i + 1]));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(valuePairList);
request.withEntity(entity);
}
return slingInstance.getRequestExecutor().execute(request.withCredentials(DISTRIBUTOR_USER, DISTRIBUTOR_PASSWORD)).assertStatus(status).getContent();
}
use of org.apache.sling.testing.tools.http.Request in project sling by apache.
the class RemoteLogDumper method failed.
@Override
protected void failed(Throwable e, Description description) {
final String baseUrl = getServerBaseUrl();
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
if (baseUrl != null) {
try {
warnIfNopMDCAdapterBeingUsed();
DefaultHttpClient httpClient = new DefaultHttpClient();
RequestExecutor executor = new RequestExecutor(httpClient);
RequestBuilder rb = new RequestBuilder(baseUrl);
Request r = rb.buildGetRequest(SERVLET_PATH, TEST_CLASS, description.getClassName(), TEST_NAME, description.getMethodName());
executor.execute(r);
int statusCode = executor.getResponse().getStatusLine().getStatusCode();
String msg = e.getMessage();
if (msg != null) {
pw.println(msg);
}
if (statusCode == 200) {
pw.printf("=============== Logs from server [%s] for [%s]===================%n", baseUrl, description.getMethodName());
pw.print(executor.getContent());
pw.println("========================================================");
} else {
pw.printf("Not able to fetch logs from [%s%s]. " + "TestLogServer probably not configured %n", baseUrl, SERVLET_PATH);
}
} catch (Throwable t) {
System.err.printf("Error occurred while fetching test logs from server [%s] %n", baseUrl);
t.printStackTrace(System.err);
}
System.err.print(sw.toString());
}
}
Aggregations