DefaultResponseBuilderTests.java 9.8 KB
Newer Older
A
Arjen Poutsma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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.reactive.function;

import java.net.URI;
20
import java.nio.ByteBuffer;
A
Arjen Poutsma 已提交
21
import java.time.ZonedDateTime;
22
import java.util.ArrayList;
A
Arjen Poutsma 已提交
23
import java.util.Collections;
24
import java.util.List;
25
import java.util.Locale;
A
Arjen Poutsma 已提交
26
import java.util.Map;
27
import java.util.function.BiFunction;
28
import java.util.function.Supplier;
A
Arjen Poutsma 已提交
29 30

import org.junit.Test;
31
import reactor.core.publisher.Mono;
A
Arjen Poutsma 已提交
32

33
import org.springframework.core.codec.CharSequenceEncoder;
34 35
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
A
Arjen Poutsma 已提交
36
import org.springframework.http.CacheControl;
37
import org.springframework.http.HttpHeaders;
A
Arjen Poutsma 已提交
38 39 40
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
41 42
import org.springframework.http.codec.EncoderHttpMessageWriter;
import org.springframework.http.codec.HttpMessageWriter;
43
import org.springframework.http.server.reactive.ServerHttpResponse;
44 45 46 47 48 49 50
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse;
import org.springframework.web.reactive.result.view.View;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.MockWebSessionManager;
A
Arjen Poutsma 已提交
51

52 53 54 55 56
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
57 58
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
A
Arjen Poutsma 已提交
59 60 61 62

/**
 * @author Arjen Poutsma
 */
A
Arjen Poutsma 已提交
63
public class DefaultResponseBuilderTests {
A
Arjen Poutsma 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 147 148 149 150 151

	@Test
	public void from() throws Exception {
		Response<Void> other = Response.ok().header("foo", "bar").build();
		Response<Void> result = Response.from(other).build();
		assertEquals(HttpStatus.OK, result.statusCode());
		assertEquals("bar", result.headers().getFirst("foo"));
	}

	@Test
	public void status() throws Exception {
		Response<Void> result = Response.status(HttpStatus.CREATED).build();
		assertEquals(HttpStatus.CREATED, result.statusCode());
	}

	@Test
	public void statusInt() throws Exception {
		Response<Void> result = Response.status(201).build();
		assertEquals(HttpStatus.CREATED, result.statusCode());
	}

	@Test
	public void ok() throws Exception {
		Response<Void> result = Response.ok().build();
		assertEquals(HttpStatus.OK, result.statusCode());
	}

	@Test
	public void created() throws Exception {
		URI location = URI.create("http://example.com");
		Response<Void> result = Response.created(location).build();
		assertEquals(HttpStatus.CREATED, result.statusCode());
		assertEquals(location, result.headers().getLocation());
	}

	@Test
	public void accepted() throws Exception {
		Response<Void> result = Response.accepted().build();
		assertEquals(HttpStatus.ACCEPTED, result.statusCode());
	}

	@Test
	public void noContent() throws Exception {
		Response<Void> result = Response.noContent().build();
		assertEquals(HttpStatus.NO_CONTENT, result.statusCode());
	}

	@Test
	public void badRequest() throws Exception {
		Response<Void> result = Response.badRequest().build();
		assertEquals(HttpStatus.BAD_REQUEST, result.statusCode());
	}

	@Test
	public void notFound() throws Exception {
		Response<Void> result = Response.notFound().build();
		assertEquals(HttpStatus.NOT_FOUND, result.statusCode());
	}

	@Test
	public void unprocessableEntity() throws Exception {
		Response<Void> result = Response.unprocessableEntity().build();
		assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, result.statusCode());
	}

	@Test
	public void allow() throws Exception {
		Response<Void> result = Response.ok().allow(HttpMethod.GET).build();
		assertEquals(Collections.singleton(HttpMethod.GET), result.headers().getAllow());
	}

	@Test
	public void contentLength() throws Exception {
		Response<Void> result = Response.ok().contentLength(42).build();
		assertEquals(42, result.headers().getContentLength());
	}

	@Test
	public void contentType() throws Exception {
		Response<Void> result = Response.ok().contentType(MediaType.APPLICATION_JSON).build();
		assertEquals(MediaType.APPLICATION_JSON, result.headers().getContentType());
	}

	@Test
	public void eTag() throws Exception {
		Response<Void> result = Response.ok().eTag("foo").build();
		assertEquals("\"foo\"", result.headers().getETag());
	}
A
Arjen Poutsma 已提交
152

A
Arjen Poutsma 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	@Test
	public void lastModified() throws Exception {
		ZonedDateTime now = ZonedDateTime.now();
		Response<Void> result = Response.ok().lastModified(now).build();
		assertEquals(now.toInstant().toEpochMilli()/1000, result.headers().getLastModified()/1000);
	}

	@Test
	public void cacheControlTag() throws Exception {
		Response<Void> result = Response.ok().cacheControl(CacheControl.noCache()).build();
		assertEquals("no-cache", result.headers().getCacheControl());
	}

	@Test
	public void varyBy() throws Exception {
		Response<Void> result = Response.ok().varyBy("foo").build();
		assertEquals(Collections.singletonList("foo"), result.headers().getVary());
	}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	@Test
	public void statusCode() throws Exception {
		HttpStatus statusCode = HttpStatus.ACCEPTED;
		Response<Void> result = Response.status(statusCode).build();
		assertSame(statusCode, result.statusCode());
	}

	@Test
	public void headers() throws Exception {
		HttpHeaders headers = new HttpHeaders();
		Response<Void> result = Response.ok().headers(headers).build();
		assertEquals(headers, result.headers());
	}

	@Test
	public void build() throws Exception {
		Response<Void> result = Response.status(201).header("MyKey", "MyValue").build();

		ServerWebExchange exchange = mock(ServerWebExchange.class);
		MockServerHttpResponse response = new MockServerHttpResponse();
		when(exchange.getResponse()).thenReturn(response);
193
		Configuration configuration = mock(Configuration.class);
194

195
		result.writeTo(exchange, configuration).block();
196 197 198 199 200 201 202 203 204 205 206 207 208 209
		assertEquals(201, response.getStatusCode().value());
		assertEquals("MyValue", response.getHeaders().getFirst("MyKey"));
		assertNull(response.getBody());

	}

	@Test
	public void buildVoidPublisher() throws Exception {
		Mono<Void> mono = Mono.empty();
		Response<Mono<Void>> result = Response.ok().build(mono);

		ServerWebExchange exchange = mock(ServerWebExchange.class);
		MockServerHttpResponse response = new MockServerHttpResponse();
		when(exchange.getResponse()).thenReturn(response);
210
		Configuration configuration = mock(Configuration.class);
211

212
		result.writeTo(exchange, configuration).block();
213 214 215 216
		assertNull(response.getBody());
	}

	@Test
217
	public void bodyInserter() throws Exception {
218
		String body = "foo";
219 220 221 222 223 224 225 226 227 228 229
		Supplier<String> supplier = () -> body;
		BiFunction<ServerHttpResponse, Configuration, Mono<Void>> writer =
				(response, configuration) -> {
					byte[] bodyBytes = body.getBytes(UTF_8);
					ByteBuffer byteBuffer = ByteBuffer.wrap(bodyBytes);
					DataBuffer buffer = new DefaultDataBufferFactory().wrap(byteBuffer);

					return response.writeWith(Mono.just(buffer));
				};

		Response<String> result = Response.ok().body(writer, supplier);
230 231 232 233 234 235 236
		assertEquals(body, result.body());

		MockServerHttpRequest request =
				new MockServerHttpRequest(HttpMethod.GET, "http://localhost");
		MockServerHttpResponse response = new MockServerHttpResponse();
		ServerWebExchange exchange =
				new DefaultServerWebExchange(request, response, new MockWebSessionManager());
237 238 239 240

		List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
		messageWriters.add(new EncoderHttpMessageWriter<CharSequence>(new CharSequenceEncoder()));

241 242
		Configuration configuration = mock(Configuration.class);
		when(configuration.messageWriters()).thenReturn(messageWriters::stream);
243

244
		result.writeTo(exchange, configuration).block();
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
		assertNotNull(response.getBody());
	}

	@Test
	public void render() throws Exception {
		Map<String, Object> model = Collections.singletonMap("foo", "bar");
		Response<Rendering> result = Response.ok().render("view", model);

		assertEquals("view", result.body().name());
		assertEquals(model, result.body().model());

		MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, URI.create("http://localhost"));
		MockServerHttpResponse response = new MockServerHttpResponse();
		ServerWebExchange exchange = new DefaultServerWebExchange(request, response, new MockWebSessionManager());
		ViewResolver viewResolver = mock(ViewResolver.class);
		View view = mock(View.class);
		when(viewResolver.resolveViewName("view", Locale.ENGLISH)).thenReturn(Mono.just(view));
		when(view.render(model, null, exchange)).thenReturn(Mono.empty());

264 265 266 267 268
		List<ViewResolver> viewResolvers = new ArrayList<>();
		viewResolvers.add(viewResolver);

		Configuration mockConfig = mock(Configuration.class);
		when(mockConfig.viewResolvers()).thenReturn(viewResolvers::stream);
269

270
		result.writeTo(exchange, mockConfig).block();
271 272
	}

A
Arjen Poutsma 已提交
273
	@Test
A
Arjen Poutsma 已提交
274 275 276 277 278 279 280
	public void renderObjectArray() throws Exception {
		Response<Rendering> result =
				Response.ok().render("name", this, Collections.emptyList(), "foo");
		Map<String, Object> model = result.body().model();
		assertEquals(2, model.size());
		assertEquals(this, model.get("defaultResponseBuilderTests"));
		assertEquals("foo", model.get("string"));
A
Arjen Poutsma 已提交
281 282 283
	}

}