Skip to content

Commit 6beb13b

Browse files
committed
Refactor support for URI path prefix modification
Fixes spring-projectsgh-945
1 parent bfb240d commit 6beb13b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,21 +228,25 @@ public byte[] modifyContent(byte[] content, MediaType contentType) {
228228
private String modify(String input) {
229229
List<String> replacements = Arrays.asList(this.scheme, this.host,
230230
StringUtils.hasText(this.port) ? ":" + this.port : this.port,
231-
this.pathPrefix);
231+
this.pathPrefix == null ? "" : this.pathPrefix.startsWith("/") ? this.pathPrefix : "/" + this.pathPrefix);
232232

233233
int previous = 0;
234234

235235
Matcher matcher = SCHEME_HOST_PORT_PATTERN.matcher(input);
236236
StringBuilder builder = new StringBuilder();
237237
while (matcher.find()) {
238-
for (int i = 1; i <= matcher.groupCount(); i++) {
238+
int matcherGroupCount = matcher.groupCount();
239+
for (int i = 1; i <= matcherGroupCount; i++) {
239240
if (matcher.start(i) >= 0) {
240241
builder.append(input, previous, matcher.start(i));
241242
}
242243
if (matcher.start(i) >= 0) {
243244
previous = matcher.end(i);
244245
}
245246
builder.append(getReplacement(matcher.group(i), replacements.get(i - 1)));
247+
if (i == matcherGroupCount) {
248+
builder.append(replacements.get(i));
249+
}
246250
}
247251
}
248252

0 commit comments

Comments
 (0)