diff --git a/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpRequest.java b/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpRequest.java new file mode 100644 index 00000000000..43eec685076 --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpRequest.java @@ -0,0 +1,122 @@ +/* + * 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.web.client.reactive.test; + +import java.net.URI; + +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.HttpMethod; +import org.springframework.http.client.reactive.AbstractClientHttpRequest; +import org.springframework.http.client.reactive.ClientHttpRequest; + +/** + * Mock implementation of {@link ClientHttpRequest}. + * @author Brian Clozel + */ +public class MockClientHttpRequest extends AbstractClientHttpRequest { + + private HttpMethod httpMethod; + + private URI uri; + + private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); + + private Publisher body; + + private Publisher> bodyWithFlushes; + + + public MockClientHttpRequest() { + } + + public MockClientHttpRequest(HttpMethod httpMethod, String uri) { + this(httpMethod, (uri != null ? URI.create(uri) : null)); + } + + public MockClientHttpRequest(HttpMethod httpMethod, URI uri) { + super(); + this.httpMethod = httpMethod; + this.uri = uri; + } + + @Override + public HttpMethod getMethod() { + return this.httpMethod; + } + + public MockClientHttpRequest setMethod(HttpMethod httpMethod) { + this.httpMethod = httpMethod; + return this; + } + + @Override + public URI getURI() { + return this.uri; + } + + public MockClientHttpRequest setUri(String uri) { + this.uri = URI.create(uri); + return this; + } + + public MockClientHttpRequest setUri(URI uri) { + this.uri = uri; + return this; + } + + @Override + public DataBufferFactory bufferFactory() { + return this.bufferFactory; + } + + @Override + public Mono writeWith(Publisher body) { + this.body = body; + return applyBeforeCommit().then(Flux.from(this.body).then()); + } + + @Override + public Mono writeAndFlushWith(Publisher> body) { + this.bodyWithFlushes = body; + return applyBeforeCommit().then(Flux.from(this.bodyWithFlushes).then()); + } + + public Publisher getBody() { + return body; + } + + public Publisher> getBodyWithFlush() { + return bodyWithFlushes; + } + + @Override + public Mono setComplete() { + return applyBeforeCommit().then(); + } + + @Override + protected void writeHeaders() { } + + @Override + protected void writeCookies() { } +} diff --git a/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpResponse.java b/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpResponse.java new file mode 100644 index 00000000000..35f6882c996 --- /dev/null +++ b/spring-web/src/test/java/org/springframework/web/client/reactive/test/MockClientHttpResponse.java @@ -0,0 +1,105 @@ +/* + * 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.web.client.reactive.test; + +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.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseCookie; +import org.springframework.http.client.reactive.ClientHttpResponse; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +/** + * Mock implementation of {@link ClientHttpResponse}. + * @author Brian Clozel + */ +public class MockClientHttpResponse implements ClientHttpResponse { + + private HttpStatus status; + + private final HttpHeaders headers = new HttpHeaders(); + + private final MultiValueMap cookies = new LinkedMultiValueMap<>(); + + private Flux body = Flux.empty(); + + @Override + public HttpHeaders getHeaders() { + return headers; + } + + public MockClientHttpResponse addHeader(String name, String value) { + getHeaders().add(name, value); + return this; + } + + public MockClientHttpResponse setHeader(String name, String value) { + getHeaders().set(name, value); + return this; + } + + @Override + public HttpStatus getStatusCode() { + return this.status; + } + + public void setStatus(HttpStatus status) { + this.status = status; + } + + @Override + public Flux getBody() { + return this.body; + } + + public MockClientHttpResponse setBody(Publisher body) { + this.body = Flux.from(body); + return this; + } + + public MockClientHttpResponse setBody(String body) { + DataBuffer buffer = toDataBuffer(body, StandardCharsets.UTF_8); + this.body = Flux.just(buffer); + return this; + } + + public MockClientHttpResponse 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 MultiValueMap getCookies() { + return this.cookies; + } +}