From de4822e8019511b9f8b56f3795369e2ff69db71c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 26 Nov 2014 13:50:27 +0000 Subject: [PATCH] Add test for DELETE on zuul proxy route Also adds support for context paths in physical URL mappings, e.g. zuul.route.http\://localhost\:8081/app=/stores/** will result in a proxy forward of /stores/foo to http://localhost:8081/app/stores/foo Fixes gh-73 --- .../route/SimpleHostRoutingFilter.java | 10 +++---- .../zuul/sample/ZuulProxyApplication.java | 11 +++++++ .../sample/ZuulProxyApplicationTests.java | 29 +++++++++++++++++-- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/SimpleHostRoutingFilter.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/SimpleHostRoutingFilter.java index ab302137..67d8b014 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/SimpleHostRoutingFilter.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/SimpleHostRoutingFilter.java @@ -53,6 +53,7 @@ import org.apache.http.params.HttpParams; import org.apache.http.protocol.HttpContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.util.StringUtils; import com.google.common.base.Optional; import com.google.common.base.Predicate; @@ -230,7 +231,9 @@ public class SimpleHostRoutingFilter extends ZuulFilter { HttpServletRequest request, Header[] headers, InputStream requestEntity) throws Exception { - HttpHost httpHost = getHttpHost(); + URL host = RequestContext.getCurrentContext().getRouteHost(); + HttpHost httpHost = getHttpHost(host); + uri = StringUtils.cleanPath(host.getPath() + uri); HttpRequest httpRequest; @@ -279,12 +282,9 @@ public class SimpleHostRoutingFilter extends ZuulFilter { return (query != null) ? "?" + query : ""; } - HttpHost getHttpHost() { - URL host = RequestContext.getCurrentContext().getRouteHost(); - + HttpHost getHttpHost(URL host) { HttpHost httpHost = new HttpHost(host.getHost(), host.getPort(), host.getProtocol()); - return httpHost; } diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/sample/ZuulProxyApplication.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/sample/ZuulProxyApplication.java index cb6c46ed..3f1788a2 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/sample/ZuulProxyApplication.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/sample/ZuulProxyApplication.java @@ -5,6 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @@ -18,6 +19,16 @@ public class ZuulProxyApplication { throw new RuntimeException("myerror"); } + @RequestMapping("/local/self") + public String local() { + return "Hello local"; + } + + @RequestMapping(value="/local/self/{id}", method=RequestMethod.DELETE) + public String delete() { + return "Deleted!"; + } + @RequestMapping("/") public String home() { return "Hello world"; diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/sample/ZuulProxyApplicationTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/sample/ZuulProxyApplicationTests.java index a692efb1..c16370e4 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/sample/ZuulProxyApplicationTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/sample/ZuulProxyApplicationTests.java @@ -1,9 +1,20 @@ package org.springframework.cloud.netflix.zuul.sample; +import static org.junit.Assert.assertEquals; + import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.cloud.netflix.zuul.RouteLocator; +import org.springframework.cloud.netflix.zuul.ZuulHandlerMapping; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -13,8 +24,22 @@ import org.springframework.test.context.web.WebAppConfiguration; @IntegrationTest("server.port=0") public class ZuulProxyApplicationTests { - @Test - public void contextLoads() { + @Value("${local.server.port}") + private int port; + + @Autowired + private RouteLocator routes; + + @Autowired + private ZuulHandlerMapping mapping; + + @Test + public void deleteOnSelf() { + routes.getRoutes().put("/self/**", "http://localhost:" + port + "/local"); + mapping.reset(); + ResponseEntity result = new TestRestTemplate().exchange("http://localhost:" + port + "/self/1", + HttpMethod.DELETE, new HttpEntity((Void) null), String.class); + assertEquals(HttpStatus.OK, result.getStatusCode()); } }