Search in sources :

Example 16 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project cxf by apache.

the class CrossOriginSimpleTest method preflightPostClassAnnotationFail.

@Test
public void preflightPostClassAnnotationFail() throws ClientProtocolException, IOException {
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpOptions httpoptions = new HttpOptions("http://localhost:" + PORT + "/antest/unannotatedPost");
    httpoptions.addHeader("Origin", "http://in.org");
    // nonsimple header
    httpoptions.addHeader("Content-Type", "application/json");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "POST");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_HEADERS, "X-custom-1");
    HttpResponse response = httpclient.execute(httpoptions);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(0, response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN).length);
    assertEquals(0, response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS).length);
    assertEquals(0, response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS).length);
    if (httpclient instanceof Closeable) {
        ((Closeable) httpclient).close();
    }
}
Also used : HttpClient(org.apache.http.client.HttpClient) Closeable(java.io.Closeable) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 17 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project cxf by apache.

the class CrossOriginSimpleTest method testAnnotatedLocalPreflightNoGo.

@Test
public void testAnnotatedLocalPreflightNoGo() throws Exception {
    configureAllowOrigins(true, null);
    String r = configClient.replacePath("/setAllowCredentials/false").accept("text/plain").post(null, String.class);
    assertEquals("ok", r);
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpOptions http = new HttpOptions("http://localhost:" + PORT + "/antest/delete");
    // this is the origin we expect to get.
    http.addHeader("Origin", "http://area51.mil:4444");
    http.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "DELETE");
    HttpResponse response = httpclient.execute(http);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertOriginResponse(false, new String[] { "http://area51.mil:4444" }, false, response);
    // we could check that the others are also missing.
    if (httpclient instanceof Closeable) {
        ((Closeable) httpclient).close();
    }
}
Also used : HttpClient(org.apache.http.client.HttpClient) Closeable(java.io.Closeable) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 18 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project cxf by apache.

the class CrossOriginSimpleTest method testAnnotatedLocalPreflight.

@Test
public void testAnnotatedLocalPreflight() throws Exception {
    configureAllowOrigins(true, null);
    String r = configClient.replacePath("/setAllowCredentials/false").accept("text/plain").post(null, String.class);
    assertEquals("ok", r);
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpOptions http = new HttpOptions("http://localhost:" + PORT + "/antest/delete");
    // this is the origin we expect to get.
    http.addHeader("Origin", "http://area51.mil:3333");
    http.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "DELETE");
    HttpResponse response = httpclient.execute(http);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertOriginResponse(false, new String[] { "http://area51.mil:3333" }, true, response);
    assertAllowCredentials(response, false);
    List<String> exposeHeadersValues = headerValues(response.getHeaders(CorsHeaderConstants.HEADER_AC_EXPOSE_HEADERS));
    // preflight never returns Expose-Headers
    assertEquals(Collections.emptyList(), exposeHeadersValues);
    List<String> allowedMethods = headerValues(response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS));
    assertEquals(Arrays.asList("DELETE PUT"), allowedMethods);
    if (httpclient instanceof Closeable) {
        ((Closeable) httpclient).close();
    }
}
Also used : HttpClient(org.apache.http.client.HttpClient) Closeable(java.io.Closeable) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 19 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project ats-framework by Axway.

the class HttpClient method performHttpRequest.

/**
     * Performs some supported HTTP request. Currently <i>read only<i> requests
     * are supported: GET, HEAD and OPTIONS
     *
     * @param requestedHostRelativeUrl location/query without host and port like: "/my_dir/res?myParam=1"
     * @param httpMethodName any of the currently supported HTTP methods: GET, HEAD and OPTIONS
     * @param needResponse whether caller wants to get the contents returned from this request,
     * if false - then null is returned
     * @return the response content if present and requested by the caller
     * @throws FileTransferClientException
     */
public String performHttpRequest(String requestedHostRelativeUrl, String httpMethodName, boolean needResponse) throws FileTransferException {
    checkClientInitialized();
    final String getUrl = constructGetUrl(requestedHostRelativeUrl);
    HttpRequestBase httpMethod = null;
    if (!StringUtils.isNullOrEmpty(httpMethodName)) {
        httpMethodName = httpMethodName.trim().toUpperCase();
        switch(httpMethodName) {
            case "GET":
                httpMethod = new HttpGet(getUrl);
                break;
            case "HEAD":
                httpMethod = new HttpHead(getUrl);
                break;
            case "OPTIONS":
                httpMethod = new HttpOptions(getUrl);
                break;
        }
    }
    if (httpMethod == null) {
        throw new IllegalArgumentException("This method supports only GET, HEAD and OPTIONS methods while you have provided '" + httpMethodName + "'");
    }
    log.info("Performing " + httpMethodName + " request to: " + getUrl);
    addRequestHeaders(httpMethod);
    return (String) processHttpRequest(httpMethod, needResponse, true);
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpHead(org.apache.http.client.methods.HttpHead)

Example 20 with HttpOptions

use of org.apache.http.client.methods.HttpOptions in project spark by perwendel.

the class SparkTestUtil method getHttpRequest.

private HttpUriRequest getHttpRequest(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) {
    try {
        String protocol = secureConnection ? "https" : "http";
        String uri = protocol + "://localhost:" + port + path;
        if (requestMethod.equals("GET")) {
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpGet);
            return httpGet;
        }
        if (requestMethod.equals("POST")) {
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPost);
            httpPost.setEntity(new StringEntity(body));
            return httpPost;
        }
        if (requestMethod.equals("PATCH")) {
            HttpPatch httpPatch = new HttpPatch(uri);
            httpPatch.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPatch);
            httpPatch.setEntity(new StringEntity(body));
            return httpPatch;
        }
        if (requestMethod.equals("DELETE")) {
            HttpDelete httpDelete = new HttpDelete(uri);
            addHeaders(reqHeaders, httpDelete);
            httpDelete.setHeader("Accept", acceptType);
            return httpDelete;
        }
        if (requestMethod.equals("PUT")) {
            HttpPut httpPut = new HttpPut(uri);
            httpPut.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPut);
            httpPut.setEntity(new StringEntity(body));
            return httpPut;
        }
        if (requestMethod.equals("HEAD")) {
            HttpHead httpHead = new HttpHead(uri);
            addHeaders(reqHeaders, httpHead);
            return httpHead;
        }
        if (requestMethod.equals("TRACE")) {
            HttpTrace httpTrace = new HttpTrace(uri);
            addHeaders(reqHeaders, httpTrace);
            return httpTrace;
        }
        if (requestMethod.equals("OPTIONS")) {
            HttpOptions httpOptions = new HttpOptions(uri);
            addHeaders(reqHeaders, httpOptions);
            return httpOptions;
        }
        if (requestMethod.equals("LOCK")) {
            HttpLock httpLock = new HttpLock(uri);
            addHeaders(reqHeaders, httpLock);
            return httpLock;
        }
        throw new IllegalArgumentException("Unknown method " + requestMethod);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) StringEntity(org.apache.http.entity.StringEntity) HttpTrace(org.apache.http.client.methods.HttpTrace)

Aggregations

HttpOptions (org.apache.http.client.methods.HttpOptions)41 Test (org.junit.Test)25 HttpResponse (org.apache.http.HttpResponse)15 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)15 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 Closeable (java.io.Closeable)9 HttpClient (org.apache.http.client.HttpClient)9 HttpHead (org.apache.http.client.methods.HttpHead)9 HttpPost (org.apache.http.client.methods.HttpPost)8 HttpPut (org.apache.http.client.methods.HttpPut)8 Header (org.apache.http.Header)7 HttpGet (org.apache.http.client.methods.HttpGet)7 IOException (java.io.IOException)5 URI (java.net.URI)5 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)5 HttpTrace (org.apache.http.client.methods.HttpTrace)5 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)5 TestRequest (com.android.volley.mock.TestRequest)4 HttpDelete (org.apache.http.client.methods.HttpDelete)4 HttpPatch (org.apache.http.client.methods.HttpPatch)4