|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.web.reactive.resource; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +import org.springframework.core.io.ClassPathResource; |
| 26 | +import org.springframework.core.io.Resource; |
| 27 | +import org.springframework.web.server.ServerWebExchange; |
| 28 | +import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest; |
| 29 | +import org.springframework.web.testfixture.server.MockServerWebExchange; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.mockito.BDDMockito.given; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.never; |
| 35 | +import static org.mockito.Mockito.times; |
| 36 | +import static org.mockito.Mockito.verify; |
| 37 | + |
| 38 | +/** |
| 39 | + * Tests for {@link WebJarsResourceResolver}. |
| 40 | + * |
| 41 | + * @author Sebastien Deleuze |
| 42 | + */ |
| 43 | +class LiteWebJarsResourceResolverTests { |
| 44 | + |
| 45 | + private static final Duration TIMEOUT = Duration.ofSeconds(1); |
| 46 | + |
| 47 | + |
| 48 | + private List<Resource> locations = List.of(new ClassPathResource("/META-INF/resources/webjars")); |
| 49 | + |
| 50 | + // for this to work, an actual WebJar must be on the test classpath |
| 51 | + private LiteWebJarsResourceResolver resolver = new LiteWebJarsResourceResolver(); |
| 52 | + |
| 53 | + private ResourceResolverChain chain = mock(); |
| 54 | + |
| 55 | + private ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("")); |
| 56 | + |
| 57 | + |
| 58 | + @Test |
| 59 | + void resolveUrlExisting() { |
| 60 | + String file = "/foo/2.3/foo.txt"; |
| 61 | + given(this.chain.resolveUrlPath(file, this.locations)).willReturn(Mono.just(file)); |
| 62 | + |
| 63 | + String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain).block(TIMEOUT); |
| 64 | + |
| 65 | + assertThat(actual).isEqualTo(file); |
| 66 | + verify(this.chain, times(1)).resolveUrlPath(file, this.locations); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void resolveUrlExistingNotInJarFile() { |
| 71 | + String file = "foo/foo.txt"; |
| 72 | + given(this.chain.resolveUrlPath(file, this.locations)).willReturn(Mono.empty()); |
| 73 | + |
| 74 | + String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain).block(TIMEOUT); |
| 75 | + |
| 76 | + assertThat(actual).isNull(); |
| 77 | + verify(this.chain, times(1)).resolveUrlPath(file, this.locations); |
| 78 | + verify(this.chain, never()).resolveUrlPath("foo/2.3/foo.txt", this.locations); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void resolveUrlWebJarResource() { |
| 83 | + String file = "underscorejs/underscore.js"; |
| 84 | + String expected = "underscorejs/1.8.3/underscore.js"; |
| 85 | + given(this.chain.resolveUrlPath(file, this.locations)).willReturn(Mono.empty()); |
| 86 | + given(this.chain.resolveUrlPath(expected, this.locations)).willReturn(Mono.just(expected)); |
| 87 | + |
| 88 | + String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain).block(TIMEOUT); |
| 89 | + |
| 90 | + assertThat(actual).isEqualTo(expected); |
| 91 | + verify(this.chain, times(1)).resolveUrlPath(file, this.locations); |
| 92 | + verify(this.chain, times(1)).resolveUrlPath(expected, this.locations); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void resolveUrlWebJarResourceNotFound() { |
| 97 | + String file = "something/something.js"; |
| 98 | + given(this.chain.resolveUrlPath(file, this.locations)).willReturn(Mono.empty()); |
| 99 | + |
| 100 | + String actual = this.resolver.resolveUrlPath(file, this.locations, this.chain).block(TIMEOUT); |
| 101 | + |
| 102 | + assertThat(actual).isNull(); |
| 103 | + verify(this.chain, times(1)).resolveUrlPath(file, this.locations); |
| 104 | + verify(this.chain, never()).resolveUrlPath(null, this.locations); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void resolveResourceExisting() { |
| 109 | + Resource expected = mock(); |
| 110 | + String file = "foo/2.3/foo.txt"; |
| 111 | + given(this.chain.resolveResource(this.exchange, file, this.locations)).willReturn(Mono.just(expected)); |
| 112 | + |
| 113 | + Resource actual = this.resolver |
| 114 | + .resolveResource(this.exchange, file, this.locations, this.chain) |
| 115 | + .block(TIMEOUT); |
| 116 | + |
| 117 | + assertThat(actual).isEqualTo(expected); |
| 118 | + verify(this.chain, times(1)).resolveResource(this.exchange, file, this.locations); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void resolveResourceNotFound() { |
| 123 | + String file = "something/something.js"; |
| 124 | + given(this.chain.resolveResource(this.exchange, file, this.locations)).willReturn(Mono.empty()); |
| 125 | + |
| 126 | + Resource actual = this.resolver |
| 127 | + .resolveResource(this.exchange, file, this.locations, this.chain) |
| 128 | + .block(TIMEOUT); |
| 129 | + |
| 130 | + assertThat(actual).isNull(); |
| 131 | + verify(this.chain, times(1)).resolveResource(this.exchange, file, this.locations); |
| 132 | + verify(this.chain, never()).resolveResource(this.exchange, null, this.locations); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void resolveResourceWebJar() { |
| 137 | + String file = "underscorejs/underscore.js"; |
| 138 | + given(this.chain.resolveResource(this.exchange, file, this.locations)).willReturn(Mono.empty()); |
| 139 | + |
| 140 | + Resource expected = mock(); |
| 141 | + String expectedPath = "underscorejs/1.8.3/underscore.js"; |
| 142 | + given(this.chain.resolveResource(this.exchange, expectedPath, this.locations)) |
| 143 | + .willReturn(Mono.just(expected)); |
| 144 | + |
| 145 | + Resource actual = this.resolver |
| 146 | + .resolveResource(this.exchange, file, this.locations, this.chain) |
| 147 | + .block(TIMEOUT); |
| 148 | + |
| 149 | + assertThat(actual).isEqualTo(expected); |
| 150 | + verify(this.chain, times(1)).resolveResource(this.exchange, file, this.locations); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments