diff --git a/src/test/java/org/takes/rq/multipart/RqMtFakeTest.java b/src/test/java/org/takes/rq/multipart/RqMtFakeTest.java index c421000dc..871a07515 100644 --- a/src/test/java/org/takes/rq/multipart/RqMtFakeTest.java +++ b/src/test/java/org/takes/rq/multipart/RqMtFakeTest.java @@ -27,10 +27,13 @@ import java.nio.channels.ClosedChannelException; import java.util.Arrays; import java.util.HashSet; +import org.cactoos.list.ListOf; +import org.cactoos.text.FormattedText; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.hamcrest.core.IsInstanceOf; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; import org.takes.rq.RqFake; import org.takes.rq.RqHeaders; @@ -43,19 +46,9 @@ * Test case for {@link RqMtFake}. * @since 0.33 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - * @checkstyle MultipleStringLiteralsCheck (500 lines) */ +@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"}) public final class RqMtFakeTest { - /** - * Form data. - */ - private static final String FORM_DATA = - "form-data; name=\"data\"; filename=\"%s\""; - - /** - * Content disposition. - */ - private static final String DISPOSITION = "Content-Disposition"; /** * RqMtFake can throw exception on no name @@ -68,7 +61,7 @@ public void throwsExceptionOnNoNameAtContentDispositionHeader() new RqMtFake( new RqWithHeader( new RqFake("", "", "340 N Wolfe Rd, Sunnyvale, CA 94085"), - RqMtFakeTest.DISPOSITION, "form-data; fake=\"t-3\"" + "Content-Disposition", "form-data; fake=\"t-3\"" ) ); } @@ -115,36 +108,35 @@ public void throwsExceptionOnInvalidContentTypeHeader() throws IOException { /** * RqMtFake can parse http body. - * @throws IOException If some problem inside + * @throws Exception */ @Test - public void parsesHttpBody() throws IOException { + public void parsesHttpBody() throws Exception { final String body = "40 N Wolfe Rd, Sunnyvale, CA 94085"; final String part = "t4"; final RqMultipart multi = new RqMtFake( new RqFake(), new RqWithHeaders( new RqFake("", "", body), - RqMtFakeTest.contentLengthHeader( - (long) body.getBytes().length - ), - RqMtFakeTest.contentDispositionHeader( - String.format("form-data; name=\"%s\"", part) - ) + new FormattedText( + "Content-Length: %s", body.getBytes().length + ).asString(), + new FormattedText( + "Content-Disposition: form-data; name=\"%s\"", part + ).asString() ), new RqWithHeaders( new RqFake("", "", ""), - RqMtFakeTest.contentLengthHeader(0L), - RqMtFakeTest.contentDispositionHeader( - String.format(RqMtFakeTest.FORM_DATA, "a.rar") - ) + "Content-Length: 0", + // @checkstyle LineLengthCheck (1 line) + "Content-Disposition: form-data; name=\"data\"; filename=\"a.rar\"" ) ); try { MatcherAssert.assertThat( new RqHeaders.Base( multi.part(part).iterator().next() - ).header(RqMtFakeTest.DISPOSITION), + ).header("Content-Disposition"), Matchers.hasItem("form-data; name=\"t4\"") ); MatcherAssert.assertThat( @@ -175,19 +167,16 @@ public void closesAllParts() throws Exception { new RqFake(), new RqWithHeaders( new RqFake("", "", body), - RqMtFakeTest.contentLengthHeader( - (long) body.getBytes().length - ), - RqMtFakeTest.contentDispositionHeader( - "form-data; name=\"name\"" - ) + new FormattedText( + "Content-Length: %s", body.getBytes().length + ).asString(), + "Content-Disposition: form-data; name=\"name\"" ), new RqWithHeaders( new RqFake("", "", body), - RqMtFakeTest.contentLengthHeader(0L), - RqMtFakeTest.contentDispositionHeader( - "form-data; name=\"content\"; filename=\"a.bin\"" - ) + "Content-Length: 0", + // @checkstyle LineLengthCheck (1 line) + "Content-Disposition: form-data; name=\"content\"; filename=\"a.bin\"" ) ); final String exmessage = @@ -241,19 +230,16 @@ public void closesExplicitlyAllParts() throws Exception { new RqFake(), new RqWithHeaders( new RqFake("", "", body), - RqMtFakeTest.contentLengthHeader( - (long) body.getBytes().length - ), - RqMtFakeTest.contentDispositionHeader( - "form-data; name=\"foo\"" - ) + new FormattedText( + "Content-Length: %s", body.getBytes().length + ).asString(), + "Content-Disposition: form-data; name=\"foo\"" ), new RqWithHeaders( new RqFake("", "", body), - RqMtFakeTest.contentLengthHeader(0L), - RqMtFakeTest.contentDispositionHeader( - "form-data; name=\"bar\"; filename=\"a.bin\"" - ) + "Content-Length: 0", + // @checkstyle LineLengthCheck (1 line) + "Content-Disposition: form-data; name=\"bar\"; filename=\"a.bin\"" ) ); final String foo = "foo"; @@ -274,28 +260,25 @@ public void closesExplicitlyAllParts() throws Exception { /** * RqMtFake can return empty iterator on invalid part request. - * @throws IOException If some problem inside + * @throws Exception If there is some problem inside */ @Test - public void returnsEmptyIteratorOnInvalidPartRequest() throws IOException { + public void returnsEmptyIteratorOnInvalidPartRequest() throws Exception { final String body = "443 N Wolfe Rd, Sunnyvale, CA 94085"; final RqMultipart multi = new RqMtFake( new RqFake(), new RqWithHeaders( new RqFake("", "", body), - RqMtFakeTest.contentLengthHeader( - (long) body.getBytes().length - ), - RqMtFakeTest.contentDispositionHeader( - "form-data; name=\"t5\"" - ) + new FormattedText( + "Content-Length: %s", body.getBytes().length + ).asString(), + "Content-Disposition: form-data; name=\"t5\"" ), new RqWithHeaders( new RqFake("", "", ""), - RqMtFakeTest.contentLengthHeader(0L), - RqMtFakeTest.contentDispositionHeader( - String.format(RqMtFakeTest.FORM_DATA, "a.zip") - ) + "Content-Length: 0", + // @checkstyle LineLengthCheck (1 line) + "Content-Disposition: form-data; name=\"data\"; filename=\"a.zip\"" ) ); MatcherAssert.assertThat( @@ -307,28 +290,25 @@ public void returnsEmptyIteratorOnInvalidPartRequest() throws IOException { /** * RqMtFake can return correct name set. - * @throws IOException If some problem inside + * @throws Exception If there is some problem inside */ @Test - public void returnsCorrectNamesSet() throws IOException { + public void returnsCorrectNamesSet() throws Exception { final String body = "441 N Wolfe Rd, Sunnyvale, CA 94085"; final RqMultipart multi = new RqMtFake( new RqFake(), new RqWithHeaders( new RqFake("", "", body), - RqMtFakeTest.contentLengthHeader( - (long) body.getBytes().length - ), - RqMtFakeTest.contentDispositionHeader( - "form-data; name=\"address\"" - ) + new FormattedText( + "Content-Length: %s", body.getBytes().length + ).asString(), + "Content-Disposition: form-data; name=\"address\"" ), new RqWithHeaders( new RqFake("", "", ""), - RqMtFakeTest.contentLengthHeader(0L), - RqMtFakeTest.contentDispositionHeader( - String.format(RqMtFakeTest.FORM_DATA, "a.bin") - ) + "Content-Length: 0", + // @checkstyle LineLengthCheck (1 line) + "Content-Disposition: form-data; name=\"data\"; filename=\"a.bin\"" ) ); try { @@ -343,21 +323,31 @@ public void returnsCorrectNamesSet() throws IOException { } } + /* + * @todo #577:30min The test below fails with the message + * "header "Content-Disposition" is mandatory". + * The error disappears when `new RqFake(new ListOf<>(""), "")` is + * changed to `new RqFake(new ListOf<>(""), "someValue")`. + * Fix the underlying problem and remove the `@Ignore` annotation. + * */ /** - * Format Content-Disposition header. - * @param dsp Disposition - * @return Content-Disposition header - */ - private static String contentDispositionHeader(final String dsp) { - return String.format("Content-Disposition: %s", dsp); - } - - /** - * Format Content-Length header. - * @param length Body length - * @return Content-Length header + * Tests the bug described in #577. + * + * @throws Exception If there is some error inside */ - private static String contentLengthHeader(final long length) { - return String.format("Content-Length: %d", length); + @Test + @Ignore("See puzzle above") + public void contentDispositionShouldBeRecognized() throws Exception { + new RqMtFake( + new RqFake(), + new RqWithHeader( + new RqFake(new ListOf<>(""), ""), + "Content-Disposition: form-data; name=\"field1\"" + ), + new RqWithHeader( + new RqFake("", "", "field2Value"), + "Content-Disposition: form-data; name=\"field2\"" + ) + ); } }