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