Skip to content

Commit 306b6f6

Browse files
committed
Configure SessionCookieConfig in MockServletWebServer
Closes gh-26479
1 parent 9022784 commit 306b6f6

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

spring-boot-project/spring-boot-tools/spring-boot-test-support/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ dependencies {
3030
implementation("org.hamcrest:hamcrest-core")
3131
implementation("org.hamcrest:hamcrest-library")
3232
implementation("org.springframework:spring-core")
33+
implementation("org.springframework:spring-test")
3334

35+
testImplementation("javax.servlet:javax.servlet-api")
3436
testImplementation("org.junit.jupiter:junit-jupiter")
3537
testImplementation("org.springframework:spring-context")
3638

3739
testRuntimeOnly("org.hibernate.validator:hibernate-validator")
40+
testRuntimeOnly("org.mockito:mockito-core")
3841
}

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/web/servlet/MockServletWebServer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,9 @@
3030
import javax.servlet.ServletContext;
3131
import javax.servlet.ServletException;
3232
import javax.servlet.ServletRegistration;
33+
import javax.servlet.SessionCookieConfig;
34+
35+
import org.springframework.mock.web.MockSessionCookieConfig;
3336

3437
import static org.mockito.ArgumentMatchers.any;
3538
import static org.mockito.ArgumentMatchers.anyString;
@@ -76,6 +79,8 @@ private void initialize() {
7679
MockServletWebServer.this.registeredFilters.add(registeredFilter);
7780
return registeredFilter.getRegistration();
7881
});
82+
final SessionCookieConfig sessionCookieConfig = new MockSessionCookieConfig();
83+
given(this.servletContext.getSessionCookieConfig()).willReturn(sessionCookieConfig);
7984
final Map<String, String> initParameters = new HashMap<>();
8085
given(this.servletContext.setInitParameter(anyString(), anyString())).will((invocation) -> {
8186
initParameters.put(invocation.getArgument(0), invocation.getArgument(1));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2021 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 org.springframework.boot.testsupport.web.servlet;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.mock.web.MockSessionCookieConfig;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link MockServletWebServer}.
27+
*
28+
* @author Stephane Nicoll
29+
*/
30+
class MockServletWebServerTests {
31+
32+
@Test
33+
void servletContextIsConfigured() {
34+
MockServletWebServer server = TestMockServletWebServer.create();
35+
assertThat(server.getServletContext()).isNotNull();
36+
}
37+
38+
@Test
39+
void servletContextHasSessionCookieConfigConfigured() {
40+
MockServletWebServer server = TestMockServletWebServer.create();
41+
assertThat(server.getServletContext().getSessionCookieConfig()).isNotNull()
42+
.isInstanceOf(MockSessionCookieConfig.class);
43+
}
44+
45+
private static final class TestMockServletWebServer extends MockServletWebServer {
46+
47+
private TestMockServletWebServer(Initializer[] initializers, int port) {
48+
super(initializers, port);
49+
}
50+
51+
static MockServletWebServer create(Initializer... initializers) {
52+
return new TestMockServletWebServer(initializers, 8080);
53+
}
54+
55+
}
56+
57+
}

0 commit comments

Comments
 (0)