PrincipalMethodArgumentResolverTests.java 3.0 KB
Newer Older
1
/*
2
 * Copyright 2002-2019 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
 *
 * 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.
 */
16

17 18 19 20 21 22 23 24 25 26 27
package org.springframework.web.reactive.result.method.annotation;

import java.security.Principal;

import io.reactivex.Single;
import org.junit.Test;
import reactor.core.publisher.Mono;

import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
28
import org.springframework.mock.web.test.server.MockServerWebExchange;
29 30 31 32
import org.springframework.web.method.ResolvableMethod;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.server.ServerWebExchange;

33
import static org.junit.Assert.*;
34 35

/**
36 37
 * Unit tests for {@link PrincipalMethodArgumentResolver}.
 *
38 39
 * @author Rossen Stoyanchev
 */
40
public class PrincipalMethodArgumentResolverTests {
41

42 43
	private final PrincipalMethodArgumentResolver resolver =
			new PrincipalMethodArgumentResolver(ReactiveAdapterRegistry.getSharedInstance());
44 45 46 47 48

	private ResolvableMethod testMethod = ResolvableMethod.on(getClass()).named("handle").build();


	@Test
49
	public void supportsParameter() {
50 51 52 53 54 55 56
		assertTrue(this.resolver.supportsParameter(this.testMethod.arg(Principal.class)));
		assertTrue(this.resolver.supportsParameter(this.testMethod.arg(Mono.class, Principal.class)));
		assertTrue(this.resolver.supportsParameter(this.testMethod.arg(Single.class, Principal.class)));
	}


	@Test
57
	public void resolverArgument() {
58 59
		BindingContext context = new BindingContext();
		Principal user = () -> "Joe";
60
		ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/"))
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
				.mutate().principal(Mono.just(user)).build();

		MethodParameter param = this.testMethod.arg(Principal.class);
		Object actual = this.resolver.resolveArgument(param, context, exchange).block();
		assertSame(user, actual);

		param = this.testMethod.arg(Mono.class, Principal.class);
		actual = this.resolver.resolveArgument(param, context, exchange).block();
		assertTrue(Mono.class.isAssignableFrom(actual.getClass()));
		assertSame(user, ((Mono<?>) actual).block());

		param = this.testMethod.arg(Single.class, Principal.class);
		actual = this.resolver.resolveArgument(param, context, exchange).block();
		assertTrue(Single.class.isAssignableFrom(actual.getClass()));
		assertSame(user, ((Single<?>) actual).blockingGet());
	}


	@SuppressWarnings("unused")
	void handle(
			Principal user,
			Mono<Principal> userMono,
			Single<Principal> singleUser) {
	}

}