@Test public void postWorks() { Mono<Map> result = webClient.post() .uri("/post") .header("Host", "www.example.org") .syncBody("testdata") .exchange() .flatMap(response -> response.body(toMono(Map.class))); StepVerifier.create(result) .consumeNextWith(map -> assertThat(map).containsEntry("data", "testdata")) .expectComplete() .verify(DURATION); }
@Test public void should_translate_error_to_polish() { // when ClientResponse response = WebClient.create().post() .uri("http://localhost:" + port + "/validate") .header("Accept-Language", "pl-PL") .body(Mono.just(new TestBody()), TestBody.class) .exchange() .block(); // then Map body = response.bodyToMono(Map.class).block(); String message = (String) ((Map) (((List) body.get("errors")).get(0))).get("defaultMessage"); Assertions.assertThat(message).isEqualTo("musi być podane"); }
private Mono<Object> doHttpRequest(HttpRequestNode<Object> step, Object state) { HttpRequest<Object> definition = step.getDefinition(); HttpEntity<?> entity = getEntity(definition.getEntity(), state); RequestBodySpec spec; if (definition.getUri() == null) { spec = webClient.method(definition.getMethod()).uri( definition.getUriTemplate(), (Object[]) definition.getUrlVariables()); } else { spec = webClient.method(definition.getMethod()).uri(definition.getUri()); } for (Entry<String, List<String>> header : entity.getHeaders().entrySet()) { spec = spec.header(header.getKey(), header.getValue().get(0)); } if (entity.getBody() != null && !entity.getBody().equals(Undefinded.INSTANCE)) { return spec.syncBody(entity.getBody()).retrieve() .bodyToMono(definition.getResponseType()); } return spec.retrieve().bodyToMono(definition.getResponseType()); }
@Test public void should_translate_error_to_polish() { // when ClientResponse response = WebClient.create().post() .uri("http://localhost:" + port + "/validate") .header("Accept-Language", "pl-PL") .body(Mono.just(new TestBody()), TestBody.class) .exchange() .block(); // then Map body = response.bodyToMono(Map.class).block(); String message = (String) ((Map) (((List) body.get("errors")).get(0))).get("defaultMessage"); Assertions.assertThat(message).isEqualTo("musi być podane"); }
private Flux<GraphAlgorithmId> proxyConfigure(Set<String> groupMembers, String appId, GraphAlgorithmCreateRequest input) { Flux<GraphAlgorithmId> flux = Flux.fromIterable(groupMembers) .filter(s -> !s.equals(getHostAndPort())) .flatMap(s -> { log.debug("proxy configure to {}", s); WebClient client = WebClient.create("http://" + s); return client.post().uri("/pregel") .accept(MediaType.APPLICATION_JSON) .header(X_KGRAPH_APPID, appId) .body(Mono.just(input), GraphAlgorithmCreateRequest.class) .retrieve() .bodyToMono(GraphAlgorithmId.class); }); return flux; }
private Flux<GraphAlgorithmStatus> proxyRun(Set<String> groupMembers, String appId, GraphAlgorithmRunRequest input) { Flux<GraphAlgorithmStatus> flux = Flux.fromIterable(groupMembers) .filter(s -> !s.equals(getHostAndPort())) .flatMap(s -> { log.debug("proxy run to {}", s); WebClient client = WebClient.create("http://" + s); return client.post().uri("/pregel/" + appId) .accept(MediaType.APPLICATION_JSON) .header(X_KGRAPH_APPID, appId) .body(Mono.just(input), GraphAlgorithmRunRequest.class) .retrieve() .bodyToMono(GraphAlgorithmStatus.class); }); return flux; }