Search in sources :

Example 81 with PostMethod

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();
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 82 with PostMethod

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);
    });
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) IOException(java.io.IOException)

Example 83 with PostMethod

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;
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Example 84 with PostMethod

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);
    }
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) PostMethod(org.apache.commons.httpclient.methods.PostMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials)

Example 85 with PostMethod

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();
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException)

Aggregations

PostMethod (org.apache.commons.httpclient.methods.PostMethod)203 HttpClient (org.apache.commons.httpclient.HttpClient)114 Test (org.junit.Test)55 IOException (java.io.IOException)32 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)28 Part (org.apache.commons.httpclient.methods.multipart.Part)25 NameValuePair (org.apache.commons.httpclient.NameValuePair)24 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)23 Map (java.util.Map)21 GetMethod (org.apache.commons.httpclient.methods.GetMethod)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)21 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)21 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 Header (org.apache.commons.httpclient.Header)16 Note (org.apache.zeppelin.notebook.Note)13 HttpException (org.apache.commons.httpclient.HttpException)12 File (java.io.File)11 InputStream (java.io.InputStream)11 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)11 JSONParser (org.json.simple.parser.JSONParser)11