From 14edf617ab865e3634f243c87ac1cb636c055b6c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 11 Sep 2016 16:20:23 +0200 Subject: [PATCH] =?utf8?q?End=20messages=20carrying=20data=20with=20?= =?utf8?q?=E2=80=9CData=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../java/net/pterodactylus/fcp/FcpMessage.java | 7 +++-- .../java/net/pterodactylus/fcp/FcpMessageTest.java | 35 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/test/java/net/pterodactylus/fcp/FcpMessageTest.java diff --git a/src/main/java/net/pterodactylus/fcp/FcpMessage.java b/src/main/java/net/pterodactylus/fcp/FcpMessage.java index 921d74d..58f28a7 100644 --- a/src/main/java/net/pterodactylus/fcp/FcpMessage.java +++ b/src/main/java/net/pterodactylus/fcp/FcpMessage.java @@ -170,12 +170,13 @@ public class FcpMessage implements Iterable { for (Entry fieldEntry : fields.entrySet()) { writeLine(outputStream, fieldEntry.getKey() + "=" + fieldEntry.getValue()); } - writeLine(outputStream, "EndMessage"); - outputStream.flush(); if (payloadInputStream != null) { + writeLine(outputStream, "Data"); FcpUtils.copy(payloadInputStream, outputStream); - outputStream.flush(); + } else { + writeLine(outputStream, "EndMessage"); } + outputStream.flush(); } // diff --git a/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java b/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java new file mode 100644 index 0000000..bc0dbd1 --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/FcpMessageTest.java @@ -0,0 +1,35 @@ +package net.pterodactylus.fcp; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +/** + * Unit test for {@link FcpMessage}. + * + * @author David Roden + */ +public class FcpMessageTest { + + private final FcpMessage fcpMessage = new FcpMessage("TestMessage"); + + @Test + public void fcpMessageWithPayloadIsTerminatedByData() throws Exception { + fcpMessage.setPayloadInputStream(new ByteArrayInputStream("Test".getBytes())); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + fcpMessage.write(outputStream); + List lines = Arrays.asList(outputStream.toString().split("\r?\n")); + assertThat(lines, contains( + "TestMessage", + "Data", + "Test" + )); + } + +} -- 2.7.4