Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
Make in style of other tests byt hiding details.

Move client test code over to client rule

TO BE SQUASHED
  • Loading branch information
jamesgorman2 committed Jun 30, 2016
1 parent 4343536 commit 522d72c
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.reactivex.netty.client.pool.SingleHostPoolingProviderFactory;
import io.reactivex.netty.test.util.embedded.EmbeddedChannelProvider;
import io.reactivex.netty.test.util.embedded.EmbeddedChannelWithFeeder;
import org.junit.Assert;
import org.junit.rules.ExternalResource;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
Expand All @@ -43,6 +44,7 @@
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.List;
import java.util.regex.Pattern;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
Expand Down Expand Up @@ -90,7 +92,7 @@ public List<EmbeddedChannelWithFeeder> getCreatedChannels() {
return channelProvider.getCreatedChannels();
}

public TestSubscriber<HttpClientResponse<ByteBuf>> sendRequest(HttpClientRequest<ByteBuf, ByteBuf> request) {
public TestSubscriber<HttpClientResponse<ByteBuf>> sendRequest(Observable<HttpClientResponse<ByteBuf>> request) {
TestSubscriber<HttpClientResponse<ByteBuf>> testSubscriber = new TestSubscriber<>();
request.subscribe(testSubscriber);
testSubscriber.assertNoErrors();
Expand Down Expand Up @@ -249,4 +251,64 @@ public void assertContentWritten(String contentStr) {
assertThat("Content not written.", outbound, is(notNullValue()));
}
}

public void assertEmptyBodyWithContentLengthZero() {
assertBodyWithContentLength(0, "");
}

public void assertBodyWithContentLength(int contentLength, String body) {
Pattern headerBlock = Pattern.compile("^(.*?\r\n)*?\r\n", Pattern.MULTILINE);
Object outbound;
String data = "";

while ((outbound = getLastCreatedChannel().readOutbound()) != null) {
if (outbound instanceof ByteBuf) {
ByteBuf bb = (ByteBuf) outbound;
data += bb.toString(Charset.defaultCharset());
}
}

if (!data.contains("content-length: " + contentLength + "\r\n")) {
Assert.fail("Missing header 'content-length: " + contentLength + "'");
}
if (data.contains("transfer-encoding: chunked\r\n")) {
Assert.fail("Unexpected header 'transfer-encoding: chunked'");
}
if (!headerBlock.matcher(data).replaceFirst("").equals(body)) {
Assert.fail("Unexpected body content '" + headerBlock.matcher(data).replaceFirst("") + "'");
}
}

public void assertEmptyBodyWithSingleChunk() {
assertChunks();
}

public void assertChunks(String... chunks) {
Pattern headerBlock = Pattern.compile("^(.*?\r\n)*?\r\n", Pattern.MULTILINE);
Object outbound;
String data = "";

while ((outbound = getLastCreatedChannel().readOutbound()) != null) {
if (outbound instanceof ByteBuf) {
ByteBuf bb = (ByteBuf) outbound;
data += bb.toString(Charset.defaultCharset());
}
}

if (data.contains("content-length: 0\r\n")) {
Assert.fail("Unexpected header 'content-length: 0'");
}
if (!data.contains("transfer-encoding: chunked\r\n")) {
Assert.fail("Missing header 'transfer-encoding: chunked'");
}
String expectedChunkContent = "";
for (String c : chunks) {
expectedChunkContent += c.getBytes().length + "\r\n";
expectedChunkContent += c + "\r\n";
}
expectedChunkContent += "0\r\n\r\n";
if (!headerBlock.matcher(data).replaceFirst("").equals(expectedChunkContent)) {
Assert.fail("Unexpected body content '" + headerBlock.matcher(data).replaceFirst("") + "'");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,97 +276,35 @@ public void call() {

@Test(timeout = 60000)
public void testRequestWithNoContentLengthHeaderOrContentReturnsEmptyBody() {
serverRule.startServer();

serverRule.assertRequestEquals(
new Func1<HttpClient<ByteBuf, ByteBuf>, Observable<HttpClientResponse<ByteBuf>>>() {
@Override
public Observable<HttpClientResponse<ByteBuf>> call(HttpClient<ByteBuf, ByteBuf> client) {
return client.createGet("/");
}
},
"GET / HTTP/1.1\r\n" +
"content-length: 0\r\n" +
"host: " + serverRule.getServerAddress().toString().replaceFirst("^/", "") + "\r\n" +
"\r\n");
clientRule.sendRequest(clientRule.getHttpClient().createGet("/"));
clientRule.assertEmptyBodyWithContentLengthZero();
}

@Test(timeout = 60000)
public void testRequestWithNoContentLengthHeaderAndContentReturnsContentChunkAndSingleEmptyChunk() {
serverRule.startServer();

serverRule.assertRequestEquals(
new Func1<HttpClient<ByteBuf, ByteBuf>, Observable<HttpClientResponse<ByteBuf>>>() {
@Override
public Observable<HttpClientResponse<ByteBuf>> call(HttpClient<ByteBuf, ByteBuf> client) {
return client.createGet("/")
.writeStringContent(Observable.just("Hello"));
}
},
"GET / HTTP/1.1\r\n" +
"transfer-encoding: chunked\r\n" +
"host: " + serverRule.getServerAddress().toString().replaceFirst("^/", "") + "\r\n" +
"\r\n" +
"5\r\n" +
"Hello\r\n" +
"0\r\n" +
"\r\n");
clientRule.sendRequest(clientRule.getHttpClient().createGet("/")
.writeStringContent(Observable.just("Hello")));
clientRule.assertChunks("Hello");
}

@Test(timeout = 60000)
public void testRequestWithContentLengthReturnsRawBody() {
serverRule.startServer();

serverRule.assertRequestEquals(
new Func1<HttpClient<ByteBuf, ByteBuf>, Observable<HttpClientResponse<ByteBuf>>>() {
@Override
public Observable<HttpClientResponse<ByteBuf>> call(HttpClient<ByteBuf, ByteBuf> client) {
return client.createGet("/")
.setHeader(HttpHeaderNames.CONTENT_LENGTH, 5)
.writeStringContent(Observable.just("Hello"));
}
},
"GET / HTTP/1.1\r\n" +
"content-length: 5\r\n" +
"host: " + serverRule.getServerAddress().toString().replaceFirst("^/", "") + "\r\n" +
"\r\n" +
"Hello");
clientRule.sendRequest(clientRule.getHttpClient().createGet("/")
.setHeader(HttpHeaderNames.CONTENT_LENGTH, 5)
.writeStringContent(Observable.just("Hello")));
clientRule.assertBodyWithContentLength(5, "Hello");
}

@Test(timeout = 60000)
public void testRequestWithZeroContentLengthReturnsEmptyBody() {
serverRule.startServer();

serverRule.assertRequestEquals(
new Func1<HttpClient<ByteBuf, ByteBuf>, Observable<HttpClientResponse<ByteBuf>>>() {
@Override
public Observable<HttpClientResponse<ByteBuf>> call(HttpClient<ByteBuf, ByteBuf> client) {
return client.createGet("/")
.setHeader(HttpHeaderNames.CONTENT_LENGTH, 0);
}
},
"GET / HTTP/1.1\r\n" +
"content-length: 0\r\n" +
"host: " + serverRule.getServerAddress().toString().replaceFirst("^/", "") + "\r\n" +
"\r\n");
clientRule.sendRequest(clientRule.getHttpClient().createGet("/").setHeader(HttpHeaderNames.CONTENT_LENGTH, 0));
clientRule.assertEmptyBodyWithContentLengthZero();
}

@Test(timeout = 60000)
public void testRequestWithOnlyPositiveContentLengthReturnsEmptyBody() {
serverRule.startServer();

serverRule.assertRequestEquals(
new Func1<HttpClient<ByteBuf, ByteBuf>, Observable<HttpClientResponse<ByteBuf>>>() {
@Override
public Observable<HttpClientResponse<ByteBuf>> call(HttpClient<ByteBuf, ByteBuf> client) {
return client.createGet("/")
.setHeader(HttpHeaderNames.CONTENT_LENGTH, 5);
}
},
"GET / HTTP/1.1\r\n" +
"content-length: 0\r\n" +
"host: " + serverRule.getServerAddress().toString().replaceFirst("^/", "") + "\r\n" +
"\r\n");
clientRule.sendRequest(clientRule.getHttpClient().createGet("/").setHeader(HttpHeaderNames.CONTENT_LENGTH, 5));
clientRule.assertEmptyBodyWithContentLengthZero();
}

protected void startServerThatNeverReplies() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.Charset;
import java.util.regex.Pattern;

import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;

public class HttpServerRule extends ExternalResource {

Expand All @@ -50,12 +52,10 @@ public class HttpServerRule extends ExternalResource {
private HttpServer<ByteBuf, ByteBuf> server;
private HttpClient<ByteBuf, ByteBuf> client;

private String lastRequest = "";
private String lastResponse = "";

@Override
public Statement apply(final Statement base, Description description) {
lastRequest = "";
lastResponse = "";
return new Statement() {
@Override
Expand All @@ -67,13 +67,7 @@ public void evaluate() throws Throwable {
new Action1<ByteBuf>() {
@Override
public void call(ByteBuf byteBuf) {
lastRequest += byteBuf.toString(Charset.forName("UTF-8"));
}
},
new Action1<ByteBuf>() {
@Override
public void call(ByteBuf byteBuf) {
lastResponse += byteBuf.toString(Charset.forName("UTF-8"));
lastResponse += byteBuf.toString(Charset.defaultCharset());
}
}
)
Expand Down Expand Up @@ -130,38 +124,48 @@ public void assertResponseContent(HttpClientResponse<ByteBuf> response) {
equalTo(WELCOME_SERVER_MSG));
}

public void assertRequestEquals(Func1<HttpClient<ByteBuf, ByteBuf>, Observable<HttpClientResponse<ByteBuf>>> request, String expectedRequest) {
lastRequest = "";
TestSubscriber<Void> clientDrain = new TestSubscriber<>();
request.call(HttpClient.newClient(getServerAddress()).enableWireLogging("test-client", LogLevel.INFO))
.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<Void>>() {
@Override
public Observable<Void> call(HttpClientResponse<ByteBuf> clientResponse) {
return clientResponse.discardContent();
}
})
.subscribe(clientDrain);
clientDrain.awaitTerminalEvent();
clientDrain.assertNoErrors();
public void assertEmptyBodyWithContentLengthZero() {
assertBodyWithContentLength(0, "");
}

public void assertBodyWithContentLength(int contentLength, String body) {
getAndDrainClient();
Pattern headerBlock = Pattern.compile("^(.*?\r\n)*?\r\n", Pattern.MULTILINE);

Assert.assertEquals(expectedRequest, lastRequest);
if (!lastResponse.contains("content-length: " + contentLength + "\r\n")) {
Assert.fail("Missing header 'content-length: " + contentLength + "'");
}
if (lastResponse.contains("transfer-encoding: chunked\r\n")) {
Assert.fail("Unexpected header 'transfer-encoding: chunked'");
}
if (!headerBlock.matcher(lastResponse).replaceFirst("").equals(body)) {
Assert.fail("Unexpected body content '" + headerBlock.matcher(lastResponse).replaceFirst("") + "'");
}
}

public void assertResponseEquals(String expectedResponse) {
lastResponse = "";
TestSubscriber<Void> clientDrain = new TestSubscriber<>();
client.createGet("/")
.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<Void>>() {
@Override
public Observable<Void> call(HttpClientResponse<ByteBuf> clientResponse) {
return clientResponse.discardContent();
}
})
.subscribe(clientDrain);
clientDrain.awaitTerminalEvent();
clientDrain.assertNoErrors();
public void assertEmptyBodyWithSingleChunk() {
assertChunks();
}

Assert.assertEquals(expectedResponse, lastResponse);
public void assertChunks(String... chunks) {
getAndDrainClient();
Pattern headerBlock = Pattern.compile("^(.*?\r\n)*?\r\n", Pattern.MULTILINE);

if (lastResponse.contains("content-length: 0\r\n")) {
Assert.fail("Unexpected header 'content-length: 0'");
}
if (!lastResponse.contains("transfer-encoding: chunked\r\n")) {
Assert.fail("Missing header 'transfer-encoding: chunked'");
}
String expectedChunkContent = "";
for (String c : chunks) {
expectedChunkContent += c.getBytes().length + "\r\n";
expectedChunkContent += c + "\r\n";
}
expectedChunkContent += "0\r\n\r\n";
if (!headerBlock.matcher(lastResponse).replaceFirst("").equals(expectedChunkContent)) {
Assert.fail("Unexpected body content '" + headerBlock.matcher(lastResponse).replaceFirst("") + "'");
}
}

public SocketAddress getServerAddress() {
Expand All @@ -180,31 +184,40 @@ public HttpClient<ByteBuf, ByteBuf> getClient() {
return client;
}

public static class RawMessageHandler extends ChannelDuplexHandler {

public static Func0<ChannelHandler> factory(final Action1<ByteBuf> onRead, final Action1<ByteBuf> onWrite) {
public void getAndDrainClient() {
lastResponse = "";
TestSubscriber<Void> clientDrain = new TestSubscriber<>();
client.createGet("/")
.flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<Void>>() {
@Override
public Observable<Void> call(HttpClientResponse<ByteBuf> clientResponse) {
return clientResponse.discardContent();
}
})
.subscribe(clientDrain);
clientDrain.awaitTerminalEvent();
clientDrain.assertNoErrors();
}


private static class RawMessageHandler extends ChannelDuplexHandler {

public static Func0<ChannelHandler> factory(final Action1<ByteBuf> onWrite) {
return new Func0<ChannelHandler>() {
@Override
public ChannelHandler call() {
return new RawMessageHandler(onRead, onWrite);
return new RawMessageHandler(onWrite);
}
};
}

private final Action1<ByteBuf> onRead;
private final Action1<ByteBuf> onWrite;

public RawMessageHandler(Action1<ByteBuf> onRead, Action1<ByteBuf> onWrite) {
this.onRead = onRead;
public RawMessageHandler(Action1<ByteBuf> onWrite) {
this.onWrite = onWrite;
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
callback(msg, onRead);
super.channelRead(ctx, msg);
}

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
callback(msg, onWrite);
Expand Down
Loading

0 comments on commit 522d72c

Please sign in to comment.