Commit e73ee5f6 by Lars Committed by Spencer Gibb

Refactors zuul tests for better readability. (#1686)

* Refactors tests for better readability. Polishes imports. Removes hostname and port from TestRestTemplate-calls. Introduces @LocalServerPort
parent de886d6c
...@@ -22,8 +22,8 @@ import org.junit.Before; ...@@ -22,8 +22,8 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -44,22 +44,27 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -44,22 +44,27 @@ import org.springframework.web.bind.annotation.RestController;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ContextPathZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = { @SpringBootTest(
"server.contextPath: /app" }) classes = ContextPathZuulProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = {"server.contextPath: /app"})
@DirtiesContext @DirtiesContext
public class ContextPathZuulProxyApplicationTests { public class ContextPathZuulProxyApplicationTests {
@Value("${local.server.port}") @LocalServerPort
private int port; private int port;
@Autowired @Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private DiscoveryClientRouteLocator routes; private DiscoveryClientRouteLocator routes;
@Autowired @Autowired
private RoutesEndpoint endpoint; private RoutesEndpoint endpoint;
@Before @Before
public void setTestRequestcontext() { public void setTestRequestContext() {
RequestContext context = new RequestContext(); RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context); RequestContext.testSetCurrentContext(context);
} }
...@@ -68,7 +73,7 @@ public class ContextPathZuulProxyApplicationTests { ...@@ -68,7 +73,7 @@ public class ContextPathZuulProxyApplicationTests {
public void getOnSelfViaSimpleHostRoutingFilter() { public void getOnSelfViaSimpleHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app/local"); this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app/local");
this.endpoint.reset(); this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/app/self/1", HttpMethod.GET, "http://localhost:" + this.port + "/app/self/1", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class); new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
...@@ -80,7 +85,7 @@ public class ContextPathZuulProxyApplicationTests { ...@@ -80,7 +85,7 @@ public class ContextPathZuulProxyApplicationTests {
this.routes.addRoute(new ZuulRoute("strip", "/strip/**", "strip", this.routes.addRoute(new ZuulRoute("strip", "/strip/**", "strip",
"http://localhost:" + this.port + "/app/local", false, false, null)); "http://localhost:" + this.port + "/app/local", false, false, null));
this.endpoint.reset(); this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/app/strip", HttpMethod.GET, "http://localhost:" + this.port + "/app/strip", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class); new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
......
...@@ -16,9 +16,16 @@ ...@@ -16,9 +16,16 @@
package org.springframework.cloud.netflix.zuul; package org.springframework.cloud.netflix.zuul;
import static java.nio.charset.Charset.defaultCharset;
import static org.junit.Assert.assertEquals;
import static org.springframework.util.StreamUtils.copyToString;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.Part;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -56,18 +63,13 @@ import com.netflix.loadbalancer.ServerList; ...@@ -56,18 +63,13 @@ import com.netflix.loadbalancer.ServerList;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
import static java.nio.charset.Charset.defaultCharset;
import static org.junit.Assert.assertEquals;
import static org.springframework.util.StreamUtils.copyToString;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import javax.inject.Inject;
import javax.servlet.http.Part;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = FormZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = { @SpringBootTest(
"zuul.routes.simple:/simple/**" }) classes = FormZuulProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = {"zuul.routes.simple:/simple/**"})
@DirtiesContext @DirtiesContext
public class FormZuulProxyApplicationTests { public class FormZuulProxyApplicationTests {
...@@ -215,7 +217,7 @@ public class FormZuulProxyApplicationTests { ...@@ -215,7 +217,7 @@ public class FormZuulProxyApplicationTests {
@EnableZuulProxy @EnableZuulProxy
@RibbonClients({ @RibbonClients({
@RibbonClient(name = "simple", configuration = FormRibbonClientConfiguration.class), @RibbonClient(name = "simple", configuration = FormRibbonClientConfiguration.class),
@RibbonClient(name = "psimple", configuration = FormRibbonClientConfiguration.class) }) @RibbonClient(name = "psimple", configuration = FormRibbonClientConfiguration.class)})
@Slf4j @Slf4j
class FormZuulProxyApplication { class FormZuulProxyApplication {
...@@ -297,7 +299,8 @@ class FormZuulProxyApplication { ...@@ -297,7 +299,8 @@ class FormZuulProxyApplication {
new SpringApplicationBuilder(FormZuulProxyApplication.class) new SpringApplicationBuilder(FormZuulProxyApplication.class)
.properties("zuul.routes.simple:/simple/**", .properties("zuul.routes.simple:/simple/**",
"zuul.routes.direct.url:http://localhost:9999", "zuul.routes.direct.url:http://localhost:9999",
"multipart.maxFileSize:4096MB", "multipart.maxRequestSize:4096MB") "multipart.maxFileSize:4096MB",
"multipart.maxRequestSize:4096MB")
.run(args); .run(args);
} }
......
...@@ -25,11 +25,12 @@ import java.util.Map; ...@@ -25,11 +25,12 @@ import java.util.Map;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.trace.InMemoryTraceRepository; import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -62,15 +63,18 @@ import com.netflix.zuul.context.RequestContext; ...@@ -62,15 +63,18 @@ import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = FormZuulServletProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = "zuul.routes.simple:/simple/**") @SpringBootTest(
classes = FormZuulServletProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = "zuul.routes.simple:/simple/**")
@DirtiesContext @DirtiesContext
public class FormZuulServletProxyApplicationTests { public class FormZuulServletProxyApplicationTests {
@Value("${local.server.port}") @Autowired
private int port; private TestRestTemplate testRestTemplate;
@Before @Before
public void setTestRequestcontext() { public void setTestRequestContext() {
RequestContext context = new RequestContext(); RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context); RequestContext.testSetCurrentContext(context);
} }
...@@ -81,8 +85,8 @@ public class FormZuulServletProxyApplicationTests { ...@@ -81,8 +85,8 @@ public class FormZuulServletProxyApplicationTests {
form.set("foo", "bar"); form.set("foo", "bar");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/zuul/simple/form", HttpMethod.POST, "/zuul/simple/form", HttpMethod.POST,
new HttpEntity<>(form, headers), String.class); new HttpEntity<>(form, headers), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! {foo=[bar]}", result.getBody()); assertEquals("Posted! {foo=[bar]}", result.getBody());
...@@ -94,8 +98,8 @@ public class FormZuulServletProxyApplicationTests { ...@@ -94,8 +98,8 @@ public class FormZuulServletProxyApplicationTests {
form.set("foo", "bar"); form.set("foo", "bar");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.setContentType(MediaType.MULTIPART_FORM_DATA);
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/zuul/simple/form", HttpMethod.POST, "/zuul/simple/form", HttpMethod.POST,
new HttpEntity<>(form, headers), String.class); new HttpEntity<>(form, headers), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! {foo=[bar]}", result.getBody()); assertEquals("Posted! {foo=[bar]}", result.getBody());
...@@ -112,8 +116,8 @@ public class FormZuulServletProxyApplicationTests { ...@@ -112,8 +116,8 @@ public class FormZuulServletProxyApplicationTests {
headers.setContentType(MediaType.MULTIPART_FORM_DATA); headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set("Transfer-Encoding", "chunked"); headers.set("Transfer-Encoding", "chunked");
headers.setContentLength(-1); headers.setContentLength(-1);
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/zuul/simple/file", HttpMethod.POST, "/zuul/simple/file", HttpMethod.POST,
new HttpEntity<>(form, headers), String.class); new HttpEntity<>(form, headers), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! bar", result.getBody()); assertEquals("Posted! bar", result.getBody());
...@@ -126,8 +130,8 @@ public class FormZuulServletProxyApplicationTests { ...@@ -126,8 +130,8 @@ public class FormZuulServletProxyApplicationTests {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf( headers.setContentType(MediaType.valueOf(
MediaType.APPLICATION_FORM_URLENCODED_VALUE + "; charset=UTF-8")); MediaType.APPLICATION_FORM_URLENCODED_VALUE + "; charset=UTF-8"));
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/zuul/simple/form", HttpMethod.POST, "/zuul/simple/form", HttpMethod.POST,
new HttpEntity<>(form, headers), String.class); new HttpEntity<>(form, headers), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! {foo=[bar]}", result.getBody()); assertEquals("Posted! {foo=[bar]}", result.getBody());
...@@ -163,8 +167,7 @@ class FormZuulServletProxyApplication { ...@@ -163,8 +167,7 @@ class FormZuulServletProxyApplication {
while (inputStream.read(buffer) >= 0) { while (inputStream.read(buffer) >= 0) {
log.info("Read more bytes"); log.info("Read more bytes");
} }
} } else {
else {
bytes = file.getBytes(); bytes = file.getBytes();
} }
} }
...@@ -216,7 +219,8 @@ class FormZuulServletProxyApplication { ...@@ -216,7 +219,8 @@ class FormZuulServletProxyApplication {
.properties("zuul.routes.simple:/zuul/simple/**", .properties("zuul.routes.simple:/zuul/simple/**",
"zuul.routes.direct.url:http://localhost:9999", "zuul.routes.direct.url:http://localhost:9999",
"zuul.routes.direct.path:/zuul/direct/**", "zuul.routes.direct.path:/zuul/direct/**",
"multipart.maxFileSize:4096MB", "multipart.maxRequestSize:4096MB") "multipart.maxFileSize:4096MB",
"multipart.maxRequestSize:4096MB")
.run(args); .run(args);
} }
...@@ -226,7 +230,7 @@ class FormZuulServletProxyApplication { ...@@ -226,7 +230,7 @@ class FormZuulServletProxyApplication {
@Configuration @Configuration
class ServletFormRibbonClientConfiguration { class ServletFormRibbonClientConfiguration {
@Value("${local.server.port}") @LocalServerPort
private int port; private int port;
@Bean @Bean
......
...@@ -6,8 +6,8 @@ import org.junit.Before; ...@@ -6,8 +6,8 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -37,14 +37,18 @@ import com.netflix.zuul.ZuulFilter; ...@@ -37,14 +37,18 @@ import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = RetryableZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = { @SpringBootTest(
"zuul.routes.simple.path: /simple/**", "zuul.routes.simple.retryable: true", classes = RetryableZuulProxyApplication.class,
"ribbon.OkToRetryOnAllOperations: true" }) webEnvironment = WebEnvironment.RANDOM_PORT,
value = {
"zuul.routes.simple.path: /simple/**",
"zuul.routes.simple.retryable: true",
"ribbon.OkToRetryOnAllOperations: true"})
@DirtiesContext @DirtiesContext
public class RetryableZuulProxyApplicationTests { public class RetryableZuulProxyApplicationTests {
@Value("${local.server.port}") @Autowired
private int port; private TestRestTemplate testRestTemplate;
@Autowired @Autowired
@SuppressWarnings("unused") @SuppressWarnings("unused")
...@@ -55,7 +59,7 @@ public class RetryableZuulProxyApplicationTests { ...@@ -55,7 +59,7 @@ public class RetryableZuulProxyApplicationTests {
private RoutesEndpoint endpoint; private RoutesEndpoint endpoint;
@Before @Before
public void setTestRequestcontext() { public void setTestRequestContext() {
RequestContext context = new RequestContext(); RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context); RequestContext.testSetCurrentContext(context);
} }
...@@ -66,10 +70,9 @@ public class RetryableZuulProxyApplicationTests { ...@@ -66,10 +70,9 @@ public class RetryableZuulProxyApplicationTests {
form.set("foo", "bar"); form.set("foo", "bar");
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/simple", HttpMethod.POST, "/simple", HttpMethod.POST,
new HttpEntity<MultiValueMap<String, String>>(form, headers), new HttpEntity<>(form, headers), String.class);
String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! {foo=[bar]}", result.getBody()); assertEquals("Posted! {foo=[bar]}", result.getBody());
} }
...@@ -120,7 +123,7 @@ class RetryableZuulProxyApplication { ...@@ -120,7 +123,7 @@ class RetryableZuulProxyApplication {
@Configuration @Configuration
class RetryableRibbonClientConfiguration { class RetryableRibbonClientConfiguration {
@Value("${local.server.port}") @LocalServerPort
private int port; private int port;
@Bean @Bean
......
...@@ -19,14 +19,13 @@ package org.springframework.cloud.netflix.zuul; ...@@ -19,14 +19,13 @@ package org.springframework.cloud.netflix.zuul;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -34,6 +33,7 @@ import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute; ...@@ -34,6 +33,7 @@ import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator; import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity; import org.springframework.http.RequestEntity;
...@@ -45,67 +45,86 @@ import org.springframework.web.bind.annotation.PathVariable; ...@@ -45,67 +45,86 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ServletPathZuulProxyApplicationTests.ServletPathZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = { @SpringBootTest(
"server.port: 0", "server.servletPath: /app" }) classes = ServletPathZuulProxyApplicationTests.ServletPathZuulProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = {"server.servletPath: /app"})
@DirtiesContext @DirtiesContext
public class ServletPathZuulProxyApplicationTests { public class ServletPathZuulProxyApplicationTests {
@Value("${local.server.port}") @LocalServerPort
private int port; private int port;
@Autowired @Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private DiscoveryClientRouteLocator routes; private DiscoveryClientRouteLocator routes;
@Autowired @Autowired
private RoutesEndpoint endpoint; private RoutesEndpoint endpoint;
@Before @Before
public void setTestRequestcontext() { public void setTestRequestContext() {
RequestContext context = new RequestContext(); RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context); RequestContext.testSetCurrentContext(context);
} }
@Test @Test
public void getOnSelfViaSimpleHostRoutingFilter() { public void getOnSelfViaSimpleHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app/local"); this.routes.addRoute("/self/**",
"http://localhost:" + this.port + "/app/local");
this.endpoint.reset(); this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange("http://localhost:" + this.port + "/app/self/1", ResponseEntity<String> result = testRestTemplate.exchange(
HttpMethod.GET, new HttpEntity<>((Void) null), String.class); "/app/self/1", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Gotten 1!", result.getBody()); assertEquals("Gotten 1!", result.getBody());
} }
@Test @Test
public void optionsOnRawEndpoint() throws Exception { public void optionsOnRawEndpoint() throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity ResponseEntity<String> result = testRestTemplate.exchange(
.options(new URI("http://localhost:" + this.port + "/app/local/1")) RequestEntity
.header("Origin", "http://localhost:9000").header("Access-Control-Request-Method", "GET").build(), .options(new URI("/app/local/1"))
.header("Origin", "http://localhost:9000")
.header("Access-Control-Request-Method", "GET")
.build(),
String.class); String.class);
HttpHeaders httpHeaders = result.getHeaders();
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("http://localhost:9000", result.getHeaders().getFirst("Access-Control-Allow-Origin")); assertEquals("http://localhost:9000", httpHeaders.getFirst("Access-Control-Allow-Origin"));
} }
@Test @Test
public void optionsOnSelf() throws Exception { public void optionsOnSelf() throws Exception {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app/local"); this.routes.addRoute("/self/**",
"http://localhost:" + this.port + "/app/local");
this.endpoint.reset(); this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity ResponseEntity<String> result = testRestTemplate.exchange(
.options(new URI("http://localhost:" + this.port + "/app/self/1")) RequestEntity
.header("Origin", "http://localhost:9000").header("Access-Control-Request-Method", "GET").build(), .options(new URI("/app/self/1"))
.header("Origin", "http://localhost:9000")
.header("Access-Control-Request-Method", "GET")
.build(),
String.class); String.class);
HttpHeaders httpHeaders = result.getHeaders();
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("http://localhost:9000", result.getHeaders().getFirst("Access-Control-Allow-Origin")); assertEquals("http://localhost:9000", httpHeaders.getFirst("Access-Control-Allow-Origin"));
} }
@Test @Test
public void contentOnRawEndpoint() throws Exception { public void contentOnRawEndpoint() throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
RequestEntity.get(new URI("http://localhost:" + this.port + "/app/local/1")).build(), String.class); RequestEntity
.get(new URI("/app/local/1"))
.build(), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Gotten 1!", result.getBody()); assertEquals("Gotten 1!", result.getBody());
} }
...@@ -115,8 +134,9 @@ public class ServletPathZuulProxyApplicationTests { ...@@ -115,8 +134,9 @@ public class ServletPathZuulProxyApplicationTests {
this.routes.addRoute(new ZuulRoute("strip", "/strip/**", "strip", this.routes.addRoute(new ZuulRoute("strip", "/strip/**", "strip",
"http://localhost:" + this.port + "/app/local", false, false, null)); "http://localhost:" + this.port + "/app/local", false, false, null));
this.endpoint.reset(); this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange("http://localhost:" + this.port + "/app/strip", ResponseEntity<String> result = testRestTemplate.exchange(
HttpMethod.GET, new HttpEntity<>((Void) null), String.class); "/app/strip", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
// Prefix not stripped to it goes to /local/strip // Prefix not stripped to it goes to /local/strip
assertEquals("Gotten strip!", result.getBody()); assertEquals("Gotten strip!", result.getBody());
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
package org.springframework.cloud.netflix.zuul; package org.springframework.cloud.netflix.zuul;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
...@@ -28,8 +28,8 @@ import org.junit.Ignore; ...@@ -28,8 +28,8 @@ import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -49,22 +49,27 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -49,22 +49,27 @@ import org.springframework.web.bind.annotation.RestController;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SimpleZuulProxyApplicationTests.SimpleZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = { @SpringBootTest(
"server.port: 0", "zuul.forceOriginalQueryStringEncoding: true" }) classes = SimpleZuulProxyApplicationTests.SimpleZuulProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = {"zuul.forceOriginalQueryStringEncoding: true"})
@DirtiesContext @DirtiesContext
public class SimpleZuulProxyApplicationTests { public class SimpleZuulProxyApplicationTests {
@Value("${local.server.port}") @LocalServerPort
private int port; private int port;
@Autowired @Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private DiscoveryClientRouteLocator routes; private DiscoveryClientRouteLocator routes;
@Autowired @Autowired
private RoutesEndpoint endpoint; private RoutesEndpoint endpoint;
@Before @Before
public void setTestRequestcontext() { public void setTestRequestContext() {
RequestContext context = new RequestContext(); RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context); RequestContext.testSetCurrentContext(context);
...@@ -111,9 +116,9 @@ public class SimpleZuulProxyApplicationTests { ...@@ -111,9 +116,9 @@ public class SimpleZuulProxyApplicationTests {
@Ignore @Ignore
public void getOnSelfWithComplexQueryParam() throws URISyntaxException { public void getOnSelfWithComplexQueryParam() throws URISyntaxException {
String encodedQueryString = "foo=%7B%22project%22%3A%22stream%22%2C%22logger%22%3A%22javascript%22%2C%22platform%22%3A%22javascript%22%2C%22request%22%3A%7B%22url%22%3A%22https%3A%2F%2Ffoo%2Fadmin"; String encodedQueryString = "foo=%7B%22project%22%3A%22stream%22%2C%22logger%22%3A%22javascript%22%2C%22platform%22%3A%22javascript%22%2C%22request%22%3A%7B%22url%22%3A%22https%3A%2F%2Ffoo%2Fadmin";
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
new URI("http://localhost:" + this.port + "/foo?" + encodedQueryString), new URI("/foo?" + encodedQueryString), HttpMethod.GET,
HttpMethod.GET, new HttpEntity<>((Void) null), String.class); new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals(encodedQueryString, result.getBody()); assertEquals(encodedQueryString, result.getBody());
...@@ -126,8 +131,8 @@ public class SimpleZuulProxyApplicationTests { ...@@ -126,8 +131,8 @@ public class SimpleZuulProxyApplicationTests {
} }
private ResponseEntity<String> executeSimpleRequest(HttpMethod httpMethod) { private ResponseEntity<String> executeSimpleRequest(HttpMethod httpMethod) {
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/foo?id=bar", httpMethod, "/foo?id=bar", httpMethod,
new HttpEntity<>((Void) null), String.class); new HttpEntity<>((Void) null), String.class);
return result; return result;
} }
...@@ -144,7 +149,7 @@ public class SimpleZuulProxyApplicationTests { ...@@ -144,7 +149,7 @@ public class SimpleZuulProxyApplicationTests {
return "get " + id; return "get " + id;
} }
@RequestMapping(value = "/bar", method = RequestMethod.GET, params = { "foo" }) @RequestMapping(value = "/bar", method = RequestMethod.GET, params = {"foo"})
public String complexGet(@RequestParam String foo, HttpServletRequest request) { public String complexGet(@RequestParam String foo, HttpServletRequest request) {
return request.getQueryString(); return request.getQueryString();
} }
......
...@@ -23,9 +23,9 @@ import org.junit.Before; ...@@ -23,9 +23,9 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.test.web.client.TestRestTemplate;
...@@ -45,14 +45,19 @@ import com.netflix.zuul.ZuulFilter; ...@@ -45,14 +45,19 @@ import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SimpleZuulServerApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(
classes = SimpleZuulServerApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext @DirtiesContext
public class SimpleZuulServerApplicationTests { public class SimpleZuulServerApplicationTests {
@Value("${local.server.port}") @LocalServerPort
private int port; private int port;
@Autowired @Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private RouteLocator routes; private RouteLocator routes;
private String getRoute(String path) { private String getRoute(String path) {
...@@ -60,7 +65,7 @@ public class SimpleZuulServerApplicationTests { ...@@ -60,7 +65,7 @@ public class SimpleZuulServerApplicationTests {
} }
@Before @Before
public void setTestRequestcontext() { public void setTestRequestContext() {
RequestContext context = new RequestContext(); RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context); RequestContext.testSetCurrentContext(context);
} }
...@@ -72,18 +77,18 @@ public class SimpleZuulServerApplicationTests { ...@@ -72,18 +77,18 @@ public class SimpleZuulServerApplicationTests {
@Test @Test
public void getOnSelf() { public void getOnSelf() {
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/", HttpMethod.GET, "/", HttpMethod.GET,
new HttpEntity<Void>((Void) null), String.class); new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Hello world", result.getBody()); assertEquals("Hello world", result.getBody());
} }
@Test @Test
public void getOnSelfViaFilter() { public void getOnSelfViaFilter() {
ResponseEntity<String> result = new TestRestTemplate().exchange( ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/testing123/1", HttpMethod.GET, "/testing123/1", HttpMethod.GET,
new HttpEntity<Void>((Void) null), String.class); new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(HttpStatus.OK, result.getStatusCode());
} }
......
...@@ -17,6 +17,11 @@ ...@@ -17,6 +17,11 @@
package org.springframework.cloud.netflix.zuul; package org.springframework.cloud.netflix.zuul;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.EnvironmentTestUtils;
...@@ -29,11 +34,6 @@ import org.springframework.cloud.netflix.zuul.filters.route.okhttp.OkHttpRibbonC ...@@ -29,11 +34,6 @@ import org.springframework.cloud.netflix.zuul.filters.route.okhttp.OkHttpRibbonC
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
*/ */
...@@ -66,7 +66,7 @@ public class ZuulProxyConfigurationTests { ...@@ -66,7 +66,7 @@ public class ZuulProxyConfigurationTests {
} }
context.refresh(); context.refresh();
RibbonCommandFactory factory = context.getBean(RibbonCommandFactory.class); RibbonCommandFactory factory = context.getBean(RibbonCommandFactory.class);
assertThat("RibbonCommandFactory is wrong type for property: "+property, factory, is(instanceOf(clientType))); assertThat("RibbonCommandFactory is wrong type for property: " + property, factory, is(instanceOf(clientType)));
context.close(); context.close();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment