Search in sources :

Example 1 with RequestBuilder

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"));
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 2 with RequestBuilder

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"));
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) MultipartEntity(org.apache.http.entity.mime.MultipartEntity) StringBody(org.apache.http.entity.mime.content.StringBody) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) Test(org.junit.Test)

Example 3 with RequestBuilder

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;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) ArrayList(java.util.ArrayList) Request(org.apache.sling.testing.tools.http.Request) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Map(java.util.Map)

Example 4 with RequestBuilder

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();
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader)

Example 5 with RequestBuilder

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"));
}
Also used : RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) StringReader(java.io.StringReader) JsonObject(javax.json.JsonObject) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test)

Aggregations

RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)10 RequestExecutor (org.apache.sling.testing.tools.http.RequestExecutor)8 StringReader (java.io.StringReader)6 Test (org.junit.Test)6 JsonObject (javax.json.JsonObject)5 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)4 StringBody (org.apache.http.entity.mime.content.StringBody)4 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)2 Request (org.apache.sling.testing.tools.http.Request)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 NameValuePair (org.apache.http.NameValuePair)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 InputStreamBody (org.apache.http.entity.mime.content.InputStreamBody)1 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 RetryLoop (org.apache.sling.testing.tools.retry.RetryLoop)1