Skip to content

Commit

Permalink
Add client HTTP request and response Mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bclozel committed Sep 20, 2016
1 parent f918a2b commit c608103
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<DataBuffer> body;

private Publisher<Publisher<DataBuffer>> 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<Void> writeWith(Publisher<DataBuffer> body) {
this.body = body;
return applyBeforeCommit().then(Flux.from(this.body).then());
}

@Override
public Mono<Void> writeAndFlushWith(Publisher<Publisher<DataBuffer>> body) {
this.bodyWithFlushes = body;
return applyBeforeCommit().then(Flux.from(this.bodyWithFlushes).then());
}

public Publisher<DataBuffer> getBody() {
return body;
}

public Publisher<Publisher<DataBuffer>> getBodyWithFlush() {
return bodyWithFlushes;
}

@Override
public Mono<Void> setComplete() {
return applyBeforeCommit().then();
}

@Override
protected void writeHeaders() { }

@Override
protected void writeCookies() { }
}
Original file line number Diff line number Diff line change
@@ -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<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();

private Flux<DataBuffer> 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<DataBuffer> getBody() {
return this.body;
}

public MockClientHttpResponse setBody(Publisher<DataBuffer> 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<String, ResponseCookie> getCookies() {
return this.cookies;
}
}

0 comments on commit c608103

Please sign in to comment.