diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java index e03d5135..7a5f903f 100644 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java +++ b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/hystrix/HystrixStreamEndpointTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2014 the original author or authors. + * Copyright 2013-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,19 +16,91 @@ package org.springframework.cloud.netflix.hystrix; +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; 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.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; -import static org.junit.Assert.assertEquals; +import java.io.InputStream; +import java.net.URL; + +import static org.junit.Assert.*; /** * @author Dave Syer + * @author Spencer Gibb */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = HystrixStreamEndpointTests.Application.class) +@WebAppConfiguration +@IntegrationTest({ "server.port=0", "spring.application.name=hystrixstreamtest" }) +@DirtiesContext public class HystrixStreamEndpointTests { + @Value("${local.server.port}") + private int port = 0; + @Test public void pathStartsWithSlash() { HystrixStreamEndpoint endpoint = new HystrixStreamEndpoint(); assertEquals("/hystrix.stream", endpoint.getPath()); } + @Test + public void hystrixStreamWorks() throws Exception { + String url = "http://localhost:" + port; + //you have to hit a Hystrix circuit breaker before the stream sends anything + ResponseEntity response = new TestRestTemplate().getForEntity(url, String.class); + assertEquals("bad response code", HttpStatus.OK, response.getStatusCode()); + + URL hystrixUrl = new URL(url + "/admin/hystrix.stream"); + InputStream in = hystrixUrl.openStream(); + byte[] buffer = new byte[1024]; + in.read(buffer); + String contents = new String(buffer); + assertTrue(contents.contains("ping")); + in.close(); + } + + @Configuration + @EnableAutoConfiguration + @RestController + @EnableCircuitBreaker + protected static class Application { + @Autowired + Service service; + + @Bean + Service service() { + return new Service(); + } + + @RequestMapping("/") + public String hello() { + return service.hello(); + } + } + + protected static class Service { + @HystrixCommand + public String hello() { + return "Hello World"; + } + } } +