spring PathResourceLookupFunctionTests 源码
spring PathResourceLookupFunctionTests 代码
文件路径:/spring-webmvc/src/test/java/org/springframework/web/servlet/function/PathResourceLookupFunctionTests.java
/*
* Copyright 2002-2020 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
*
* https://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.servlet.function;
import java.io.File;
import java.util.Collections;
import java.util.Optional;
import java.util.function.Function;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Arjen Poutsma
*/
class PathResourceLookupFunctionTests {
@Test
void normal() throws Exception {
ClassPathResource location = new ClassPathResource("org/springframework/web/servlet/function/");
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
ServerRequest request = initRequest("GET", "/resources/response.txt");
Optional<Resource> result = function.apply(request);
assertThat(result.isPresent()).isTrue();
File expected = new ClassPathResource("response.txt", getClass()).getFile();
assertThat(result.get().getFile()).isEqualTo(expected);
}
@Test
void subPath() throws Exception {
ClassPathResource location = new ClassPathResource("org/springframework/web/servlet/function/");
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
ServerRequest request = initRequest("GET", "/resources/child/response.txt");
Optional<Resource> result = function.apply(request);
assertThat(result.isPresent()).isTrue();
File expected = new ClassPathResource("org/springframework/web/servlet/function/child/response.txt").getFile();
assertThat(result.get().getFile()).isEqualTo(expected);
}
@Test
void notFound() {
ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/");
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
ServerRequest request = initRequest("GET", "/resources/foo.txt");
Optional<Resource> result = function.apply(request);
assertThat(result.isPresent()).isFalse();
}
@Test
void composeResourceLookupFunction() throws Exception {
ClassPathResource defaultResource = new ClassPathResource("response.txt", getClass());
Function<ServerRequest, Optional<Resource>> lookupFunction =
new PathResourceLookupFunction("/resources/**",
new ClassPathResource("org/springframework/web/servlet/function/"));
Function<ServerRequest, Optional<Resource>> customLookupFunction =
lookupFunction.andThen((Optional<Resource> optionalResource) -> {
if (optionalResource.isPresent()) {
return optionalResource;
}
else {
return Optional.of(defaultResource);
}
});
ServerRequest request = initRequest("GET", "/resources/foo");
Optional<Resource> result = customLookupFunction.apply(request);
assertThat(result.isPresent()).isTrue();
assertThat(result.get().getFile()).isEqualTo(defaultResource.getFile());
}
private ServerRequest initRequest(String httpMethod, String requestUri) {
return new DefaultServerRequest(
PathPatternsTestUtils.initRequest(httpMethod, requestUri, true),
Collections.emptyList());
}
}
相关信息
相关文章
spring AttributesTestVisitor 源码
spring DefaultAsyncServerResponseTests 源码
spring DefaultEntityResponseBuilderTests 源码
spring DefaultRenderingResponseTests 源码
spring DefaultServerRequestBuilderTests 源码
spring DefaultServerRequestTests 源码
spring DefaultServerResponseBuilderTests 源码
spring RequestPredicateTests 源码
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦