-
Notifications
You must be signed in to change notification settings - Fork 33
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
Better error message for HostAndPort containing a protocol #983
Open
ash211
wants to merge
3
commits into
develop
Choose a base branch
from
aash/hostAndPortWithHttp
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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: Better error message for HostAndPort containing a protocol | ||
links: | ||
- https://github.com/palantir/conjure-java-runtime-api/pull/983 |
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
54 changes: 54 additions & 0 deletions
54
...ce-config/src/test/java/com/palantir/conjure/java/api/config/service/HostAndPortTest.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,54 @@ | ||
/* | ||
* (c) Copyright 2023 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.conjure.java.api.config.service; | ||
|
||
import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.palantir.logsafe.UnsafeArg; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class HostAndPortTest { | ||
|
||
@Test | ||
void fromString_onHostAndPort_succeeds() { | ||
HostAndPort hostAndPort = HostAndPort.fromString("example.com:8080"); | ||
assertThat(hostAndPort.getHost()).isEqualTo("example.com"); | ||
assertThat(hostAndPort.getPort()).isEqualTo(8080); | ||
} | ||
|
||
@Test | ||
void fromString_onHostAndPortWithHttpProtocolPrefix_doesNotThrow() { | ||
assertThatLoggableExceptionThrownBy(() -> HostAndPort.fromString("http://example.com:8080")) | ||
.hasMessageContaining("hostPortString must not contain a protocol prefix like http:// or https://") | ||
.containsArgs(UnsafeArg.of("hostPortString", "http://example.com:8080")); | ||
} | ||
|
||
@Test | ||
void fromString_onHostAndPortWithHttpsProtocolPrefix_doesNotThrow() { | ||
assertThatLoggableExceptionThrownBy(() -> HostAndPort.fromString("https://example.com:8080")) | ||
.hasMessageContaining("hostPortString must not contain a protocol prefix like http:// or https://") | ||
.containsArgs(UnsafeArg.of("hostPortString", "https://example.com:8080")); | ||
} | ||
|
||
@Test | ||
void fromString_onHostAndPortWithAbcProtocolPrefix_doesNotThrow() { | ||
assertThatLoggableExceptionThrownBy(() -> HostAndPort.fromString("abc://example.com:8080")) | ||
.hasMessageContaining("hostPortString must not contain a protocol prefix like http:// or https://") | ||
.containsArgs(UnsafeArg.of("hostPortString", "abc://example.com:8080")); | ||
} | ||
Comment on lines
+34
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the test names |
||
} |
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
package com.palantir.conjure.java.api.config.service; | ||
|
||
import static com.palantir.logsafe.testing.Assertions.assertThatLoggableExceptionThrownBy; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
|
@@ -24,6 +25,7 @@ | |
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | ||
import com.google.common.io.Resources; | ||
import com.palantir.conjure.java.api.ext.jackson.ObjectMappers; | ||
import com.palantir.logsafe.UnsafeArg; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -147,4 +149,14 @@ public void serDe() throws Exception { | |
assertThat(ObjectMappers.newClientObjectMapper().readValue(kebabCase, ProxyConfiguration.class)) | ||
.isEqualTo(config); | ||
} | ||
|
||
@Test | ||
public void incorrectlyFormattedHostAndPort() { | ||
assertThatLoggableExceptionThrownBy(() -> ProxyConfiguration.builder() | ||
.hostAndPort("http://squid:3128") | ||
.type(ProxyConfiguration.Type.HTTP) | ||
.build()) | ||
.hasMessageContaining("hostPortString must not contain a protocol prefix like http:// or https://") | ||
.containsArgs(UnsafeArg.of("hostPortString", "http://squid:3128")); | ||
Comment on lines
+154
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we add similar tests for these to |
||
} | ||
} |
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.
host
?HostAndPort
from a valid RFC 2396 URI viajava.net.URI#create(String)
with something like the following (though I hate the exception as control flow)?conjure-java-runtime-api/service-config/src/main/java/com/palantir/conjure/java/api/config/service/ProxyConfiguration.java
Lines 89 to 98 in acf99a6
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.
InetSocketAddress.createUnresolved
at https://github.com/palantir/conjure-java-runtime/blob/33525f67c7acc31106ef4311f2fd2f9ef3ac92ae/client-config/src/main/java/com/palantir/conjure/java/client/config/ClientConfigurations.java#L173So we would preferably match whatever validation that the JDK does when opening the socket.
that might be nice. So instead of providing a better exception message for
hostAndPort: http://example.com:8080
, accept it as equivalent toexample.com:8080
By handling deser and checks more gracefully, do you mean deserialize successfully but into an error state, rather than failing to deserialize?
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.
Making the deserialization more lenient to permit
host-and-port: https://your.proxy.host.example.com:1234
seems reasonable to me, but I'm not super close to all the consumers here and would defer to @carterkozak. I think we would still want to check and throw if one specified a URI scheme not in {http
,https
,mesh
,socks
} or if non-scheme/host/port elements of URI are specified (e.g. path, authority, etc.).For 3, I was just referring to the
serDe()
test above and adding similar test cases that reads raw json (or yaml) with various valid & invalidhost-and-port:
URIsconjure-java-runtime-api/service-config/src/test/java/com/palantir/conjure/java/api/config/service/ProxyConfigurationTests.java
Lines 137 to 151 in 31afb48