private BodyInserter<?, ? super ClientHttpRequest> buildBody(Object[] args) { if (bodyIndex == null) { return BodyInserters.empty(); } Object body = args[bodyIndex]; if (isDataBufferPublisher(requestBodyType)) { return BodyInserters.fromDataBuffers((Publisher<DataBuffer>) body); } else if (isPublisher(requestBodyType)) { return BodyInserters.fromPublisher((Publisher) body, requestBodyType.getGeneric(0).getRawClass()); } else if (isResource(requestBodyType)) { return BodyInserters.fromResource((Resource) body); } else if (isFormData(requestBodyType)) { return BodyInserters.fromFormData((MultiValueMap<String, String>) body); } else { return BodyInserters.fromObject(body); } }
@Test public void justLoginRequestShouldLogin() { ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/auth/cert/login"); MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); response.getHeaders().setContentType(MediaType.APPLICATION_JSON); response.setBody("{" + "\"auth\":{\"client_token\":\"my-token\", \"renewable\": true, \"lease_duration\": 10}" + "}"); ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then( Mono.just(response)); WebClient webClient = WebClient.builder().clientConnector(connector).build(); AuthenticationSteps steps = AuthenticationSteps.just(post("/auth/{path}/login", "cert").as(VaultResponse.class)); StepVerifier.create(login(steps, webClient)) .expectNext(VaultToken.of("my-token")).verifyComplete(); }
@Test public void justLoginShouldFail() { ClientHttpRequest request = new MockClientHttpRequest(HttpMethod.POST, "/auth/cert/login"); MockClientHttpResponse response = new MockClientHttpResponse( HttpStatus.BAD_REQUEST); ClientHttpConnector connector = (method, uri, fn) -> fn.apply(request).then( Mono.just(response)); WebClient webClient = WebClient.builder().clientConnector(connector).build(); AuthenticationSteps steps = AuthenticationSteps.just(post("/auth/{path}/login", "cert").as(VaultResponse.class)); StepVerifier.create(login(steps, webClient)).expectError().verify(); }
public DefaultRequest(UriBuilder uriBuilder, HttpMethod httpMethod, HttpHeaders httpHeaders, Map<String, Object> variables, BodyInserter<?, ? super ClientHttpRequest> bodyInserter) { this.uriBuilder = uriBuilder; this.httpMethod = httpMethod; this.bodyInserter = bodyInserter; this.variables = new HashMap<>(); this.variables.putAll(variables); this.httpHeaders = new HttpHeaders(); this.httpHeaders.putAll(httpHeaders); }
protected Mono<ClientResponse> forward(String instanceId, URI uri, HttpMethod method, HttpHeaders headers, Supplier<BodyInserter<?, ? super ClientHttpRequest>> bodyInserter) { log.trace("Proxy-Request for instance {} / {}", instanceId, uri); WebClient.RequestBodySpec bodySpec = instanceWebClient.instance(registry.getInstance(InstanceId.of(instanceId))) .method(method) .uri(uri) .headers(instanceHeaders -> instanceHeaders.addAll( filterHeaders(headers))); WebClient.RequestHeadersSpec<?> headersSpec = bodySpec; if (requiresBody(method)) { try { headersSpec = bodySpec.body(bodyInserter.get()); } catch (Exception ex) { return Mono.error(ex); } } return headersSpec.exchange() .onErrorMap(InstanceWebClientException.class, error -> new ResponseStatusException(HttpStatus.BAD_REQUEST, null, error)) .onErrorMap(ConnectException.class, error -> new ResponseStatusException(HttpStatus.BAD_GATEWAY, null, error)); }
@Override public BodyInserter<?, ? super ClientHttpRequest> bodyInserter() { return bodyInserter; }
BodyInserter<?, ? super ClientHttpRequest> bodyInserter();