This is an automated email from the ASF dual-hosted git repository.

Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 084577885a fix(plugin): default missing load balance request data 
(#7114)
084577885a is described below

commit 084577885a67e9276aefdcb025e855ebbaf46f82
Author: Liming Deng <[email protected]>
AuthorDate: Tue Sep 22 19:14:58 2026 +0800

    fix(plugin): default missing load balance request data (#7114)
    
    Co-authored-by: aias00 <[email protected]>
---
 .../plugin/base/utils/LoadbalancerUtils.java       |  9 ++--
 .../plugin/base/utils/LoadbalancerUtilsTest.java   | 58 ++++++++++++++++++++++
 2 files changed, 64 insertions(+), 3 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/utils/LoadbalancerUtils.java
 
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/utils/LoadbalancerUtils.java
index 91e8e850ab..46acbba76c 100644
--- 
a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/utils/LoadbalancerUtils.java
+++ 
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/utils/LoadbalancerUtils.java
@@ -32,7 +32,7 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Objects;
+import java.util.Optional;
 
 /**
  * The type Loadbalancer utils.
@@ -68,8 +68,11 @@ public final class LoadbalancerUtils {
     
     private static LoadBalanceData buildLoadBalanceData(final 
ServerWebExchange exchange) {
         ServerHttpRequest request = exchange.getRequest();
-        String ip = 
Objects.requireNonNull(request.getRemoteAddress()).getAddress().getHostAddress();
-        String httpMethod = request.getMethod().name();
+        String ip = Optional.ofNullable(request.getRemoteAddress())
+                .map(address -> address.getAddress())
+                .map(address -> address.getHostAddress())
+                .orElse("127.0.0.1");
+        String httpMethod = 
Optional.ofNullable(request.getMethod()).map(method -> 
method.name()).orElse("GET");
         URI uri = exchange.getRequest().getURI();
         HttpHeaders headers = request.getHeaders();
         MultiValueMap<String, HttpCookie> cookies = request.getCookies();
diff --git 
a/shenyu-plugin/shenyu-plugin-base/src/test/java/org/apache/shenyu/plugin/base/utils/LoadbalancerUtilsTest.java
 
b/shenyu-plugin/shenyu-plugin-base/src/test/java/org/apache/shenyu/plugin/base/utils/LoadbalancerUtilsTest.java
new file mode 100644
index 0000000000..a2d8f27af1
--- /dev/null
+++ 
b/shenyu-plugin/shenyu-plugin-base/src/test/java/org/apache/shenyu/plugin/base/utils/LoadbalancerUtilsTest.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ *     http://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.apache.shenyu.plugin.base.utils;
+
+import org.apache.shenyu.common.enums.LoadBalanceEnum;
+import org.apache.shenyu.loadbalancer.entity.Upstream;
+import org.junit.jupiter.api.Test;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.server.reactive.ServerHttpRequest;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.web.server.ServerWebExchange;
+
+import java.net.URI;
+import java.util.Collections;
+import java.util.HashMap;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test cases for LoadbalancerUtils.
+ */
+public final class LoadbalancerUtilsTest {
+
+    @Test
+    public void testMissingRemoteAddressAndMethodUseDefaults() {
+        ServerHttpRequest request = mock(ServerHttpRequest.class);
+        when(request.getURI()).thenReturn(URI.create("http://localhost/test";));
+        when(request.getHeaders()).thenReturn(HttpHeaders.EMPTY);
+        when(request.getCookies()).thenReturn(new LinkedMultiValueMap<>());
+        when(request.getQueryParams()).thenReturn(new LinkedMultiValueMap<>());
+        ServerWebExchange exchange = mock(ServerWebExchange.class);
+        when(exchange.getRequest()).thenReturn(request);
+        when(exchange.getAttributes()).thenReturn(new HashMap<>());
+        Upstream upstream = 
Upstream.builder().url("http://localhost:8080";).build();
+
+        Upstream selected = 
LoadbalancerUtils.getForExchange(Collections.singletonList(upstream),
+                LoadBalanceEnum.RANDOM.getName(), exchange);
+
+        assertSame(upstream, selected);
+    }
+}

Reply via email to