Skip to content

Commit

Permalink
yegor256#306 - unit tests for persistent connections
Browse files Browse the repository at this point in the history
  • Loading branch information
prondzyn committed Oct 21, 2015
1 parent 3978812 commit 1ace5d0
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 1 deletion.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/org/takes/http/BkReuse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.http;

import java.io.IOException;
import java.net.Socket;
import org.apache.commons.lang.NotImplementedException;

/**
* Reusable back-end.
*
* @author Piotr Pradzynski (prondzyn@gmail.com)
* @version $Id$
*/
public final class BkReuse implements Back {

/**
* Origin back-end.
*/
@SuppressWarnings({"PMD.UnusedPrivateField", "PMD.SingularField"})
private final transient Back origin;

/**
* Constructor of BkReuse.
* @param back Origin back-end.
*/
public BkReuse(final Back back) {
this.origin = back;
}

@Override
public void accept(final Socket socket) throws IOException {
throw new NotImplementedException();
}
}
170 changes: 170 additions & 0 deletions src/test/java/org/takes/http/BkReuseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.http;

import com.google.common.base.Joiner;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.regex.Pattern;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.takes.tk.TkGreedy;
import org.takes.tk.TkText;

/**
* Test case for {@link BkReuse}.
*
* @author Piotr Pradzynski (prondzyn@gmail.com)
* @version $Id$
* @checkstyle MultipleStringLiteralsCheck (500 lines)
* @todo #306 Implement missing BkReuse.accept method to support
* HTTP persistent connections and to pass below three tests.
*
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class BkReuseTest {

/**
* BkReuse can handle two requests in one connection.
* @throws Exception If some problem inside
*/
@Ignore
@Test
public void handlesTwoRequestInOneConnection() throws Exception {
final String text = "Hello world!";
final Socket socket = Mockito.mock(Socket.class);
Mockito.when(socket.getInputStream()).thenReturn(
new ByteArrayInputStream(
Joiner.on("\r\n").join(
"POST / HTTP/1.1",
"Host: localhost",
"Content-Length: 4",
"",
"hi",
"POST / HTTP/1.1",
"Host: localhost",
"Content-Length: 4",
"",
"hi"
).getBytes()
)
);
Mockito.when(socket.getLocalAddress()).thenReturn(
InetAddress.getLocalHost()
);
Mockito.when(socket.getLocalPort()).thenReturn(0);
Mockito.when(socket.getInetAddress()).thenReturn(
InetAddress.getLocalHost()
);
Mockito.when(socket.getPort()).thenReturn(0);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
Mockito.when(socket.getOutputStream()).thenReturn(baos);
new BkReuse(new BkBasic(new TkGreedy(new TkText(text)))).accept(socket);
final Pattern pattern = Pattern.compile(
text + ".*?" + text,
Pattern.DOTALL
);
Assert.assertTrue(
String.format("Actual is %s", baos.toString()),
pattern.matcher(baos.toString()).find()
);
}

/**
* BkReuse can return HTTP status 411 when a persistent connection request
* has no Content-Length.
* @throws Exception If some problem inside
*/
@Ignore
@Test
public void returnsProperResponseCodeOnNoContentLength() throws Exception {
final Socket socket = Mockito.mock(Socket.class);
Mockito.when(socket.getInputStream()).thenReturn(
new ByteArrayInputStream(Joiner.on("\r\n").join(
"POST / HTTP/1.1",
"Host: localhost",
"",
"hi"
).getBytes()
)
);
Mockito.when(socket.getLocalAddress()).thenReturn(
InetAddress.getLocalHost()
);
Mockito.when(socket.getLocalPort()).thenReturn(0);
Mockito.when(socket.getInetAddress()).thenReturn(
InetAddress.getLocalHost()
);
Mockito.when(socket.getPort()).thenReturn(0);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
Mockito.when(socket.getOutputStream()).thenReturn(baos);
new BkReuse(new BkBasic(new TkText("411 Test"))).accept(socket);
MatcherAssert.assertThat(
baos.toString(),
Matchers.containsString("HTTP/1.1 411 Length Required")
);
}

/**
* BkReuse can accept no content-length on closed connection.
* @throws Exception If some problem inside
*/
@Ignore
@Test
public void acceptsNoContentLengthOnClosedConnection() throws Exception {
final String text = "Close Test";
final Socket socket = Mockito.mock(Socket.class);
Mockito.when(socket.getInputStream()).thenReturn(
new ByteArrayInputStream(Joiner.on("\r\n").join(
"POST / HTTP/1.1",
"Host: localhost",
"Connection: Close",
"",
"hi"
).getBytes()
)
);
Mockito.when(socket.getLocalAddress()).thenReturn(
InetAddress.getLocalHost()
);
Mockito.when(socket.getLocalPort()).thenReturn(0);
Mockito.when(socket.getInetAddress()).thenReturn(
InetAddress.getLocalHost()
);
Mockito.when(socket.getPort()).thenReturn(0);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
Mockito.when(socket.getOutputStream()).thenReturn(baos);
new BkReuse(new BkBasic(new TkText(text))).accept(socket);
MatcherAssert.assertThat(
baos.toString(),
Matchers.containsString(text)
);
}
}

0 comments on commit 1ace5d0

Please sign in to comment.