diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java index 42e0df1c..e16ee4e5 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -168,7 +168,8 @@ public class UriModifyingOperationPreprocessor implements OperationPreprocessor private static final class UriModifyingContentModifier implements ContentModifier { - private static final Pattern SCHEME_HOST_PORT_PATTERN = Pattern.compile("(http[s]?)://([^/:#?]+)(:[0-9]+)?"); + private static final Pattern SCHEME_HOST_PORT_PATTERN = Pattern + .compile("(http[s]?)://([a-zA-Z0-9-\\.]+)(:[0-9]+)?"); private String scheme; diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java index 2b9562bd..f20d1507 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/operation/preprocess/UriModifyingOperationPreprocessorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2019 the original author or authors. + * Copyright 2014-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -110,34 +110,55 @@ public class UriModifyingOperationPreprocessorTests { @Test public void requestContentUriSchemeCanBeModified() { this.preprocessor.scheme("https"); - OperationRequest processed = this.preprocessor - .preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used")); - assertThat(new String(processed.getContent())).isEqualTo("The uri 'https://localhost:12345' should be used"); + OperationRequest processed = this.preprocessor.preprocess(createRequestWithContent( + "The uri 'https://localhost:12345' should be used. foo:bar will be unaffected")); + assertThat(new String(processed.getContent())) + .isEqualTo("The uri 'https://localhost:12345' should be used. foo:bar will be unaffected"); } @Test public void requestContentUriHostCanBeModified() { this.preprocessor.host("api.example.com"); - OperationRequest processed = this.preprocessor - .preprocess(createRequestWithContent("The uri 'https://localhost:12345' should be used")); + OperationRequest processed = this.preprocessor.preprocess(createRequestWithContent( + "The uri 'https://localhost:12345' should be used. foo:bar will be unaffected")); assertThat(new String(processed.getContent())) - .isEqualTo("The uri 'https://api.example.com:12345' should be used"); + .isEqualTo("The uri 'https://api.example.com:12345' should be used. foo:bar will be unaffected"); + } + + @Test + public void requestContentHostOfUriWithoutPortCanBeModified() { + this.preprocessor.host("api.example.com"); + OperationRequest processed = this.preprocessor.preprocess( + createRequestWithContent("The uri 'https://localhost' should be used. foo:bar will be unaffected")); + assertThat(new String(processed.getContent())) + .isEqualTo("The uri 'https://api.example.com' should be used. foo:bar will be unaffected"); + } + + @Test + public void requestContentUriPortCanBeAdded() { + this.preprocessor.port(23456); + OperationRequest processed = this.preprocessor.preprocess( + createRequestWithContent("The uri 'http://localhost' should be used. foo:bar will be unaffected")); + assertThat(new String(processed.getContent())) + .isEqualTo("The uri 'http://localhost:23456' should be used. foo:bar will be unaffected"); } @Test public void requestContentUriPortCanBeModified() { this.preprocessor.port(23456); - OperationRequest processed = this.preprocessor - .preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used")); - assertThat(new String(processed.getContent())).isEqualTo("The uri 'http://localhost:23456' should be used"); + OperationRequest processed = this.preprocessor.preprocess(createRequestWithContent( + "The uri 'http://localhost:12345' should be used. foo:bar will be unaffected")); + assertThat(new String(processed.getContent())) + .isEqualTo("The uri 'http://localhost:23456' should be used. foo:bar will be unaffected"); } @Test public void requestContentUriPortCanBeRemoved() { this.preprocessor.removePort(); - OperationRequest processed = this.preprocessor - .preprocess(createRequestWithContent("The uri 'http://localhost:12345' should be used")); - assertThat(new String(processed.getContent())).isEqualTo("The uri 'http://localhost' should be used"); + OperationRequest processed = this.preprocessor.preprocess(createRequestWithContent( + "The uri 'http://localhost:12345' should be used. foo:bar will be unaffected")); + assertThat(new String(processed.getContent())) + .isEqualTo("The uri 'http://localhost' should be used. foo:bar will be unaffected"); } @Test