Search in sources :

Example 11 with HttpOptions

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

the class TestCorsHttpServer method doTestOption.

@Test
public void doTestOption() throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html");
    CloseableHttpResponse response = httpclient.execute(httpOption);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    Assert.assertNotNull("Cors should have added a header: Access-Control-Allow-Origin", response.getLastHeader("Access-Control-Allow-Origin"));
    Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Origin: *", "*", response.getLastHeader("Access-Control-Allow-Origin").getValue());
    response.close();
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpOptions(org.apache.http.client.methods.HttpOptions) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 12 with HttpOptions

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

the class TestCorsHttpServer method testAccessControlAllowHeaderUsesSystemPropertyWhenSet.

@Test
public void testAccessControlAllowHeaderUsesSystemPropertyWhenSet() throws Exception {
    Assert.assertNull("no System " + SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME + " shoudl be set", System.getProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME));
    final String expectedValue = "origin";
    System.setProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME, expectedValue);
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpOptions httpOption = new HttpOptions("http://localhost:9090/xxx/yyy.html");
        CloseableHttpResponse response = httpclient.execute(httpOption);
        Assert.assertEquals(200, response.getStatusLine().getStatusCode());
        Assert.assertEquals("Cors should have added a header: Access-Control-Allow-Headers: " + expectedValue, expectedValue, response.getLastHeader("Access-Control-Allow-Headers").getValue());
        response.close();
    } finally {
        System.clearProperty(SimpleWebServer.ACCESS_CONTROL_ALLOW_HEADER_PROPERTY_NAME);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpOptions(org.apache.http.client.methods.HttpOptions) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Test(org.junit.Test)

Example 13 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)

Example 14 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 15 with HttpOptions

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

the class FusekiTest method execOptions.

/** Do an HTTP Options. */
public static String execOptions(String url) {
    // Prepare and execute
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
        HttpUriRequest request = new HttpOptions(url);
        HttpResponse response = httpClient.execute(request, (HttpContext) null);
        // Response
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (HttpSC.isClientError(statusCode) || HttpSC.isServerError(statusCode)) {
            // Error responses can have bodies so it is important to clear up.
            String contentPayload = "";
            if (response.getEntity() != null)
                contentPayload = EntityUtils.toString(response.getEntity());
            throw new HttpException(statusCode, statusLine.getReasonPhrase(), contentPayload);
        }
        HttpResponseLib.nullResponse.handle(url, response);
        return response.getFirstHeader(HttpNames.hAllow).getValue();
    } catch (IOException ex) {
        throw new HttpException(ex);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.jena.atlas.web.HttpException) IOException(java.io.IOException)

Aggregations

HttpOptions (org.apache.http.client.methods.HttpOptions)15 Test (org.junit.Test)8 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)7 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)6 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 HttpHead (org.apache.http.client.methods.HttpHead)5 TestRequest (com.android.volley.mock.TestRequest)4 HttpPatch (org.apache.http.client.methods.HttpPatch)4 HttpPost (org.apache.http.client.methods.HttpPost)4 HttpPut (org.apache.http.client.methods.HttpPut)4 HttpTrace (org.apache.http.client.methods.HttpTrace)4 HttpGet (org.apache.http.client.methods.HttpGet)3 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URI (java.net.URI)2 Header (org.apache.http.Header)2 HttpEntity (org.apache.http.HttpEntity)2 HttpResponse (org.apache.http.HttpResponse)2 StatusLine (org.apache.http.StatusLine)2 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)2