mirror of https://github.com/dapr/java-sdk.git
Fix URL building logic (#1320)
* Fix URL building logic Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Add test for query params Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Fix the assertion in the test Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Adjust the tests Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Remove uneeded changes from IT test Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Revert some unintended changes Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Simplify the testing a little bit Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Adjust the test to use ServerRequest Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Test removing things from method invoke controller Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Add query param encoding test Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Revert some unintended changes Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> * Some tiny styles Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> --------- Signed-off-by: Artur Ciocanu <ciocanu@adobe.com> Co-authored-by: Artur Ciocanu <ciocanu@adobe.com>
This commit is contained in:
parent
80c3a6d122
commit
b7e45a06c1
|
@ -6,6 +6,7 @@ import org.springframework.web.bind.annotation.PathVariable;
|
|||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -88,6 +89,11 @@ public class MethodInvokeController {
|
|||
Thread.sleep(seconds * 1000);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/query")
|
||||
public Map<String, String> getQuery(@RequestParam("uri") String uri) {
|
||||
return Map.of("uri", uri);
|
||||
}
|
||||
|
||||
@GetMapping(path = "/health")
|
||||
public void health() {
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package io.dapr.it.methodinvoke.http;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.dapr.client.DaprClient;
|
||||
import io.dapr.client.DaprClientBuilder;
|
||||
import io.dapr.client.DaprHttp;
|
||||
import io.dapr.client.domain.HttpExtension;
|
||||
import io.dapr.exceptions.DaprException;
|
||||
import io.dapr.it.BaseIT;
|
||||
|
@ -140,4 +141,24 @@ public class MethodInvokeIT extends BaseIT {
|
|||
assertTrue(new String(exception.getPayload()).contains("Internal Server Error"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvokeQueryParamEncoding() throws Exception {
|
||||
try (DaprClient client = daprRun.newDaprClientBuilder().build()) {
|
||||
client.waitForSidecar(10000).block();
|
||||
|
||||
String uri = "abc/pqr";
|
||||
Map<String, List<String>> queryParams = Map.of("uri", List.of(uri));
|
||||
HttpExtension httpExtension = new HttpExtension(DaprHttp.HttpMethods.GET, queryParams, Map.of());
|
||||
JsonNode result = client.invokeMethod(
|
||||
daprRun.getAppName(),
|
||||
"/query",
|
||||
null,
|
||||
httpExtension,
|
||||
JsonNode.class
|
||||
).block();
|
||||
|
||||
assertEquals(uri, result.get("uri").asText());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,6 @@ import reactor.util.context.ContextView;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
|
@ -324,10 +323,17 @@ public class DaprHttp implements AutoCloseable {
|
|||
private static URI createUri(URI uri, String[] pathSegments, Map<String, List<String>> urlParameters) {
|
||||
String path = createPath(uri, pathSegments);
|
||||
String query = createQuery(urlParameters);
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
result.append(uri.getScheme()).append("://").append(uri.getAuthority()).append(path);
|
||||
|
||||
if (query != null) {
|
||||
result.append("?").append(query);
|
||||
}
|
||||
|
||||
try {
|
||||
return new URI(uri.getScheme(), uri.getAuthority(), path, query, null);
|
||||
} catch (URISyntaxException exception) {
|
||||
return URI.create(result.toString());
|
||||
} catch (IllegalArgumentException exception) {
|
||||
throw new DaprException(exception);
|
||||
}
|
||||
}
|
||||
|
@ -346,6 +352,10 @@ public class DaprHttp implements AutoCloseable {
|
|||
}
|
||||
|
||||
for (String segment : pathSegments) {
|
||||
if (segment == null || segment.isEmpty()) {
|
||||
continue; // Skip empty segments
|
||||
}
|
||||
|
||||
pathBuilder.append(encodePathSegment(segment)).append("/"); // Encode each segment
|
||||
}
|
||||
|
||||
|
@ -363,6 +373,11 @@ public class DaprHttp implements AutoCloseable {
|
|||
|
||||
for (Map.Entry<String, List<String>> entry : urlParameters.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
|
||||
if (key == null || key.isEmpty()) {
|
||||
continue; // Skip empty keys
|
||||
}
|
||||
|
||||
List<String> values = entry.getValue();
|
||||
|
||||
for (String value : values) {
|
||||
|
|
Loading…
Reference in New Issue