Skip to content

Commit

Permalink
Added a Test for PingHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanu committed Oct 28, 2014
1 parent 8b0a41b commit f6ac278
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
69 changes: 69 additions & 0 deletions src/test/java/PingHandlerTest.java
Original file line number Diff line number Diff line change
@@ -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<ByteBuf, ByteBuf> server;

private HttpClient<ByteBuf, ByteBuf> client;

private final int port = 23455;

@Before
public void setUp() {
server = RxNetty.newHttpServerBuilder(port, new PingHandler())
.pipelineConfigurator(PipelineConfigurators.<ByteBuf, ByteBuf>httpServerConfigurator()).build();
server.start();
client = RxNetty.<ByteBuf, ByteBuf>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<ByteBuf> request = HttpClientRequest.<ByteBuf>createGet("/ping");
String response = client.submit(request).flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<String>>() {
@Override
public Observable<String> call(HttpClientResponse<ByteBuf> response) {
return response.getContent().map(new Func1<ByteBuf, String>() {
@Override
public String call(ByteBuf byteBuf) {
return byteBuf.toString(Charset.defaultCharset());
}
});
}
}).onErrorFlatMap(new Func1<OnErrorThrowable, Observable<String>>() {
@Override
public Observable<String> call(OnErrorThrowable onErrorThrowable) {
throw onErrorThrowable;
}
}).toBlocking().first();

Assert.assertEquals("pong", response);
}

}
15 changes: 0 additions & 15 deletions src/test/java/ProxyHandlerTest.java

This file was deleted.

0 comments on commit f6ac278

Please sign in to comment.