mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-23 22:59:03 +00:00
Add support for configuring SslInfo in WebTestClient
Prior to this commit, there was no easy way to configure an SslInfo
instance for use with WebTestClient.
To address that, this commit introduces a new sslInfo(SslInfo) method
in WebTestClient.MockServerSpec, which can be used as follows.
var client = WebTestClient.bindToApplicationContext(context)
.sslInfo(new MockSslInfo("mock ID"))
// ...
.build();
Closes gh-35042
This commit is contained in:
+38
@@ -17,27 +17,37 @@
|
||||
package org.springframework.test.web.reactive.server.samples.bind;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.mock.http.server.reactive.MockSslInfo;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
import org.springframework.test.web.reactive.server.WebTestClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.reactive.config.EnableWebFlux;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
import org.springframework.web.server.WebFilterChain;
|
||||
|
||||
/**
|
||||
* Sample tests demonstrating "mock" server tests binding to server infrastructure
|
||||
* declared in a Spring ApplicationContext.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sam Brannen
|
||||
* @since 5.0
|
||||
*/
|
||||
@SpringJUnitConfig
|
||||
class ApplicationContextTests {
|
||||
|
||||
private static final String SSL_SESSION_ID = "sslSessionId";
|
||||
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
|
||||
@@ -52,6 +62,19 @@ class ApplicationContextTests {
|
||||
.expectBody(String.class).isEqualTo("It works!");
|
||||
}
|
||||
|
||||
@Test // gh-35042
|
||||
void buildWithSslInfo() {
|
||||
var client = WebTestClient.bindToApplicationContext(context)
|
||||
.sslInfo(new MockSslInfo("test123"))
|
||||
.webFilter(new SslSessionIdFilter())
|
||||
.build();
|
||||
|
||||
client.get().uri("/sslInfo")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(String.class).isEqualTo("Session ID: test123");
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableWebFlux
|
||||
@@ -66,6 +89,21 @@ class ApplicationContextTests {
|
||||
String test() {
|
||||
return "It works!";
|
||||
}
|
||||
|
||||
@GetMapping("/sslInfo")
|
||||
String sslInfo(ServerHttpRequest request) {
|
||||
return "Session ID: " + request.getAttributes().get(SSL_SESSION_ID);
|
||||
}
|
||||
}
|
||||
|
||||
private static class SslSessionIdFilter implements WebFilter {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
var request = exchange.getRequest();
|
||||
request.getAttributes().put(SSL_SESSION_ID, request.getSslInfo().getSessionId());
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user