Skip to content

Commit bb030d1

Browse files
author
Artur Ciocanu
committed
Fix URL building logic
Signed-off-by: Artur Ciocanu <[email protected]>
1 parent 8b8684a commit bb030d1

File tree

2 files changed

+105
-3
lines changed

2 files changed

+105
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package io.dapr.it.methodinvoke.http;
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer;
4+
import com.github.tomakehurst.wiremock.client.WireMock;
5+
import io.dapr.client.DaprClient;
6+
import io.dapr.client.DaprClientBuilder;
7+
import io.dapr.client.domain.HttpExtension;
8+
import org.junit.jupiter.api.AfterAll;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.util.Map;
13+
14+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
15+
import static com.github.tomakehurst.wiremock.client.WireMock.post;
16+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
17+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.fail;
20+
21+
public class GH_1314_IT {
22+
23+
private static WireMockServer wireMockServer;
24+
25+
@BeforeAll
26+
static void setup() {
27+
wireMockServer = new WireMockServer(3500);
28+
29+
wireMockServer.start();
30+
31+
WireMock.configureFor("localhost", 3500);
32+
33+
stubFor(post(
34+
urlEqualTo("/v1.0/invoke/say-hello/method/say-hello/hello")
35+
)
36+
.willReturn(aResponse()
37+
.withStatus(200)
38+
.withHeader("Content-Type", "application/json")
39+
.withBody("\"Hello from say-hello app!\"")));
40+
}
41+
42+
@AfterAll
43+
static void teardown() {
44+
wireMockServer.stop();
45+
}
46+
47+
@Test
48+
void testInvokeSayHelloWithoutLeadingSlash() {
49+
String urlWithoutLeadingSlash = "say-hello/hello";
50+
51+
sayHelloUsingURL(urlWithoutLeadingSlash);
52+
}
53+
54+
@Test
55+
void testInvokeSayHelloWithLeadingSlash() {
56+
String urlWithLeadingSlash = "/say-hello/hello";
57+
58+
sayHelloUsingURL(urlWithLeadingSlash);
59+
}
60+
61+
private static void sayHelloUsingURL(String url) {
62+
DaprClient client = new DaprClientBuilder().build();
63+
64+
try {
65+
Map<String, String> requestData = Map.of("message", "Hello");
66+
67+
String response = client.invokeMethod(
68+
"say-hello",
69+
url,
70+
requestData,
71+
HttpExtension.POST,
72+
String.class
73+
).block();
74+
75+
assertEquals("Hello from say-hello app!", response);
76+
} catch (Exception e) {
77+
fail("Exception occurred: " + e.getMessage());
78+
} finally {
79+
try {
80+
client.close();
81+
} catch (Exception e) {
82+
fail(e);
83+
}
84+
}
85+
}
86+
87+
}

sdk/src/main/java/io/dapr/client/DaprHttp.java

+18-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import java.io.IOException;
2626
import java.net.URI;
27-
import java.net.URISyntaxException;
2827
import java.net.URLEncoder;
2928
import java.net.http.HttpClient;
3029
import java.net.http.HttpRequest;
@@ -324,10 +323,17 @@ private static String getContentType(Map<String, String> headers) {
324323
private static URI createUri(URI uri, String[] pathSegments, Map<String, List<String>> urlParameters) {
325324
String path = createPath(uri, pathSegments);
326325
String query = createQuery(urlParameters);
326+
StringBuilder result = new StringBuilder();
327+
328+
result.append(uri.getScheme()).append("://").append(uri.getAuthority()).append(path);
329+
330+
if (query != null) {
331+
result.append("?").append(query);
332+
}
327333

328334
try {
329-
return new URI(uri.getScheme(), uri.getAuthority(), path, query, null);
330-
} catch (URISyntaxException exception) {
335+
return URI.create(result.toString());
336+
} catch (IllegalArgumentException exception) {
331337
throw new DaprException(exception);
332338
}
333339
}
@@ -346,6 +352,10 @@ private static String createPath(URI uri, String[] pathSegments) {
346352
}
347353

348354
for (String segment : pathSegments) {
355+
if (segment == null || segment.isEmpty()) {
356+
continue; // Skip empty segments
357+
}
358+
349359
pathBuilder.append(encodePathSegment(segment)).append("/"); // Encode each segment
350360
}
351361

@@ -363,6 +373,11 @@ private static String createQuery(Map<String, List<String>> urlParameters) {
363373

364374
for (Map.Entry<String, List<String>> entry : urlParameters.entrySet()) {
365375
String key = entry.getKey();
376+
377+
if (key == null || key.isEmpty()) {
378+
continue; // Skip empty keys
379+
}
380+
366381
List<String> values = entry.getValue();
367382

368383
for (String value : values) {

0 commit comments

Comments
 (0)