Change the type of site.url to URL instead of URI (#3910)

#### What type of PR is this?

/kind bug
/area core
/milestone 2.6.x

#### What this PR does / why we need it:

This PR make the type of `site.url` to URL instead of URI. If we don't configure `halo.external-url`, the request URI will be used.

#### Which issue(s) this PR fixes:

Fixes https://github.com/halo-dev/halo/issues/3908

#### Special notes for your reviewer:

Add the line below into any templates:

```html
<b th:text="${site.url}"></b>
```

And check if the output is correct by accessing `http://localhost:8090` or `http://127.0.0.1:8090` or `http://192.168.xxx.xxx:8090` when `halo.external-url` is not set.

Check if the output is correct by accessing `http://localhost:8090` or `http://127.0.0.1:8090` or `http://192.168.xxx.xxx:8090` when `halo.external-url` is set to `https://halo.run/`.

#### Does this PR introduce a user-facing change?

```release-note
修复 site.url 解析有误的问题
```
pull/3917/head
John Niang 2023-05-08 18:21:41 +08:00 committed by GitHub
parent 8ffdb5ed1b
commit 7d27cc9712
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 12 deletions

View File

@ -28,7 +28,7 @@ public class SiteSettingVariablesAcquirer implements ViewContextBasedVariablesAc
.filter(configMap -> configMap.getData() != null)
.map(configMap -> {
SiteSettingVo siteSettingVo = SiteSettingVo.from(configMap)
.withUrl(externalUrlSupplier.get());
.withUrl(externalUrlSupplier.getURL(exchange.getRequest()));
return Map.of("site", siteSettingVo);
});
}

View File

@ -1,6 +1,6 @@
package run.halo.app.theme.finders.vo;
import java.net.URI;
import java.net.URL;
import java.util.Map;
import lombok.Builder;
import lombok.Value;
@ -23,7 +23,7 @@ public class SiteSettingVo {
String title;
@With
URI url;
URL url;
String subtitle;

View File

@ -1,16 +1,18 @@
package run.halo.app.theme;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@ -37,23 +39,24 @@ public class SiteSettingVariablesAcquirerTest {
private SiteSettingVariablesAcquirer siteSettingVariablesAcquirer;
@Test
void acquire() throws URISyntaxException {
ConfigMap configMap = new ConfigMap();
void acquireWhenExternalUrlSet() throws MalformedURLException {
var configMap = new ConfigMap();
configMap.setData(Map.of());
URI uri = new URI("https://halo.run");
when(externalUrlSupplier.get()).thenReturn(uri);
var url = new URL("https://halo.run");
when(externalUrlSupplier.getURL(any())).thenReturn(url);
when(environmentFetcher.getConfigMap()).thenReturn(Mono.just(configMap));
siteSettingVariablesAcquirer.acquire(Mockito.mock(ServerWebExchange.class))
siteSettingVariablesAcquirer.acquire(mock(ServerWebExchange.class))
.as(StepVerifier::create)
.consumeNextWith(result -> {
assertThat(result).containsKey("site");
assertThat(result.get("site")).isInstanceOf(SiteSettingVo.class);
assertThat((SiteSettingVo) result.get("site"))
.extracting(SiteSettingVo::getUrl)
.isEqualTo(uri);
.isEqualTo(url);
})
.verifyComplete();
verify(externalUrlSupplier).getURL(any());
}
}