-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Introduce path encoding (fixes #1561) #1565
Conversation
HttpRequestBuilder.java - replaced armeria QueryParamsBuilder with apache http URIBuilder. - minor generics cleanup - introduced support for fragments. - URIBuilder now handles path segment encoding, query parameters and any fragment. - care was taken to ensure that path's prefixed with / are accepted, but a warning is printed letting people know they should remove the prefixing / encoding.feature - added more demo/test cases for path encoding HttpUtils.java - minor generics cleanup - final is redundant on static methods karate-demo/pom.xml - scope = import only works in the dependencyManagement section, this fixes the maven warning. Request.java - minor generics cleanup - use computeIfAbsent() HttpClientFactory.java - public static final are redundant on interface fields - also use method reference
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.
thanks much ! saw you switched to the develop
branch
also in case you use slack: https://twitter.com/KarateDSL/status/937271659785961477
karate-core/src/main/java/com/intuit/karate/http/HttpRequestBuilder.java
Show resolved
Hide resolved
karate-core/src/test/java/com/intuit/karate/core/KarateHttpMockHandlerTest.java
Outdated
Show resolved
Hide resolved
HttpRequestBuilder.java - replaced armeria QueryParamsBuilder with apache http `URIBuilder`. - minor generics cleanup - `URIBuilder` now handles path segment encoding and query parameters. - we now check if any paths are prefixed with `/` and issue a warning log, that this is probably a mistake encoding.feature - added more demo/test cases for path encoding HttpUtils.java - minor generics cleanup - final is redundant on static methods karate-demo/pom.xml - scope = import only works in the dependencyManagement section, this fixes the maven warning. Request.java - minor generics cleanup - use computeIfAbsent() HttpClientFactory.java - public static final are redundant on interface fields - also use method reference KarateMockHandlerTest.java KarateHttpMockHandlerTest.java - removed all `/` path prefixes README.md - updated with details on how to correctly use path - changes any erroneous examples of path usage
encoding.feature - now passes both mock and spring boot + tomcat tests EncodingController.java - because this controller is used in two different context's 1) spring's mock mvc environment 2) spring boot + tomcat - it needs to account for the different quirks of those environments. MockHttpClient.java - spring is somehow messing up the decoding of the pathInfo from the supplied url, using ISO 8859-1 instead of UTF-8, so here we explicitly set the pathInfo variable using the result of the trusted URI class :) Request.java - cleanup the setUrl() method, no longer need to hack around Spring's decoding issue, at least in this class..
List<String> merged = Stream.of(builder.getPathSegments(), paths) | ||
.flatMap(List::stream) | ||
.collect(Collectors.toList()); | ||
builder.setPathSegments(merged); |
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.
Single level of abstraction:
You could probably organize this like so (forgive any syntax errors. Wrote this in the browser):
try {
URIBuilder builder = createBuilder(url, params);
if (Objects.nonNull(paths)) {
builder.setPathSegments(createSegments(builder, paths));
}
// Rest of the code
}
private List<String> createSegments(URIBuilder builder, List<String> paths) {
paths.stream()
.filter(path -> path.startsWith("/"))
.forEach(this::warnPathSegment);
return Stream.of(builder.getPathSegments(), paths)
.flatMap(List::stream)
.collect(Collectors.toList());
}
private void warnPathSegment(String path) {
logger.warn("Path segment: '{}' starts with a '/', this is probably a mistake. The '/' character will be escaped and sent to the remote server as '%2F'. " +
"If you want to include multiple paths please separate them using commas. Ie. 'hello', 'world' instead of '/hello/world'.", path);
}
// Not sure the type of params
private URIBuilder createBuilder(String url, List<K, V> params) {
URIBuilder builder = Objects.isNull(url) ? new URIBuilder() : new URIBuilder(url);
if (Objects.nonNull(params)) {
params.forEach((key, values) -> values.forEach(value -> builder.addParameter(key, value)));
}
return builder;
}
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.
thats nice, I like that, thanks @lodenrogue
closing because of inactivity, perhaps @lodenrogue would be interested to pick this up |
HttpRequestBuilder.java
encoding.feature
HttpUtils.java
karate-demo/pom.xml
Request.java
HttpClientFactory.java
Description
Thanks for contributing this Pull Request. Make sure that you submit this Pull Request against the
develop
branch of this repository, add a brief description, and tag the relevant issue(s) and PR(s) below.