Browse Source

More Zuul context resets

pull/6/head
Dave Syer 9 years ago
parent
commit
d5277925bc
  1. 9
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ContextPathZuulProxyApplicationTests.java
  2. 12
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/FormZuulServletProxyApplicationTests.java
  3. 132
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/RetryableZuulProxyApplicationTests.java
  4. 9
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ServletPathZuulProxyApplicationTests.java
  5. 8
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/SimpleZuulServerApplicationTests.java
  6. 8
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperIntegrationTests.java
  7. 15
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperTests.java
  8. 9
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/SendErrorFilterTests.java
  9. 14
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/SendForwardFilterTests.java

9
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ContextPathZuulProxyApplicationTests.java

@ -16,6 +16,7 @@ @@ -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; @@ -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 { @@ -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");

12
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/FormZuulServletProxyApplicationTests.java

@ -20,11 +20,6 @@ import java.io.IOException; @@ -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; @@ -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)

132
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/RetryableZuulProxyApplicationTests.java

@ -1,7 +1,6 @@ @@ -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; @@ -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<String, String> form = new LinkedMultiValueMap<String, String>();
form.set("foo", "bar");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + port + "/simple", HttpMethod.POST,
new HttpEntity<MultiValueMap<String,String>>(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<String, String> form = new LinkedMultiValueMap<String, String>();
form.set("foo", "bar");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/simple", HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(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 { @@ -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<String, String> 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<String, String> 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 { @@ -124,9 +130,7 @@ class RetryableRibbonClientConfiguration {
@Bean
public ServerList<Server> 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));
}
}

9
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/ServletPathZuulProxyApplicationTests.java

@ -16,6 +16,7 @@ @@ -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; @@ -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 { @@ -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");

8
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/SimpleZuulServerApplicationTests.java

@ -16,6 +16,7 @@ @@ -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; @@ -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 { @@ -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/**"));

8
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; @@ -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; @@ -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 { @@ -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();

15
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/discovery/PatternServiceRouteMapperTests.java

@ -1,7 +1,9 @@ @@ -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 { @@ -17,6 +19,12 @@ public class PatternServiceRouteMapperTests {
public static final String SERVICE_PATTERN = "(?<domain>^\\w+)(-(?<name>\\w+)-|-)(?<version>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 { @@ -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
+ "(?<nevermatch>.)?", "/${version}/${nevermatch}/${domain}/${name}/");
PatternServiceRouteMapper toTest = new PatternServiceRouteMapper(
SERVICE_PATTERN + "(?<nevermatch>.)?",
"/${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",

9
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; @@ -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; @@ -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

14
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/SendForwardFilterTests.java

@ -16,19 +16,19 @@ @@ -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 { @@ -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());

Loading…
Cancel
Save