aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Becker2012-11-19 09:27:11 (EST)
committerThomas Becker2012-11-19 09:27:20 (EST)
commit3edae8a52df20a479f50436c944395eb3de0a899 (patch)
tree409dc7296b1daf1330b05d318e74a65210e3032f
parent3642de3b8ae2970b34f5616f45a4c4c69c1e7559 (diff)
downloadorg.eclipse.jetty.project-3edae8a52df20a479f50436c944395eb3de0a899.zip
org.eclipse.jetty.project-3edae8a52df20a479f50436c944395eb3de0a899.tar.gz
org.eclipse.jetty.project-3edae8a52df20a479f50436c944395eb3de0a899.tar.bz2
394370 Add unit tests for HttpTransportOverSPDY.send()
-rw-r--r--jetty-spdy/spdy-http-server/src/main/java/org/eclipse/jetty/spdy/server/http/HttpTransportOverSPDY.java11
-rw-r--r--jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/HttpTransportOverSPDYTest.java249
2 files changed, 254 insertions, 6 deletions
diff --git a/jetty-spdy/spdy-http-server/src/main/java/org/eclipse/jetty/spdy/server/http/HttpTransportOverSPDY.java b/jetty-spdy/spdy-http-server/src/main/java/org/eclipse/jetty/spdy/server/http/HttpTransportOverSPDY.java
index 46d0b51..ce5d349 100644
--- a/jetty-spdy/spdy-http-server/src/main/java/org/eclipse/jetty/spdy/server/http/HttpTransportOverSPDY.java
+++ b/jetty-spdy/spdy-http-server/src/main/java/org/eclipse/jetty/spdy/server/http/HttpTransportOverSPDY.java
@@ -22,7 +22,6 @@ import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Set;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpGenerator;
@@ -84,10 +83,10 @@ public class HttpTransportOverSPDY implements HttpTransport
// info==null content==null lastContent==true signals no more content - complete
// info==null content!=null lastContent==false send data on committed response
// info==null content!=null lastContent==true send last data on committed response - complete
- // info!=null content==null lastContent==false commit
- // info!=null content==null lastContent==true commit and complete
- // info!=null content!=null lastContent==false commit with content
- // info!=null content!=null lastContent==true commit with content and complete
+ // info!=null content==null lastContent==false reply, commit
+ // info!=null content==null lastContent==true reply, commit and complete
+ // info!=null content!=null lastContent==false reply, commit with content
+ // info!=null content!=null lastContent==true reply, commit with content and complete
boolean hasContent = BufferUtil.hasContent(content);
@@ -155,7 +154,7 @@ public class HttpTransportOverSPDY implements HttpTransport
callback.completed(context);
}
-
+
@Override
public void send(HttpGenerator.ResponseInfo info, ByteBuffer content, boolean lastContent) throws IOException
{
diff --git a/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/HttpTransportOverSPDYTest.java b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/HttpTransportOverSPDYTest.java
new file mode 100644
index 0000000..320b571
--- /dev/null
+++ b/jetty-spdy/spdy-http-server/src/test/java/org/eclipse/jetty/spdy/server/http/HttpTransportOverSPDYTest.java
@@ -0,0 +1,249 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+package org.eclipse.jetty.spdy.server.http;
+
+import java.nio.ByteBuffer;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+
+import org.eclipse.jetty.http.HttpGenerator;
+import org.eclipse.jetty.io.EndPoint;
+import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.HttpConfiguration;
+import org.eclipse.jetty.spdy.api.ByteBufferDataInfo;
+import org.eclipse.jetty.spdy.api.ReplyInfo;
+import org.eclipse.jetty.spdy.api.SPDY;
+import org.eclipse.jetty.spdy.api.Session;
+import org.eclipse.jetty.spdy.api.Stream;
+import org.eclipse.jetty.util.BufferUtil;
+import org.eclipse.jetty.util.Callback;
+import org.eclipse.jetty.util.Fields;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class HttpTransportOverSPDYTest
+{
+ @Mock
+ Connector connector;
+ @Mock
+ HttpConfiguration httpConfiguration;
+ @Mock
+ EndPoint endPoint;
+ @Mock
+ PushStrategy pushStrategy;
+ @Mock
+ Stream stream;
+ @Mock
+ Callback callback;
+ @Mock
+ Session session;
+ @Mock
+ HttpGenerator.ResponseInfo responseInfo;
+
+ private Random random = new Random();
+
+ HttpTransportOverSPDY httpTransportOverSPDY;
+
+ @Before
+ public void setUp() throws Exception
+ {
+ httpTransportOverSPDY = new HttpTransportOverSPDY(connector, httpConfiguration, endPoint, pushStrategy,
+ stream, new Fields());
+ when(stream.getSession()).thenReturn(session);
+ when(session.getVersion()).thenReturn(SPDY.V3);
+ when(stream.isClosed()).thenReturn(false);
+ }
+
+ @Test
+ public void testSendWithResponseInfoNullAndContentNullAndLastContentTrue() throws Exception
+ {
+ ByteBuffer content = null;
+ boolean lastContent = true;
+ Object context = null;
+
+ httpTransportOverSPDY.send(null, content, lastContent, context, callback);
+ ArgumentCaptor<ByteBufferDataInfo> dataInfoCaptor = ArgumentCaptor.forClass(ByteBufferDataInfo.class);
+ verify(stream, times(1)).data(dataInfoCaptor.capture(), anyLong(), any(TimeUnit.class), anyObject(), any(Callback.class));
+ assertThat("lastContent is true", dataInfoCaptor.getValue().isClose(), is(true));
+ assertThat("ByteBuffer is empty", dataInfoCaptor.getValue().length(), is(0));
+ }
+
+ @Test
+ public void testSendWithResponseInfoNullAndContentAndLastContentTrue() throws Exception
+ {
+ ByteBuffer content = createRandomByteBuffer();
+
+ boolean lastContent = true;
+ Object context = null;
+
+ httpTransportOverSPDY.send(null, content, lastContent, context, callback);
+ ArgumentCaptor<ByteBufferDataInfo> dataInfoCaptor = ArgumentCaptor.forClass(ByteBufferDataInfo.class);
+ verify(stream, times(1)).data(dataInfoCaptor.capture(), anyLong(), any(TimeUnit.class), anyObject(), any(Callback.class));
+ assertThat("lastContent is true", dataInfoCaptor.getValue().isClose(), is(true));
+ assertThat("ByteBuffer length is 4096", dataInfoCaptor.getValue().length(), is(4096));
+ }
+
+ @Test
+ public void testSendWithResponseInfoNullAndEmptyContentAndLastContentTrue() throws Exception
+ {
+ ByteBuffer content = BufferUtil.EMPTY_BUFFER;
+ boolean lastContent = true;
+ Object context = null;
+
+ httpTransportOverSPDY.send(null, content, lastContent, context, callback);
+ ArgumentCaptor<ByteBufferDataInfo> dataInfoCaptor = ArgumentCaptor.forClass(ByteBufferDataInfo.class);
+ verify(stream, times(1)).data(dataInfoCaptor.capture(), anyLong(), any(TimeUnit.class), anyObject(), any(Callback.class));
+ assertThat("lastContent is true", dataInfoCaptor.getValue().isClose(), is(true));
+ assertThat("ByteBuffer is empty", dataInfoCaptor.getValue().length(), is(0));
+ }
+
+ @Test
+ public void testSendWithResponseInfoNullAndContentNullAndLastContentFalse() throws Exception
+ {
+ ByteBuffer content = null;
+ boolean lastContent = false;
+ Object context = null;
+
+ httpTransportOverSPDY.send(null, content, lastContent, context, callback);
+ verify(stream, times(0)).data(any(ByteBufferDataInfo.class), anyLong(), any(TimeUnit.class), anyObject(),
+ any(Callback.class));
+ }
+
+ @Test
+ public void testSendWithResponseInfoNullAndContentAndLastContentFalse() throws Exception
+ {
+ ByteBuffer content = createRandomByteBuffer();
+ boolean lastContent = false;
+ Object context = null;
+
+ httpTransportOverSPDY.send(null, content, lastContent, context, callback);
+ ArgumentCaptor<ByteBufferDataInfo> dataInfoCaptor = ArgumentCaptor.forClass(ByteBufferDataInfo.class);
+ verify(stream, times(1)).data(dataInfoCaptor.capture(), anyLong(), any(TimeUnit.class), anyObject(), any(Callback.class));
+ assertThat("lastContent is false", dataInfoCaptor.getValue().isClose(), is(false));
+ assertThat("ByteBuffer is empty", dataInfoCaptor.getValue().length(), is(4096));
+ }
+
+ @Test
+ public void testSendWithResponseInfoNullAndEmptyContentAndLastContentFalse() throws Exception
+ {
+ ByteBuffer content = BufferUtil.EMPTY_BUFFER;
+ boolean lastContent = false;
+ Object context = null;
+
+ httpTransportOverSPDY.send(null, content, lastContent, context, callback);
+ verify(stream, times(0)).data(any(ByteBufferDataInfo.class), anyLong(), any(TimeUnit.class), anyObject(),
+ any(Callback.class));
+ verify(callback, times(1)).completed(context);
+ }
+
+ @Test
+ public void testSendWithResponseInfoAndContentNullAndLastContentTrue() throws Exception
+ {
+ ByteBuffer content = null;
+ boolean lastContent = true;
+ Object context = null;
+ // when stream.isClosed() is called a 2nd time, the teply has closed the stream already
+ when(stream.isClosed()).thenReturn(false).thenReturn(true);
+
+ httpTransportOverSPDY.send(responseInfo, content, lastContent, context, callback);
+ ArgumentCaptor<ReplyInfo> replyInfoCaptor = ArgumentCaptor.forClass(ReplyInfo.class);
+ verify(stream, times(1)).reply(replyInfoCaptor.capture());
+ assertThat("ReplyInfo close is true", replyInfoCaptor.getValue().isClose(), is(true));
+
+ verify(callback, times(1)).completed(context);
+ }
+
+ @Test
+ public void testSendWithResponseInfoAndContentAndLastContentTrue() throws Exception
+ {
+ ByteBuffer content = createRandomByteBuffer();
+
+ boolean lastContent = true;
+ Object context = null;
+
+ httpTransportOverSPDY.send(responseInfo, content, lastContent, context, callback);
+
+ ArgumentCaptor<ReplyInfo> replyInfoCaptor = ArgumentCaptor.forClass(ReplyInfo.class);
+ verify(stream, times(1)).reply(replyInfoCaptor.capture());
+ assertThat("ReplyInfo close is false", replyInfoCaptor.getValue().isClose(), is(false));
+
+ ArgumentCaptor<ByteBufferDataInfo> dataInfoCaptor = ArgumentCaptor.forClass(ByteBufferDataInfo.class);
+ verify(stream, times(1)).data(dataInfoCaptor.capture(), anyLong(), any(TimeUnit.class), anyObject(), any(Callback.class));
+ assertThat("lastContent is true", dataInfoCaptor.getValue().isClose(), is(true));
+ assertThat("ByteBuffer length is 4096", dataInfoCaptor.getValue().length(), is(4096));
+ }
+
+ @Test
+ public void testSendWithResponseInfoAndContentNullAndLastContentFalse() throws Exception
+ {
+ ByteBuffer content = null;
+ boolean lastContent = false;
+ Object context = null;
+
+ httpTransportOverSPDY.send(responseInfo, content, lastContent, context, callback);
+ ArgumentCaptor<ReplyInfo> replyInfoCaptor = ArgumentCaptor.forClass(ReplyInfo.class);
+ verify(stream, times(1)).reply(replyInfoCaptor.capture());
+ assertThat("ReplyInfo close is true", replyInfoCaptor.getValue().isClose(), is(false));
+
+ verify(stream, times(0)).data(any(ByteBufferDataInfo.class), anyLong(), any(TimeUnit.class), anyObject(),
+ any(Callback.class));
+ }
+
+ @Test
+ public void testSendWithResponseInfoAndContentAndLastContentFalse() throws Exception
+ {
+ ByteBuffer content = createRandomByteBuffer();
+
+ boolean lastContent = false;
+ Object context = null;
+
+ httpTransportOverSPDY.send(responseInfo, content, lastContent, context, callback);
+ ArgumentCaptor<ReplyInfo> replyInfoCaptor = ArgumentCaptor.forClass(ReplyInfo.class);
+ verify(stream, times(1)).reply(replyInfoCaptor.capture());
+ assertThat("ReplyInfo close is false", replyInfoCaptor.getValue().isClose(), is(false));
+
+ ArgumentCaptor<ByteBufferDataInfo> dataInfoCaptor = ArgumentCaptor.forClass(ByteBufferDataInfo.class);
+ verify(stream, times(1)).data(dataInfoCaptor.capture(), anyLong(), any(TimeUnit.class), anyObject(), any(Callback.class));
+ assertThat("lastContent is false", dataInfoCaptor.getValue().isClose(), is(false));
+ assertThat("ByteBuffer length is 4096", dataInfoCaptor.getValue().length(), is(4096));
+ }
+
+ private ByteBuffer createRandomByteBuffer()
+ {
+ ByteBuffer content = BufferUtil.allocate(8192);
+ BufferUtil.flipToFill(content);
+ byte[] randomBytes = new byte[4096];
+ random.nextBytes(randomBytes);
+ content.put(randomBytes);
+ return content;
+ }
+}