Search in sources :

Example 1 with HttpOptions

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

the class RestClientSingleHostTests method performRandomRequest.

private HttpUriRequest performRandomRequest(String method) throws Exception {
    String uriAsString = "/" + randomStatusCode(getRandom());
    URIBuilder uriBuilder = new URIBuilder(uriAsString);
    final Map<String, String> params = new HashMap<>();
    boolean hasParams = randomBoolean();
    if (hasParams) {
        int numParams = randomIntBetween(1, 3);
        for (int i = 0; i < numParams; i++) {
            String paramKey = "param-" + i;
            String paramValue = randomAsciiOfLengthBetween(3, 10);
            params.put(paramKey, paramValue);
            uriBuilder.addParameter(paramKey, paramValue);
        }
    }
    if (randomBoolean()) {
        //randomly add some ignore parameter, which doesn't get sent as part of the request
        String ignore = Integer.toString(randomFrom(RestClientTestUtil.getAllErrorStatusCodes()));
        if (randomBoolean()) {
            ignore += "," + Integer.toString(randomFrom(RestClientTestUtil.getAllErrorStatusCodes()));
        }
        params.put("ignore", ignore);
    }
    URI uri = uriBuilder.build();
    HttpUriRequest request;
    switch(method) {
        case "DELETE":
            request = new HttpDeleteWithEntity(uri);
            break;
        case "GET":
            request = new HttpGetWithEntity(uri);
            break;
        case "HEAD":
            request = new HttpHead(uri);
            break;
        case "OPTIONS":
            request = new HttpOptions(uri);
            break;
        case "PATCH":
            request = new HttpPatch(uri);
            break;
        case "POST":
            request = new HttpPost(uri);
            break;
        case "PUT":
            request = new HttpPut(uri);
            break;
        case "TRACE":
            request = new HttpTrace(uri);
            break;
        default:
            throw new UnsupportedOperationException("method not supported: " + method);
    }
    HttpEntity entity = null;
    boolean hasBody = request instanceof HttpEntityEnclosingRequest && getRandom().nextBoolean();
    if (hasBody) {
        entity = new StringEntity(randomAsciiOfLengthBetween(10, 100), ContentType.APPLICATION_JSON);
        ((HttpEntityEnclosingRequest) request).setEntity(entity);
    }
    Header[] headers = new Header[0];
    final Set<String> uniqueNames = new HashSet<>();
    if (randomBoolean()) {
        headers = RestClientTestUtil.randomHeaders(getRandom(), "Header");
        for (Header header : headers) {
            request.addHeader(header);
            uniqueNames.add(header.getName());
        }
    }
    for (Header defaultHeader : defaultHeaders) {
        // request level headers override default headers
        if (uniqueNames.contains(defaultHeader.getName()) == false) {
            request.addHeader(defaultHeader);
        }
    }
    try {
        if (hasParams == false && hasBody == false && randomBoolean()) {
            restClient.performRequest(method, uriAsString, headers);
        } else if (hasBody == false && randomBoolean()) {
            restClient.performRequest(method, uriAsString, params, headers);
        } else {
            restClient.performRequest(method, uriAsString, params, entity, headers);
        }
    } catch (ResponseException e) {
    //all good
    }
    return request;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HashMap(java.util.HashMap) HttpOptions(org.apache.http.client.methods.HttpOptions) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut) StringEntity(org.apache.http.entity.StringEntity) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) HashSet(java.util.HashSet) URIBuilder(org.apache.http.client.utils.URIBuilder) HttpTrace(org.apache.http.client.methods.HttpTrace) Header(org.apache.http.Header)

Example 2 with HttpOptions

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

the class RestClient method createHttpRequest.

private static HttpRequestBase createHttpRequest(String method, URI uri, HttpEntity entity) {
    switch(method.toUpperCase(Locale.ROOT)) {
        case HttpDeleteWithEntity.METHOD_NAME:
            return addRequestBody(new HttpDeleteWithEntity(uri), entity);
        case HttpGetWithEntity.METHOD_NAME:
            return addRequestBody(new HttpGetWithEntity(uri), entity);
        case HttpHead.METHOD_NAME:
            return addRequestBody(new HttpHead(uri), entity);
        case HttpOptions.METHOD_NAME:
            return addRequestBody(new HttpOptions(uri), entity);
        case HttpPatch.METHOD_NAME:
            return addRequestBody(new HttpPatch(uri), entity);
        case HttpPost.METHOD_NAME:
            HttpPost httpPost = new HttpPost(uri);
            addRequestBody(httpPost, entity);
            return httpPost;
        case HttpPut.METHOD_NAME:
            return addRequestBody(new HttpPut(uri), entity);
        case HttpTrace.METHOD_NAME:
            return addRequestBody(new HttpTrace(uri), entity);
        default:
            throw new UnsupportedOperationException("http method not supported: " + method);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpTrace(org.apache.http.client.methods.HttpTrace) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpHead(org.apache.http.client.methods.HttpHead) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut)

Example 3 with HttpOptions

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

the class HttpClientStackTest method testCreateOptionsRequest.

public void testCreateOptionsRequest() throws Exception {
    TestRequest.Options request = new TestRequest.Options();
    assertEquals(request.getMethod(), Method.OPTIONS);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpOptions);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpOptions(org.apache.http.client.methods.HttpOptions) TestRequest(com.android.volley.mock.TestRequest)

Example 4 with HttpOptions

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

the class RestServerTest method checkCORSRequest.

public void checkCORSRequest(String corsDomain, String origin, String expectedHeader, String method) throws IOException {
    Map<String, String> workerProps = baseWorkerProps();
    workerProps.put(WorkerConfig.ACCESS_CONTROL_ALLOW_ORIGIN_CONFIG, corsDomain);
    workerProps.put(WorkerConfig.ACCESS_CONTROL_ALLOW_METHODS_CONFIG, method);
    WorkerConfig workerConfig = new DistributedConfig(workerProps);
    doReturn(KAFKA_CLUSTER_ID).when(herder).kafkaClusterId();
    doReturn(plugins).when(herder).plugins();
    doReturn(Collections.emptyList()).when(plugins).newPlugins(Collections.emptyList(), workerConfig, ConnectRestExtension.class);
    doReturn(Arrays.asList("a", "b")).when(herder).connectors();
    server = new RestServer(workerConfig);
    server.initializeServer();
    server.initializeResources(herder);
    HttpRequest request = new HttpGet("/connectors");
    request.addHeader("Referer", origin + "/page");
    request.addHeader("Origin", origin);
    CloseableHttpClient httpClient = HttpClients.createMinimal();
    HttpHost httpHost = new HttpHost(server.advertisedUrl().getHost(), server.advertisedUrl().getPort());
    CloseableHttpResponse response = httpClient.execute(httpHost, request);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    if (expectedHeader != null) {
        Assert.assertEquals(expectedHeader, response.getFirstHeader("Access-Control-Allow-Origin").getValue());
    }
    request = new HttpOptions("/connector-plugins/FileStreamSource/validate");
    request.addHeader("Referer", origin + "/page");
    request.addHeader("Origin", origin);
    request.addHeader("Access-Control-Request-Method", method);
    response = httpClient.execute(httpHost, request);
    Assert.assertEquals(404, response.getStatusLine().getStatusCode());
    if (expectedHeader != null) {
        Assert.assertEquals(expectedHeader, response.getFirstHeader("Access-Control-Allow-Origin").getValue());
    }
    if (method != null) {
        Assert.assertEquals(method, response.getFirstHeader("Access-Control-Allow-Methods").getValue());
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpHost(org.apache.http.HttpHost) DistributedConfig(org.apache.kafka.connect.runtime.distributed.DistributedConfig) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpOptions(org.apache.http.client.methods.HttpOptions) WorkerConfig(org.apache.kafka.connect.runtime.WorkerConfig)

Example 5 with HttpOptions

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

the class HttpClientStackTest method createOptionsRequest.

@Test
public void createOptionsRequest() throws Exception {
    TestRequest.Options request = new TestRequest.Options();
    assertEquals(request.getMethod(), Method.OPTIONS);
    HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null);
    assertTrue(httpRequest instanceof HttpOptions);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpOptions(org.apache.http.client.methods.HttpOptions) HttpOptions(org.apache.http.client.methods.HttpOptions) TestRequest(com.android.volley.mock.TestRequest) Test(org.junit.Test)

Aggregations

HttpOptions (org.apache.http.client.methods.HttpOptions)38 Test (org.junit.Test)24 HttpResponse (org.apache.http.HttpResponse)14 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)12 Closeable (java.io.Closeable)9 HttpClient (org.apache.http.client.HttpClient)9 HttpHead (org.apache.http.client.methods.HttpHead)8 Header (org.apache.http.Header)7 HttpPost (org.apache.http.client.methods.HttpPost)7 HttpPut (org.apache.http.client.methods.HttpPut)7 HttpGet (org.apache.http.client.methods.HttpGet)6 URI (java.net.URI)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 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)4 StringEntity (org.apache.http.entity.StringEntity)4