|
| 1 | +/* |
| 2 | + * Copyright 2012-2022 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 | + * https://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 smoketest.actuator; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint; |
| 22 | +import org.springframework.boot.test.context.SpringBootTest; |
| 23 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 24 | +import org.springframework.boot.test.web.server.LocalManagementPort; |
| 25 | +import org.springframework.context.annotation.Bean; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.core.io.ClassPathResource; |
| 28 | +import org.springframework.http.HttpEntity; |
| 29 | +import org.springframework.http.HttpHeaders; |
| 30 | +import org.springframework.http.HttpStatus; |
| 31 | +import org.springframework.http.MediaType; |
| 32 | +import org.springframework.http.ResponseEntity; |
| 33 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 34 | +import org.springframework.security.web.SecurityFilterChain; |
| 35 | +import org.springframework.util.LinkedMultiValueMap; |
| 36 | +import org.springframework.util.MultiValueMap; |
| 37 | + |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; |
| 39 | + |
| 40 | +/** |
| 41 | + * Integration tests for separate management and main service ports with Actuator's MVC |
| 42 | + * {@link RestControllerEndpoint rest controller endpoints} and multipart uploads. |
| 43 | + * |
| 44 | + * @author Guirong Hu |
| 45 | + */ |
| 46 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { |
| 47 | + ManagementDifferentPortAndEndpointWithMultipartUploadSampleActuatorApplicationTests.SecurityConfiguration.class, |
| 48 | + SampleActuatorApplication.class }, |
| 49 | + properties = { "management.endpoints.web.exposure.include=*", "management.server.port=0" }) |
| 50 | +class ManagementDifferentPortAndEndpointWithMultipartUploadSampleActuatorApplicationTests { |
| 51 | + |
| 52 | + @LocalManagementPort |
| 53 | + private int managementPort; |
| 54 | + |
| 55 | + @Test |
| 56 | + void endpointShouldBeSupportMultipartUpload() { |
| 57 | + HttpHeaders requestHeaders = new HttpHeaders(); |
| 58 | + requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 59 | + |
| 60 | + MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>(); |
| 61 | + requestBody.add("file", new ClassPathResource("test-file.txt")); |
| 62 | + |
| 63 | + HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestBody, requestHeaders); |
| 64 | + |
| 65 | + ResponseEntity<String> responseEntity = new TestRestTemplate("user", "password").postForEntity( |
| 66 | + "http://localhost:" + this.managementPort + "/actuator/rest/upload", requestEntity, String.class); |
| 67 | + assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 68 | + assertThat(responseEntity.getBody()).isEqualTo("this is a test file"); |
| 69 | + } |
| 70 | + |
| 71 | + @Configuration(proxyBeanMethods = false) |
| 72 | + static class SecurityConfiguration { |
| 73 | + |
| 74 | + @Bean |
| 75 | + SecurityFilterChain configure(HttpSecurity http) throws Exception { |
| 76 | + http.csrf().disable(); |
| 77 | + return http.build(); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments