Add MockServerHttpRequest/Response to spring-test
Issue: SPR-14421
This commit is contained in:
parent
73752c295d
commit
a1ac51256a
|
@ -1016,6 +1016,8 @@ project("spring-test") {
|
|||
}
|
||||
optional("org.skyscreamer:jsonassert:1.3.0")
|
||||
optional("com.jayway.jsonpath:json-path:2.2.0")
|
||||
optional("org.reactivestreams:reactive-streams:${reactivestreamsVersion}")
|
||||
optional("io.projectreactor:reactor-core:${reactorCoreVersion}")
|
||||
testCompile(project(":spring-context-support"))
|
||||
testCompile(project(":spring-oxm"))
|
||||
testCompile("javax.mail:javax.mail-api:${javamailVersion}")
|
||||
|
|
|
@ -0,0 +1,155 @@
|
|||
/*
|
||||
* Copyright 2002-2016 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
|
||||
*
|
||||
* http://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 org.springframework.mock.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
* Mock implementation of {@link ServerHttpRequest}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MockServerHttpRequest implements ServerHttpRequest {
|
||||
|
||||
private HttpMethod httpMethod;
|
||||
|
||||
private URI url;
|
||||
|
||||
private final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
private final MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
|
||||
|
||||
private Flux<DataBuffer> body = Flux.empty();
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance where the HTTP method and/or URL can be set later
|
||||
* via {@link #setHttpMethod(HttpMethod)} and {@link #setUri(URI)}.
|
||||
*/
|
||||
public MockServerHttpRequest() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience alternative to {@link #MockServerHttpRequest(HttpMethod, URI)}
|
||||
* that accepts a String URL.
|
||||
*/
|
||||
public MockServerHttpRequest(HttpMethod httpMethod, String url) {
|
||||
this(httpMethod, (url != null ? URI.create(url) : null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance with the given HTTP method and URL.
|
||||
*/
|
||||
public MockServerHttpRequest(HttpMethod httpMethod, URI url) {
|
||||
this.httpMethod = httpMethod;
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
|
||||
public void setHttpMethod(HttpMethod httpMethod) {
|
||||
this.httpMethod = httpMethod;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpMethod getMethod() {
|
||||
return this.httpMethod;
|
||||
}
|
||||
|
||||
public MockServerHttpRequest setUri(String url) {
|
||||
this.url = URI.create(url);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MockServerHttpRequest setUri(URI uri) {
|
||||
this.url = uri;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getURI() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public MockServerHttpRequest addHeader(String name, String value) {
|
||||
getHeaders().add(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MockServerHttpRequest setHeader(String name, String value) {
|
||||
getHeaders().set(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiValueMap<String, String> getQueryParams() {
|
||||
return this.queryParams;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiValueMap<String, HttpCookie> getCookies() {
|
||||
return this.cookies;
|
||||
}
|
||||
|
||||
public MockServerHttpRequest setBody(Publisher<DataBuffer> body) {
|
||||
this.body = Flux.from(body);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MockServerHttpRequest setBody(String body) {
|
||||
DataBuffer buffer = toDataBuffer(body, StandardCharsets.UTF_8);
|
||||
this.body = Flux.just(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MockServerHttpRequest setBody(String body, Charset charset) {
|
||||
DataBuffer buffer = toDataBuffer(body, charset);
|
||||
this.body = Flux.just(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
private DataBuffer toDataBuffer(String body, Charset charset) {
|
||||
byte[] bytes = body.getBytes(charset);
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
|
||||
return new DefaultDataBufferFactory().wrap(byteBuffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Flux<DataBuffer> getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* Copyright 2002-2016 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
|
||||
*
|
||||
* http://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 org.springframework.mock.http.server.reactive;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferFactory;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
* Mock implementation of {@link ServerHttpResponse}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class MockServerHttpResponse implements ServerHttpResponse {
|
||||
|
||||
private HttpStatus status;
|
||||
|
||||
private final HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
private final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
|
||||
|
||||
private Publisher<DataBuffer> body;
|
||||
|
||||
private Publisher<Publisher<DataBuffer>> bodyWithFlushes;
|
||||
|
||||
private DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
|
||||
|
||||
|
||||
@Override
|
||||
public boolean setStatusCode(HttpStatus status) {
|
||||
this.status = status;
|
||||
return true;
|
||||
}
|
||||
|
||||
public HttpStatus getStatusCode() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MultiValueMap<String, ResponseCookie> getCookies() {
|
||||
return this.cookies;
|
||||
}
|
||||
|
||||
public Publisher<DataBuffer> getBody() {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
public Publisher<Publisher<DataBuffer>> getBodyWithFlush() {
|
||||
return this.bodyWithFlushes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> writeWith(Publisher<DataBuffer> body) {
|
||||
this.body = body;
|
||||
return Flux.from(this.body).then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> writeAndFlushWith(Publisher<Publisher<DataBuffer>> body) {
|
||||
this.bodyWithFlushes = body;
|
||||
return Flux.from(this.bodyWithFlushes).then();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeCommit(Supplier<? extends Mono<Void>> action) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> setComplete() {
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataBufferFactory bufferFactory() {
|
||||
return this.bufferFactory;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
/**
|
||||
* Mock implementations of reactive HTTP server contracts.
|
||||
*/
|
||||
package org.springframework.mock.http.server.reactive;
|
|
@ -32,7 +32,6 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
|
|||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
|
||||
/**
|
||||
* Mock implementation of {@link ServerHttpRequest}.
|
||||
* @author Rossen Stoyanchev
|
||||
|
|
Loading…
Reference in New Issue