Support encodeUrl mechanism via ServerHttpResponse
Issue: SPR-14529
This commit is contained in:
@@ -42,6 +42,27 @@ import static org.junit.Assert.assertSame;
|
||||
*/
|
||||
public class ServerHttpResponseTests {
|
||||
|
||||
@Test
|
||||
public void encodeUrlDefault() throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
assertEquals("/foo", response.encodeUrl("/foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeUrlWithEncoder() throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
response.registerUrlEncoder(s -> s + "?nonce=123");
|
||||
assertEquals("/foo?nonce=123", response.encodeUrl("/foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeUrlWithMultipleEncoders() throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
response.registerUrlEncoder(s -> s + ";p=abc");
|
||||
response.registerUrlEncoder(s -> s + "?q=123");
|
||||
assertEquals("/foo;p=abc?q=123", response.encodeUrl("/foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void writeWith() throws Exception {
|
||||
TestServerHttpResponse response = new TestServerHttpResponse();
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.mock.http.server.reactive.test;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
@@ -49,6 +50,8 @@ public class MockServerHttpResponse implements ServerHttpResponse {
|
||||
|
||||
private final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
|
||||
|
||||
private Function<String, String> urlEncoder = url -> url;
|
||||
|
||||
private Flux<DataBuffer> body;
|
||||
|
||||
private Flux<Publisher<DataBuffer>> bodyWithFlushes;
|
||||
@@ -77,6 +80,16 @@ public class MockServerHttpResponse implements ServerHttpResponse {
|
||||
return this.cookies;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String encodeUrl(String url) {
|
||||
return (this.urlEncoder != null ? this.urlEncoder.apply(url) : url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerUrlEncoder(Function<String, String> encoder) {
|
||||
this.urlEncoder = (this.urlEncoder != null ? this.urlEncoder.andThen(encoder) : encoder);
|
||||
}
|
||||
|
||||
public Publisher<DataBuffer> getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user