Skip to content

Commit

Permalink
Dont add USER_AGENT header if one already exists and add query params…
Browse files Browse the repository at this point in the history
… to uri if they exist
  • Loading branch information
tylerscoville authored and jamezp committed May 3, 2023
1 parent 373bc3a commit 7c88f9e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ private CompletableFuture<ClientResponse> submit(final ClientInvocation request)
if (body != null) {
headers.set(HttpHeaders.CONTENT_LENGTH, "" + body.length());
}
options.addHeader(HttpHeaders.USER_AGENT.toString(), "Vertx");

if (!headers.contains(HttpHeaders.USER_AGENT)) {
options.addHeader(HttpHeaders.USER_AGENT.toString(), "Vertx");
}

URI uri = request.getUri();
options.setHost(uri.getHost());
Expand All @@ -164,7 +167,11 @@ private CompletableFuture<ClientResponse> submit(final ClientInvocation request)
options.setPort(uri.getPort());
}

options.setURI(uri.getRawPath());
String relativeUri = uri.getRawPath();
if (uri.getRawQuery() != null && !uri.getRawQuery().trim().isEmpty()) {
relativeUri = relativeUri + "?" + uri.getRawQuery();
}
options.setURI(relativeUri);

Object timeout = request.getConfiguration().getProperty(REQUEST_TIMEOUT_MS);
if (timeout != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,47 @@ public void testSimple() throws Exception {
assertEquals("Success", response.readEntity(String.class));
}

@Test
public void testQueryParams() throws Exception {
final String queryParam = "testQueryParam";
final String queryParamValue = "testQueryParamValue";
server.requestHandler(req -> {
HttpServerResponse response = req.response();
if (String.format("%s=%s", queryParam, queryParamValue).equals(req.query())) {
response.setStatusCode(200).end("Success");
} else {
response.setStatusCode(503).end("fail");
}
});

final Response response = client().target(baseUri())
.queryParam(queryParam, queryParamValue)
.request()
.get();
assertEquals(200, response.getStatus());
assertEquals("Success", response.readEntity(String.class));
}

@Test
public void testSimpleCustomUserAgent() throws Exception {
final String customUserAgent = "CUSTOM_USER_AGENT";
server.requestHandler(req -> {
HttpServerResponse response = req.response();
if (req.getHeader(HttpHeaders.USER_AGENT).equals(customUserAgent)) {
response.setStatusCode(200).end("Success");
} else {
response.setStatusCode(503).end("fail");
}
});

final Response response = client().target(baseUri()).request()
.header(HttpHeaders.USER_AGENT.toString(), customUserAgent)
.get();

assertEquals(200, response.getStatus());
assertEquals("Success", response.readEntity(String.class));
}

@Test
public void testHTTP() throws Exception {
server.requestHandler(req -> {
Expand Down

0 comments on commit 7c88f9e

Please sign in to comment.