Skip to content

Commit

Permalink
yegor256#533 - mock socket refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Eliseev committed Jan 22, 2016
1 parent 9fed710 commit a101379
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/test/java/org/takes/http/BkBasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,15 @@ public final class BkBasicTest {
*/
@Test
public void handlesSocket() throws IOException {
final Socket socket = createMockSocket();
final Socket socket = createMockSocket(
Joiner.on(BkBasicTest.CRLF).join(
"GET /1 HTTP/1.1",
"Host: localhost",
"Content-Length: 1",
"",
"hi1"
)
);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
Mockito.when(socket.getOutputStream()).thenReturn(baos);
new BkBasic(new TkText("Hello world!")).accept(socket);
Expand Down Expand Up @@ -141,7 +149,15 @@ public void exec(final URI home) throws IOException {
*/
@Test
public void addressesInHeadersAddedWithoutSlashes() throws IOException {
final Socket socket = BkBasicTest.createMockSocket();
final Socket socket = BkBasicTest.createMockSocket(
Joiner.on(BkBasicTest.CRLF).join(
"GET / HTTP/1.1",
"Host:localhost",
"Content-Length: 2",
"",
"hi"
)
);
final AtomicReference<Request> ref = new AtomicReference<Request>();
new BkBasic(
new Take() {
Expand Down Expand Up @@ -367,22 +383,15 @@ public void run() {

/**
* Creates Socket mock for reuse.
*
* @param req Request string
* @return Prepared Socket mock
* @throws IOException If some problem inside
*/
private static Socket createMockSocket() throws IOException {
private static Socket createMockSocket(final String req)
throws IOException {
final Socket socket = Mockito.mock(Socket.class);
Mockito.when(socket.getInputStream()).thenReturn(
new ByteArrayInputStream(
Joiner.on(BkBasicTest.CRLF).join(
"GET / HTTP/1.1",
"Host:localhost",
"Content-Length: 2",
"",
"hi"
).getBytes()
)
new ByteArrayInputStream(req.getBytes())
);
Mockito.when(socket.getLocalAddress()).thenReturn(
InetAddress.getLocalHost()
Expand Down

0 comments on commit a101379

Please sign in to comment.