spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

Some Route Predicates Don't Work With Regex Patterns

Open ryanjbaxter opened this issue 2 years ago • 1 comments

The cookie, header, and query route predicate factories don't match .* patterns and instead return a 404. At the very least it should return a 500, but I believe these patterns should match

spring:  
  cloud:    
    gateway:      
      routes:      
      - id: query_route        
         uri: https://example.org        
         predicates:        
         - Query=test, a.*b
spring:  
  cloud:    
    gateway:      
      routes:      
      - id: header_route        
         uri: https://example.org        
         predicates:        
         - Header=Test, a.*b
spring:  
  cloud:    
    gateway:      
      routes:      
      - id: header_route        
         uri: https://example.org        
         predicates:        
         - Cookie=test, a.*b

ryanjbaxter avatar Jun 23 '22 23:06 ryanjbaxter

Could you please specify exact steps to reproduce the problem? What is your Spring Boot/Spring Cloud version? I tried to reproduce the issue myself, but I didn't. Here's a small MRE that doesn't reproduce it

package com.example.gatewaydemo.routeDefinition;

import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.reactive.function.client.WebClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class RouteDefinitionRouteLocatorTest {
	@LocalServerPort
	int port;

	@Test
	void test_ab() {
		testWithTestHeaderValue("ab");
	}

	@Test
	void test_aab() {
		testWithTestHeaderValue("aab");
	}

	private void testWithTestHeaderValue(String headerValue) {
		Mono<ResponseEntity<Void>> responseEntityMono = WebClient.builder()
				.baseUrl("http://localhost:" + port)
				.build()
				.get()
				.header("test", headerValue)
				.retrieve()
				.toBodilessEntity();
		StepVerifier.create(responseEntityMono)
				.expectNextMatches(r -> r.getStatusCode().is2xxSuccessful())
				.verifyComplete();
	}

	@Configuration
	@EnableAutoConfiguration
	static class RouteDefinitionRouteLocatorConfig {
	}
}
# application.yml

spring:
  cloud:
    gateway:
      routes:
        - id: test-header-route
          uri: https://httpbin.org/status/200
          predicates:
            - Header=test, a.*b
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>gatewaydemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gatewaydemo</name>
    <description>gatewaydemo</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2022.0.4</spring-cloud.version>
    </properties>

NadChel avatar Mar 22 '24 08:03 NadChel