From f6ac2783ac80a6e74a97af174d6f8abdb5ea8e6a Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Tue, 28 Oct 2014 14:45:02 -0700 Subject: [PATCH] Added a Test for PingHandler --- src/test/java/PingHandlerTest.java | 69 +++++++++++++++++++++++++++++ src/test/java/ProxyHandlerTest.java | 15 ------- 2 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 src/test/java/PingHandlerTest.java delete mode 100644 src/test/java/ProxyHandlerTest.java diff --git a/src/test/java/PingHandlerTest.java b/src/test/java/PingHandlerTest.java new file mode 100644 index 0000000..124e8ad --- /dev/null +++ b/src/test/java/PingHandlerTest.java @@ -0,0 +1,69 @@ +import com.netflix.prana.http.api.PingHandler; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelOption; +import io.reactivex.netty.RxNetty; +import io.reactivex.netty.pipeline.PipelineConfigurators; +import io.reactivex.netty.protocol.http.client.HttpClient; +import io.reactivex.netty.protocol.http.client.HttpClientRequest; +import io.reactivex.netty.protocol.http.client.HttpClientResponse; +import io.reactivex.netty.protocol.http.server.HttpServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.Assert; +import rx.Observable; +import rx.exceptions.OnErrorThrowable; +import rx.functions.Action1; +import rx.functions.Func1; + +import java.nio.charset.Charset; + +public class PingHandlerTest { + + private HttpServer server; + + private HttpClient client; + + private final int port = 23455; + + @Before + public void setUp() { + server = RxNetty.newHttpServerBuilder(port, new PingHandler()) + .pipelineConfigurator(PipelineConfigurators.httpServerConfigurator()).build(); + server.start(); + client = RxNetty.newHttpClientBuilder("localhost", port) + .pipelineConfigurator(PipelineConfigurators.httpClientConfigurator()) + .channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000) + .build(); + + } + + @After + public void tearDown() throws InterruptedException { + server.shutdown(); + } + + @Test + public void shouldRespondWithPong() { + HttpClientRequest request = HttpClientRequest.createGet("/ping"); + String response = client.submit(request).flatMap(new Func1, Observable>() { + @Override + public Observable call(HttpClientResponse response) { + return response.getContent().map(new Func1() { + @Override + public String call(ByteBuf byteBuf) { + return byteBuf.toString(Charset.defaultCharset()); + } + }); + } + }).onErrorFlatMap(new Func1>() { + @Override + public Observable call(OnErrorThrowable onErrorThrowable) { + throw onErrorThrowable; + } + }).toBlocking().first(); + + Assert.assertEquals("pong", response); + } + +} diff --git a/src/test/java/ProxyHandlerTest.java b/src/test/java/ProxyHandlerTest.java deleted file mode 100644 index 3954976..0000000 --- a/src/test/java/ProxyHandlerTest.java +++ /dev/null @@ -1,15 +0,0 @@ -import org.junit.Assert; -import org.junit.Test; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class ProxyHandlerTest { - - @Test - public void shouldMatchURIStartingWithProxy() { - Pattern pattern = Pattern.compile("^/proxy.*"); - Matcher matcher = pattern.matcher("/proxy/VIPNAME/path"); - Assert.assertEquals(true, matcher.matches()); - } -}