From 488d36d4bfea50ea0bd28026d10e7a3c5a0120c4 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 4 Apr 2016 10:47:11 +0100 Subject: [PATCH] Do not double-encode URIs when they are being modified Previously, when UriModifyingOperationPreprocessor modified a URI it would always encode it. However, the URIs being modified will always have been encoded, resulting in double-encoding of the URI. This commit updates UriModifyingOperationPreprocessor to tell UriComponentsBuilder that the components have already been encoded. This ensures that they are not encoded for a second time when the URI is being built. Closes gh-211 --- .../preprocess/UriModifyingOperationPreprocessor.java | 2 +- .../UriModifyingOperationPreprocessorTests.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/operation/preprocess/UriModifyingOperationPreprocessor.java b/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/operation/preprocess/UriModifyingOperationPreprocessor.java index b06530b6..9063ac94 100644 --- a/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/operation/preprocess/UriModifyingOperationPreprocessor.java +++ b/spring-restdocs-restassured/src/main/java/org/springframework/restdocs/restassured/operation/preprocess/UriModifyingOperationPreprocessor.java @@ -140,7 +140,7 @@ public final class UriModifyingOperationPreprocessor implements OperationPreproc modifiedHeaders.set(HttpHeaders.HOST, this.host); } return this.contentModifyingDelegate.preprocess( - new OperationRequestFactory().create(uriBuilder.build().toUri(), + new OperationRequestFactory().create(uriBuilder.build(true).toUri(), request.getMethod(), request.getContent(), modifiedHeaders, request.getParameters(), modify(request.getParts()))); } diff --git a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/operation/preprocess/UriModifyingOperationPreprocessorTests.java b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/operation/preprocess/UriModifyingOperationPreprocessorTests.java index 30677718..60bda2db 100644 --- a/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/operation/preprocess/UriModifyingOperationPreprocessorTests.java +++ b/spring-restdocs-restassured/src/test/java/org/springframework/restdocs/restassured/operation/preprocess/UriModifyingOperationPreprocessorTests.java @@ -307,6 +307,16 @@ public class UriModifyingOperationPreprocessorTests { is(equalTo("The uri 'http://api.example.com:12345' should be used"))); } + @Test + public void modifiedUriDoesNotGetDoubleEncoded() { + this.preprocessor.scheme("https"); + OperationRequest processed = this.preprocessor + .preprocess(createRequestWithUri("http://localhost:12345?foo=%7B%7D")); + assertThat(processed.getUri(), + is(equalTo(URI.create("https://localhost:12345?foo=%7B%7D")))); + + } + private OperationRequest createRequestWithUri(String uri) { return this.requestFactory.create(URI.create(uri), HttpMethod.GET, new byte[0], new HttpHeaders(), new Parameters(),