Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(RequestBuilder): retain trailing slash on operation path #206

Merged
merged 1 commit into from
May 24, 2023
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
7 changes: 7 additions & 0 deletions src/main/java/com/ibm/cloud/sdk/core/http/RequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ public static HttpUrl resolveRequestUrl(String serviceUrl, String path, Map<Stri
// Add the segment to the URL, noting that it will be URL encoded by okhttp
builder.addPathSegment(paramResolvedSegment);
}

// Due to the way in which we're handling the path segments one at a time above,
// if our original path string contained a trailing slash (e.g. /v1/mypath/), then we need to
// add "" as a path segment to ensure that the final request URL also has a trailing slash.
if (path.endsWith("/")) {
builder.addPathSegment("");
}
}

// Return the final HttpUrl object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,13 @@ public void testResolveRequestUrlPathSlash2() {
assertEquals("https://myserver.com/", url.toString());
}

@Test
public void testResolveRequestUrlPathSlash3() {
HttpUrl url = RequestBuilder.resolveRequestUrl("https://myserver.com", "/v1/serviceids/");
assertNotNull(url);
assertEquals("https://myserver.com/v1/serviceids/", url.toString());
}

@Test
public void testResolveRequestUrlEncodedPathParams() {
Map<String, String> pathParameters = new HashMap<String, String>() {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import okhttp3.mockwebserver.RecordedRequest;

public class RequestTest extends BaseServiceUnitTest {
private static String OperationPath = "/v1/test/";
private class TestModel extends GenericModel {
String city;

Expand All @@ -67,7 +68,7 @@ ServiceCall<TestModel> postTestModel() {

final JsonObject contentJson = new JsonObject();
contentJson.addProperty("city", model.getCity());
RequestBuilder builder = RequestBuilder.post(HttpUrl.parse(getServiceUrl() + "/v1/test"));
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), OperationPath));
builder.bodyJson(contentJson).build();
return createServiceCall(builder.build(), ResponseConverterUtils.getObject(TestModel.class));
}
Expand Down Expand Up @@ -118,6 +119,7 @@ public void testRequestIncludeAuthGzipEnabled() throws Throwable {
// Validate the next request was compressed, which should be the call to the service
String expectedOperationBody = "{\"city\":\"Columbus\"}";
request = server.takeRequest();
assertEquals(OperationPath, request.getPath());
assertEquals("gzip", request.getHeader(CONTENT_ENCODING));
assertFalse(expectedOperationBody == request.getBody().readUtf8());
assertEquals(request.getMethod(), "POST");
Expand Down Expand Up @@ -156,6 +158,7 @@ public void testRequestIncludeAuthGzipDisabled() throws Throwable {
// Validate the next request was compressed, which should be the call to the service
String expectedOperationBody = "{\"city\":\"Columbus\"}";
request = server.takeRequest();
assertEquals(OperationPath, request.getPath());
assertEquals("gzip", request.getHeader(CONTENT_ENCODING));
assertFalse(expectedOperationBody == request.getBody().readUtf8());

Expand Down