Skip to content

Commit

Permalink
issue #511 allow setting version on a per-request basis
Browse files Browse the repository at this point in the history
  • Loading branch information
ryber committed Jan 9, 2024
1 parent ad95b01 commit 11993b3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
27 changes: 27 additions & 0 deletions unirest-bdd-tests/src/test/java/BehaviorTests/Http2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.net.http.HttpClient;

public class Http2Test extends BddTest {

@Test @Disabled
void canMakeHttp2Requests() {
Unirest.config().version(HttpClient.Version.HTTP_2);
Expand Down Expand Up @@ -69,4 +70,30 @@ void dontSendHttp2HeadersForHttp1() {
.assertNoHeader("Upgrade")
.assertNoHeader("HTTP2-Settings");
}

@Test
void overrideConfigPerRequest() {
Unirest.config().version(HttpClient.Version.HTTP_1_1);

Unirest.get(MockServer.GET)
.version(HttpClient.Version.HTTP_2)
.asObject(RequestCapture.class)
.getBody()
.assertHeader("Connection", "Upgrade, HTTP2-Settings")
.assertHeader("Upgrade", "h2c")
.assertHeader("HTTP2-Settings", "AAEAAEAAAAIAAAABAAMAAABkAAQBAAAAAAUAAEAA");
}

@Test
void overrideConfigPerRequest2() {
Unirest.config().version(HttpClient.Version.HTTP_2);

Unirest.get(MockServer.GET)
.version(HttpClient.Version.HTTP_1_1)
.asObject(RequestCapture.class)
.getBody()
.assertNoHeader("Connection")
.assertNoHeader("Upgrade")
.assertNoHeader("HTTP2-Settings");
}
}
14 changes: 14 additions & 0 deletions unirest/src/main/java/kong/unirest/core/BaseRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package kong.unirest.core;

import java.io.File;
import java.net.http.HttpClient;
import java.nio.file.CopyOption;
import java.time.Instant;
import java.util.Collection;
Expand All @@ -51,6 +52,7 @@ abstract class BaseRequest<R extends HttpRequest> implements HttpRequest<R> {
protected Path url;
private Integer connectTimeout;
private ProgressMonitor downloadMonitor;
private HttpClient.Version version;

BaseRequest(BaseRequest httpRequest) {
this.config = httpRequest.config;
Expand All @@ -59,6 +61,7 @@ abstract class BaseRequest<R extends HttpRequest> implements HttpRequest<R> {
this.headers.putAll(httpRequest.headers);
this.connectTimeout = httpRequest.connectTimeout;
this.objectMapper = httpRequest.objectMapper;
this.version = httpRequest.version;
}

BaseRequest(Config config, HttpMethod method, String url) {
Expand Down Expand Up @@ -177,6 +180,12 @@ public R downloadMonitor(ProgressMonitor monitor) {
return (R) this;
}

@Override
public R version(HttpClient.Version value) {
this.version = value;
return (R) this;
}

@Override
public HttpResponse<Empty> asEmpty() {
return request(BasicResponse::new, Empty.class);
Expand Down Expand Up @@ -424,6 +433,11 @@ public Instant getCreationTime() {
return creation;
}

@Override
public HttpClient.Version getVersion() {
return version;
}

private <T> T valueOr(T x, Supplier<T> o) {
if (x != null) {
return x;
Expand Down
15 changes: 15 additions & 0 deletions unirest/src/main/java/kong/unirest/core/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package kong.unirest.core;

import java.io.File;
import java.net.http.HttpClient;
import java.nio.file.CopyOption;
import java.time.Instant;
import java.util.Collection;
Expand Down Expand Up @@ -186,6 +187,14 @@ public interface HttpRequest<R extends HttpRequest> {
*/
R downloadMonitor(ProgressMonitor monitor);


/**
* sets the HTTP version for the request. This will override any global config
* @param version the version
* @return this request builder
*/
R version(HttpClient.Version version);

/**
* Executes the request and returns the response with the body mapped into a String
* @return response
Expand Down Expand Up @@ -422,4 +431,10 @@ default Optional<Body> getBody(){
* @return the instant the request object was created in UTC (not when it was sent).
*/
Instant getCreationTime();

/**
* gets the version for the request, or null if not set.
* @return the version
*/
HttpClient.Version getVersion();
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private java.net.http.HttpRequest getRequest(HttpRequest<?> request) {
try {
var url = URI.create(request.getUrl());
var jreq = newBuilder(url)
.version(config.getVersion())
.version(request.getVersion() != null ? request.getVersion() : config.getVersion())
.method(
request.getHttpMethod().name(),
new BodyBuilder(request).getBody()
Expand Down

0 comments on commit 11993b3

Please sign in to comment.