Search in sources :

Example 1 with Headers

use of io.helidon.common.http.Headers in project helidon by oracle.

the class TestCORS method testNamedGreetWithCors.

// Run after CORS test changes greeting to Cheers.
@Order(12)
@Test
void testNamedGreetWithCors() {
    WebClientRequestBuilder builder = client.get();
    Headers headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    WebClientResponse r = getResponse("/greet/Maria", builder);
    assertThat("HTTP response", r.status().code(), is(200));
    assertThat(fromPayload(r), containsString("Cheers Maria"));
    headers = r.headers();
    Optional<String> allowOrigin = headers.value(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat("Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " presence check", allowOrigin.isPresent(), is(true));
    assertThat(allowOrigin.get(), is("*"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 2 with Headers

use of io.helidon.common.http.Headers in project helidon by oracle.

the class TestCORS method testAnonymousGreetWithCors.

// Run after the non-CORS tests (so the greeting is Hola) but before the CORS test that changes the greeting again.
@Order(10)
@Test
void testAnonymousGreetWithCors() {
    WebClientRequestBuilder builder = client.get();
    Headers headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    WebClientResponse r = getResponse("/greet", builder);
    assertThat("HTTP response", r.status().code(), is(200));
    String payload = fromPayload(r);
    assertThat("HTTP response payload", payload, is("Hola World!"));
    headers = r.headers();
    Optional<String> allowOrigin = headers.value(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat("Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " is present", allowOrigin.isPresent(), is(true));
    assertThat("CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigin.get(), is("*"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 3 with Headers

use of io.helidon.common.http.Headers in project helidon by oracle.

the class TestCORS method testGreetingChangeWithCors.

// Run after the non-CORS tests but before other CORS tests.
@Order(11)
@Test
void testGreetingChangeWithCors() {
    // Send the pre-flight request and check the response.
    WebClientRequestBuilder builder = client.method("OPTIONS");
    Headers headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    headers.add("Access-Control-Request-Method", "PUT");
    WebClientResponse r = builder.path("/greet/greeting").submit().await();
    assertThat("pre-flight status", r.status().code(), is(200));
    Headers preflightResponseHeaders = r.headers();
    List<String> allowMethods = preflightResponseHeaders.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_METHODS);
    assertThat("pre-flight response check for " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_METHODS, allowMethods, is(not(empty())));
    assertThat("Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_METHODS, allowMethods, contains("PUT"));
    List<String> allowOrigins = preflightResponseHeaders.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat("pre-flight response check for " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigins, is(not(empty())));
    assertThat("Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigins, contains("http://foo.com"));
    // Send the follow-up request.
    builder = client.put();
    headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    headers.addAll(preflightResponseHeaders);
    r = putResponse("/greet/greeting", "Cheers", builder);
    assertThat("HTTP response3", r.status().code(), is(204));
    headers = r.headers();
    allowOrigins = headers.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertThat("Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigins, is(not(empty())));
    assertThat("Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN, allowOrigins, contains("http://foo.com"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) Matchers.containsString(org.hamcrest.Matchers.containsString) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 4 with Headers

use of io.helidon.common.http.Headers in project helidon by oracle.

the class TestCORS method testGreetingChangeWithCorsAndOtherOrigin.

// After all other tests so we can rely on deterministic greetings.
@Order(100)
@Test
void testGreetingChangeWithCorsAndOtherOrigin() {
    WebClientRequestBuilder builder = client.put();
    Headers headers = builder.headers();
    headers.add("Origin", "http://other.com");
    headers.add("Host", "here.com");
    WebClientResponse r = putResponse("/greet/greeting", "Ahoy", builder);
    // Result depends on whether we are using overrides or not.
    boolean isOverriding = Config.create().get("cors").exists();
    assertThat("HTTP response3", r.status().code(), is(isOverriding ? 204 : 403));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 5 with Headers

use of io.helidon.common.http.Headers in project helidon by oracle.

the class MainTest method testNamedGreetWithCors.

// Run after CORS test changes greeting to Cheers.
@Order(12)
@Test
void testNamedGreetWithCors() {
    WebClientRequestBuilder builder = webClient.get();
    Headers headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    WebClientResponse r = getResponse("/greet/Maria", builder);
    assertEquals(200, r.status().code(), "HTTP response");
    assertTrue(fromPayload(r).getMessage().contains("Cheers Maria"));
    headers = r.headers();
    Optional<String> allowOrigin = headers.value(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertTrue(allowOrigin.isPresent(), "Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " is absent");
    assertEquals(allowOrigin.get(), "*");
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Headers(io.helidon.common.http.Headers) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Aggregations

Headers (io.helidon.common.http.Headers)25 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)24 WebClientResponse (io.helidon.webclient.WebClientResponse)24 Test (org.junit.jupiter.api.Test)23 Order (org.junit.jupiter.api.Order)8 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)8 Http (io.helidon.common.http.Http)4 Matchers.containsString (org.hamcrest.Matchers.containsString)3 WebClient (io.helidon.webclient.WebClient)2 WebClientResponseHeaders (io.helidon.webclient.WebClientResponseHeaders)2 WebServer (io.helidon.webserver.WebServer)2 GenericType (io.helidon.common.GenericType)1 Context (io.helidon.common.context.Context)1 Contexts (io.helidon.common.context.Contexts)1 DataChunk (io.helidon.common.http.DataChunk)1 HttpRequest (io.helidon.common.http.HttpRequest)1 MediaType (io.helidon.common.http.MediaType)1 Parameters (io.helidon.common.http.Parameters)1 Single (io.helidon.common.reactive.Single)1 MessageBodyReadableContent (io.helidon.media.common.MessageBodyReadableContent)1