/** * Marshal value to json, for async http using. Default using block api to get all data once, and then publish. * * @param value the value * @param charset the charset * @return http body publisher */ HttpRequest.BodyPublisher asPublisher(Object value, Charset charset) { try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { marshal(value, bos, charset); return HttpRequest.BodyPublishers.ofByteArray(bos.toByteArray()); } catch (IOException e) { throw new UncheckedIOException(e); } } }
/** * Simulates a FORM post by setting: * * Content-Type to application/x-www-form-urlencoded * HTTP method to POST * URLEncoding of the given parameters * * @param parameters The parameters to use * @return TestResponse instance */ public TestResponse withForm(Multimap<String, String> parameters) { String form = parameters.entries() .stream() .map(entry -> entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), Charset.forName(Default.ENCODING.toString()))) .collect(Collectors.joining("&")); this.httpRequest.header(CONTENT_TYPE, "application/x-www-form-urlencoded"); this.body = BodyPublishers.ofString(form); this.method = Methods.POST.toString(); return this; }
@Override public BodyPublisher asBodyPublisher() { try { return BodyPublishers.ofFile(body()); } catch (FileNotFoundException e) { throw new UncheckedIOException(e); } } }
protected HttpRequest.BodyPublisher provideBody(ReactiveHttpRequest request) { if(bodyActualClass == null){ return HttpRequest.BodyPublishers.noBody(); } Publisher<ByteBuffer> bodyPublisher; if(request.body() instanceof Mono){ if(bodyActualClass == ByteBuffer.class){ bodyPublisher = (Mono)request.body(); } else if (CharSequence.class.isAssignableFrom(bodyActualClass)){ bodyPublisher = Flux.from(request.body()).map(this::toCharSequenceChunk); } else { bodyPublisher = Flux.from(request.body()).map(data -> toJsonChunk(data, false)); } } else { if(bodyActualClass == ByteBuffer.class){ bodyPublisher = (Publisher)request.body(); } else { bodyPublisher = Flux.from(request.body()).map(data -> toJsonChunk(data, true)); } } return HttpRequest.BodyPublishers.fromPublisher(publisherToFlowPublisher(bodyPublisher)); }
/** * Sets a String body to the request * * @param body The request body to use * * @return TestResponse instance */ public TestResponse withStringBody(String body) { if (StringUtils.isNotBlank(body)) { this.body = BodyPublishers.ofString(body); } return this; }
@Override public BodyPublisher asBodyPublisher() { List<BodyPublisher> publishers = new ArrayList<>(); for (Part part : body()) { publishers.add(BodyPublishers.ofString("--" + BOUNDARY + LINE_END, US_ASCII)); String header = part.headerData(); publishers.add(BodyPublishers.ofString(header, US_ASCII)); publishers.add(part.asPublisher()); publishers.add(BodyPublishers.ofByteArray(LINE_END.getBytes())); } publishers.add(BodyPublishers.ofString("--" + BOUNDARY + "--" + LINE_END, US_ASCII)); return new AggregateBodyPublisher(publishers); }
@Override protected BodyPublisher asPublisher() { return BodyPublishers.ofInputStream(content()); }
@Override public BodyPublisher asBodyPublisher() { var charset = contentType().charset().orElse(UTF_8); return BodyPublishers.ofInputStream(InputSuppliers.of(body(), charset)); } }
@Override public BodyPublisher asBodyPublisher() { Charset charset = contentType().charset().orElse(UTF_8); String content = UrlUtils.encodeForms(body(), charset); return BodyPublishers.ofInputStream(InputSuppliers.of(content, charset)); } }
private void upgradeToH2c(Target target){ try { httpClient.send(HttpRequest.newBuilder() .method("options", HttpRequest.BodyPublishers.noBody()) .uri(URI.create(target.url())) .build(), HttpResponse.BodyHandlers.discarding()); } catch (IOException | InterruptedException e) { throw new RuntimeException(e); } } }
@Override protected BodyPublisher asPublisher() { //TODO: just use utf-8 charset here? return BodyPublishers.ofInputStream(InputSuppliers.of(content(), UTF_8)); }
@Override public BodyPublisher asBodyPublisher() { return BodyPublishers.ofInputStream(body()); } }
@Override public BodyPublisher asBodyPublisher() { return BodyPublishers.ofByteArray(body()); } }