Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ public interface HttpRequest<T> {
HttpRequest<T> uri(String value);

/**
* @return the request uri or {@code null} when none is set for absolute URI templates
* @return the request uri or {@code null} when none is set for absolute URI templates, when the request
* uri is a template, the template is extrapolated tolerating missing variables.
*/
String uri();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.codec.BodyCodec;
import io.vertx.ext.web.multipart.MultipartForm;
import io.vertx.uritemplate.ExpandOptions;
import io.vertx.uritemplate.UriTemplate;
import io.vertx.uritemplate.Variables;

Expand All @@ -48,6 +49,8 @@
*/
public class HttpRequestImpl<T> implements HttpRequest<T> {

private static final ExpandOptions INTERNAL_EXPAND_OPTIONS = new ExpandOptions().setAllowVariableMiss(true);

private final WebClientBase client;
private ProxyOptions proxyOptions;
private final Address address;
Expand Down Expand Up @@ -205,7 +208,14 @@ public HttpRequest<T> uri(String value) {
}

public String uri() {
return uri.toString();
if (uri == null) {
return null;
} else if (uri instanceof UriTemplate) {
UriTemplate uriTemplate = (UriTemplate) uri;
return uriTemplate.expandToString(templateParams(), INTERNAL_EXPAND_OPTIONS);
} else {
return uri.toString();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ public void testTemplateExpansion() throws Exception {
Map<String, String> query = new HashMap<>();
query.put("color", "red");
query.put("currency", EURO_SYMBOL);
testRequest(client -> client.request(HttpMethod.GET, UriTemplate.of("/{action}?username={username}{&query*}"))
.setTemplateParam("action", "info")
.setTemplateParam("username", "vietj")
.setTemplateParam("query", query), req -> {
testRequest(client -> {
HttpRequest<Buffer> request = client.request(HttpMethod.GET, UriTemplate.of("/{action}?username={username}{&query*}"))
.setTemplateParam("action", "info")
.setTemplateParam("query", query);
// Missing variable is accepted
assertEquals("/info?username=&color=red&currency=%E2%82%AC", request.uri());
request.setTemplateParam("username", "vietj");
assertEquals("/info?username=vietj&color=red&currency=%E2%82%AC", request.uri());
return request;
}, req -> {
assertEquals("/info", req.path());
assertEquals("vietj", req.getParam("username"));
assertEquals("red", req.getParam("color"));
Expand Down