Search in sources :

Example 31 with HttpOptions

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

the class CrossOriginSimpleTest method preflightPostClassAnnotationPass2.

@Test
public void preflightPostClassAnnotationPass2() throws ClientProtocolException, IOException {
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpOptions httpoptions = new HttpOptions("http://localhost:" + PORT + "/antest/unannotatedPost");
    httpoptions.addHeader("Origin", "http://area51.mil:31415");
    httpoptions.addHeader("Content-Type", "application/json");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "POST");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_HEADERS, "X-custom-1, X-custom-2");
    HttpResponse response = httpclient.execute(httpoptions);
    assertEquals(200, response.getStatusLine().getStatusCode());
    Header[] origin = response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_ORIGIN);
    assertEquals(1, origin.length);
    assertEquals("http://area51.mil:31415", origin[0].getValue());
    Header[] method = response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS);
    assertEquals(1, method.length);
    assertEquals("POST", method[0].getValue());
    Header[] requestHeaders = response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS);
    assertEquals(1, requestHeaders.length);
    assertTrue(requestHeaders[0].getValue().contains("X-custom-1"));
    assertTrue(requestHeaders[0].getValue().contains("X-custom-2"));
    if (httpclient instanceof Closeable) {
        ((Closeable) httpclient).close();
    }
}
Also used : Header(org.apache.http.Header) 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 32 with HttpOptions

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

the class CrossOriginSimpleTest method simplePostClassAnnotation.

@Test
public void simplePostClassAnnotation() 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", "text/plain");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "POST");
    HttpResponse response = httpclient.execute(httpoptions);
    assertEquals(200, response.getStatusLine().getStatusCode());
    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 33 with HttpOptions

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

the class CrossOriginSimpleTest method testAnnotatedMethodPreflight2.

@Test
public void testAnnotatedMethodPreflight2() 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 + "/untest/annotatedPut2");
    // this is the origin we expect to get.
    http.addHeader("Origin", "http://area51.mil:31415");
    http.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "PUT");
    http.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_HEADERS, "X-custom-1, x-custom-2");
    HttpResponse response = httpclient.execute(http);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertOriginResponse(false, new String[] { "http://area51.mil:31415" }, true, response);
    assertAllowCredentials(response, true);
    List<String> exposeHeadersValues = headerValues(response.getHeaders(CorsHeaderConstants.HEADER_AC_EXPOSE_HEADERS));
    // preflight never returns Expose-Headers
    assertEquals(Collections.emptyList(), exposeHeadersValues);
    List<String> allowHeadersValues = headerValues(response.getHeaders(CorsHeaderConstants.HEADER_AC_ALLOW_HEADERS));
    assertEquals(Arrays.asList(new String[] { "X-custom-1", "x-custom-2" }), allowHeadersValues);
    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 34 with HttpOptions

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

the class CrossOriginSimpleTest method preflightPostClassAnnotationFail2.

@Test
public void preflightPostClassAnnotationFail2() throws ClientProtocolException, IOException {
    HttpClient httpclient = HttpClientBuilder.create().build();
    HttpOptions httpoptions = new HttpOptions("http://localhost:" + PORT + "/antest/unannotatedPost");
    httpoptions.addHeader("Origin", "http://area51.mil:31415");
    httpoptions.addHeader("Content-Type", "application/json");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_METHOD, "POST");
    httpoptions.addHeader(CorsHeaderConstants.HEADER_AC_REQUEST_HEADERS, "X-custom-3");
    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 35 with HttpOptions

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

the class RestServerTest method testOptionsDoesNotIncludeWadlOutput.

@Test
public void testOptionsDoesNotIncludeWadlOutput() throws IOException {
    Map<String, String> configMap = new HashMap<>(baseWorkerProps());
    DistributedConfig workerConfig = new DistributedConfig(configMap);
    doReturn(KAFKA_CLUSTER_ID).when(herder).kafkaClusterId();
    doReturn(plugins).when(herder).plugins();
    doReturn(Collections.emptyList()).when(plugins).newPlugins(Collections.emptyList(), workerConfig, ConnectRestExtension.class);
    server = new RestServer(workerConfig);
    server.initializeServer();
    server.initializeResources(herder);
    HttpOptions request = new HttpOptions("/connectors");
    request.addHeader("Content-Type", MediaType.WILDCARD);
    CloseableHttpClient httpClient = HttpClients.createMinimal();
    HttpHost httpHost = new HttpHost(server.advertisedUrl().getHost(), server.advertisedUrl().getPort());
    CloseableHttpResponse response = httpClient.execute(httpHost, request);
    Assert.assertEquals(MediaType.TEXT_PLAIN, response.getEntity().getContentType().getValue());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    response.getEntity().writeTo(baos);
    Assert.assertArrayEquals(request.getAllowedMethods(response).toArray(), new String(baos.toByteArray(), StandardCharsets.UTF_8).split(", "));
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) DistributedConfig(org.apache.kafka.connect.runtime.distributed.DistributedConfig) HttpOptions(org.apache.http.client.methods.HttpOptions) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

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