/** * 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; }
/** * 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); }