Search in sources :

Example 1 with Request

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();
}
Also used : ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Request(org.apache.sling.testing.tools.http.Request)

Example 2 with Request

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

Example 3 with Request

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;
}
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 Request

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

Example 5 with Request

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());
    }
}
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

Request (org.apache.sling.testing.tools.http.Request)5 ArrayList (java.util.ArrayList)2 NameValuePair (org.apache.http.NameValuePair)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 RequestBuilder (org.apache.sling.testing.tools.http.RequestBuilder)2 RequestExecutor (org.apache.sling.testing.tools.http.RequestExecutor)2 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1 JsonString (javax.json.JsonString)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1