Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public boolean checkAndSubmit(final String selectorId, final CommonUpstream comm
if (!REGISTER_TYPE_HTTP.equalsIgnoreCase(registerType) || !checked) {
return false;
}
final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getUpstreamUrl());
final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getProtocol(), commonUpstream.getUpstreamUrl());
if (pass) {
this.submit(selectorId, commonUpstream);
return false;
Expand Down Expand Up @@ -310,7 +310,7 @@ private void checkZombie0(final ZombieUpstream zombieUpstream) {
ZOMBIE_SET.remove(zombieUpstream);
String selectorId = zombieUpstream.getSelectorId();
CommonUpstream commonUpstream = zombieUpstream.getCommonUpstream();
final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getUpstreamUrl());
final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getProtocol(), commonUpstream.getUpstreamUrl());
if (pass) {
commonUpstream.setTimestamp(System.currentTimeMillis());
commonUpstream.setStatus(true);
Expand All @@ -332,7 +332,7 @@ private void check(final String selectorId, final List<CommonUpstream> upstreamL
final List<CompletableFuture<CommonUpstream>> checkFutures = new ArrayList<>(upstreamList.size());
for (CommonUpstream commonUpstream : upstreamList) {
checkFutures.add(CompletableFuture.supplyAsync(() -> {
final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getUpstreamUrl());
final boolean pass = UpstreamCheckUtils.checkUrl(commonUpstream.getProtocol(), commonUpstream.getUpstreamUrl());
if (pass) {
if (!commonUpstream.isStatus()) {
commonUpstream.setTimestamp(System.currentTimeMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ public void testScheduled() {
.handle("[{\"upstreamHost\":\"localhost\",\"protocol\":\"http://\",\"localhost\":\"divide-upstream-60\",\"weight\":60}]")
.build();
try (MockedStatic<UpstreamCheckUtils> mocked = mockStatic(UpstreamCheckUtils.class)) {
mocked.when(() -> UpstreamCheckUtils.checkUrl("ReachableUrl"))
mocked.when(() -> UpstreamCheckUtils.checkUrl("https://", "ReachableUrl"))
.thenReturn(true);
mocked.when(() -> UpstreamCheckUtils.checkUrl("ErrorUrl"))
mocked.when(() -> UpstreamCheckUtils.checkUrl("https://", "ErrorUrl"))
.thenReturn(false);

zombieSet.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,24 @@ public class UpstreamCheckUtils {
/**
* Check url boolean.
*
* @param url the url
* @param protocol the protocol
* @param url the url
* @return the boolean
*/
public static boolean checkUrl(final String url) {
return checkUrl(url, DEFAULT_TIMEOUT);
public static boolean checkUrl(final String protocol, final String url) {
return checkUrl(protocol, url, DEFAULT_TIMEOUT);
}

/**
* Check url boolean.
*
* @param url the url
* @param timeout timeout
* @param protocol the protocol
* @param url the url
* @param timeout timeout
* @return the boolean
*/
public static boolean checkUrl(final String url, final int timeout) {
if (StringUtils.isBlank(url)) {
public static boolean checkUrl(final String protocol, final String url, final int timeout) {
if (StringUtils.isBlank(protocol) || StringUtils.isBlank(url)) {
return false;
}
String[] hostPort;
Expand All @@ -69,7 +71,7 @@ public static boolean checkUrl(final String url, final int timeout) {
} else {
hostPort = StringUtils.split(url, Constants.COLONS);
}
final boolean isHttps = url.startsWith(HTTPS);
final boolean isHttps = protocol.startsWith(HTTPS);
final int port = hostPort.length > 1 ? Integer.parseInt(hostPort[1].trim()) : isHttps ? 443 : 80;
return isHostConnector(hostPort[0].trim(), port, timeout);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class UpstreamCheckUtilsTest {

@Test
public void testBlank() {
assertFalse(UpstreamCheckUtils.checkUrl(""));
assertFalse(UpstreamCheckUtils.checkUrl("https://", ""));
}

@Test
Expand All @@ -63,9 +63,9 @@ public void testSocketConnect() {
Thread.yield();
}

assertTrue(UpstreamCheckUtils.checkUrl("127.0.0.1:" + port));
assertFalse(UpstreamCheckUtils.checkUrl("http://127.0.0.1:" + (port == 0 ? port + 1 : port - 1)));
assertTrue(UpstreamCheckUtils.checkUrl("http://127.0.0.1:" + port));
assertTrue(UpstreamCheckUtils.checkUrl("https://shenyu.apache.org"));
assertTrue(UpstreamCheckUtils.checkUrl("https://", "127.0.0.1:" + port));
assertFalse(UpstreamCheckUtils.checkUrl("https://", "127.0.0.1:" + (port == 0 ? port + 1 : port - 1)));
assertTrue(UpstreamCheckUtils.checkUrl("http://", "127.0.0.1:" + port));
assertTrue(UpstreamCheckUtils.checkUrl("https://", "shenyu.apache.org"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void check(final Map<String, List<Upstream>> map) {
}

private UpstreamWithSelectorId check(final String selectorId, final Upstream upstream) {
boolean pass = UpstreamCheckUtils.checkUrl(upstream.getUrl(), checkTimeout);
boolean pass = UpstreamCheckUtils.checkUrl(upstream.getProtocol(), upstream.getUrl(), checkTimeout);
if (pass) {
if (upstream.isHealthy()) {
upstream.setLastHealthTimestamp(System.currentTimeMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void setup() {

// mock static
mockCheckUtils = mockStatic(UpstreamCheckUtils.class);
mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyInt())).thenReturn(true);
mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyString(), anyInt())).thenReturn(true);
initMockInfo();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void setUp() {

// mock static
mockCheckUtils = mockStatic(UpstreamCheckUtils.class);
mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyInt())).thenReturn(true);
mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyString(), anyInt())).thenReturn(true);
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void setUp() {

// mock static
mockCheckUtils = mockStatic(UpstreamCheckUtils.class);
mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyInt())).thenReturn(true);
mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyString(), anyInt())).thenReturn(true);
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void setup() {

// mock static
mockCheckUtils = mockStatic(UpstreamCheckUtils.class);
mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyInt())).thenReturn(true);
mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyString(), anyInt())).thenReturn(true);
}

@AfterEach
Expand Down
Loading