public DefaultClientRequestBuilder(ClientRequest other) { Assert.notNull(other, "ClientRequest must not be null"); this.method = other.method(); this.url = other.url(); headers(headers -> headers.addAll(other.headers())); cookies(cookies -> cookies.addAll(other.cookies())); attributes(attributes -> attributes.putAll(other.attributes())); body(other.body()); }
@Test public void cookie() { ClientRequest result = ClientRequest.create(GET, URI.create("http://example.com")) .cookie("foo", "bar").build(); assertEquals("bar", result.cookies().getFirst("foo")); }
@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 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); }
private Mono<ClientResponse> redirectIfNecessary(ClientRequest request, ExchangeFunction next, ClientResponse response) { URI location = response.headers().asHttpHeaders().getLocation(); String host = request.url().getHost(); String scheme = request.url().getScheme(); if (location != null) { String redirectUrl = location.toASCIIString(); if (location.getHost() == null) { redirectUrl = scheme+ "://" + host + location.toASCIIString(); } ClientRequest redirect = ClientRequest.method(HttpMethod.GET, URI.create(redirectUrl)) .headers(headers -> headers.addAll(request.headers())) .cookies(cookies -> cookies.addAll(request.cookies())) .attributes(attributes -> attributes.putAll(request.attributes())) .build(); return next.exchange(redirect).flatMap( r -> redirectIfNecessary(request, next, r)); } return Mono.just(response); } }
@Test public void from() throws URISyntaxException { ClientRequest other = ClientRequest.create(GET, URI.create("http://example.com")) .header("foo", "bar") .cookie("baz", "qux").build(); ClientRequest result = ClientRequest.from(other) .headers(httpHeaders -> httpHeaders.set("foo", "baar")) .cookies(cookies -> cookies.set("baz", "quux")) .build(); assertEquals(new URI("http://example.com"), result.url()); assertEquals(GET, result.method()); assertEquals(1, result.headers().size()); assertEquals("baar", result.headers().getFirst("foo")); assertEquals(1, result.cookies().size()); assertEquals("quux", result.cookies().getFirst("baz")); }
@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 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 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); }
@Override public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) { URI originalUrl = request.url(); String serviceId = originalUrl.getHost(); if(serviceId == null) { String msg = String.format("Request URI does not contain a valid hostname: %s", originalUrl.toString()); logger.warn(msg); return Mono.just(ClientResponse.create(HttpStatus.BAD_REQUEST).body(msg).build()); } //TODO: reactive lb client ServiceInstance instance = this.loadBalancerClient.choose(serviceId); if(instance == null) { String msg = String.format("Load balancer does not contain an instance for the service %s", serviceId); logger.warn(msg); return Mono.just(ClientResponse.create(HttpStatus.SERVICE_UNAVAILABLE).body(msg).build()); } URI uri = this.loadBalancerClient.reconstructURI(instance, originalUrl); ClientRequest newRequest = ClientRequest.method(request.method(), uri) .headers(headers -> headers.addAll(request.headers())) .cookies(cookies -> cookies.addAll(request.cookies())) .attributes(attributes -> attributes.putAll(request.attributes())) .body(request.body()) .build(); return next.exchange(newRequest); }
/** * Create a builder with the method, URI, headers, and cookies of the given request. * * @param other the request to copy the method, URI, headers, and cookies from * @return the created builder */ static BodyBuilder from(ClientRequest<?> other) { Assert.notNull(other, "'other' must not be null"); return new DefaultClientRequestBuilder(other.method(), other.url()) .headers(other.headers()) .cookies(other.cookies()); }
@Override public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) { URI originalUrl = request.url(); String serviceId = originalUrl.getHost(); if(serviceId == null) { String msg = String.format("Request URI does not contain a valid hostname: %s", originalUrl.toString()); logger.warn(msg); return Mono.just(ClientResponse.create(HttpStatus.BAD_REQUEST).body(msg).build()); } //TODO: reactive lb client ServiceInstance instance = this.loadBalancerClient.choose(serviceId); if(instance == null) { String msg = String.format("Load balancer does not contain an instance for the service %s", serviceId); logger.warn(msg); return Mono.just(ClientResponse.create(HttpStatus.SERVICE_UNAVAILABLE).body(msg).build()); } URI uri = this.loadBalancerClient.reconstructURI(instance, originalUrl); ClientRequest newRequest = ClientRequest.method(request.method(), uri) .headers(headers -> headers.addAll(request.headers())) .cookies(cookies -> cookies.addAll(request.cookies())) .attributes(attributes -> attributes.putAll(request.attributes())) .body(request.body()) .build(); return next.exchange(newRequest); }