use of org.apache.sling.testing.tools.http.RequestBuilder in project sling by apache.
the class ValidationServiceIT method testValidRequestModel1.
@Test
public void testValidRequestModel1() throws IOException, JsonException {
final String url = String.format("http://localhost:%s", httpPort());
final RequestBuilder requestBuilder = new RequestBuilder(url);
MultipartEntity entity = new MultipartEntity();
entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
entity.addPart("field1", new StringBody("HELLOWORLD"));
entity.addPart("field2", new StringBody("30.01.1988"));
entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
String content = re.getContent();
JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
assertTrue(jsonResponse.getBoolean("valid"));
}
use of org.apache.sling.testing.tools.http.RequestBuilder in project sling by apache.
the class ValidationServiceIT method testInvalidRequestModel1.
@Test
public void testInvalidRequestModel1() throws IOException, JsonException {
MultipartEntity entity = new MultipartEntity();
entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
entity.addPart("field1", new StringBody("Hello World"));
entity.addPart(SlingPostConstants.RP_OPERATION, new StringBody("validation"));
final String url = String.format("http://localhost:%s", httpPort());
RequestBuilder requestBuilder = new RequestBuilder(url);
RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/validation/testing/fakeFolder1/resource").withEntity(entity)).assertStatus(200);
String content = re.getContent();
JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
assertFalse(jsonResponse.getBoolean("valid"));
JsonObject failure = jsonResponse.getJsonArray("failures").getJsonObject(0);
assertEquals("Property does not match the pattern \"^\\p{Upper}+$\".", failure.getString("message"));
assertEquals("field1", failure.getString("location"));
assertEquals(10, failure.getInt("severity"));
failure = jsonResponse.getJsonArray("failures").getJsonObject(1);
assertEquals("Missing required property with name \"field2\".", failure.getString("message"));
// location is empty as the property is not found (property name is part of the message rather)
assertEquals("", failure.getString("location"));
assertEquals(0, failure.getInt("severity"));
}
use of org.apache.sling.testing.tools.http.RequestBuilder 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.RequestBuilder in project sling by apache.
the class U method getBundleData.
/** Get JSON bundle data from webconsole */
static JsonObject getBundleData(CrankstartSetup C, DefaultHttpClient client, String symbolicName) throws ClientProtocolException, IOException, JsonException {
final RequestBuilder b = new RequestBuilder(C.getBaseUrl());
final RequestExecutor e = new RequestExecutor(client);
return Json.createReader(new StringReader((e.execute(b.buildGetRequest("/system/console/bundles/" + symbolicName + ".json").withCredentials(U.ADMIN, U.ADMIN))).assertStatus(200).getContent())).readObject();
}
use of org.apache.sling.testing.tools.http.RequestBuilder in project sling by apache.
the class ITMDCFilter method testWihCustomData.
@Test
public void testWihCustomData() throws Exception {
RequestBuilder rb = new RequestBuilder(ServerConfiguration.getServerUrl());
//Create test config via servlet
executor.execute(rb.buildGetRequest("/mdc", "createTestConfig", "true"));
TimeUnit.SECONDS.sleep(1);
//Pass custom cookie
BasicClientCookie cookie = new BasicClientCookie("mdc-test-cookie", "foo-test-cookie");
cookie.setPath("/");
cookie.setDomain("localhost");
httpClient.getCookieStore().addCookie(cookie);
//Execute request
RequestExecutor result = executor.execute(rb.buildGetRequest("/mdc", "mdc-test-param", "foo-test-param", "ignored-param", "ignored-value").withHeader("X-Forwarded-For", "foo-forwarded-for").withHeader("mdc-test-header", "foo-test-header"));
JsonObject jb = Json.createReader(new StringReader(result.getContent())).readObject();
log.info("Response {}", result.getContent());
assertEquals("/mdc", jb.getString("req.requestURI"));
assertEquals(ServerConfiguration.getServerUrl() + "/mdc", jb.getString("req.requestURL"));
assertEquals("foo-forwarded-for", jb.getString("req.xForwardedFor"));
assertEquals("foo-test-header", jb.getString("mdc-test-header"));
assertEquals("foo-test-param", jb.getString("mdc-test-param"));
assertEquals("foo-test-cookie", jb.getString("mdc-test-cookie"));
//Only configured params must be returned
assertFalse(jb.containsKey("ignored-param"));
}
Aggregations