SimpleUrlHandlerMappingIntegrationTests.java 5.1 KB
Newer Older
1
/*
2
 * Copyright 2002-2016 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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.
 */
A
Arjen Poutsma 已提交
16

17
package org.springframework.web.reactive.result;
18 19

import java.net.URI;
20
import java.nio.charset.StandardCharsets;
21 22 23 24
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
25 26
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
27

R
Rossen Stoyanchev 已提交
28 29 30
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
31
import org.springframework.core.io.buffer.DataBuffer;
R
Rossen Stoyanchev 已提交
32
import org.springframework.core.io.buffer.DefaultDataBuffer;
33
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
34
import org.springframework.http.HttpStatus;
35 36
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
37
import org.springframework.http.server.reactive.AbstractHttpHandlerIntegrationTests;
38
import org.springframework.http.server.reactive.HttpHandler;
39
import org.springframework.web.client.HttpClientErrorException;
40
import org.springframework.web.client.RestTemplate;
41
import org.springframework.web.reactive.DispatcherHandler;
42
import org.springframework.web.server.handler.ResponseStatusExceptionHandler;
43
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
R
Rossen Stoyanchev 已提交
44
import org.springframework.web.server.WebHandler;
45
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
46 47

import static org.junit.Assert.assertArrayEquals;
48
import static org.junit.Assert.assertEquals;
49 50

/**
R
Rossen Stoyanchev 已提交
51 52
 * Integration tests with requests mapped via
 * {@link SimpleUrlHandlerMapping} to plain {@link WebHandler}s.
53
 *
54 55
 * @author Rossen Stoyanchev
 */
R
Rossen Stoyanchev 已提交
56
public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandlerIntegrationTests {
57 58

	@Override
59
	protected HttpHandler createHttpHandler() {
R
Rossen Stoyanchev 已提交
60 61
		AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext();
		wac.register(WebConfig.class);
62 63
		wac.refresh();

64
		return WebHttpHandlerBuilder.webHandler(new DispatcherHandler(wac))
R
Rossen Stoyanchev 已提交
65 66
				.exceptionHandlers(new ResponseStatusExceptionHandler())
				.build();
67 68 69
	}

	@Test
R
Rossen Stoyanchev 已提交
70 71
	public void testRequestToFooHandler() throws Exception {
		URI url = new URI("http://localhost:" + this.port + "/foo");
72
		RequestEntity<Void> request = RequestEntity.get(url).build();
R
Rossen Stoyanchev 已提交
73
		ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class);
74

75
		assertEquals(HttpStatus.OK, response.getStatusCode());
R
Rossen Stoyanchev 已提交
76
		assertArrayEquals("foo".getBytes("UTF-8"), response.getBody());
77 78 79
	}

	@Test
R
Rossen Stoyanchev 已提交
80 81
	public void testRequestToBarHandler() throws Exception {
		URI url = new URI("http://localhost:" + this.port + "/bar");
82
		RequestEntity<Void> request = RequestEntity.get(url).build();
R
Rossen Stoyanchev 已提交
83
		ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class);
84

85
		assertEquals(HttpStatus.OK, response.getStatusCode());
R
Rossen Stoyanchev 已提交
86
		assertArrayEquals("bar".getBytes("UTF-8"), response.getBody());
87 88
	}

89
	@Test
R
Rossen Stoyanchev 已提交
90 91
	public void testRequestToHeaderSettingHandler() throws Exception {
		URI url = new URI("http://localhost:" + this.port + "/header");
92
		RequestEntity<Void> request = RequestEntity.get(url).build();
R
Rossen Stoyanchev 已提交
93
		ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class);
94 95 96 97 98

		assertEquals(HttpStatus.OK, response.getStatusCode());
		assertEquals("bar", response.getHeaders().getFirst("foo"));
	}

99
	@Test
R
Rossen Stoyanchev 已提交
100 101
	public void testHandlerNotFound() throws Exception {
		URI url = new URI("http://localhost:" + this.port + "/oops");
102 103
		RequestEntity<Void> request = RequestEntity.get(url).build();
		try {
R
Rossen Stoyanchev 已提交
104
			new RestTemplate().exchange(request, byte[].class);
105 106 107 108 109 110
		}
		catch (HttpClientErrorException ex) {
			assertEquals(HttpStatus.NOT_FOUND, ex.getStatusCode());
		}
	}

111
	private static DataBuffer asDataBuffer(String text) {
R
Rossen Stoyanchev 已提交
112 113
		DefaultDataBuffer buffer = new DefaultDataBufferFactory().allocateBuffer();
		return buffer.write(text.getBytes(StandardCharsets.UTF_8));
114 115
	}

116

R
Rossen Stoyanchev 已提交
117
	@Configuration
R
Rossen Stoyanchev 已提交
118
	@SuppressWarnings({"unused", "WeakerAccess"})
R
Rossen Stoyanchev 已提交
119 120 121
	static class WebConfig {

		@Bean
R
Rossen Stoyanchev 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		public SimpleUrlHandlerMapping handlerMapping() {
			return new SimpleUrlHandlerMapping() {
				{
					Map<String, Object> map = new HashMap<>();
					map.put("/foo", (WebHandler) exchange ->
							exchange.getResponse().writeWith(Flux.just(asDataBuffer("foo"))));
					map.put("/bar", (WebHandler) exchange ->
							exchange.getResponse().writeWith(Flux.just(asDataBuffer("bar"))));
					map.put("/header", (WebHandler) exchange -> {
						exchange.getResponse().getHeaders().add("foo", "bar");
						return Mono.empty();
					});
					setUrlMap(map);
				}
			};
R
Rossen Stoyanchev 已提交
137 138 139 140 141 142 143 144
		}

		@Bean
		public SimpleHandlerAdapter handlerAdapter() {
			return new SimpleHandlerAdapter();
		}
	}

145
}