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