@Test public void withNullAttribute() { Map<String, Object> actual = new HashMap<>(); ExchangeFilterFunction filter = (request, next) -> { actual.putAll(request.attributes()); return next.exchange(request); }; this.builder.filter(filter).build() .get().uri("/path") .attribute("foo", null) .exchange(); assertNull(actual.get("foo")); ClientRequest request = verifyAndGetRequest(); assertFalse(request.attribute("foo").isPresent()); }
@Test public void withStringAttribute() { Map<String, Object> actual = new HashMap<>(); ExchangeFilterFunction filter = (request, next) -> { actual.putAll(request.attributes()); return next.exchange(request); }; this.builder.filter(filter).build() .get().uri("/path") .attribute("foo", "bar") .exchange(); assertEquals("bar", actual.get("foo")); ClientRequest request = verifyAndGetRequest(); assertEquals("bar", request.attribute("foo").get()); }
@Test public void basic() { this.builder.build().get().uri("/path").exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("/base/path", request.url().toString()); assertEquals(new HttpHeaders(), request.headers()); assertEquals(Collections.emptyMap(), request.cookies()); }
@Test public void streaming() { Flux<Msg> result = this.webClient.get() .uri("/message-stream") .exchange() .doOnNext(response -> { Assert.assertEquals("true", response.headers().contentType().get().getParameters().get("delimited")); Assert.assertEquals("sample.proto", response.headers().header("X-Protobuf-Schema").get(0)); Assert.assertEquals("Msg", response.headers().header("X-Protobuf-Message").get(0)); }) .flatMapMany(response -> response.bodyToFlux(Msg.class)); StepVerifier.create(result) .expectNext(Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(0).build()).build()) .expectNext(Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(1).build()).build()) .thenCancel() .verify(); }
@Test public void value() { Mono<Msg> result = this.webClient.get() .uri("/message") .exchange() .doOnNext(response -> { Assert.assertFalse(response.headers().contentType().get().getParameters().containsKey("delimited")); Assert.assertEquals("sample.proto", response.headers().header("X-Protobuf-Schema").get(0)); Assert.assertEquals("Msg", response.headers().header("X-Protobuf-Message").get(0)); }) .flatMap(response -> response.bodyToMono(Msg.class)); StepVerifier.create(result) .expectNext(TEST_MSG) .verifyComplete(); }
@Test public void shouldReceive404Response() { prepareResponse(response -> response.setResponseCode(404) .setHeader("Content-Type", "text/plain").setBody("Not Found")); Mono<ClientResponse> result = this.webClient.get().uri("/greeting?name=Spring").exchange(); StepVerifier.create(result) .consumeNextWith(response -> assertEquals(HttpStatus.NOT_FOUND, response.statusCode())) .expectComplete() .verify(Duration.ofSeconds(3)); expectRequestCount(1); expectRequest(request -> { assertEquals("*/*", request.getHeader(HttpHeaders.ACCEPT)); assertEquals("/greeting?name=Spring", request.getPath()); }); }
@Test public void apply() { WebClient client = this.builder .apply(builder -> builder .defaultHeader("Accept", "application/json") .defaultCookie("id", "123")) .build(); client.get().uri("/path").exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("application/json", request.headers().getFirst("Accept")); assertEquals("123", request.cookies().getFirst("id")); verifyNoMoreInteractions(this.exchangeFunction); }
@Test public void fixedLocale() { Mono<ClientResponse> result = webClient .get() .uri("http://localhost:" + this.port + "/") .exchange(); StepVerifier .create(result) .consumeNextWith(response -> { assertEquals(HttpStatus.OK, response.statusCode()); assertEquals(Locale.GERMANY, response.headers().asHttpHeaders().getContentLanguage()); }) .verifyComplete(); }
@Test public void fixedLocale() { Mono<ClientResponse> result = webClient .get() .uri("http://localhost:" + this.port + "/") .exchange(); StepVerifier.create(result) .consumeNextWith(response -> { assertEquals(HttpStatus.OK, response.statusCode()); assertEquals(Locale.GERMANY, response.headers().asHttpHeaders().getContentLanguage()); }) .verifyComplete(); }
@Test public void switchToErrorOnEmptyClientResponseMono() { StepVerifier.create(builder.build().get().uri("/path").exchange()) .expectErrorMessage("The underlying HTTP client completed without emitting a response.") .verify(Duration.ofSeconds(5)); }
@Test public void defaultHeaderAndCookie() { WebClient client = this.builder .defaultHeader("Accept", "application/json").defaultCookie("id", "123") .build(); client.get().uri("/path").exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("application/json", request.headers().getFirst("Accept")); assertEquals("123", request.cookies().getFirst("id")); verifyNoMoreInteractions(this.exchangeFunction); }
@Test public void requestHeaderAndCookie() { this.builder.build().get().uri("/path").accept(MediaType.APPLICATION_JSON) .cookies(cookies -> cookies.add("id", "123")) // SPR-16178 .exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("application/json", request.headers().getFirst("Accept")); assertEquals("123", request.cookies().getFirst("id")); verifyNoMoreInteractions(this.exchangeFunction); }
@Test public void uriBuilder() { this.builder.build().get() .uri(builder -> builder.path("/path").queryParam("q", "12").build()) .exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("/base/path?q=12", request.url().toString()); verifyNoMoreInteractions(this.exchangeFunction); }
@Test public void parts() { Mono<ClientResponse> result = webClient .post() .uri("http://localhost:" + this.port + "/parts") .syncBody(generateBody()) .exchange(); StepVerifier .create(result) .consumeNextWith(response -> assertEquals(HttpStatus.OK, response.statusCode())) .verifyComplete(); }
@Test public void multipartData() { Mono<ClientResponse> result = webClient .post() .uri("http://localhost:" + this.port + "/multipartData") .syncBody(generateBody()) .exchange(); StepVerifier .create(result) .consumeNextWith(response -> assertEquals(HttpStatus.OK, response.statusCode())) .verifyComplete(); }
@Test public void defaultHeaderAndCookieOverrides() { WebClient client = this.builder .defaultHeader("Accept", "application/json") .defaultCookie("id", "123") .build(); client.get().uri("/path").header("Accept", "application/xml").cookie("id", "456").exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("application/xml", request.headers().getFirst("Accept")); assertEquals("456", request.cookies().getFirst("id")); verifyNoMoreInteractions(this.exchangeFunction); }
@Test public void uriBuilderWithPathOverride() { this.builder.build().get() .uri(builder -> builder.replacePath("/path").build()) .exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("/path", request.url().toString()); verifyNoMoreInteractions(this.exchangeFunction); }
@Test // SPR-15782 public void shouldFailWithRelativeUrls() { String uri = "/api/v4/groups/1"; Mono<ClientResponse> responseMono = WebClient.builder().build().get().uri(uri).exchange(); StepVerifier.create(responseMono) .expectErrorMessage("URI is not absolute: " + uri) .verify(Duration.ofSeconds(5)); }
@Test public void shouldReceiveEmptyResponse() { prepareResponse(response -> response.setHeader("Content-Length", "0").setBody("")); Mono<ResponseEntity<Void>> result = this.webClient.get() .uri("/noContent") .exchange() .flatMap(response -> response.toEntity(Void.class)); StepVerifier.create(result).assertNext(r -> { assertTrue(r.getStatusCode().is2xxSuccessful()); }).verifyComplete(); }
@Test public void requestPart() { Mono<ClientResponse> result = webClient .post() .uri("/requestPart") .syncBody(generateBody()) .exchange(); StepVerifier .create(result) .consumeNextWith(response -> assertEquals(HttpStatus.OK, response.statusCode())) .verifyComplete(); }