Search in sources :

Example 6 with RequestExecutor

use of org.apache.sling.testing.tools.http.RequestExecutor 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)

Example 7 with RequestExecutor

use of org.apache.sling.testing.tools.http.RequestExecutor in project sling by apache.

the class ValidationServiceIT method setup.

@Before
public void setup() throws IOException {
    defaultHttpClient = new DefaultHttpClient();
    requestExecutor = new RequestExecutor(defaultHttpClient);
}
Also used : RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) Before(org.junit.Before)

Example 8 with RequestExecutor

use of org.apache.sling.testing.tools.http.RequestExecutor in project sling by apache.

the class ValidationServiceIT method testPostProcessorWithInvalidModel.

@Test
public void testPostProcessorWithInvalidModel() throws IOException, JsonException {
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("sling:resourceType", new StringBody("validation/test/resourceType1"));
    entity.addPart("field1", new StringBody("Hello World"));
    final String url = String.format("http://localhost:%s", httpPort());
    RequestBuilder requestBuilder = new RequestBuilder(url);
    // test JSON response, because the HTML response overwrites the original exception (https://issues.apache.org/jira/browse/SLING-6703)
    RequestExecutor re = requestExecutor.execute(requestBuilder.buildPostRequest("/content/validated/invalidresource").withEntity(entity).withHeader("Accept", "application/json").withCredentials("admin", "admin")).assertStatus(500);
    String content = re.getContent();
    JsonObject jsonResponse = Json.createReader(new StringReader(content)).readObject();
    JsonObject error = jsonResponse.getJsonObject("error");
    assertEquals("org.apache.sling.validation.impl.postprocessor.InvalidResourcePostProcessorException", error.getString("class"));
    assertEquals("Validation errors: field1 : Property does not match the pattern \"^\\p{Upper}+$\"., Missing required property with name \"field2\".", error.getString("message"));
}
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 9 with RequestExecutor

use of org.apache.sling.testing.tools.http.RequestExecutor in project sling by apache.

the class ITMDCFilter method testDefault.

@Test
public void testDefault() throws Exception {
    RequestBuilder rb = new RequestBuilder(ServerConfiguration.getServerUrl());
    // Add Sling POST options
    RequestExecutor result = executor.execute(rb.buildGetRequest("/mdc", "foo", "bar"));
    JsonObject jb = Json.createReader(new StringReader(result.getContent())).readObject();
    assertEquals("/mdc", jb.getString("req.requestURI"));
    assertEquals("foo=bar", jb.getString("req.queryString"));
    assertEquals(ServerConfiguration.getServerUrl() + "/mdc", jb.getString("req.requestURL"));
    log.info("Response  {}", result.getContent());
}
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) Test(org.junit.Test)

Example 10 with RequestExecutor

use of org.apache.sling.testing.tools.http.RequestExecutor 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());
    }
}
Also used : StringWriter(java.io.StringWriter) RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RequestExecutor(org.apache.sling.testing.tools.http.RequestExecutor) Request(org.apache.sling.testing.tools.http.Request) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) PrintWriter(java.io.PrintWriter)

Aggregations

RequestExecutor (org.apache.sling.testing.tools.http.RequestExecutor)12 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)8 StringReader (java.io.StringReader)7 JsonObject (javax.json.JsonObject)7 Test (org.junit.Test)5 MultipartEntity (org.apache.http.entity.mime.MultipartEntity)3 StringBody (org.apache.http.entity.mime.content.StringBody)3 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)3 RemoteTestHttpClient (org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient)3 JsonArray (javax.json.JsonArray)2 Request (org.apache.sling.testing.tools.http.Request)2 ObjectInputStream (java.io.ObjectInputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1