PathResourceLookupFunctionTests.java 3.0 KB
Newer Older
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;
18

19 20
import java.io.File;
import java.io.IOException;
21 22 23
import java.net.URI;

import org.junit.Test;
24 25
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
26 27 28 29 30 31 32 33 34 35 36

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @author Arjen Poutsma
 */
public class PathResourceLookupFunctionTests {

	@Test
	public void normal() throws Exception {
37
		ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/");
38

39 40
		PathResourceLookupFunction
				function = new PathResourceLookupFunction("/resources/**", location);
41
		MockServerRequest request = MockServerRequest.builder()
42 43
				.uri(new URI("http://localhost/resources/response.txt"))
				.build();
44
		Mono<Resource> result = function.apply(request);
45

46 47 48 49 50 51 52 53 54 55 56 57
		File expected = new ClassPathResource("response.txt", getClass()).getFile();
		StepVerifier.create(result)
				.expectNextMatches(resource -> {
					try {
						return expected.equals(resource.getFile());
					}
					catch (IOException ex) {
						return false;
					}
				})
				.expectComplete()
				.verify();
58 59 60 61
	}

	@Test
	public void subPath() throws Exception {
62
		ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/");
63 64

		PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
65
		MockServerRequest request = MockServerRequest.builder()
66 67
				.uri(new URI("http://localhost/resources/child/response.txt"))
				.build();
68
		Mono<Resource> result = function.apply(request);
69
		File expected = new ClassPathResource("org/springframework/web/reactive/function/server/child/response.txt").getFile();
70 71 72 73 74 75 76 77 78 79 80
		StepVerifier.create(result)
				.expectNextMatches(resource -> {
					try {
						return expected.equals(resource.getFile());
					}
					catch (IOException ex) {
						return false;
					}
				})
				.expectComplete()
				.verify();
81 82 83 84
	}

	@Test
	public void notFound() throws Exception {
85
		ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/");
86 87

		PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
88
		MockServerRequest request = MockServerRequest.builder()
89 90
				.uri(new URI("http://localhost/resources/foo"))
				.build();
91 92 93 94
		Mono<Resource> result = function.apply(request);
		StepVerifier.create(result)
				.expectComplete()
				.verify();
95 96 97
	}

}