ServerRequestWrapperTests.java 3.4 KB
Newer Older
A
Arjen Poutsma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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.
 */

17
package org.springframework.web.reactive.function.server.support;
A
Arjen Poutsma 已提交
18 19 20 21 22 23 24 25 26 27 28

import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;

import org.springframework.http.HttpMethod;
29
import org.springframework.web.reactive.function.server.ServerRequest;
A
Arjen Poutsma 已提交
30 31 32 33 34 35 36 37 38

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * @author Arjen Poutsma
 */
39
public class ServerRequestWrapperTests {
A
Arjen Poutsma 已提交
40

41
	private ServerRequest mockRequest;
A
Arjen Poutsma 已提交
42

43
	private ServerRequestWrapper wrapper;
A
Arjen Poutsma 已提交
44 45 46

	@Before
	public void createWrapper() {
47 48
		mockRequest = mock(ServerRequest.class);
		wrapper = new ServerRequestWrapper(mockRequest);
A
Arjen Poutsma 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	}

	@Test
	public void request() throws Exception {
		assertSame(mockRequest, wrapper.request());
	}

	@Test
	public void method() throws Exception {
		HttpMethod method = HttpMethod.POST;
		when(mockRequest.method()).thenReturn(method);

		assertSame(method, wrapper.method());
	}

	@Test
	public void uri() throws Exception {
		URI uri = URI.create("https://example.com");
		when(mockRequest.uri()).thenReturn(uri);

		assertSame(uri, wrapper.uri());
	}

	@Test
	public void path() throws Exception {
		String path = "/foo/bar";
		when(mockRequest.path()).thenReturn(path);

		assertSame(path, wrapper.path());
	}

	@Test
	public void headers() throws Exception {
82
		ServerRequest.Headers headers = mock(ServerRequest.Headers.class);
A
Arjen Poutsma 已提交
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
		when(mockRequest.headers()).thenReturn(headers);

		assertSame(headers, wrapper.headers());
	}

	@Test
	public void attribute() throws Exception {
		String name = "foo";
		String value = "bar";
		when(mockRequest.attribute(name)).thenReturn(Optional.of(value));

		assertEquals(Optional.of(value), wrapper.attribute(name));
	}

	@Test
	public void queryParam() throws Exception {
		String name = "foo";
		String value = "bar";
		when(mockRequest.queryParam(name)).thenReturn(Optional.of(value));

		assertEquals(Optional.of(value), wrapper.queryParam(name));
	}

	@Test
	public void queryParams() throws Exception {
		String name = "foo";
		List<String> value = Collections.singletonList("bar");
		when(mockRequest.queryParams(name)).thenReturn(value);

		assertSame(value, wrapper.queryParams(name));
	}

	@Test
	public void pathVariable() throws Exception {
		String name = "foo";
		String value = "bar";
119
		when(mockRequest.pathVariable(name)).thenReturn(value);
A
Arjen Poutsma 已提交
120

121
		assertEquals(value, wrapper.pathVariable(name));
A
Arjen Poutsma 已提交
122 123 124 125 126 127 128 129 130 131 132
	}

	@Test
	public void pathVariables() throws Exception {
		Map<String, String> pathVariables = Collections.singletonMap("foo", "bar");
		when(mockRequest.pathVariables()).thenReturn(pathVariables);

		assertSame(pathVariables, wrapper.pathVariables());
	}

}