/** * 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; }
httpRequestBuilder.GET(); break; case DELETE: httpRequestBuilder.DELETE(); break; default: httpRequestBuilder.method(request.method().name(), bodyPublisher); httpRequestBuilder.uri(uri); httpRequestBuilder.expectContinue(false); body.ifPresent(b -> { var contentType = b.contentType(); httpRequestBuilder.setHeader(HeaderNames.CONTENT_TYPE, contentType.toString()); }); httpRequestBuilder.timeout(request.timeout()); request.userAgent().ifPresent(ua -> httpRequestBuilder.setHeader(HeaderNames.USER_AGENT, ua)); request.referer().ifPresent(referer -> httpRequestBuilder.setHeader(HeaderNames.REFERER, referer)); httpRequestBuilder.setHeader(HeaderNames.AUTHORIZATION, auth); }); httpRequestBuilder.setHeader(ACCEPT_ENCODING, COMPRESS_ENCODINGS); httpRequestBuilder.header(HeaderNames.COOKIE, cookie.name() + "=" + cookie.value()); httpRequestBuilder.setHeader(header.name(), String.valueOf(header.value()));
this.url = "http://" + host + ":" + port; this.httpRequest .uri(new URI(this.url + this.uri)) .method(this.method, this.body); .send(this.httpRequest.build(), HttpResponse.BodyHandlers.ofString()); } catch (URISyntaxException | IOException | InterruptedException e) { LOG.error("Failed to execute HTTP request", e);
@Override public Mono<ReactiveHttpResponse> executeRequest(ReactiveHttpRequest request) { HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(request.uri()) .method(request.method().toUpperCase(), provideBody(request)); setUpHeaders(request, requestBuilder); if(requestTimeout > 0){ requestBuilder = requestBuilder.timeout(Duration.ofMillis(requestTimeout)); } if(tryUseCompression){ requestBuilder = requestBuilder.setHeader(ACCEPT_ENCODING_HEADER, GZIP); } Java11ReactiveHttpResponse.ReactiveBodySubscriber bodySubscriber = new Java11ReactiveHttpResponse.ReactiveBodySubscriber(); CompletableFuture<HttpResponse<Void>> response = httpClient.sendAsync( requestBuilder.build(), fromSubscriber(bodySubscriber)); return Mono.fromFuture(response) .<ReactiveHttpResponse>map(resp -> { if(!resp.version().equals(httpClient.version())){ throw new IllegalArgumentException("Incorrect response version:"+resp.version()); } return new Java11ReactiveHttpResponse(resp, bodySubscriber.content(), returnPublisherClass, returnActualClass, jsonFactory, responseReader); }) .onErrorMap(ex -> ex instanceof CompletionException && ex.getCause() instanceof java.net.http.HttpTimeoutException, ReadTimeoutException::new); }
/** * Sets the timeout of the HTTP request * * Default is 2 seconds * * @param amount The amount of time * @param unit The unit of time * * @return TestResponse instance */ public TestResponse withTimeout(long amount, TemporalUnit unit) { Objects.requireNonNull(method, "method can not be null"); this.httpRequest.timeout(Duration.of(amount, unit)); return this; }
/** * Adds an additional header to the request * * @param name The name of the header * @param value The value of the header * @return TestResponse instance */ public TestResponse withHeader(String name, String value) { Objects.requireNonNull(name, "name can not be null"); Objects.requireNonNull(value, "value can not be null"); this.httpRequest.header(name, value); return this; }
private final void init() { this.httpRequest.timeout(Duration.of(TWO_SECONDS, ChronoUnit.SECONDS)); this.httpClient.followRedirects(HttpClient.Redirect.ALWAYS); this.httpClient.cookieHandler(this.cookieManager); }
/** * Sets the ContentType of the request * * @param contentType The content type to use * * @return TestResponse instance */ public TestResponse withContentType(String contentType) { Objects.requireNonNull(contentType, "contentType can not be null"); this.httpRequest.header(CONTENT_TYPE, contentType); return this; }
protected void setUpHeaders(ReactiveHttpRequest request, HttpRequest.Builder requestBuilder) { request.headers().forEach((key, values) -> values.forEach(value -> requestBuilder.header(key, value))); String contentTypeHeader = getContentTypeHeader(request); if(contentTypeHeader != null) { requestBuilder.header(CONTENT_TYPE_HEADER, contentTypeHeader); } requestBuilder.header(ACCEPT_HEADER, getAcceptHeader()); }
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); } } }