protected List<ResourceResolver> getResourceResolvers() { if (!this.hasPathResolver) { List<ResourceResolver> result = new ArrayList<>(this.resolvers); if (isWebJarsAssetLocatorPresent && !this.hasWebjarsResolver) { result.add(new WebJarsResourceResolver()); } result.add(new PathResourceResolver()); return result; } return this.resolvers; }
@Override protected Mono<Resource> resolveResourceInternal(@Nullable ServerWebExchange exchange, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) { return chain.resolveResource(exchange, requestPath, locations) .switchIfEmpty(Mono.defer(() -> { String webJarsResourcePath = findWebJarResourcePath(requestPath); if (webJarsResourcePath != null) { return chain.resolveResource(exchange, webJarsResourcePath, locations); } else { return Mono.empty(); } })); }
@Test public void resolveUrlExisting() { this.locations = singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "/foo/2.3/foo.txt"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(Mono.just(file)); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain).block(TIMEOUT); assertEquals(file, actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); }
@Test public void resolveResourceExisting() { Resource expected = mock(Resource.class); this.locations = singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "foo/2.3/foo.txt"; given(this.chain.resolveResource(this.exchange, file, this.locations)).willReturn(Mono.just(expected)); Resource actual = this.resolver .resolveResource(this.exchange, file, this.locations, this.chain) .block(TIMEOUT); assertEquals(expected, actual); verify(this.chain, times(1)).resolveResource(this.exchange, file, this.locations); }
@Test public void resolveUrlExistingNotInJarFile() { this.locations = singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "foo/foo.txt"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(Mono.empty()); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain).block(TIMEOUT); assertNull(actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); verify(this.chain, never()).resolveUrlPath("foo/2.3/foo.txt", this.locations); }
@Test public void resolveResourceWebJar() { this.locations = singletonList(new ClassPathResource("/META-INF/resources/webjars/", getClass())); String file = "underscorejs/underscore.js"; given(this.chain.resolveResource(this.exchange, file, this.locations)).willReturn(Mono.empty()); Resource expected = mock(Resource.class); String expectedPath = "underscorejs/1.8.3/underscore.js"; given(this.chain.resolveResource(this.exchange, expectedPath, this.locations)) .willReturn(Mono.just(expected)); Resource actual = this.resolver .resolveResource(this.exchange, file, this.locations, this.chain) .block(TIMEOUT); assertEquals(expected, actual); verify(this.chain, times(1)).resolveResource(this.exchange, file, this.locations); }
@Before public void setup() { // for this to work, an actual WebJar must be on the test classpath this.locations = singletonList(new ClassPathResource("/META-INF/resources/webjars")); this.resolver = new WebJarsResourceResolver(); this.chain = mock(ResourceResolverChain.class); this.exchange = MockServerWebExchange.from(MockServerHttpRequest.get("")); }
@Override protected Mono<String> resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) { return chain.resolveUrlPath(resourceUrlPath, locations) .switchIfEmpty(Mono.defer(() -> { String webJarResourcePath = findWebJarResourcePath(resourceUrlPath); if (webJarResourcePath != null) { return chain.resolveUrlPath(webJarResourcePath, locations); } else { return Mono.empty(); } })); }
@Test public void resolveUrlWebJarResourceNotFound() { String file = "something/something.js"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(Mono.empty()); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain).block(TIMEOUT); assertNull(actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); verify(this.chain, never()).resolveUrlPath(null, this.locations); }
@Test public void resolveResourceNotFound() { String file = "something/something.js"; given(this.chain.resolveResource(this.exchange, file, this.locations)).willReturn(Mono.empty()); Resource actual = this.resolver .resolveResource(this.exchange, file, this.locations, this.chain) .block(TIMEOUT); assertNull(actual); verify(this.chain, times(1)).resolveResource(this.exchange, file, this.locations); verify(this.chain, never()).resolveResource(this.exchange, null, this.locations); }
protected List<ResourceResolver> getResourceResolvers() { if (!this.hasPathResolver) { List<ResourceResolver> result = new ArrayList<>(this.resolvers); if (isWebJarsAssetLocatorPresent && !this.hasWebjarsResolver) { result.add(new WebJarsResourceResolver()); } result.add(new PathResourceResolver()); return result; } return this.resolvers; }
@Override protected Mono<Resource> resolveResourceInternal(ServerWebExchange exchange, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) { return chain.resolveResource(exchange, requestPath, locations) .otherwiseIfEmpty(Mono.defer(() -> { String webJarsResourcePath = findWebJarResourcePath(requestPath); if (webJarsResourcePath != null) { return chain.resolveResource(exchange, webJarsResourcePath, locations); } else { return Mono.empty(); } })); }
@Test public void resolveUrlWebJarResource() { String file = "underscorejs/underscore.js"; String expected = "underscorejs/1.8.3/underscore.js"; given(this.chain.resolveUrlPath(file, this.locations)).willReturn(Mono.empty()); given(this.chain.resolveUrlPath(expected, this.locations)).willReturn(Mono.just(expected)); String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain).block(TIMEOUT); assertEquals(expected, actual); verify(this.chain, times(1)).resolveUrlPath(file, this.locations); verify(this.chain, times(1)).resolveUrlPath(expected, this.locations); }
@Override protected Mono<String> resolveUrlPathInternal(String resourceUrlPath, List<? extends Resource> locations, ResourceResolverChain chain) { return chain.resolveUrlPath(resourceUrlPath, locations) .otherwiseIfEmpty(Mono.defer(() -> { String webJarResourcePath = findWebJarResourcePath(resourceUrlPath); if (webJarResourcePath != null) { return chain.resolveUrlPath(webJarResourcePath, locations); } else { return Mono.empty(); } })); }