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
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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<String> result = new TestRestTemplate().exchange("http://localhost:" + port + "/self/1",
|
||||
HttpMethod.DELETE, new HttpEntity<Void>((Void) null), String.class);
|
||||
assertEquals(HttpStatus.OK, result.getStatusCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user