Search in sources :

Example 46 with WebClientRequestBuilder

use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.

the class MaxPayloadSizeTest method testContentLengthExceededWithPayload.

/**
 * If content length is greater than max, a 413 must be returned.
 */
@Test
public void testContentLengthExceededWithPayload() {
    WebClientRequestBuilder builder = webClient.post();
    WebClientResponse response = builder.path("/maxpayload").contentType(MediaType.APPLICATION_OCTET_STREAM).submit(PAYLOAD).await(5, TimeUnit.SECONDS);
    assertThat(response.status().code(), is(Http.Status.REQUEST_ENTITY_TOO_LARGE_413.code()));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 47 with WebClientRequestBuilder

use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.

the class CompressionTest method testDeflateContent.

/**
 * Test that the entity is decompressed using the correct algorithm.
 *
 * @throws Exception if error occurs.
 */
@Test
public void testDeflateContent() throws Exception {
    WebClientRequestBuilder builder = webClient.get();
    builder.headers().add("Accept-Encoding", "deflate");
    WebClientResponse response = builder.path("/compressed").request().await(10, TimeUnit.SECONDS);
    assertThat(response.content().as(String.class).get(), equalTo("It works!"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 48 with WebClientRequestBuilder

use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.

the class CompressionTest method testGzipContent.

/**
 * Test that the entity is decompressed using the correct algorithm.
 *
 * @throws Exception if error occurs.
 */
@Test
public void testGzipContent() throws Exception {
    WebClientRequestBuilder builder = webClient.get();
    builder.headers().add("Accept-Encoding", "gzip");
    WebClientResponse response = builder.path("/compressed").request().await(10, TimeUnit.SECONDS);
    assertThat(response.content().as(String.class).get(), equalTo("It works!"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebClientRequestBuilder(io.helidon.webclient.WebClientRequestBuilder) Test(org.junit.jupiter.api.Test)

Example 49 with WebClientRequestBuilder

use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.

the class MainTest 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 = webClient.get();
    Headers headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    WebClientResponse r = getResponse("/greet", builder);
    assertEquals(200, r.status().code(), "HTTP response");
    String payload = fromPayload(r).getMessage();
    assertTrue(payload.contains("Hola World"), "HTTP response payload was " + payload);
    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)

Example 50 with WebClientRequestBuilder

use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.

the class MainTest 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 = webClient.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();
    Headers preflightResponseHeaders = r.headers();
    List<String> allowMethods = preflightResponseHeaders.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_METHODS);
    assertFalse(allowMethods.isEmpty(), "pre-flight response does not include " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_METHODS);
    assertTrue(allowMethods.contains("PUT"));
    List<String> allowOrigins = preflightResponseHeaders.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertFalse(allowOrigins.isEmpty(), "pre-flight response does not include " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertTrue(allowOrigins.contains("http://foo.com"), "Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " should contain '*' but does not; " + allowOrigins);
    // Send the follow-up request.
    builder = webClient.put();
    headers = builder.headers();
    headers.add("Origin", "http://foo.com");
    headers.add("Host", "here.com");
    headers.addAll(preflightResponseHeaders);
    r = putResponse("/greet/greeting", new GreetingMessage("Cheers"), builder);
    assertEquals(204, r.status().code(), "HTTP response3");
    headers = r.headers();
    allowOrigins = headers.values(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
    assertFalse(allowOrigins.isEmpty(), "Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " has no value(s)");
    assertTrue(allowOrigins.contains("http://foo.com"), "Header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " should contain '*' but does not; " + allowOrigins);
}
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

WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)50 WebClientResponse (io.helidon.webclient.WebClientResponse)37 Test (org.junit.jupiter.api.Test)34 Headers (io.helidon.common.http.Headers)24 Http (io.helidon.common.http.Http)9 WebClient (io.helidon.webclient.WebClient)9 JsonObject (jakarta.json.JsonObject)8 Order (org.junit.jupiter.api.Order)8 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)8 Single (io.helidon.common.reactive.Single)7 Logger (java.util.logging.Logger)7 Optional (java.util.Optional)6 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)5 Contexts (io.helidon.common.context.Contexts)4 Config (io.helidon.config.Config)4 URI (java.net.URI)4 DataChunk (io.helidon.common.http.DataChunk)3 MediaType (io.helidon.common.http.MediaType)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Flow (java.util.concurrent.Flow)3