MockClientHttpResponse.java 4.3 KB
Newer Older
1
/*
2
 * Copyright 2002-2018 the original author or authors.
3 4 5 6 7
 *
 * 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
 *
S
Spring Operator 已提交
8
 *      https://www.apache.org/licenses/LICENSE-2.0
9 10 11 12 13 14 15 16
 *
 * 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.
 */

17
package org.springframework.mock.http.client.reactive.test;
18 19 20 21

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
22
import java.util.Collection;
23 24 25

import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
26
import reactor.core.publisher.Mono;
27 28

import org.springframework.core.io.buffer.DataBuffer;
29 30
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
31 32 33
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
34
import org.springframework.http.MediaType;
35 36
import org.springframework.http.ResponseCookie;
import org.springframework.http.client.reactive.ClientHttpResponse;
37
import org.springframework.util.Assert;
38 39 40 41 42
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * Mock implementation of {@link ClientHttpResponse}.
43
 *
44
 * @author Brian Clozel
45 46
 * @author Rossen Stoyanchev
 * @since 5.0
47 48 49
 */
public class MockClientHttpResponse implements ClientHttpResponse {

50
	private final HttpStatus status;
51 52 53 54 55 56 57

	private final HttpHeaders headers = new HttpHeaders();

	private final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();

	private Flux<DataBuffer> body = Flux.empty();

58
	private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
59 60


61 62 63
	public MockClientHttpResponse(HttpStatus status) {
		Assert.notNull(status, "HttpStatus is required");
		this.status = status;
64 65
	}

66

67
	@Override
68 69 70 71
	public HttpStatus getStatusCode() {
		return this.status;
	}

72 73 74 75 76
	@Override
	public int getRawStatusCode() {
		return this.status.value();
	}

77 78
	@Override
	public HttpHeaders getHeaders() {
79 80 81 82 83
		String headerName = HttpHeaders.SET_COOKIE;
		if (!getCookies().isEmpty() && this.headers.get(headerName) == null) {
			getCookies().values().stream().flatMap(Collection::stream)
					.forEach(cookie -> getHeaders().add(headerName, cookie.toString()));
		}
84
		return this.headers;
85 86
	}

87
	@Override
88 89
	public MultiValueMap<String, ResponseCookie> getCookies() {
		return this.cookies;
90 91
	}

92
	public void setBody(Publisher<DataBuffer> body) {
93 94 95
		this.body = Flux.from(body);
	}

96 97
	public void setBody(String body) {
		setBody(body, StandardCharsets.UTF_8);
98 99
	}

100
	public void setBody(String body, Charset charset) {
101 102 103 104 105 106 107
		DataBuffer buffer = toDataBuffer(body, charset);
		this.body = Flux.just(buffer);
	}

	private DataBuffer toDataBuffer(String body, Charset charset) {
		byte[] bytes = body.getBytes(charset);
		ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
108
		return this.bufferFactory.wrap(byteBuffer);
109 110
	}

111
	@Override
112 113
	public Flux<DataBuffer> getBody() {
		return this.body;
114
	}
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

	/**
	 * Return the response body aggregated and converted to a String using the
	 * charset of the Content-Type response or otherwise as "UTF-8".
	 */
	public Mono<String> getBodyAsString() {
		Charset charset = getCharset();
		return Flux.from(getBody())
				.reduce(bufferFactory.allocateBuffer(), (previous, current) -> {
					previous.write(current);
					DataBufferUtils.release(current);
					return previous;
				})
				.map(buffer -> dumpString(buffer, charset));
	}

	private static String dumpString(DataBuffer buffer, Charset charset) {
		Assert.notNull(charset, "'charset' must not be null");
		byte[] bytes = new byte[buffer.readableByteCount()];
		buffer.read(bytes);
		return new String(bytes, charset);
	}

	private Charset getCharset() {
		Charset charset = null;
		MediaType contentType = getHeaders().getContentType();
		if (contentType != null) {
			charset = contentType.getCharset();
		}
		return (charset != null ? charset : StandardCharsets.UTF_8);
	}

147
}