-
Notifications
You must be signed in to change notification settings - Fork 16
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
ApacheHttpClientBlockingChannel uses URL to build request #2437
Merged
bulldozer-bot
merged 8 commits into
develop
from
davids/ApacheHttpClientBlockingChannel-target
Dec 2, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2806b9d
ApacheHttpClientBlockingChannel uses URL to build request
schlosna 1a36d5f
Expand tests
schlosna 76686b4
Fast path for non-IPv6
schlosna 932bbaa
Add generated changelog entries
svc-changelog b79adbb
Add generated changelog entries
svc-changelog 1b9f708
test
schlosna d2dfdaa
test user info
schlosna a03da51
Simplify test
schlosna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: ApacheHttpClientBlockingChannel uses URL to build request. | ||
links: | ||
- https://github.com/palantir/dialogue/pull/2437 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
...5-client/src/test/java/com/palantir/dialogue/hc5/ApacheHttpClientBlockingChannelTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.dialogue.hc5; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.common.collect.ListMultimap; | ||
import com.google.common.primitives.UnsignedBytes; | ||
import com.palantir.dialogue.Endpoint; | ||
import com.palantir.dialogue.HttpMethod; | ||
import com.palantir.dialogue.PathTemplate; | ||
import com.palantir.dialogue.Request; | ||
import com.palantir.dialogue.UrlBuilder; | ||
import com.palantir.dialogue.core.BaseUrl; | ||
import java.net.InetAddress; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.net.UnknownHostException; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
import org.apache.hc.core5.http.ClassicHttpRequest; | ||
import org.apache.hc.core5.net.URIAuthority; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
class ApacheHttpClientBlockingChannelTest { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"GET", "PUT", "POST"}) | ||
void createRequest(String method) throws Exception { | ||
|
||
BaseUrl baseUrl = BaseUrl.of(new URL("https://www.example.local/foo/api")); | ||
Endpoint endpoint = new Endpoint() { | ||
private final PathTemplate pathTemplate = PathTemplate.builder() | ||
.fixed("fixed") | ||
.variable("endpoint") | ||
.variable("index") | ||
.build(); | ||
|
||
@Override | ||
public HttpMethod httpMethod() { | ||
return HttpMethod.valueOf(method); | ||
} | ||
|
||
@Override | ||
public String serviceName() { | ||
return "testService"; | ||
} | ||
|
||
@Override | ||
public String endpointName() { | ||
return "testEndpoint"; | ||
} | ||
|
||
@Override | ||
public String version() { | ||
return "1.2.3"; | ||
} | ||
|
||
@Override | ||
public void renderPath(ListMultimap<String, String> params, UrlBuilder url) { | ||
pathTemplate.fill(params, url); | ||
} | ||
}; | ||
|
||
Request request = Request.builder() | ||
.pathParams(Map.of("fixed", "bar", "endpoint", "baz", "index", "42")) | ||
.putQueryParams("q", "1") | ||
.putQueryParams("test", "true") | ||
.build(); | ||
|
||
ClassicHttpRequest httpRequest = ApacheHttpClientBlockingChannel.createRequest(baseUrl, endpoint, request); | ||
assertThat(httpRequest.getMethod()).isEqualTo(method); | ||
assertThat(httpRequest.getPath()) | ||
.isEqualTo(httpRequest.getRequestUri()) | ||
.isEqualTo("/foo/api/fixed/baz/42?q=1&test=true"); | ||
assertThat(httpRequest.getUri()) | ||
.isEqualTo(URI.create("https://www.example.local/foo/api/fixed/baz/42?q=1&test=true")); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({ | ||
"http://example.local, example.local, -1,", | ||
"https://example.local, example.local, -1,", | ||
"https://localhost:1234, localhost, 1234,", | ||
"https://127.0.0.1, 127.0.0.1, -1,", | ||
"https://[0:0:0:0:0:ffff:c0a8:0102], 0:0:0:0:0:ffff:c0a8:0102, -1,", | ||
"https://[0000:0000:0000:0000:0000:ffff:c0a8:0102], 0000:0000:0000:0000:0000:ffff:c0a8:0102, -1,", | ||
"https://[::1], ::1, -1,", | ||
"https://[::ffff:c0a8:102], ::ffff:c0a8:102, -1,", | ||
"https://127.0.0.1:1234, 127.0.0.1, 1234,", | ||
"https://[::1]:1234, ::1, 1234,", | ||
"https://www.example.local, www.example.local, -1,", | ||
"https://www.example.local:443, www.example.local, 443,", | ||
"https://www.example.local/path/to/foo/bar, www.example.local, -1,", | ||
"https://www.example.local/path/to/foo/bar?baz=quux&hello=world#hash-octothorpe, www.example.local, -1,", | ||
"https://user@www.example.local:8443/path/to/foo/bar?baz=quux&hello=world#hash-octothorpe ," | ||
+ " www.example.local, 8443, user", | ||
"https://user@[::1]:8443/path/to/foo/bar?baz=quux&hello=world#hash-octothorpe , ::1, 8443, user", | ||
"https://user@[0000:0000:0000:0000:0000:ffff:c0a8:0102]:8443/path/to/foo/bar?baz=quux&hello=world#an-octothorpe" | ||
+ " , 0000:0000:0000:0000:0000:ffff:c0a8:0102, 8443, user", | ||
"https://user:slash%2Fslash@www.example.local, www.example.local, -1, user:slash%2Fslash", | ||
}) | ||
void parseAuthority(String input, String expectedHost, int expectedPort, String expectedUserInfo) throws Exception { | ||
URL url = new URL(input); | ||
assertThat(ApacheHttpClientBlockingChannel.parseAuthority(url)) | ||
.isEqualTo(URIAuthority.create(url.toURI().getRawAuthority())) | ||
.isEqualTo(URIAuthority.create(url.getAuthority())) | ||
.satisfies(authority -> { | ||
assertThat(authority.getHostName()) | ||
.usingComparator(hostComparator) | ||
.isEqualTo(expectedHost) | ||
.isEqualTo(url.getHost()); | ||
assertThat(authority.getPort()).isEqualTo(expectedPort).isEqualTo(url.getPort()); | ||
assertThat(authority.getUserInfo()) | ||
.isEqualTo(expectedUserInfo) | ||
.isEqualTo(url.getUserInfo()); | ||
}); | ||
} | ||
|
||
@Test | ||
void testHostComparator() { | ||
assertThat("www.example.local") | ||
.usingComparator(hostComparator) | ||
.isEqualTo("www.example.local") | ||
.isNotEqualTo("www.example.com"); | ||
assertThat("127.0.0.1") | ||
.usingComparator(hostComparator) | ||
.isEqualTo("127.0.0.1") | ||
.isNotEqualTo("127.0.0.2"); | ||
assertThat("::1") | ||
.usingComparator(hostComparator) | ||
.isEqualTo("::1") | ||
.isEqualTo("[::1]") | ||
.isEqualTo("[0000:0000:0000:0000:0000:0000:0000:0001]") | ||
.isNotEqualTo("[::2]") | ||
.isNotEqualTo("127.0.0.1"); | ||
assertThat("::ffff:c0a8:102") | ||
.usingComparator(hostComparator) | ||
.isEqualTo("::ffff:c0a8:102") | ||
.isEqualTo("[0000:0000:0000:0000:0000:ffff:c0a8:0102]") | ||
.isNotEqualTo("::ffff:c0a8:101") | ||
.isNotEqualTo("[::ffff:c0a8:101]"); | ||
} | ||
|
||
private static final Comparator<? super String> hostComparator = (host1, host2) -> { | ||
if (host1.equals(host2)) { | ||
return 0; | ||
} | ||
// treat IPv6 addresses with and without brackets as equivalent | ||
InetAddress address1 = tryGetAddress(host1); | ||
InetAddress address2 = tryGetAddress(host2); | ||
if (address1 != null && address2 != null) { | ||
return UnsignedBytes.lexicographicalComparator().compare(address1.getAddress(), address2.getAddress()); | ||
} | ||
return host1.compareTo(host2); | ||
}; | ||
|
||
private static InetAddress tryGetAddress(String host) { | ||
try { | ||
return InetAddress.getByName(host); | ||
} catch (UnknownHostException e) { | ||
return null; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 I've verified that
url.getAuthority()
is equivalent toURI.getRawAuthority()
, which is used internally within apache httpclient.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically we're computing the authority similar to what is done in:
https://github.com/apache/httpcomponents-core/blob/rel/v5.3.1/httpcore5/src/main/java/org/apache/hc/core5/http/support/AbstractRequestBuilder.java#L176-L186