Skip to content

Commit

Permalink
Added test to check that a 100 Continue response and a normal response,
Browse files Browse the repository at this point in the history
when read in a single read by the client, are processed correctly.
  • Loading branch information
sbordet committed Feb 20, 2015
1 parent 98a7ad3 commit ddde0db
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
Expand Up @@ -70,16 +70,25 @@ public void start(Handler handler) throws Exception
}

if (server == null)
server = new Server();
{
QueuedThreadPool serverThreads = new QueuedThreadPool();
serverThreads.setName("server");
server = new Server(serverThreads);
}
connector = new ServerConnector(server, sslContextFactory);
server.addConnector(connector);
server.setHandler(handler);
server.start();

QueuedThreadPool executor = new QueuedThreadPool();
executor.setName(executor.getName() + "-client");
startClient();
}

protected void startClient() throws Exception
{
QueuedThreadPool clientThreads = new QueuedThreadPool();
clientThreads.setName("client");
client = new HttpClient(sslContextFactory);
client.setExecutor(executor);
client.setExecutor(clientThreads);
client.start();
}

Expand Down
Expand Up @@ -20,6 +20,11 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
Expand Down Expand Up @@ -645,4 +650,75 @@ public void onComplete(Result result)

Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}

@Test
public void test_Expect100Continue_WithTwoResponsesInOneRead() throws Exception
{
// There is a chance that the server replies with the 100 Continue response
// and immediately after with the "normal" response, say a 200 OK.
// These may be read by the client in a single read, and must be handled correctly.

startClient();

try (ServerSocket server = new ServerSocket())
{
server.bind(new InetSocketAddress("localhost", 0));

final CountDownLatch latch = new CountDownLatch(1);
client.newRequest("localhost", server.getLocalPort())
.header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString())
.content(new BytesContentProvider(new byte[]{0}))
.send(new Response.CompleteListener()
{
@Override
public void onComplete(Result result)
{
Assert.assertTrue(result.toString(), result.isSucceeded());
Assert.assertEquals(200, result.getResponse().getStatus());
latch.countDown();
}
});

try (Socket socket = server.accept())
{
// Read the request headers.
InputStream input = socket.getInputStream();
int crlfs = 0;
while (true)
{
int read = input.read();
if (read == '\r' || read == '\n')
++crlfs;
else
crlfs = 0;
if (crlfs == 4)
break;
}

OutputStream output = socket.getOutputStream();
String responses = "" +
"HTTP/1.1 100 Continue\r\n" +
"\r\n" +
"HTTP/1.1 200 OK\r\n" +
"Transfer-Encoding: chunked\r\n" +
"\r\n" +
"10\r\n" +
"0123456789ABCDEF\r\n";
output.write(responses.getBytes(StandardCharsets.UTF_8));
output.flush();

Thread.sleep(1000);

String content = "" +
"10\r\n" +
"0123456789ABCDEF\r\n" +
"0\r\n" +
"\r\n";
output.write(content.getBytes(StandardCharsets.UTF_8));
output.flush();

Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
}
}
}

0 comments on commit ddde0db

Please sign in to comment.