Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#533 - mock socket refactoring #539

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@exper0 I didn't get what is the point of moving this code back from createMockSocket() method?
Do you understand issue you are trying to solve?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@longtimeago absolutely no. I asked about it in #533 but decided it will be faster to create PR with anything. Could you please explain what should I do here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@exper0 from my point of view, the issue is pointless, unless you could get rid of mocking and introduce fake Stream as it was suggested by Yegor

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