Remove parameterization of session smoke tests
There are dedicated smoke tests for Hazelcast, MongoDB and Redis that run on CI. This commit also polishes some of the other smoke tests related to Spring Sessionpull/28398/head
parent
718e727829
commit
42d21a8336
@ -0,0 +1,17 @@
|
||||
plugins {
|
||||
id "java"
|
||||
id "org.springframework.boot.conventions"
|
||||
}
|
||||
|
||||
description = "Spring Boot Session JDBC smoke test"
|
||||
|
||||
dependencies {
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-security"))
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
|
||||
runtimeOnly(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-jdbc"))
|
||||
runtimeOnly("org.springframework.session:spring-session-jdbc")
|
||||
runtimeOnly("com.h2database:h2")
|
||||
|
||||
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package smoketest.session;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SampleSessionJdbcApplication}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Vedran Pavic
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
properties = "server.servlet.session.timeout:2")
|
||||
class SampleSessionJdbcApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
private static final URI ROOT_URI = URI.create("/");
|
||||
|
||||
@Test
|
||||
void sessionExpiry() throws Exception {
|
||||
ResponseEntity<String> firstResponse = performRequest(ROOT_URI, null);
|
||||
String sessionId1 = firstResponse.getBody();
|
||||
String cookie = firstResponse.getHeaders().getFirst("Set-Cookie");
|
||||
String sessionId2 = performRequest(ROOT_URI, cookie).getBody();
|
||||
assertThat(sessionId1).isEqualTo(sessionId2);
|
||||
Thread.sleep(2100);
|
||||
String loginPage = performRequest(ROOT_URI, cookie).getBody();
|
||||
assertThat(loginPage).containsIgnoringCase("login");
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
void sessionsEndpointShouldReturnUserSession() {
|
||||
performRequest(ROOT_URI, null);
|
||||
ResponseEntity<Map<String, Object>> response = getSessions();
|
||||
assertThat(response).isNotNull();
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
List<Map<String, Object>> sessions = (List<Map<String, Object>>) response.getBody().get("sessions");
|
||||
assertThat(sessions.size()).isEqualTo(1);
|
||||
}
|
||||
|
||||
private ResponseEntity<String> performRequest(URI uri, String cookie) {
|
||||
HttpHeaders headers = getHeaders(cookie);
|
||||
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
|
||||
return this.restTemplate.exchange(request, String.class);
|
||||
}
|
||||
|
||||
private HttpHeaders getHeaders(String cookie) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
if (cookie != null) {
|
||||
headers.set("Cookie", cookie);
|
||||
}
|
||||
else {
|
||||
headers.set("Authorization", getBasicAuth());
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
private String getBasicAuth() {
|
||||
return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private ResponseEntity<Map<String, Object>> getSessions() {
|
||||
HttpHeaders headers = getHeaders(null);
|
||||
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET,
|
||||
URI.create("/actuator/sessions?username=user"));
|
||||
return (ResponseEntity<Map<String, Object>>) (ResponseEntity) this.restTemplate.exchange(request, Map.class);
|
||||
}
|
||||
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
plugins {
|
||||
id "java"
|
||||
id "org.springframework.boot.conventions"
|
||||
}
|
||||
|
||||
description = "Spring Boot Session smoke test"
|
||||
|
||||
def sessionStores = [
|
||||
"hazelcast": [
|
||||
"com.hazelcast:hazelcast",
|
||||
"org.springframework.session:spring-session-hazelcast"
|
||||
],
|
||||
"jdbc": [
|
||||
project(":spring-boot-project:spring-boot-starters:spring-boot-starter-jdbc"),
|
||||
"org.springframework.session:spring-session-jdbc" ,
|
||||
"com.h2database:h2"
|
||||
],
|
||||
"mongodb": [
|
||||
project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-mongodb"),
|
||||
"org.springframework.session:spring-session-data-mongodb"
|
||||
],
|
||||
"redis": [
|
||||
project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-redis"),
|
||||
"org.springframework.session:spring-session-data-redis"
|
||||
]
|
||||
]
|
||||
|
||||
dependencies {
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-security"))
|
||||
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
|
||||
|
||||
sessionStores[project.findProperty("sessionStore") ?: "jdbc"].each { runtimeOnly it }
|
||||
|
||||
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-4.0.xsd"
|
||||
xmlns="http://www.hazelcast.com/schema/config"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
|
||||
<map name="spring:session:sessions">
|
||||
<attributes>
|
||||
<attribute extractor="org.springframework.session.hazelcast.PrincipalNameExtractor">principalName</attribute>
|
||||
</attributes>
|
||||
<indexes>
|
||||
<index>principalName</index>
|
||||
</indexes>
|
||||
</map>
|
||||
|
||||
<network>
|
||||
<join>
|
||||
<multicast enabled="false"/>
|
||||
</join>
|
||||
</network>
|
||||
|
||||
</hazelcast>
|
@ -1,84 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2021 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package smoketest.session;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Base64;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link SampleSessionApplication}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Vedran Pavic
|
||||
*/
|
||||
class SampleSessionApplicationTests {
|
||||
|
||||
@Test
|
||||
void sessionExpiry() throws Exception {
|
||||
ConfigurableApplicationContext context = createContext();
|
||||
String port = context.getEnvironment().getProperty("local.server.port");
|
||||
URI uri = URI.create("http://localhost:" + port + "/");
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
ResponseEntity<String> firstResponse = firstRequest(restTemplate, uri);
|
||||
String sessionId1 = firstResponse.getBody();
|
||||
String cookie = firstResponse.getHeaders().getFirst("Set-Cookie");
|
||||
String sessionId2 = nextRequest(restTemplate, uri, cookie).getBody();
|
||||
assertThat(sessionId1).isEqualTo(sessionId2);
|
||||
Thread.sleep(2100);
|
||||
String loginPage = nextRequest(restTemplate, uri, cookie).getBody();
|
||||
assertThat(loginPage).containsIgnoringCase("login");
|
||||
}
|
||||
|
||||
private ConfigurableApplicationContext createContext() {
|
||||
ConfigurableApplicationContext context = new SpringApplicationBuilder().sources(SampleSessionApplication.class)
|
||||
.properties("server.port:0", "server.servlet.session.timeout:2")
|
||||
.initializers(new ServerPortInfoApplicationContextInitializer()).run();
|
||||
return context;
|
||||
}
|
||||
|
||||
private ResponseEntity<String> firstRequest(RestTemplate restTemplate, URI uri) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Authorization", getBasicAuth());
|
||||
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
|
||||
return restTemplate.exchange(request, String.class);
|
||||
}
|
||||
|
||||
private ResponseEntity<String> nextRequest(RestTemplate restTemplate, URI uri, String cookie) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Cookie", cookie);
|
||||
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
|
||||
return restTemplate.exchange(request, String.class);
|
||||
}
|
||||
|
||||
private String getBasicAuth() {
|
||||
return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue