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;
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.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
......@@ -44,22 +44,27 @@ import org.springframework.web.bind.annotation.RestController;
import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ContextPathZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"server.contextPath: /app" })
@SpringBootTest(
classes = ContextPathZuulProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = {"server.contextPath: /app"})
@DirtiesContext
public class ContextPathZuulProxyApplicationTests {
@Value("${local.server.port}")
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private DiscoveryClientRouteLocator routes;
@Autowired
private RoutesEndpoint endpoint;
@Before
public void setTestRequestcontext() {
public void setTestRequestContext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
......@@ -68,7 +73,7 @@ public class ContextPathZuulProxyApplicationTests {
public void getOnSelfViaSimpleHostRoutingFilter() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/app/local");
this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange(
ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/app/self/1", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
......@@ -80,7 +85,7 @@ public class ContextPathZuulProxyApplicationTests {
this.routes.addRoute(new ZuulRoute("strip", "/strip/**", "strip",
"http://localhost:" + this.port + "/app/local", false, false, null));
this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange(
ResponseEntity<String> result = testRestTemplate.exchange(
"http://localhost:" + this.port + "/app/strip", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
......
......@@ -16,9 +16,16 @@
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.util.Map;
import javax.inject.Inject;
import javax.servlet.http.Part;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -56,18 +63,13 @@ import com.netflix.loadbalancer.ServerList;
import com.netflix.zuul.ZuulFilter;
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 javax.inject.Inject;
import javax.servlet.http.Part;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = FormZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"zuul.routes.simple:/simple/**" })
@SpringBootTest(
classes = FormZuulProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = {"zuul.routes.simple:/simple/**"})
@DirtiesContext
public class FormZuulProxyApplicationTests {
......@@ -215,7 +217,7 @@ public class FormZuulProxyApplicationTests {
@EnableZuulProxy
@RibbonClients({
@RibbonClient(name = "simple", configuration = FormRibbonClientConfiguration.class),
@RibbonClient(name = "psimple", configuration = FormRibbonClientConfiguration.class) })
@RibbonClient(name = "psimple", configuration = FormRibbonClientConfiguration.class)})
@Slf4j
class FormZuulProxyApplication {
......@@ -297,7 +299,8 @@ class FormZuulProxyApplication {
new SpringApplicationBuilder(FormZuulProxyApplication.class)
.properties("zuul.routes.simple:/simple/**",
"zuul.routes.direct.url:http://localhost:9999",
"multipart.maxFileSize:4096MB", "multipart.maxRequestSize:4096MB")
"multipart.maxFileSize:4096MB",
"multipart.maxRequestSize:4096MB")
.run(args);
}
......
......@@ -25,11 +25,12 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
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.TraceRepository;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
......@@ -62,15 +63,18 @@ import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
@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
public class FormZuulServletProxyApplicationTests {
@Value("${local.server.port}")
private int port;
@Autowired
private TestRestTemplate testRestTemplate;
@Before
public void setTestRequestcontext() {
public void setTestRequestContext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
......@@ -81,8 +85,8 @@ public class FormZuulServletProxyApplicationTests {
form.set("foo", "bar");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/zuul/simple/form", HttpMethod.POST,
ResponseEntity<String> result = testRestTemplate.exchange(
"/zuul/simple/form", HttpMethod.POST,
new HttpEntity<>(form, headers), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! {foo=[bar]}", result.getBody());
......@@ -94,8 +98,8 @@ public class FormZuulServletProxyApplicationTests {
form.set("foo", "bar");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/zuul/simple/form", HttpMethod.POST,
ResponseEntity<String> result = testRestTemplate.exchange(
"/zuul/simple/form", HttpMethod.POST,
new HttpEntity<>(form, headers), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! {foo=[bar]}", result.getBody());
......@@ -112,8 +116,8 @@ public class FormZuulServletProxyApplicationTests {
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set("Transfer-Encoding", "chunked");
headers.setContentLength(-1);
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/zuul/simple/file", HttpMethod.POST,
ResponseEntity<String> result = testRestTemplate.exchange(
"/zuul/simple/file", HttpMethod.POST,
new HttpEntity<>(form, headers), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! bar", result.getBody());
......@@ -126,8 +130,8 @@ public class FormZuulServletProxyApplicationTests {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf(
MediaType.APPLICATION_FORM_URLENCODED_VALUE + "; charset=UTF-8"));
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/zuul/simple/form", HttpMethod.POST,
ResponseEntity<String> result = testRestTemplate.exchange(
"/zuul/simple/form", HttpMethod.POST,
new HttpEntity<>(form, headers), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! {foo=[bar]}", result.getBody());
......@@ -163,8 +167,7 @@ class FormZuulServletProxyApplication {
while (inputStream.read(buffer) >= 0) {
log.info("Read more bytes");
}
}
else {
} else {
bytes = file.getBytes();
}
}
......@@ -216,7 +219,8 @@ class FormZuulServletProxyApplication {
.properties("zuul.routes.simple:/zuul/simple/**",
"zuul.routes.direct.url:http://localhost:9999",
"zuul.routes.direct.path:/zuul/direct/**",
"multipart.maxFileSize:4096MB", "multipart.maxRequestSize:4096MB")
"multipart.maxFileSize:4096MB",
"multipart.maxRequestSize:4096MB")
.run(args);
}
......@@ -226,7 +230,7 @@ class FormZuulServletProxyApplication {
@Configuration
class ServletFormRibbonClientConfiguration {
@Value("${local.server.port}")
@LocalServerPort
private int port;
@Bean
......
......@@ -6,8 +6,8 @@ import org.junit.Before;
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.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
......@@ -37,14 +37,18 @@ import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = RetryableZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"zuul.routes.simple.path: /simple/**", "zuul.routes.simple.retryable: true",
"ribbon.OkToRetryOnAllOperations: true" })
@SpringBootTest(
classes = RetryableZuulProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = {
"zuul.routes.simple.path: /simple/**",
"zuul.routes.simple.retryable: true",
"ribbon.OkToRetryOnAllOperations: true"})
@DirtiesContext
public class RetryableZuulProxyApplicationTests {
@Value("${local.server.port}")
private int port;
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
@SuppressWarnings("unused")
......@@ -55,7 +59,7 @@ public class RetryableZuulProxyApplicationTests {
private RoutesEndpoint endpoint;
@Before
public void setTestRequestcontext() {
public void setTestRequestContext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
......@@ -66,10 +70,9 @@ public class RetryableZuulProxyApplicationTests {
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);
ResponseEntity<String> result = testRestTemplate.exchange(
"/simple", HttpMethod.POST,
new HttpEntity<>(form, headers), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! {foo=[bar]}", result.getBody());
}
......@@ -120,7 +123,7 @@ class RetryableZuulProxyApplication {
@Configuration
class RetryableRibbonClientConfiguration {
@Value("${local.server.port}")
@LocalServerPort
private int port;
@Bean
......
......@@ -19,14 +19,13 @@ package org.springframework.cloud.netflix.zuul;
import static org.junit.Assert.assertEquals;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Before;
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.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
......@@ -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.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
......@@ -45,67 +45,86 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ServletPathZuulProxyApplicationTests.ServletPathZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"server.port: 0", "server.servletPath: /app" })
@SpringBootTest(
classes = ServletPathZuulProxyApplicationTests.ServletPathZuulProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = {"server.servletPath: /app"})
@DirtiesContext
public class ServletPathZuulProxyApplicationTests {
@Value("${local.server.port}")
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private DiscoveryClientRouteLocator routes;
@Autowired
private RoutesEndpoint endpoint;
@Before
public void setTestRequestcontext() {
public void setTestRequestContext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
@Test
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();
ResponseEntity<String> result = new TestRestTemplate().exchange("http://localhost:" + this.port + "/app/self/1",
HttpMethod.GET, new HttpEntity<>((Void) null), String.class);
ResponseEntity<String> result = testRestTemplate.exchange(
"/app/self/1", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Gotten 1!", result.getBody());
}
@Test
public void optionsOnRawEndpoint() throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.options(new URI("http://localhost:" + this.port + "/app/local/1"))
.header("Origin", "http://localhost:9000").header("Access-Control-Request-Method", "GET").build(),
ResponseEntity<String> result = testRestTemplate.exchange(
RequestEntity
.options(new URI("/app/local/1"))
.header("Origin", "http://localhost:9000")
.header("Access-Control-Request-Method", "GET")
.build(),
String.class);
HttpHeaders httpHeaders = result.getHeaders();
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
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();
ResponseEntity<String> result = new TestRestTemplate().exchange(RequestEntity
.options(new URI("http://localhost:" + this.port + "/app/self/1"))
.header("Origin", "http://localhost:9000").header("Access-Control-Request-Method", "GET").build(),
ResponseEntity<String> result = testRestTemplate.exchange(
RequestEntity
.options(new URI("/app/self/1"))
.header("Origin", "http://localhost:9000")
.header("Access-Control-Request-Method", "GET")
.build(),
String.class);
HttpHeaders httpHeaders = result.getHeaders();
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
public void contentOnRawEndpoint() throws Exception {
ResponseEntity<String> result = new TestRestTemplate().exchange(
RequestEntity.get(new URI("http://localhost:" + this.port + "/app/local/1")).build(), String.class);
ResponseEntity<String> result = testRestTemplate.exchange(
RequestEntity
.get(new URI("/app/local/1"))
.build(), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Gotten 1!", result.getBody());
}
......@@ -115,8 +134,9 @@ public class ServletPathZuulProxyApplicationTests {
this.routes.addRoute(new ZuulRoute("strip", "/strip/**", "strip",
"http://localhost:" + this.port + "/app/local", false, false, null));
this.endpoint.reset();
ResponseEntity<String> result = new TestRestTemplate().exchange("http://localhost:" + this.port + "/app/strip",
HttpMethod.GET, new HttpEntity<>((Void) null), String.class);
ResponseEntity<String> result = testRestTemplate.exchange(
"/app/strip", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
// Prefix not stripped to it goes to /local/strip
assertEquals("Gotten strip!", result.getBody());
......
......@@ -16,7 +16,7 @@
package org.springframework.cloud.netflix.zuul;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.net.URI;
import java.net.URISyntaxException;
......@@ -28,8 +28,8 @@ import org.junit.Ignore;
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.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
......@@ -49,22 +49,27 @@ import org.springframework.web.bind.annotation.RestController;
import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SimpleZuulProxyApplicationTests.SimpleZuulProxyApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"server.port: 0", "zuul.forceOriginalQueryStringEncoding: true" })
@SpringBootTest(
classes = SimpleZuulProxyApplicationTests.SimpleZuulProxyApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT,
value = {"zuul.forceOriginalQueryStringEncoding: true"})
@DirtiesContext
public class SimpleZuulProxyApplicationTests {
@Value("${local.server.port}")
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private DiscoveryClientRouteLocator routes;
@Autowired
private RoutesEndpoint endpoint;
@Before
public void setTestRequestcontext() {
public void setTestRequestContext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
......@@ -111,23 +116,23 @@ public class SimpleZuulProxyApplicationTests {
@Ignore
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";
ResponseEntity<String> result = new TestRestTemplate().exchange(
new URI("http://localhost:" + this.port + "/foo?" + encodedQueryString),
HttpMethod.GET, new HttpEntity<>((Void) null), String.class);
ResponseEntity<String> result = testRestTemplate.exchange(
new URI("/foo?" + encodedQueryString), HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals(encodedQueryString, result.getBody());
}
private void assertResponseCodeAndBody(ResponseEntity<String> result,
String expectedBody) {
String expectedBody) {
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals(expectedBody, result.getBody());
}
private ResponseEntity<String> executeSimpleRequest(HttpMethod httpMethod) {
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/foo?id=bar", httpMethod,
ResponseEntity<String> result = testRestTemplate.exchange(
"/foo?id=bar", httpMethod,
new HttpEntity<>((Void) null), String.class);
return result;
}
......@@ -144,7 +149,7 @@ public class SimpleZuulProxyApplicationTests {
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) {
return request.getQueryString();
}
......
......@@ -23,9 +23,9 @@ import org.junit.Before;
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.SpringApplication;
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.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
......@@ -45,14 +45,19 @@ import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SimpleZuulServerApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@SpringBootTest(
classes = SimpleZuulServerApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class SimpleZuulServerApplicationTests {
@Value("${local.server.port}")
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private RouteLocator routes;
private String getRoute(String path) {
......@@ -60,7 +65,7 @@ public class SimpleZuulServerApplicationTests {
}
@Before
public void setTestRequestcontext() {
public void setTestRequestContext() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
......@@ -72,18 +77,18 @@ public class SimpleZuulServerApplicationTests {
@Test
public void getOnSelf() {
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/", HttpMethod.GET,
new HttpEntity<Void>((Void) null), String.class);
ResponseEntity<String> result = testRestTemplate.exchange(
"/", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Hello world", result.getBody());
}
@Test
public void getOnSelfViaFilter() {
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/testing123/1", HttpMethod.GET,
new HttpEntity<Void>((Void) null), String.class);
ResponseEntity<String> result = testRestTemplate.exchange(
"/testing123/1", HttpMethod.GET,
new HttpEntity<>((Void) null), String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
}
......
......@@ -17,6 +17,11 @@
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.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.test.util.EnvironmentTestUtils;
......@@ -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.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
*/
......@@ -66,7 +66,7 @@ public class ZuulProxyConfigurationTests {
}
context.refresh();
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();
}
......
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