Fix some zuul tests (clear request context before test)

This commit is contained in:
Dave Syer
2016-01-27 13:53:22 +00:00
parent 22b08f1a7f
commit 29e691a4da
4 changed files with 184 additions and 169 deletions

View File

@@ -20,7 +20,9 @@ import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.cloud.netflix.feign.valid.FeignHttpClientTests;
import org.springframework.cloud.netflix.zuul.filters.CustomHostRoutingFilterTests;
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelperTests;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocatorTests;
/**
* A test suite for probing weird ordering problems in the tests.
@@ -28,7 +30,8 @@ import org.springframework.cloud.netflix.feign.valid.FeignHttpClientTests;
* @author Dave Syer
*/
@RunWith(Suite.class)
@SuiteClasses({ FeignHttpClientTests.class })
@SuiteClasses({ DiscoveryClientRouteLocatorTests.class,
CustomHostRoutingFilterTests.class, ProxyRequestHelperTests.class })
@Ignore
public class AdhocTestSuite {

View File

@@ -1,10 +1,14 @@
package org.springframework.cloud.netflix.zuul.filters;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -37,8 +41,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.netflix.zuul.context.RequestContext;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
@@ -47,92 +50,98 @@ import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleCustomZuulProxyApplication.class)
@WebAppConfiguration
@IntegrationTest({"server.port: 0", "server.contextPath: /app"})
@IntegrationTest({ "server.port: 0", "server.contextPath: /app" })
@DirtiesContext
public class CustomHostRoutingFilterTests {
@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 getOnSelfViaCustomHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/app/self/get/1", String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Get 1", result.getBody());
}
@Before
public void setTestRequestcontext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
@Test
public void postOnSelfViaCustomHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("id", "2");
ResponseEntity<String> result = new TestRestTemplate().postForEntity(
"http://localhost:" + this.port + "/app/self/post", params, String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Post 2", result.getBody());
}
@Test
public void getOnSelfViaCustomHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/app/self/get/1", String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Get 1", result.getBody());
}
@Test
public void putOnSelfViaCustomHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/app/self/put/3", HttpMethod.PUT,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Put 3", result.getBody());
}
@Test
public void postOnSelfViaCustomHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("id", "2");
ResponseEntity<String> result = new TestRestTemplate().postForEntity(
"http://localhost:" + this.port + "/app/self/post", params, String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Post 2", result.getBody());
}
@Test
public void patchOnSelfViaCustomHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("patch", "5");
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/app/self/patch/4", HttpMethod.PATCH,
new HttpEntity<>(params), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Patch 45", result.getBody());
}
@Test
public void putOnSelfViaCustomHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/app/self/put/3", HttpMethod.PUT,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Put 3", result.getBody());
}
@Test
public void getOnSelfIgnoredHeaders() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/app/self/get/1", String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertTrue(result.getHeaders().containsKey("X-NotIgnored"));
assertFalse(result.getHeaders().containsKey("X-Ignored"));
}
@Test
public void patchOnSelfViaCustomHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("patch", "5");
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/app/self/patch/4", HttpMethod.PATCH,
new HttpEntity<>(params), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Patch 45", result.getBody());
}
@Test
public void getOnSelfWithSessionCookie() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
@Test
public void getOnSelfIgnoredHeaders() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().getForEntity(
"http://localhost:" + this.port + "/app/self/get/1", String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertTrue(result.getHeaders().containsKey("X-NotIgnored"));
assertFalse(result.getHeaders().containsKey("X-Ignored"));
}
RestTemplate restTemplate = new RestTemplate();
@Test
public void getOnSelfWithSessionCookie() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app");
this.endpoint.reset();
ResponseEntity<String> result1 = restTemplate.getForEntity(
"http://localhost:" + this.port + "/app/self/cookie/1", String.class);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> result2 = restTemplate.getForEntity(
"http://localhost:" + this.port + "/app/self/cookie/2", String.class);
ResponseEntity<String> result1 = restTemplate.getForEntity(
"http://localhost:" + this.port + "/app/self/cookie/1", String.class);
assertEquals("SetCookie 1", result1.getBody());
assertEquals("GetCookie 1", result2.getBody());
}
ResponseEntity<String> result2 = restTemplate.getForEntity(
"http://localhost:" + this.port + "/app/self/cookie/2", String.class);
assertEquals("SetCookie 1", result1.getBody());
assertEquals("GetCookie 1", result2.getBody());
}
}
@@ -141,72 +150,71 @@ public class CustomHostRoutingFilterTests {
@RestController
class SampleCustomZuulProxyApplication {
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
public String get(@PathVariable String id, HttpServletResponse response) {
response.setHeader("X-Ignored", "foo");
response.setHeader("X-NotIgnored", "bar");
return "Get " + id;
}
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
public String get(@PathVariable String id, HttpServletResponse response) {
response.setHeader("X-Ignored", "foo");
response.setHeader("X-NotIgnored", "bar");
return "Get " + id;
}
@RequestMapping(value = "/cookie/{id}", method = RequestMethod.GET)
public String getWithCookie(@PathVariable String id, HttpSession session) {
Object testCookie = session.getAttribute("testCookie");
if (testCookie != null) {
return "GetCookie " + testCookie;
}
session.setAttribute("testCookie", id);
return "SetCookie " + id;
}
@RequestMapping(value = "/cookie/{id}", method = RequestMethod.GET)
public String getWithCookie(@PathVariable String id, HttpSession session) {
Object testCookie = session.getAttribute("testCookie");
if (testCookie != null) {
return "GetCookie " + testCookie;
}
session.setAttribute("testCookie", id);
return "SetCookie " + id;
}
@RequestMapping(value = "/post", method = RequestMethod.POST)
public String post(@RequestParam("id") String id) {
return "Post " + id;
}
@RequestMapping(value = "/post", method = RequestMethod.POST)
public String post(@RequestParam("id") String id) {
return "Post " + id;
}
@RequestMapping(value = "/put/{id}", method = RequestMethod.PUT)
public String put(@PathVariable String id) {
return "Put " + id;
}
@RequestMapping(value = "/put/{id}", method = RequestMethod.PUT)
public String put(@PathVariable String id) {
return "Put " + id;
}
@RequestMapping(value = "/patch/{id}", method = RequestMethod.PATCH)
public String patch(@PathVariable String id, @RequestParam("patch") String patch) {
return "Patch " + id + patch;
}
@RequestMapping(value = "/patch/{id}", method = RequestMethod.PATCH)
public String patch(@PathVariable String id, @RequestParam("patch") String patch) {
return "Patch " + id + patch;
}
public static void main(String[] args) {
SpringApplication.run(SampleCustomZuulProxyApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(SampleCustomZuulProxyApplication.class, args);
}
@Configuration
@EnableZuulProxy
protected static class CustomZuulProxyConfig extends ZuulProxyConfiguration {
@Bean
@Override
public SimpleHostRoutingFilter simpleHostRoutingFilter() {
return new CustomHostRoutingFilter();
}
@Configuration
@EnableZuulProxy
protected static class CustomZuulProxyConfig extends ZuulProxyConfiguration {
@Bean
@Override
public SimpleHostRoutingFilter simpleHostRoutingFilter() {
return new CustomHostRoutingFilter();
}
private class CustomHostRoutingFilter extends SimpleHostRoutingFilter {
private class CustomHostRoutingFilter extends SimpleHostRoutingFilter {
@Override
public Object run() {
super.addIgnoredHeaders("X-Ignored");
return super.run();
}
@Override
public Object run() {
super.addIgnoredHeaders("X-Ignored");
return super.run();
}
@Override
protected CloseableHttpClient newClient() {
// Custom client with cookie support.
// In practice, we would want a custom cookie store using a multimap with a user key.
return HttpClients.custom()
.setConnectionManager(newConnectionManager())
.setDefaultCookieStore(new BasicCookieStore())
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.build())
.build();
}
}
}
@Override
protected CloseableHttpClient newClient() {
// Custom client with cookie support.
// In practice, we would want a custom cookie store using a multimap with
// a user key.
return HttpClients.custom().setConnectionManager(newConnectionManager())
.setDefaultCookieStore(new BasicCookieStore())
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT).build())
.build();
}
}
}
}

View File

@@ -56,6 +56,12 @@ public class ProxyRequestHelperTests {
initMocks(this);
}
@Before
public void setTestRequestcontext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
@Test
public void debug() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/");
@@ -130,10 +136,7 @@ public class ProxyRequestHelperTests {
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_ENCODING.toLowerCase(), "gzip");
helper.setResponse(
200,
request.getInputStream(),
headers);
helper.setResponse(200, request.getInputStream(), headers);
assertTrue(context.getResponseGZipped());
}
@@ -151,10 +154,7 @@ public class ProxyRequestHelperTests {
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_ENCODING, "gzip");
helper.setResponse(
200,
request.getInputStream(),
headers);
helper.setResponse(200, request.getInputStream(), headers);
assertTrue(context.getResponseGZipped());
}
}

View File

@@ -66,7 +66,7 @@ public class DiscoveryClientRouteLocatorTests {
@Before
public void init() {
initMocks(this);
setTestRequestcontext(); //re-initialize Zuul context for each test
setTestRequestcontext(); // re-initialize Zuul context for each test
}
@Test
@@ -96,8 +96,9 @@ public class DiscoveryClientRouteLocatorTests {
@Test
public void testGetMatchingPathWithServletPath() throws Exception {
setTestRequestcontext();
RequestContext.getCurrentContext().set(RequestUtils.IS_DISPATCHERSERVLETREQUEST, true);
setTestRequestcontext();
RequestContext.getCurrentContext().set(RequestUtils.IS_DISPATCHERSERVLETREQUEST,
true);
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/app",
this.discovery, this.properties);
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
@@ -107,20 +108,20 @@ public class DiscoveryClientRouteLocatorTests {
assertEquals("foo", route.getLocation());
assertEquals("/1", route.getPath());
}
@Test
public void testGetMatchingPathWithZuulServletPath() throws Exception {
RequestContext.getCurrentContext().setZuulEngineRan();
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/app",
this.discovery, this.properties);
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
this.properties.init();
routeLocator.getRoutes(); // force refresh
Route route = routeLocator.getMatchingRoute("/zuul/foo/1");
assertEquals("foo", route.getLocation());
assertEquals("/1", route.getPath());
}
@Test
public void testGetMatchingPathWithZuulServletPath() throws Exception {
RequestContext.getCurrentContext().setZuulEngineRan();
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/app",
this.discovery, this.properties);
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
this.properties.init();
routeLocator.getRoutes(); // force refresh
Route route = routeLocator.getMatchingRoute("/zuul/foo/1");
assertEquals("foo", route.getLocation());
assertEquals("/1", route.getPath());
}
@Test
public void testGetMatchingPathWithNoPrefixStripping() throws Exception {
@@ -161,10 +162,12 @@ public class DiscoveryClientRouteLocatorTests {
assertEquals("foo", route.getLocation());
assertEquals("/foo/1", route.getPath());
}
@Test
public void testGetMatchingPathWithGlobalPrefixStrippingAndServletPath() throws Exception {
RequestContext.getCurrentContext().set(RequestUtils.IS_DISPATCHERSERVLETREQUEST, true);
public void testGetMatchingPathWithGlobalPrefixStrippingAndServletPath()
throws Exception {
RequestContext.getCurrentContext().set(RequestUtils.IS_DISPATCHERSERVLETREQUEST,
true);
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/app",
this.discovery, this.properties);
this.properties.getRoutes().put("foo",
@@ -174,10 +177,11 @@ public class DiscoveryClientRouteLocatorTests {
Route route = routeLocator.getMatchingRoute("/app/proxy/foo/1");
assertEquals("foo", route.getLocation());
assertEquals("/foo/1", route.getPath());
}
}
@Test
public void testGetMatchingPathWithGlobalPrefixStrippingAndZuulServletPath() throws Exception {
public void testGetMatchingPathWithGlobalPrefixStrippingAndZuulServletPath()
throws Exception {
RequestContext.getCurrentContext().setZuulEngineRan();
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/",
this.discovery, this.properties);
@@ -188,7 +192,7 @@ public class DiscoveryClientRouteLocatorTests {
Route route = routeLocator.getMatchingRoute("/zuul/proxy/foo/1");
assertEquals("foo", route.getLocation());
assertEquals("/foo/1", route.getPath());
}
}
@Test
public void testGetMatchingPathWithRoutePrefixStripping() throws Exception {
@@ -644,10 +648,10 @@ public class DiscoveryClientRouteLocatorTests {
}
return null;
}
private void setTestRequestcontext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
}