From d5277925bc89995dba6ebf6b61472ecf333d09f9 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 3 Feb 2016 10:17:59 +0000 Subject: [PATCH] More Zuul context resets --- .../ContextPathZuulProxyApplicationTests.java | 9 ++ .../FormZuulServletProxyApplicationTests.java | 12 +- .../RetryableZuulProxyApplicationTests.java | 132 +++++++++--------- .../ServletPathZuulProxyApplicationTests.java | 9 ++ .../SimpleZuulServerApplicationTests.java | 8 ++ ...ernServiceRouteMapperIntegrationTests.java | 8 ++ .../PatternServiceRouteMapperTests.java | 15 +- .../filters/post/SendErrorFilterTests.java | 9 +- .../filters/route/SendForwardFilterTests.java | 14 +- 9 files changed, 138 insertions(+), 78 deletions(-) diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ContextPathZuulProxyApplicationTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ContextPathZuulProxyApplicationTests.java index 9359f7e5..2960b2e0 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ContextPathZuulProxyApplicationTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ContextPathZuulProxyApplicationTests.java @@ -16,6 +16,7 @@ package org.springframework.cloud.netflix.zuul; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -40,6 +41,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; +import com.netflix.zuul.context.RequestContext; + import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @@ -58,6 +61,12 @@ public class ContextPathZuulProxyApplicationTests { @Autowired private RoutesEndpoint endpoint; + @Before + public void setTestRequestcontext() { + RequestContext context = new RequestContext(); + RequestContext.testSetCurrentContext(context); + } + @Test public void getOnSelfViaSimpleHostRoutingFilter() { this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app/local"); diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/FormZuulServletProxyApplicationTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/FormZuulServletProxyApplicationTests.java index 549cd7cd..3c158023 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/FormZuulServletProxyApplicationTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/FormZuulServletProxyApplicationTests.java @@ -20,11 +20,6 @@ import java.io.IOException; import java.io.InputStream; import java.util.Map; -import com.netflix.loadbalancer.Server; -import com.netflix.loadbalancer.ServerList; -import com.netflix.zuul.ZuulFilter; -import com.netflix.zuul.context.RequestContext; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -57,10 +52,15 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import lombok.extern.slf4j.Slf4j; +import com.netflix.loadbalancer.Server; +import com.netflix.loadbalancer.ServerList; +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.context.RequestContext; import static org.junit.Assert.assertEquals; +import lombok.extern.slf4j.Slf4j; + @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = FormZuulServletProxyApplication.class) @WebIntegrationTest(value = "zuul.routes.simple:/simple/**", randomPort = true) diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/RetryableZuulProxyApplicationTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/RetryableZuulProxyApplicationTests.java index 02d52a77..c6c7e375 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/RetryableZuulProxyApplicationTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/RetryableZuulProxyApplicationTests.java @@ -1,7 +1,6 @@ package org.springframework.cloud.netflix.zuul; -import static org.junit.Assert.assertEquals; - +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -35,43 +34,50 @@ import org.springframework.web.bind.annotation.RestController; import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.ServerList; import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.context.RequestContext; + +import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = RetryableZuulProxyApplication.class) @WebAppConfiguration -@IntegrationTest({ "server.port: 0", - "zuul.routes.simple.path: /simple/**", - "zuul.routes.simple.retryable: true", - "ribbon.OkToRetryOnAllOperations: true" -}) +@IntegrationTest({ "server.port: 0", "zuul.routes.simple.path: /simple/**", + "zuul.routes.simple.retryable: true", "ribbon.OkToRetryOnAllOperations: true" }) @DirtiesContext public class RetryableZuulProxyApplicationTests { - @Value("${local.server.port}") - private int port; + @Value("${local.server.port}") + private int port; - @Autowired - private DiscoveryClientRouteLocator routes; + @Autowired + private DiscoveryClientRouteLocator routes; - @Autowired - private RoutesEndpoint endpoint; + @Autowired + private RoutesEndpoint endpoint; - @Test - public void postWithForm() { - MultiValueMap form = new LinkedMultiValueMap(); - form.set("foo", "bar"); - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); - ResponseEntity result = new TestRestTemplate().exchange( - "http://localhost:" + port + "/simple", HttpMethod.POST, - new HttpEntity>(form, headers), String.class); - assertEquals(HttpStatus.OK, result.getStatusCode()); - assertEquals("Posted! {foo=[bar]}", result.getBody()); - } + @Before + public void setTestRequestcontext() { + RequestContext context = new RequestContext(); + RequestContext.testSetCurrentContext(context); + } + + @Test + public void postWithForm() { + MultiValueMap form = new LinkedMultiValueMap(); + form.set("foo", "bar"); + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); + ResponseEntity result = new TestRestTemplate().exchange( + "http://localhost:" + this.port + "/simple", HttpMethod.POST, + new HttpEntity>(form, headers), + String.class); + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertEquals("Posted! {foo=[bar]}", result.getBody()); + } } -//Don't use @SpringBootApplication because we don't want to component scan +// Don't use @SpringBootApplication because we don't want to component scan @Configuration @EnableAutoConfiguration @RestController @@ -79,43 +85,43 @@ public class RetryableZuulProxyApplicationTests { @RibbonClient(name = "simple", configuration = RetryableRibbonClientConfiguration.class) class RetryableZuulProxyApplication { - @RequestMapping(value = "/", method = RequestMethod.POST) - public String delete(@RequestBody MultiValueMap form) { - return "Posted! " + form; - } - - @Bean - public ZuulFilter sampleFilter() { - return new ZuulFilter() { - @Override - public String filterType() { - return "pre"; - } - - @Override - public boolean shouldFilter() { - return true; - } - - @Override - public Object run() { - return null; - } - - @Override - public int filterOrder() { - return 0; - } - }; - } - - public static void main(String[] args) { - SpringApplication.run(SampleZuulProxyApplication.class, args); - } + @RequestMapping(value = "/", method = RequestMethod.POST) + public String delete(@RequestBody MultiValueMap form) { + return "Posted! " + form; + } + + @Bean + public ZuulFilter sampleFilter() { + return new ZuulFilter() { + @Override + public String filterType() { + return "pre"; + } + + @Override + public boolean shouldFilter() { + return true; + } + + @Override + public Object run() { + return null; + } + + @Override + public int filterOrder() { + return 0; + } + }; + } + + public static void main(String[] args) { + SpringApplication.run(SampleZuulProxyApplication.class, args); + } } -//Load balancer with fixed server list for "simple" pointing to localhost +// Load balancer with fixed server list for "simple" pointing to localhost @Configuration class RetryableRibbonClientConfiguration { @@ -124,9 +130,7 @@ class RetryableRibbonClientConfiguration { @Bean public ServerList ribbonServerList() { - return new StaticServerList<>( - new Server("localhost", port), - new Server("failed-localhost", port) - ); + return new StaticServerList<>(new Server("localhost", this.port), + new Server("failed-localhost", this.port)); } } \ No newline at end of file diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ServletPathZuulProxyApplicationTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ServletPathZuulProxyApplicationTests.java index eb6f9963..84a0d3e6 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ServletPathZuulProxyApplicationTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ServletPathZuulProxyApplicationTests.java @@ -16,6 +16,7 @@ package org.springframework.cloud.netflix.zuul; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -40,6 +41,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; +import com.netflix.zuul.context.RequestContext; + import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @@ -58,6 +61,12 @@ public class ServletPathZuulProxyApplicationTests { @Autowired private RoutesEndpoint endpoint; + @Before + public void setTestRequestcontext() { + RequestContext context = new RequestContext(); + RequestContext.testSetCurrentContext(context); + } + @Test public void getOnSelfViaSimpleHostRoutingFilter() { this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app/local"); diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/SimpleZuulServerApplicationTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/SimpleZuulServerApplicationTests.java index de02f851..643101df 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/SimpleZuulServerApplicationTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/SimpleZuulServerApplicationTests.java @@ -16,6 +16,7 @@ package org.springframework.cloud.netflix.zuul; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -39,6 +40,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.context.RequestContext; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -60,6 +62,12 @@ public class SimpleZuulServerApplicationTests { return this.routes.getMatchingRoute(path).getLocation(); } + @Before + public void setTestRequestcontext() { + RequestContext context = new RequestContext(); + RequestContext.testSetCurrentContext(context); + } + @Test public void bindRoute() { assertNotNull(getRoute("/testing123/**")); diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperIntegrationTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperIntegrationTests.java index 82b52f52..be2a06f1 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperIntegrationTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperIntegrationTests.java @@ -3,6 +3,7 @@ package org.springframework.cloud.netflix.zuul.filters.discovery; import java.util.ArrayList; import java.util.List; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -33,6 +34,7 @@ import org.springframework.web.bind.annotation.RestController; import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.ServerList; +import com.netflix.zuul.context.RequestContext; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; @@ -61,6 +63,12 @@ public class PatternServiceRouteMapperIntegrationTests { @Autowired private RoutesEndpoint endpoint; + @Before + public void setTestRequestcontext() { + RequestContext context = new RequestContext(); + RequestContext.testSetCurrentContext(context); + } + @Test public void getRegexMappedService() { this.endpoint.reset(); diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperTests.java index 1a26a2c7..62b457fc 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperTests.java @@ -1,7 +1,9 @@ package org.springframework.cloud.netflix.zuul.filters.discovery; +import org.junit.Before; import org.junit.Test; -import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper; + +import com.netflix.zuul.context.RequestContext; import static org.junit.Assert.assertEquals; @@ -17,6 +19,12 @@ public class PatternServiceRouteMapperTests { public static final String SERVICE_PATTERN = "(?^\\w+)(-(?\\w+)-|-)(?v\\d+$)"; public static final String ROUTE_PATTERN = "${version}/${domain}/${name}"; + @Before + public void setTestRequestcontext() { + RequestContext context = new RequestContext(); + RequestContext.testSetCurrentContext(context); + } + @Test public void test_return_mapped_route_if_serviceid_matches() { PatternServiceRouteMapper toTest = new PatternServiceRouteMapper(SERVICE_PATTERN, @@ -39,8 +47,9 @@ public class PatternServiceRouteMapperTests { @Test public void test_route_should_be_cleaned_before_returned() { // Messy patterns - PatternServiceRouteMapper toTest = new PatternServiceRouteMapper(SERVICE_PATTERN - + "(?.)?", "/${version}/${nevermatch}/${domain}/${name}/"); + PatternServiceRouteMapper toTest = new PatternServiceRouteMapper( + SERVICE_PATTERN + "(?.)?", + "/${version}/${nevermatch}/${domain}/${name}/"); assertEquals("No matches for this service id", "v1/domain/service", toTest.apply("domain-service-v1")); assertEquals("No matches for this service id", "v1/domain", diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/SendErrorFilterTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/SendErrorFilterTests.java index f134d853..ce270fb4 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/SendErrorFilterTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/SendErrorFilterTests.java @@ -19,6 +19,7 @@ package org.springframework.cloud.netflix.zuul.filters.post; import javax.servlet.http.HttpServletRequest; import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.springframework.http.HttpStatus; import org.springframework.mock.web.MockHttpServletRequest; @@ -35,9 +36,15 @@ import static org.mockito.Mockito.mock; */ public class SendErrorFilterTests { + @Before + public void setTestRequestcontext() { + RequestContext context = new RequestContext(); + RequestContext.testSetCurrentContext(context); + } + @After public void reset() { - RequestContext.testSetCurrentContext(null); + RequestContext.getCurrentContext().clear(); } @Test diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/SendForwardFilterTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/SendForwardFilterTests.java index 2cbf601e..91598e34 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/SendForwardFilterTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/SendForwardFilterTests.java @@ -16,19 +16,19 @@ package org.springframework.cloud.netflix.zuul.filters.route; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - import javax.servlet.http.HttpServletRequest; import org.junit.After; +import org.junit.Before; import org.junit.Test; -import org.springframework.cloud.netflix.zuul.filters.route.SendForwardFilter; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import com.netflix.zuul.context.RequestContext; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + /** * @author Dave Syer */ @@ -39,6 +39,12 @@ public class SendForwardFilterTests { RequestContext.testSetCurrentContext(null); } + @Before + public void setTestRequestcontext() { + RequestContext context = new RequestContext(); + RequestContext.testSetCurrentContext(context); + } + @Test public void runsNormally() { SendForwardFilter filter = createSendForwardFilter(new MockHttpServletRequest());