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

Introduce path encoding (fixes #1561) #1565

Closed
wants to merge 3 commits into from

Conversation

aaronjwhiteside
Copy link
Contributor

@aaronjwhiteside aaronjwhiteside commented Apr 19, 2021

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

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.

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
Copy link
Member

@ptrthomas ptrthomas left a 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

aaronjwhiteside and others added 2 commits April 20, 2021 21:00
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..
@aaronjwhiteside aaronjwhiteside marked this pull request as ready for review April 22, 2021 07:20
List<String> merged = Stream.of(builder.getPathSegments(), paths)
.flatMap(List::stream)
.collect(Collectors.toList());
builder.setPathSegments(merged);

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;
}

Copy link
Contributor Author

@aaronjwhiteside aaronjwhiteside May 5, 2021

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

@ptrthomas
Copy link
Member

closing because of inactivity, perhaps @lodenrogue would be interested to pick this up

@ptrthomas ptrthomas closed this May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants