|
| 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.session; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.Base64; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.testcontainers.containers.MongoDBContainer; |
| 24 | +import org.testcontainers.junit.jupiter.Container; |
| 25 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 26 | +import reactor.util.function.Tuples; |
| 27 | + |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.boot.test.context.SpringBootTest; |
| 30 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 31 | +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; |
| 32 | +import org.springframework.http.HttpStatus; |
| 33 | +import org.springframework.test.context.DynamicPropertyRegistry; |
| 34 | +import org.springframework.test.context.DynamicPropertySource; |
| 35 | +import org.springframework.web.reactive.function.client.WebClient; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +/** |
| 40 | + * Integration tests for {@link SampleSessionWebFluxMongoApplication}. |
| 41 | + * |
| 42 | + * @author Vedran Pavic |
| 43 | + * @author Scott Frederick |
| 44 | + */ |
| 45 | +@SpringBootTest(properties = "spring.session.timeout:10", webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 46 | +@Testcontainers(disabledWithoutDocker = true) |
| 47 | +class SampleSessionWebFluxMongoApplicationTests { |
| 48 | + |
| 49 | + @Container |
| 50 | + private static final MongoDBContainer mongo = new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(3) |
| 51 | + .withStartupTimeout(Duration.ofMinutes(2)); |
| 52 | + |
| 53 | + @LocalServerPort |
| 54 | + private int port; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private WebClient.Builder webClientBuilder; |
| 58 | + |
| 59 | + @DynamicPropertySource |
| 60 | + static void applicationProperties(DynamicPropertyRegistry registry) { |
| 61 | + registry.add("spring.data.mongodb.uri", mongo::getReplicaSetUrl); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void userDefinedMappingsSecureByDefault() { |
| 66 | + WebClient client = this.webClientBuilder.baseUrl("http://localhost:" + this.port + "/").build(); |
| 67 | + client.get().header("Authorization", getBasicAuth()).exchangeToMono((response) -> { |
| 68 | + assertThat(response.statusCode()).isEqualTo(HttpStatus.OK); |
| 69 | + return response.bodyToMono(String.class) |
| 70 | + .map((sessionId) -> Tuples.of(response.cookies().getFirst("SESSION").getValue(), sessionId)); |
| 71 | + }).flatMap((tuple) -> { |
| 72 | + String sessionCookie = tuple.getT1(); |
| 73 | + return client.get().cookie("SESSION", sessionCookie).exchangeToMono((response) -> { |
| 74 | + assertThat(response.statusCode()).isEqualTo(HttpStatus.OK); |
| 75 | + return response.bodyToMono(String.class) |
| 76 | + .doOnNext((sessionId) -> assertThat(sessionId).isEqualTo(tuple.getT2())) |
| 77 | + .thenReturn(sessionCookie); |
| 78 | + }); |
| 79 | + }).delayElement(Duration.ofSeconds(10)) |
| 80 | + .flatMap((sessionCookie) -> client.get().cookie("SESSION", sessionCookie).exchangeToMono((response) -> { |
| 81 | + assertThat(response.statusCode()).isEqualTo(HttpStatus.UNAUTHORIZED); |
| 82 | + return response.releaseBody(); |
| 83 | + })).block(Duration.ofSeconds(30)); |
| 84 | + } |
| 85 | + |
| 86 | + private String getBasicAuth() { |
| 87 | + return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments