use of org.apache.commons.httpclient.methods.PostMethod in project sling by apache.
the class HttpTestBase method getContent.
/** retrieve the contents of given URL and assert its content type
* @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
* @param httpMethod supports just GET and POST methods
* @throws IOException
* @throws HttpException */
public String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode, String httpMethod) throws IOException {
HttpMethodBase method = null;
if (HTTP_METHOD_GET.equals(httpMethod)) {
method = new GetMethod(url);
} else if (HTTP_METHOD_POST.equals(httpMethod)) {
method = new PostMethod(url);
} else {
fail("Http Method not supported in this test suite, method: " + httpMethod);
}
if (params != null) {
final NameValuePair[] nvp = new NameValuePair[0];
method.setQueryString(params.toArray(nvp));
}
final int status = httpClient.executeMethod(method);
final String content = getResponseBodyAsStream(method, 0);
assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
final Header h = method.getResponseHeader("Content-Type");
if (expectedContentType == null) {
if (h != null) {
fail("Expected null Content-Type, got " + h.getValue());
}
} else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
// no check
} else if (h == null) {
fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
} else {
assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
}
return content.toString();
}
use of org.apache.commons.httpclient.methods.PostMethod in project intellij-plugins by JetBrains.
the class JstdDebugBrowserInfo method fixIfChrome.
/**
* Posts 'heartbeat' event to the server. Usually, a captured browser posts this event, but Chrome is a special case:
* When execution suspended on a breakpoint, it can't perform any background activity.
* Emulating 'heartbeat' event keeps alive the debug session.
*
* @param testRunnerProcessHandler
*/
public void fixIfChrome(@NotNull final ProcessHandler testRunnerProcessHandler) {
if (!BrowserFamily.CHROME.equals(myWebBrowser.getFamily())) {
return;
}
ApplicationManager.getApplication().executeOnPooledThread(() -> {
HttpClient client = new HttpClient();
while (!testRunnerProcessHandler.isProcessTerminated()) {
String url = "http://127.0.0.1:" + myServerSettings.getPort() + "/heartbeat";
PostMethod method = new PostMethod(url);
method.addParameter("id", myBrowserInfo.getId());
try {
int responseCode = client.executeMethod(method);
if (responseCode != 200) {
LOG.warn(url + ": response code: " + responseCode);
}
} catch (IOException e) {
LOG.warn("Cannot request " + url, e);
}
TimeoutUtil.sleep(5000);
}
client.getHttpConnectionManager().closeIdleConnections(0);
});
}
use of org.apache.commons.httpclient.methods.PostMethod in project sling by apache.
the class SelectorAuthenticationResponseCodeTest method assertPostStatus.
// TODO - move this method into commons.testing
protected HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams, List<Header> headers, String assertMessage) throws IOException {
final PostMethod post = new PostMethod(url);
post.setFollowRedirects(false);
if (headers != null) {
for (Header header : headers) {
post.addRequestHeader(header);
}
}
if (postParams != null) {
final NameValuePair[] nvp = {};
post.setRequestBody(postParams.toArray(nvp));
}
final int status = H.getHttpClient().executeMethod(post);
if (assertMessage == null) {
assertEquals(expectedStatusCode, status);
} else {
assertEquals(assertMessage, expectedStatusCode, status);
}
return post;
}
use of org.apache.commons.httpclient.methods.PostMethod in project sling by apache.
the class AuthenticatedTestUtil method assertAuthenticatedPostStatus.
/** Execute a POST request and check status */
public void assertAuthenticatedPostStatus(Credentials creds, String url, int expectedStatusCode, List<NameValuePair> postParams, String assertMessage) throws IOException {
final PostMethod post = new PostMethod(url);
post.setFollowRedirects(false);
URL baseUrl = new URL(HTTP_BASE_URL);
AuthScope authScope = new AuthScope(baseUrl.getHost(), baseUrl.getPort(), AuthScope.ANY_REALM);
post.setDoAuthentication(true);
Credentials oldCredentials = httpClient.getState().getCredentials(authScope);
try {
httpClient.getState().setCredentials(authScope, creds);
if (postParams != null) {
final NameValuePair[] nvp = {};
post.setRequestBody(postParams.toArray(nvp));
}
final int status = httpClient.executeMethod(post);
if (assertMessage == null) {
assertEquals(expectedStatusCode, status);
} else {
assertEquals(assertMessage, expectedStatusCode, status);
}
} finally {
httpClient.getState().setCredentials(authScope, oldCredentials);
}
}
use of org.apache.commons.httpclient.methods.PostMethod in project sling by apache.
the class FsMountHelper method removeConfiguration.
/**
* Remove configuration.
*/
private void removeConfiguration(final String targetUrl, final String pid) throws MojoExecutionException {
final String postUrl = targetUrl + "/configMgr/" + pid;
final PostMethod post = new PostMethod(postUrl);
post.addParameter("apply", "true");
post.addParameter("delete", "true");
try {
final int status = httpClient.executeMethod(post);
// we get a moved temporarily back from the configMgr plugin
if (status == HttpStatus.SC_MOVED_TEMPORARILY || status == HttpStatus.SC_OK) {
log.debug("Configuration removed.");
} else {
log.error("Removing configuration failed, cause: " + HttpStatus.getStatusText(status));
}
} catch (HttpException ex) {
throw new MojoExecutionException("Removing configuration at " + postUrl + " failed, cause: " + ex.getMessage(), ex);
} catch (IOException ex) {
throw new MojoExecutionException("Removing configuration at " + postUrl + " failed, cause: " + ex.getMessage(), ex);
} finally {
post.releaseConnection();
}
}
Aggregations