Skip to content

Commit 826c0f8

Browse files
committed
Add interface to customize RestAssuredRestDocumentationConfigurer
1 parent 166545a commit 826c0f8

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,14 @@ static class RestDocsRestAssuredAutoConfiguration {
8585
@Bean
8686
@ConditionalOnMissingBean(RequestSpecification.class)
8787
public RequestSpecification restDocsRestAssuredConfigurer(
88+
ObjectProvider<RestDocsRestAssuredConfigurationCustomizer> configurationCustomizerProvider,
8889
RestDocumentationContextProvider contextProvider) {
8990
RestAssuredRestDocumentationConfigurer configurer = RestAssuredRestDocumentation
9091
.documentationConfiguration(contextProvider);
92+
RestDocsRestAssuredConfigurationCustomizer configurationCustomizer = configurationCustomizerProvider.getIfAvailable();
93+
if (configurationCustomizer != null) {
94+
configurationCustomizer.customize(configurer);
95+
}
9196
return new RequestSpecBuilder().addFilter(configurer).build();
9297
}
9398

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.restdocs;
18+
19+
import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer;
20+
21+
/**
22+
* A customizer for {@link RestAssuredRestDocumentationConfigurer}. If a
23+
* {@code RestDocsRestAssuredConfigurationCustomizer} bean is found in the application
24+
* context it will be {@link #customize called} to customize the
25+
* {@code RestAssuredRestDocumentationConfigurer} before it is applied. Intended for use
26+
* only when the attributes on {@link AutoConfigureRestDocs} do not provide sufficient
27+
* customization.
28+
*
29+
* @author Eddú Meléndez
30+
* @since 2.0.0
31+
*/
32+
@FunctionalInterface
33+
public interface RestDocsRestAssuredConfigurationCustomizer {
34+
35+
/**
36+
* Customize the given {@code configurer}.
37+
* @param configurer the configurer
38+
*/
39+
void customize(RestAssuredRestDocumentationConfigurer configurer);
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.restdocs;
18+
19+
import java.io.File;
20+
21+
import io.restassured.specification.RequestSpecification;
22+
import org.assertj.core.api.Condition;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.context.SpringBootTest;
29+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
30+
import org.springframework.boot.test.context.TestConfiguration;
31+
import org.springframework.boot.web.server.LocalServerPort;
32+
import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer;
33+
import org.springframework.restdocs.templates.TemplateFormats;
34+
import org.springframework.test.context.junit4.SpringRunner;
35+
import org.springframework.util.FileSystemUtils;
36+
37+
import static io.restassured.RestAssured.given;
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.hamcrest.CoreMatchers.is;
40+
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest;
41+
import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document;
42+
import static org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.modifyUris;
43+
44+
/**
45+
* Tests for {@link AutoConfigureRestDocs}.
46+
*
47+
* @author Eddú Meléndez
48+
*/
49+
@RunWith(SpringRunner.class)
50+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
51+
@AutoConfigureRestDocs
52+
public class RestAssuredAutoConfigurationAdvancedConfigurationIntegrationTests {
53+
54+
@LocalServerPort
55+
private int port;
56+
57+
@Before
58+
public void deleteSnippets() {
59+
FileSystemUtils.deleteRecursively(new File("target/generated-snippets"));
60+
}
61+
62+
@Autowired
63+
private RequestSpecification documentationSpec;
64+
65+
@Test
66+
public void snippetGeneration() throws Exception {
67+
given(this.documentationSpec)
68+
.filter(document("default-snippets", preprocessRequest(modifyUris()
69+
.scheme("https").host("api.example.com").removePort()))).when()
70+
.port(this.port).get("/").then().assertThat().statusCode(is(200));
71+
File defaultSnippetsDir = new File("target/generated-snippets/default-snippets");
72+
assertThat(defaultSnippetsDir).exists();
73+
assertThat(new File(defaultSnippetsDir, "curl-request.md")).has(
74+
contentContaining("'https://api.example.com/'"));
75+
assertThat(new File(defaultSnippetsDir, "http-request.md")).has(
76+
contentContaining("api.example.com"));
77+
assertThat(new File(defaultSnippetsDir, "http-response.md")).isFile();
78+
}
79+
80+
private Condition<File> contentContaining(String toContain) {
81+
return new ContentContainingCondition(toContain);
82+
}
83+
84+
@TestConfiguration
85+
public static class CustomizationConfiguration implements
86+
RestDocsRestAssuredConfigurationCustomizer {
87+
88+
@Override
89+
public void customize(RestAssuredRestDocumentationConfigurer configurer) {
90+
configurer.snippets().withTemplateFormat(TemplateFormats.markdown());
91+
}
92+
93+
}
94+
95+
}

0 commit comments

Comments
 (0)