Search in sources :

Example 6 with RequestBuilder

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

the class ITMDCFilter method startContainer.

@Before
public void startContainer() throws Exception {
    if (testContainer == null) {
        ServerConfiguration sc = new ServerConfiguration();
        ExamSystem system = DefaultExamSystem.create(sc.config());
        testContainer = PaxExamRuntime.createContainer(system);
        testContainer.start();
        new RetryLoop(new RetryLoop.Condition() {

            public String getDescription() {
                return "Check if MDCTestServlet is up";
            }

            public boolean isTrue() throws Exception {
                RequestBuilder rb = new RequestBuilder(ServerConfiguration.getServerUrl());
                executor.execute(rb.buildGetRequest("/mdc")).assertStatus(200);
                rb = new RequestBuilder(ServerConfiguration.getServerUrl());
                //Create test config via servlet
                executor.execute(rb.buildGetRequest("/mdc", "createTestConfig", "true"));
                TimeUnit.SECONDS.sleep(1);
                return true;
            }
        }, 5, 100);
    }
}
Also used : ExamSystem(org.ops4j.pax.exam.ExamSystem) DefaultExamSystem(org.ops4j.pax.exam.spi.DefaultExamSystem) RequestBuilder(org.apache.sling.testing.tools.http.RequestBuilder) RetryLoop(org.apache.sling.testing.tools.retry.RetryLoop) IOException(java.io.IOException) Before(org.junit.Before)

Example 7 with RequestBuilder

use of org.apache.sling.testing.tools.http.RequestBuilder 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 8 with RequestBuilder

use of org.apache.sling.testing.tools.http.RequestBuilder 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 9 with RequestBuilder

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

Example 10 with RequestBuilder

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

the class ConsoleTestClient method testResourceResolver.

@Test
public void testResourceResolver() throws Exception {
    RequestBuilder rb = new RequestBuilder("http://localhost:9000");
    final MultipartEntity entity = new MultipartEntity();
    // Add Sling POST options
    entity.addPart("lang", new StringBody("esp"));
    entity.addPart("code", new InputStreamBody(getClass().getResourceAsStream("/test.js"), "test.js"));
    executor.execute(rb.buildPostRequest("/system/console/scriptconsole.json").withEntity(entity).withCredentials("admin", "admin")).assertStatus(200);
}
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) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody) 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