Java Code Examples for org.apache.http.client.methods.HttpOptions#setHeader()

The following examples show how to use org.apache.http.client.methods.HttpOptions#setHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OAuthClient.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public CloseableHttpResponse doPreflightRequest() {
    try (CloseableHttpClient client = httpClient.get()) {
        HttpOptions options = new HttpOptions(getAccessTokenUrl());
        options.setHeader("Origin", "http://example.com");

        return client.execute(options);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}
 
Example 2
Source File: AdminPreflightTest.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Test
public void testPreflight() throws IOException {
    HttpOptions options = new HttpOptions(getAdminUrl("realms/master/users"));
    options.setHeader("Origin", "http://test");

    CloseableHttpResponse response = client.execute(options);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals("true", response.getFirstHeader(Cors.ACCESS_CONTROL_ALLOW_CREDENTIALS).getValue());
    assertEquals("DELETE, POST, GET, PUT", response.getFirstHeader(Cors.ACCESS_CONTROL_ALLOW_METHODS).getValue());
    assertEquals("http://test", response.getFirstHeader(Cors.ACCESS_CONTROL_ALLOW_ORIGIN).getValue());
    assertEquals("3600", response.getFirstHeader(Cors.ACCESS_CONTROL_MAX_AGE).getValue());
    assertTrue(response.getFirstHeader(Cors.ACCESS_CONTROL_ALLOW_HEADERS).getValue().contains("Authorization"));
    assertTrue(response.getFirstHeader(Cors.ACCESS_CONTROL_ALLOW_HEADERS).getValue().contains("Content-Type"));
}
 
Example 3
Source File: AuthenticationIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testOptionsRequestDoesNotRequireAuth() throws Exception {
    HttpServerTransport httpTransport = internalCluster().getInstance(HttpServerTransport.class);
    InetSocketAddress address = httpTransport.boundAddress().publishAddress().address();
    String uri = String.format(Locale.ENGLISH, "http://%s:%s/", address.getHostName(), address.getPort());
    HttpOptions request = new HttpOptions(uri);
    request.setHeader(HttpHeaderNames.AUTHORIZATION.toString(), "Basic QXJ0aHVyOkV4Y2FsaWJ1cg==");
    request.setHeader(HttpHeaderNames.ORIGIN.toString(), "http://example.com");
    request.setHeader(HttpHeaderNames.ACCESS_CONTROL_REQUEST_METHOD.toString(), "GET");
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse resp = httpClient.execute(request);
    assertThat(resp.getStatusLine().getReasonPhrase(), is("OK"));
}
 
Example 4
Source File: CrossOriginResourceSharingResponseTest.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Test
public void testCorsPreflightNegative() throws Exception {
    // No CORS headers
    HttpOptions request = new HttpOptions(presignedGET);
    HttpResponse response = httpClient.execute(request);
    /*
     * For non presigned URLs that should give a 400, but the
     * Access-Control-Request-Method header is needed for presigned URLs
     * to calculate the same signature. If this is missing it fails already
     * with 403 - Signature mismatch before processing the OPTIONS request
     * See testCorsPreflightPublicRead for that cases
     */
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_FORBIDDEN);

    // Not allowed origin
    request.reset();
    request.setHeader(HttpHeaders.ORIGIN, "https://example.org");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    response = httpClient.execute(request);
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_FORBIDDEN);

    // Not allowed method
    request.reset();
    request.setHeader(HttpHeaders.ORIGIN, "https://example.com");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PATCH");
    response = httpClient.execute(request);
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_FORBIDDEN);

    // Not allowed header
    request.reset();
    request.setHeader(HttpHeaders.ORIGIN, "https://example.com");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS,
          "Accept-Encoding");
    response = httpClient.execute(request);
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_FORBIDDEN);

    // Not allowed header combination
    request.reset();
    request.setHeader(HttpHeaders.ORIGIN, "https://example.com");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS,
            "Accept, Accept-Encoding");
    response = httpClient.execute(request);
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_FORBIDDEN);
}
 
Example 5
Source File: CrossOriginResourceSharingResponseTest.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Test
public void testCorsPreflight() throws Exception {
    // Allowed origin and method
    HttpOptions request = new HttpOptions(presignedGET);
    request.setHeader(HttpHeaders.ORIGIN, "https://example.com");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    HttpResponse response = httpClient.execute(request);
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_OK);
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN).getValue())
            .isEqualTo("https://example.com");
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS).getValue())
            .isEqualTo("GET, PUT");

    // Allowed origin, method and header
    request.reset();
    request.setHeader(HttpHeaders.ORIGIN, "https://example.com");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Accept");
    response = httpClient.execute(request);
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_OK);
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN).getValue())
            .isEqualTo("https://example.com");
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS).getValue())
            .isEqualTo("GET, PUT");
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS).getValue())
            .isEqualTo("Accept");

    // Allowed origin, method and header combination
    request.reset();
    request.setHeader(HttpHeaders.ORIGIN, "https://example.com");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS,
            "Accept, Content-Type");
    response = httpClient.execute(request);
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_OK);
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN).getValue())
            .isEqualTo("https://example.com");
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS).getValue())
            .isEqualTo("GET, PUT");
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS).getValue())
            .isEqualTo("Accept, Content-Type");
}
 
Example 6
Source File: CrossOriginResourceSharingResponseTest.java    From s3proxy with Apache License 2.0 4 votes vote down vote up
@Test
public void testCorsPreflightPublicRead() throws Exception {
    // No CORS headers
    HttpOptions request = new HttpOptions(publicGET);
    HttpResponse response = httpClient.execute(request);

    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_BAD_REQUEST);

    // Not allowed method
    request.reset();
    request.setHeader(HttpHeaders.ORIGIN, "https://example.com");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "PATCH");
    response = httpClient.execute(request);
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_BAD_REQUEST);

    // Allowed origin and method
    request.reset();
    request.setHeader(HttpHeaders.ORIGIN, "https://example.com");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET");
    request.setHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS,
            "Accept, Content-Type");
    response = httpClient.execute(request);
    assertThat(response.getStatusLine().getStatusCode())
            .isEqualTo(HttpStatus.SC_OK);
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN).getValue())
            .isEqualTo("https://example.com");
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS).getValue())
            .isEqualTo("GET, PUT");
    assertThat(response.containsHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS)).isTrue();
    assertThat(response.getFirstHeader(
            HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS).getValue())
            .isEqualTo("Accept, Content-Type");
}