From 797db1c750735d37de491be58559252304df9f76 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Sat, 16 Jul 2022 08:25:04 +0300 Subject: [PATCH] #1134 fixed --- src/main/java/org/takes/Body.java | 1 + .../org/takes/facets/auth/codecs/CcXor.java | 5 +- src/main/java/org/takes/http/BkBasic.java | 28 ++-- .../org/takes/rs/{Body.java => RsBody.java} | 24 +-- .../rs/{BodyPrint.java => RsBodyPrint.java} | 42 ++---- .../rs/{HeadPrint.java => RsHeadPrint.java} | 30 +--- src/main/java/org/takes/rs/RsHtml.java | 2 +- src/main/java/org/takes/rs/RsPrint.java | 140 +++++++++++++++--- src/main/java/org/takes/rs/RsText.java | 6 +- src/main/java/org/takes/rs/RsWithBody.java | 8 +- .../org/takes/facets/auth/PsBasicTest.java | 4 +- .../takes/facets/fallback/FbChainTest.java | 4 +- .../takes/facets/fallback/FbStatusTest.java | 12 +- .../takes/facets/fallback/TkFallbackTest.java | 6 +- .../org/takes/facets/fork/RsForkTest.java | 10 +- ...tBodyTest.java => HmRqTextRsBodyTest.java} | 2 +- .../{HmBodyTest.java => HmRsBodyTest.java} | 2 +- ...tBodyTest.java => HmRsTextRsBodyTest.java} | 2 +- ...extBodyTest.java => HmTextRsBodyTest.java} | 2 +- .../java/org/takes/http/FtRemoteTest.java | 18 ++- ...ithBodyTest.java => RqWithRsBodyTest.java} | 2 +- ...rrayTest.java => RsBodyByteArrayTest.java} | 8 +- ...odyPrintTest.java => RsBodyPrintTest.java} | 6 +- ...yStreamTest.java => RsBodyStreamTest.java} | 8 +- ...pFileTest.java => RsBodyTempFileTest.java} | 8 +- .../{BodyUrlTest.java => RsBodyUrlTest.java} | 8 +- src/test/java/org/takes/rs/RsGzipTest.java | 6 +- ...eadPrintTest.java => RsHeadPrintTest.java} | 10 +- .../java/org/takes/rs/RsPrettyJsonTest.java | 8 +- .../java/org/takes/rs/RsPrettyXmlTest.java | 16 +- src/test/java/org/takes/rs/RsPrintTest.java | 20 ++- src/test/java/org/takes/rs/RsTextTest.java | 2 +- .../java/org/takes/tk/TkClasspathTest.java | 4 +- src/test/java/org/takes/tk/TkCorsTest.java | 4 +- src/test/java/org/takes/tk/TkFilesTest.java | 4 +- src/test/java/org/takes/tk/TkGzipTest.java | 85 ++++++++--- src/test/java/org/takes/tk/TkProxyTest.java | 12 +- 37 files changed, 331 insertions(+), 228 deletions(-) rename src/main/java/org/takes/rs/{Body.java => RsBody.java} (92%) rename src/main/java/org/takes/rs/{BodyPrint.java => RsBodyPrint.java} (60%) rename src/main/java/org/takes/rs/{HeadPrint.java => RsHeadPrint.java} (71%) rename src/test/java/org/takes/facets/hamcrest/{HmRqTextBodyTest.java => HmRqTextRsBodyTest.java} (98%) rename src/test/java/org/takes/facets/hamcrest/{HmBodyTest.java => HmRsBodyTest.java} (99%) rename src/test/java/org/takes/facets/hamcrest/{HmRsTextBodyTest.java => HmRsTextRsBodyTest.java} (98%) rename src/test/java/org/takes/facets/hamcrest/{HmTextBodyTest.java => HmTextRsBodyTest.java} (99%) rename src/test/java/org/takes/rq/{RqWithBodyTest.java => RqWithRsBodyTest.java} (98%) rename src/test/java/org/takes/rs/{BodyByteArrayTest.java => RsBodyByteArrayTest.java} (91%) rename src/test/java/org/takes/rs/{BodyPrintTest.java => RsBodyPrintTest.java} (91%) rename src/test/java/org/takes/rs/{BodyStreamTest.java => RsBodyStreamTest.java} (92%) rename src/test/java/org/takes/rs/{BodyTempFileTest.java => RsBodyTempFileTest.java} (92%) rename src/test/java/org/takes/rs/{BodyUrlTest.java => RsBodyUrlTest.java} (93%) rename src/test/java/org/takes/rs/{HeadPrintTest.java => RsHeadPrintTest.java} (93%) diff --git a/src/main/java/org/takes/Body.java b/src/main/java/org/takes/Body.java index 71c0526d8..f70027acd 100644 --- a/src/main/java/org/takes/Body.java +++ b/src/main/java/org/takes/Body.java @@ -28,6 +28,7 @@ /** * Body abstraction for {@link Request} and {@link Response}. + * * @since 2.0 */ public interface Body { diff --git a/src/main/java/org/takes/facets/auth/codecs/CcXor.java b/src/main/java/org/takes/facets/auth/codecs/CcXor.java index 2b81f2b50..cf874e694 100644 --- a/src/main/java/org/takes/facets/auth/codecs/CcXor.java +++ b/src/main/java/org/takes/facets/auth/codecs/CcXor.java @@ -58,9 +58,8 @@ public final class CcXor implements Codec { public CcXor(final Codec codec, final String key) { this( codec, - new UncheckedBytes( - new BytesOf(key) - ).asBytes()); + new UncheckedBytes(new BytesOf(key)).asBytes() + ); } /** diff --git a/src/main/java/org/takes/http/BkBasic.java b/src/main/java/org/takes/http/BkBasic.java index 283e34f7f..d3c2c280e 100644 --- a/src/main/java/org/takes/http/BkBasic.java +++ b/src/main/java/org/takes/http/BkBasic.java @@ -31,7 +31,6 @@ import java.net.Socket; import lombok.EqualsAndHashCode; import org.cactoos.bytes.BytesOf; -import org.cactoos.bytes.UncheckedBytes; import org.cactoos.io.InputStreamOf; import org.takes.HttpException; import org.takes.Request; @@ -123,27 +122,17 @@ public void accept(final Socket socket) throws IOException { private void print(final Request req, final OutputStream output) throws IOException { try { - output.write( - new RsPrint(this.take.act(req)).asBytes() - ); + new RsPrint(this.take.act(req)).print(output); } catch (final HttpException ex) { - output.write( - new UncheckedBytes( - new RsPrint(BkBasic.failure(ex, ex.code())) - ).asBytes() - ); + new RsPrint(BkBasic.failure(ex, ex.code())).print(output); // @checkstyle IllegalCatchCheck (10 lines) } catch (final Throwable ex) { - output.write( - new UncheckedBytes( - new RsPrint( - BkBasic.failure( - ex, - HttpURLConnection.HTTP_INTERNAL_ERROR - ) - ) - ).asBytes() - ); + new RsPrint( + BkBasic.failure( + ex, + HttpURLConnection.HTTP_INTERNAL_ERROR + ) + ).print(output); } } @@ -189,4 +178,5 @@ private static Request addSocketHeaders(final Request req, String.format("%s: %d", BkBasic.REMOTEPORT, socket.getPort()) ); } + } diff --git a/src/main/java/org/takes/rs/Body.java b/src/main/java/org/takes/rs/RsBody.java similarity index 92% rename from src/main/java/org/takes/rs/Body.java rename to src/main/java/org/takes/rs/RsBody.java index fde655ded..a3725dd25 100644 --- a/src/main/java/org/takes/rs/Body.java +++ b/src/main/java/org/takes/rs/RsBody.java @@ -39,7 +39,7 @@ * * @since 0.32 */ -interface Body extends Input { +interface RsBody extends Input { /** * Gives an {@code InputStream} corresponding to the content of * the body. @@ -61,7 +61,7 @@ interface Body extends Input { * Content of a body based on an {@link java.net.URL}. * @since 0.32 */ - final class Url implements Body { + final class Url implements RsBody { /** * The {@link java.net.URL} of the content. @@ -93,7 +93,7 @@ public int length() throws IOException { * Content of a body based on a byte array. * @since 0.32 */ - final class ByteArray implements Body { + final class ByteArray implements RsBody { /** * The content of the body in a byte array. @@ -123,7 +123,7 @@ public int length() { * The content of the body based on an {@link InputStream}. * @since 0.32 */ - final class Stream implements Body { + final class Stream implements RsBody { /** * The content of the body in an InputStream. @@ -173,12 +173,12 @@ private void estimate() throws IOException { * *

The content of the Body will be stored into a temporary * file to be able to read it as many times as we want so use it only - * for large content, for small content use {@link Body.ByteArray} + * for large content, for small content use {@link RsBody.ByteArray} * instead. * * @since 0.32 */ - final class TempFile implements Body { + final class TempFile implements RsBody { /** * The temporary file that contains the content of the body. @@ -188,20 +188,20 @@ final class TempFile implements Body { /** * The underlying body. */ - private final Body body; + private final RsBody body; /** - * Constructs a {@code TempFile} with the specified {@link Body}. - * @param body The content of the body to store into a temporary file. + * Constructs a {@code TempFile} with the specified {@link RsBody}. + * @param content The content of the body to store into a temporary file. */ @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") - TempFile(final Body body) { - this.body = body; + TempFile(final RsBody content) { + this.body = content; this.file = new File( System.getProperty("java.io.tmpdir"), String.format( "%s-%s.tmp", - Body.TempFile.class.getName(), + RsBody.TempFile.class.getName(), UUID.randomUUID().toString() ) ); diff --git a/src/main/java/org/takes/rs/BodyPrint.java b/src/main/java/org/takes/rs/RsBodyPrint.java similarity index 60% rename from src/main/java/org/takes/rs/BodyPrint.java rename to src/main/java/org/takes/rs/RsBodyPrint.java index edaaac285..087ee672d 100644 --- a/src/main/java/org/takes/rs/BodyPrint.java +++ b/src/main/java/org/takes/rs/RsBodyPrint.java @@ -23,22 +23,23 @@ */ package org.takes.rs; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; import org.cactoos.Text; -import org.cactoos.text.TextOf; import org.takes.Response; /** - * Response body decorator that can print an entire body response in HTTP - * format. + * Response body decorator that can print an entire textual (!) + * body response in HTTP format. + * + *

This class is mostly used for testing. Don't use it for + * production code, since it will break the binary content of your + * HTTP response. It's only suitable for texts in HTTP responses.

* *

The class is immutable and thread-safe. * * @since 2.0 */ -public final class BodyPrint implements Body, Text { +public final class RsBodyPrint implements Text { /** * The HTTP Response. @@ -50,37 +51,12 @@ public final class BodyPrint implements Body, Text { * * @param res Original response */ - public BodyPrint(final Response res) { + public RsBodyPrint(final Response res) { this.response = res; } @Override public String asString() throws IOException { - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - // @checkstyle MagicNumber (1 line) - final byte[] buf = new byte[4096]; - final InputStream body = this.response.body(); - try { - while (true) { - final int bts = body.read(buf); - if (bts < 0) { - break; - } - baos.write(buf, 0, bts); - } - } finally { - baos.flush(); - } - return new TextOf(baos.toByteArray()).toString(); - } - - @Override - public InputStream stream() throws IOException { - return this.response.body(); - } - - @Override - public int length() throws IOException { - return this.asString().length(); + return new RsPrint(this.response).printBody(); } } diff --git a/src/main/java/org/takes/rs/HeadPrint.java b/src/main/java/org/takes/rs/RsHeadPrint.java similarity index 71% rename from src/main/java/org/takes/rs/HeadPrint.java rename to src/main/java/org/takes/rs/RsHeadPrint.java index 5a03688e9..e31a69f84 100644 --- a/src/main/java/org/takes/rs/HeadPrint.java +++ b/src/main/java/org/takes/rs/RsHeadPrint.java @@ -25,9 +25,7 @@ import java.io.IOException; import org.cactoos.Text; -import org.cactoos.text.FormattedText; -import org.cactoos.text.Joined; -import org.takes.Head; +import org.takes.Response; /** * Response head decorator that can print an entire head response in HTTP @@ -37,42 +35,24 @@ * * @since 2.0 */ -public final class HeadPrint implements Head, Text { - - /** - * HTTP End Of Line. - */ - private static final String EOL = "\r\n"; +public final class RsHeadPrint implements Text { /** * The HTTP Response. */ - private final Head origin; + private final Response origin; /** * Ctor. * @param head Original head */ - public HeadPrint(final Head head) { + public RsHeadPrint(final Response head) { this.origin = head; } @Override public String asString() throws IOException { - return new FormattedText( - "%s%s%s", - new Joined( - HeadPrint.EOL, - this.head() - ), - HeadPrint.EOL, - HeadPrint.EOL - ).toString(); - } - - @Override - public Iterable head() throws IOException { - return this.origin.head(); + return new RsPrint(this.origin).printHead(); } } diff --git a/src/main/java/org/takes/rs/RsHtml.java b/src/main/java/org/takes/rs/RsHtml.java index 66264f41f..204f33405 100644 --- a/src/main/java/org/takes/rs/RsHtml.java +++ b/src/main/java/org/takes/rs/RsHtml.java @@ -106,7 +106,7 @@ public RsHtml(final Response res, final byte[] body) { * @param body HTML body */ public RsHtml(final Response res, final InputStream body) { - this(new RsWithBody(res, new Body.TempFile(new Body.Stream(body)))); + this(new RsWithBody(res, new RsBody.TempFile(new RsBody.Stream(body)))); } /** diff --git a/src/main/java/org/takes/rs/RsPrint.java b/src/main/java/org/takes/rs/RsPrint.java index a6695122c..e3c232e77 100644 --- a/src/main/java/org/takes/rs/RsPrint.java +++ b/src/main/java/org/takes/rs/RsPrint.java @@ -23,14 +23,17 @@ */ package org.takes.rs; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.util.regex.Pattern; import lombok.EqualsAndHashCode; import lombok.ToString; -import org.cactoos.Bytes; import org.cactoos.Text; -import org.cactoos.bytes.BytesOf; -import org.cactoos.text.Joined; -import org.cactoos.text.Sticky; -import org.cactoos.text.TextOf; import org.takes.Response; /** @@ -42,17 +45,21 @@ */ @ToString(callSuper = true) @EqualsAndHashCode(callSuper = true) -public final class RsPrint extends RsWrap implements Text, Bytes { +public final class RsPrint extends RsWrap implements Text { /** - * Head print representation. + * Pattern for first line. */ - private final Text head; + private static final Pattern FIRST = Pattern.compile( + "HTTP/1\\.1 \\d{3} [a-zA-Z- ]+" + ); /** - * Body print representation. + * Pattern for all other lines in the head. */ - private final Text body; + private static final Pattern OTHERS = Pattern.compile( + "[a-zA-Z0-9\\-]+:\\p{Print}+" + ); /** * Ctor. @@ -60,21 +67,114 @@ public final class RsPrint extends RsWrap implements Text, Bytes { */ public RsPrint(final Response res) { super(res); - this.head = new Sticky(new HeadPrint(res)); - this.body = new Sticky(new BodyPrint(res)); } @Override public String asString() throws Exception { - return new Joined( - new TextOf(""), - this.head, - this.body - ).asString(); + return this.print(); } - @Override - public byte[] asBytes() throws Exception { - return new BytesOf(this.asString()).asBytes(); + /** + * Print it into string. + * @return Entire HTTP response + * @throws IOException If fails + */ + public String print() throws IOException { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + this.print(baos); + return new String(baos.toByteArray(), StandardCharsets.UTF_8); + } + + /** + * Print body into string. + * @return Entire body of HTTP response + * @throws IOException If fails + */ + public String printBody() throws IOException { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + this.printBody(baos); + return new String(baos.toByteArray(), StandardCharsets.UTF_8); + } + + /** + * Print head into string. + * @return Entire head of HTTP response + * @throws IOException If fails + * @since 0.10 + */ + public String printHead() throws IOException { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + this.printHead(baos); + return new String(baos.toByteArray(), StandardCharsets.UTF_8); + } + + /** + * Print it into output stream. + * @param output Output to print into + * @throws IOException If fails + */ + public void print(final OutputStream output) throws IOException { + this.printHead(output); + this.printBody(output); + } + + /** + * Print it into output stream. + * @param output Output to print into + * @throws IOException If fails + * @since 0.10 + */ + public void printHead(final OutputStream output) throws IOException { + final String eol = "\r\n"; + final Writer writer = + new OutputStreamWriter(output, StandardCharsets.UTF_8); + int pos = 0; + for (final String line : this.head()) { + if (pos == 0 && !RsPrint.FIRST.matcher(line).matches()) { + throw new IllegalArgumentException( + String.format( + // @checkstyle LineLength (1 line) + "first line of HTTP response \"%s\" doesn't match \"%s\" regular expression, but it should, according to RFC 7230", + line, RsPrint.FIRST + ) + ); + } + if (pos > 0 && !RsPrint.OTHERS.matcher(line).matches()) { + throw new IllegalArgumentException( + String.format( + // @checkstyle LineLength (1 line) + "header line #%d of HTTP response \"%s\" doesn't match \"%s\" regular expression, but it should, according to RFC 7230", + pos + 1, line, RsPrint.OTHERS + ) + ); + } + writer.append(line); + writer.append(eol); + ++pos; + } + writer.append(eol); + writer.flush(); + } + + /** + * Print it into output stream. + * @param output Output to print into + * @throws IOException If fails + */ + public void printBody(final OutputStream output) throws IOException { + final InputStream body = this.body(); + try { + //@checkstyle MagicNumberCheck (1 line) + final byte[] buf = new byte[4096]; + while (true) { + final int bytes = body.read(buf); + if (bytes < 0) { + break; + } + output.write(buf, 0, bytes); + } + } finally { + body.close(); + } } } diff --git a/src/main/java/org/takes/rs/RsText.java b/src/main/java/org/takes/rs/RsText.java index f7b54f81e..41dd48da1 100644 --- a/src/main/java/org/takes/rs/RsText.java +++ b/src/main/java/org/takes/rs/RsText.java @@ -106,7 +106,7 @@ public RsText(final Response res, final byte[] body) { * @param body HTML body */ public RsText(final Response res, final InputStream body) { - this(new RsWithBody(res, new Body.TempFile(new Body.Stream(body)))); + this(new RsWithBody(res, new RsBody.TempFile(new RsBody.Stream(body)))); } /** @@ -124,9 +124,7 @@ public RsText(final Response res, final URL url) { * @since 0.10 */ public RsText(final Response res) { - super( - new RsWithType(res, "text/plain") - ); + super(new RsWithType(res, "text/plain")); } } diff --git a/src/main/java/org/takes/rs/RsWithBody.java b/src/main/java/org/takes/rs/RsWithBody.java index 0bb62dc43..b99bb3757 100644 --- a/src/main/java/org/takes/rs/RsWithBody.java +++ b/src/main/java/org/takes/rs/RsWithBody.java @@ -112,7 +112,7 @@ public RsWithBody(final Response res, final CharSequence body, * @param url URL with body */ public RsWithBody(final Response res, final URL url) { - this(res, new Body.Url(url)); + this(res, new RsBody.Url(url)); } /** @@ -121,7 +121,7 @@ public RsWithBody(final Response res, final URL url) { * @param body Body */ public RsWithBody(final Response res, final byte[] body) { - this(res, new Body.ByteArray(body)); + this(res, new RsBody.ByteArray(body)); } /** @@ -130,7 +130,7 @@ public RsWithBody(final Response res, final byte[] body) { * @param body Body */ public RsWithBody(final Response res, final InputStream body) { - this(res, new Body.Stream(body)); + this(res, new RsBody.Stream(body)); } /** @@ -139,7 +139,7 @@ public RsWithBody(final Response res, final InputStream body) { * @param res Original response * @param body The content of the body */ - RsWithBody(final Response res, final Body body) { + RsWithBody(final Response res, final RsBody body) { super( new ResponseOf( () -> { diff --git a/src/test/java/org/takes/facets/auth/PsBasicTest.java b/src/test/java/org/takes/facets/auth/PsBasicTest.java index cc870cccc..718d42693 100644 --- a/src/test/java/org/takes/facets/auth/PsBasicTest.java +++ b/src/test/java/org/takes/facets/auth/PsBasicTest.java @@ -42,7 +42,7 @@ import org.takes.rq.RqMethod; import org.takes.rq.RqWithHeader; import org.takes.rq.RqWithHeaders; -import org.takes.rs.HeadPrint; +import org.takes.rs.RsHeadPrint; import org.takes.rs.RsPrint; import org.takes.tk.TkText; @@ -154,7 +154,7 @@ void handleConnectionWithInvalidCredential() throws Exception { forward = ex; } MatcherAssert.assertThat( - new HeadPrint(forward).asString(), + new RsHeadPrint(forward).asString(), Matchers.allOf( Matchers.containsString("HTTP/1.1 401 Unauthorized"), Matchers.containsString( diff --git a/src/test/java/org/takes/facets/fallback/FbChainTest.java b/src/test/java/org/takes/facets/fallback/FbChainTest.java index 08169492a..1625b6f4e 100644 --- a/src/test/java/org/takes/facets/fallback/FbChainTest.java +++ b/src/test/java/org/takes/facets/fallback/FbChainTest.java @@ -27,7 +27,7 @@ import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; -import org.takes.rs.BodyPrint; +import org.takes.rs.RsBodyPrint; import org.takes.rs.RsText; /** @@ -46,7 +46,7 @@ void chainsFallbacks() throws Exception { HttpURLConnection.HTTP_NOT_FOUND ); MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new FbChain( new FbEmpty(), new FbFixed(new RsText("first rs")), diff --git a/src/test/java/org/takes/facets/fallback/FbStatusTest.java b/src/test/java/org/takes/facets/fallback/FbStatusTest.java index a777b80a3..b4329b26a 100644 --- a/src/test/java/org/takes/facets/fallback/FbStatusTest.java +++ b/src/test/java/org/takes/facets/fallback/FbStatusTest.java @@ -31,8 +31,8 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.IsText; -import org.takes.rs.BodyPrint; -import org.takes.rs.HeadPrint; +import org.takes.rs.RsBodyPrint; +import org.takes.rs.RsHeadPrint; import org.takes.rs.RsPrint; import org.takes.rs.RsText; import org.takes.tk.TkFixed; @@ -53,7 +53,7 @@ void reactsToCorrectStatus() throws Exception { final int status = HttpURLConnection.HTTP_NOT_FOUND; final RqFallback req = new RqFallback.Fake(status); MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new FbStatus( status, new TkFixed(new RsText("not found response")) @@ -73,7 +73,7 @@ void reactsToCondition() throws Exception { HttpURLConnection.HTTP_MOVED_PERM ); MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new FbStatus( new Filtered<>( status -> status == HttpURLConnection.HTTP_MOVED_PERM @@ -125,11 +125,11 @@ void sendsCorrectDefaultResponse() throws Exception { new FbStatus(code).route(req).get() ); MatcherAssert.assertThat( - new BodyPrint(response), + new RsBodyPrint(response), new IsText("404 Not Found: Exception message") ); MatcherAssert.assertThat( - new HeadPrint(response).asString(), + new RsHeadPrint(response).asString(), Matchers.both( Matchers.containsString("Content-Type: text/plain") ).and(Matchers.containsString("404 Not Found")) diff --git a/src/test/java/org/takes/facets/fallback/TkFallbackTest.java b/src/test/java/org/takes/facets/fallback/TkFallbackTest.java index 6657e51be..9908db56b 100644 --- a/src/test/java/org/takes/facets/fallback/TkFallbackTest.java +++ b/src/test/java/org/takes/facets/fallback/TkFallbackTest.java @@ -28,8 +28,8 @@ import org.junit.jupiter.api.Test; import org.takes.misc.Opt; import org.takes.rq.RqFake; -import org.takes.rs.BodyPrint; import org.takes.rs.ResponseOf; +import org.takes.rs.RsBodyPrint; import org.takes.rs.RsText; import org.takes.tk.TkFailure; @@ -48,7 +48,7 @@ final class TkFallbackTest { void fallsBack() throws Exception { final String err = "message"; MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new TkFallback( new TkFailure(err), req -> new Opt.Single<>( @@ -67,7 +67,7 @@ void fallsBack() throws Exception { @Test void fallsBackInsideResponse() throws Exception { MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new TkFallback( req -> new ResponseOf( () -> { diff --git a/src/test/java/org/takes/facets/fork/RsForkTest.java b/src/test/java/org/takes/facets/fork/RsForkTest.java index bb1464815..9fc497b6c 100644 --- a/src/test/java/org/takes/facets/fork/RsForkTest.java +++ b/src/test/java/org/takes/facets/fork/RsForkTest.java @@ -30,7 +30,7 @@ import org.junit.jupiter.api.Test; import org.takes.Request; import org.takes.rq.RqFake; -import org.takes.rs.BodyPrint; +import org.takes.rs.RsBodyPrint; import org.takes.rs.RsText; /** @@ -54,7 +54,7 @@ void negotiatesContent() throws IOException { "" ); MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new RsFork( req, new FkTypes("text/plain", new RsText("it's a text")), @@ -72,7 +72,7 @@ void negotiatesContent() throws IOException { @Test void negotiatesContentWithoutAccept() throws IOException { MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new RsFork( new RqFake(), new FkTypes("image/png,*/*", new RsText("a png")) @@ -96,7 +96,7 @@ void negotiatesContentWithComplexHeader() throws IOException { "" ); MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new RsFork( req, new FkTypes( @@ -116,7 +116,7 @@ void negotiatesContentWithComplexHeader() throws IOException { @Test void dispatchesByRequestMethod() throws IOException { MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new RsFork( new RqFake("POST", "/test?1", "alpha=1"), new FkMethods("GET", new RsText("it's a GET")), diff --git a/src/test/java/org/takes/facets/hamcrest/HmRqTextBodyTest.java b/src/test/java/org/takes/facets/hamcrest/HmRqTextRsBodyTest.java similarity index 98% rename from src/test/java/org/takes/facets/hamcrest/HmRqTextBodyTest.java rename to src/test/java/org/takes/facets/hamcrest/HmRqTextRsBodyTest.java index f2546f094..1aaab4bf8 100644 --- a/src/test/java/org/takes/facets/hamcrest/HmRqTextBodyTest.java +++ b/src/test/java/org/takes/facets/hamcrest/HmRqTextRsBodyTest.java @@ -34,7 +34,7 @@ * * @since 2.0 */ -final class HmRqTextBodyTest { +final class HmRqTextRsBodyTest { /** * HmRqTextBody can test if body equals text. diff --git a/src/test/java/org/takes/facets/hamcrest/HmBodyTest.java b/src/test/java/org/takes/facets/hamcrest/HmRsBodyTest.java similarity index 99% rename from src/test/java/org/takes/facets/hamcrest/HmBodyTest.java rename to src/test/java/org/takes/facets/hamcrest/HmRsBodyTest.java index bf942c92c..0ab44874e 100644 --- a/src/test/java/org/takes/facets/hamcrest/HmBodyTest.java +++ b/src/test/java/org/takes/facets/hamcrest/HmRsBodyTest.java @@ -39,7 +39,7 @@ * * @since 2.0 */ -final class HmBodyTest { +final class HmRsBodyTest { /** * HmRqBody can test if values of bodies are same. diff --git a/src/test/java/org/takes/facets/hamcrest/HmRsTextBodyTest.java b/src/test/java/org/takes/facets/hamcrest/HmRsTextRsBodyTest.java similarity index 98% rename from src/test/java/org/takes/facets/hamcrest/HmRsTextBodyTest.java rename to src/test/java/org/takes/facets/hamcrest/HmRsTextRsBodyTest.java index 5f3fc5a65..b01bd34b8 100644 --- a/src/test/java/org/takes/facets/hamcrest/HmRsTextBodyTest.java +++ b/src/test/java/org/takes/facets/hamcrest/HmRsTextRsBodyTest.java @@ -33,7 +33,7 @@ * * @since 2.0 */ -final class HmRsTextBodyTest { +final class HmRsTextRsBodyTest { /** * HmRsTextBody can test if body equals text. diff --git a/src/test/java/org/takes/facets/hamcrest/HmTextBodyTest.java b/src/test/java/org/takes/facets/hamcrest/HmTextRsBodyTest.java similarity index 99% rename from src/test/java/org/takes/facets/hamcrest/HmTextBodyTest.java rename to src/test/java/org/takes/facets/hamcrest/HmTextRsBodyTest.java index acfa7ead4..46bb9990b 100644 --- a/src/test/java/org/takes/facets/hamcrest/HmTextBodyTest.java +++ b/src/test/java/org/takes/facets/hamcrest/HmTextRsBodyTest.java @@ -39,7 +39,7 @@ * * @since 2.0 */ -final class HmTextBodyTest { +final class HmTextRsBodyTest { /** * Tests mismatch description. diff --git a/src/test/java/org/takes/http/FtRemoteTest.java b/src/test/java/org/takes/http/FtRemoteTest.java index 64698607f..619f03351 100644 --- a/src/test/java/org/takes/http/FtRemoteTest.java +++ b/src/test/java/org/takes/http/FtRemoteTest.java @@ -55,12 +55,18 @@ final class FtRemoteTest { */ @Test void simplyWorks() throws Exception { - new FtRemote(new TkFixed(new RsText("simple answer"))).exec( - home -> new JdkRequest(home) - .fetch() - .as(RestResponse.class) - .assertStatus(HttpURLConnection.HTTP_OK) - .assertBody(Matchers.startsWith("simple")) + // @checkstyle MagicNumber (2 lines) + final byte[] data = new byte[4]; + data[0] = (byte) 0xff; + new FtRemote(new TkFixed(new RsText(data))).exec( + home -> MatcherAssert.assertThat( + new JdkRequest(home) + .fetch() + .as(RestResponse.class) + .assertStatus(HttpURLConnection.HTTP_OK) + .binary(), + Matchers.equalTo(data) + ) ); } diff --git a/src/test/java/org/takes/rq/RqWithBodyTest.java b/src/test/java/org/takes/rq/RqWithRsBodyTest.java similarity index 98% rename from src/test/java/org/takes/rq/RqWithBodyTest.java rename to src/test/java/org/takes/rq/RqWithRsBodyTest.java index 06bb7c7d1..b6e92f636 100644 --- a/src/test/java/org/takes/rq/RqWithBodyTest.java +++ b/src/test/java/org/takes/rq/RqWithRsBodyTest.java @@ -31,7 +31,7 @@ * Test case for {@link RqWithBody}. * @since 0.22 */ -final class RqWithBodyTest { +final class RqWithRsBodyTest { /** * RqWithBody returns body. diff --git a/src/test/java/org/takes/rs/BodyByteArrayTest.java b/src/test/java/org/takes/rs/RsBodyByteArrayTest.java similarity index 91% rename from src/test/java/org/takes/rs/BodyByteArrayTest.java rename to src/test/java/org/takes/rs/RsBodyByteArrayTest.java index 79c3ae6ed..553a5493b 100644 --- a/src/test/java/org/takes/rs/BodyByteArrayTest.java +++ b/src/test/java/org/takes/rs/RsBodyByteArrayTest.java @@ -30,11 +30,11 @@ import org.junit.jupiter.api.Test; /** - * Test case for {@link Body.ByteArray}. + * Test case for {@link RsBody.ByteArray}. * * @since 1.15 */ -final class BodyByteArrayTest { +final class RsBodyByteArrayTest { /** * Body.ByteArray can provide the expected input. @@ -46,7 +46,7 @@ void returnsCorrectInputWithByteArray() throws Exception { new BytesOf("ByteArray returnsCorrectInput!").asBytes(); MatcherAssert.assertThat( "Body content of Body.ByteArray doesn't provide the correct bytes", - new BytesOf(new Body.ByteArray(bytes)).asBytes(), + new BytesOf(new RsBody.ByteArray(bytes)).asBytes(), new IsEqual<>(bytes) ); } @@ -61,7 +61,7 @@ void returnsCorrectLengthWithStream() throws Exception { new BytesOf("Stream returnsCorrectLength!").asBytes(); MatcherAssert.assertThat( "Body content of Body.ByteArray doesn't have the correct length", - new LengthOf(new Body.ByteArray(bytes)).value(), + new LengthOf(new RsBody.ByteArray(bytes)).value(), new IsEqual<>((long) bytes.length) ); } diff --git a/src/test/java/org/takes/rs/BodyPrintTest.java b/src/test/java/org/takes/rs/RsBodyPrintTest.java similarity index 91% rename from src/test/java/org/takes/rs/BodyPrintTest.java rename to src/test/java/org/takes/rs/RsBodyPrintTest.java index 59d759c0f..3e40d5786 100644 --- a/src/test/java/org/takes/rs/BodyPrintTest.java +++ b/src/test/java/org/takes/rs/RsBodyPrintTest.java @@ -30,16 +30,16 @@ import org.junit.jupiter.api.Test; /** - * Test case for {@link BodyPrint}. + * Test case for {@link RsBodyPrint}. * @since 1.19 */ -@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class BodyPrintTest { +@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RsBodyPrintTest { @Test void simple() throws IOException { MatcherAssert.assertThat( "must write body", - new BodyPrint( + new RsBodyPrint( new RsText("World!") ).asString(), new HasToString<>( diff --git a/src/test/java/org/takes/rs/BodyStreamTest.java b/src/test/java/org/takes/rs/RsBodyStreamTest.java similarity index 92% rename from src/test/java/org/takes/rs/BodyStreamTest.java rename to src/test/java/org/takes/rs/RsBodyStreamTest.java index 63144f94a..4f72024e7 100644 --- a/src/test/java/org/takes/rs/BodyStreamTest.java +++ b/src/test/java/org/takes/rs/RsBodyStreamTest.java @@ -31,11 +31,11 @@ import org.junit.jupiter.api.Test; /** - * Test case for {@link Body.Stream}. + * Test case for {@link RsBody.Stream}. * * @since 1.15 */ -final class BodyStreamTest { +final class RsBodyStreamTest { /** * Body.Stream can provide the expected input. @@ -48,7 +48,7 @@ void returnsCorrectInputWithStream() throws Exception { MatcherAssert.assertThat( "Body content of Body.Stream doesn't provide the correct bytes", new BytesOf( - new Body.Stream(new InputOf(bytes).stream()) + new RsBody.Stream(new InputOf(bytes).stream()) ).asBytes(), new IsEqual<>(bytes) ); @@ -65,7 +65,7 @@ void returnsCorrectLengthWithStream() throws Exception { MatcherAssert.assertThat( "Body content of Body.Stream doesn't have the correct length", new LengthOf( - new Body.Stream(new InputOf(bytes).stream()) + new RsBody.Stream(new InputOf(bytes).stream()) ).value(), new IsEqual<>((long) bytes.length) ); diff --git a/src/test/java/org/takes/rs/BodyTempFileTest.java b/src/test/java/org/takes/rs/RsBodyTempFileTest.java similarity index 92% rename from src/test/java/org/takes/rs/BodyTempFileTest.java rename to src/test/java/org/takes/rs/RsBodyTempFileTest.java index d793c5d2c..bfa87dcfe 100644 --- a/src/test/java/org/takes/rs/BodyTempFileTest.java +++ b/src/test/java/org/takes/rs/RsBodyTempFileTest.java @@ -30,11 +30,11 @@ import org.junit.jupiter.api.Test; /** - * Test case for {@link Body.TempFile}. + * Test case for {@link RsBody.TempFile}. * * @since 1.15 */ -final class BodyTempFileTest { +final class RsBodyTempFileTest { /** * Body.TemFile can provide the expected input. @@ -47,7 +47,7 @@ void returnsCorrectInputWithStream() throws Exception { MatcherAssert.assertThat( "Body content of Body.TempFile doesn't provide the correct bytes", new BytesOf( - new Body.TempFile(new Body.ByteArray(bytes)) + new RsBody.TempFile(new RsBody.ByteArray(bytes)) ).asBytes(), new IsEqual<>(bytes) ); @@ -64,7 +64,7 @@ void returnsCorrectLengthWithTempFile() throws Exception { MatcherAssert.assertThat( "Body content of Body.TempFile doesn't have the correct length", new LengthOf( - new Body.TempFile(new Body.ByteArray(bytes)) + new RsBody.TempFile(new RsBody.ByteArray(bytes)) ).value(), new IsEqual<>((long) bytes.length) ); diff --git a/src/test/java/org/takes/rs/BodyUrlTest.java b/src/test/java/org/takes/rs/RsBodyUrlTest.java similarity index 93% rename from src/test/java/org/takes/rs/BodyUrlTest.java rename to src/test/java/org/takes/rs/RsBodyUrlTest.java index 2188c1b8e..d93ab8a1b 100644 --- a/src/test/java/org/takes/rs/BodyUrlTest.java +++ b/src/test/java/org/takes/rs/RsBodyUrlTest.java @@ -35,12 +35,12 @@ import org.junit.jupiter.api.Test; /** - * Test case for {@link Body.Url}. + * Test case for {@link RsBody.Url}. * * @since 1.15 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ -final class BodyUrlTest { +final class RsBodyUrlTest { /** * Body.URL can provide the expected input. @@ -55,7 +55,7 @@ void returnsCorrectInputWithUrl() throws Exception { new TeeInput(body, new OutputTo(out)) ).value(); } - final Body.Url input = new Body.Url(file.value().toUri().toURL()); + final RsBody.Url input = new RsBody.Url(file.value().toUri().toURL()); MatcherAssert.assertThat( "Body content of Body.Url doesn't provide the correct bytes", new BytesOf(input).asBytes(), @@ -80,7 +80,7 @@ void returnsCorrectLengthWithUrl() throws Exception { MatcherAssert.assertThat( "Body content of Body.Url doesn't have the correct length", new LengthOf( - new Body.Url(file.value().toUri().toURL()) + new RsBody.Url(file.value().toUri().toURL()) ).value(), new IsEqual<>((long) body.asBytes().length) ); diff --git a/src/test/java/org/takes/rs/RsGzipTest.java b/src/test/java/org/takes/rs/RsGzipTest.java index e1f4d5a9b..95ddec202 100644 --- a/src/test/java/org/takes/rs/RsGzipTest.java +++ b/src/test/java/org/takes/rs/RsGzipTest.java @@ -52,7 +52,7 @@ void makesCompressedResponse() throws IOException { final String text = "some unicode text: \u20ac\n\t"; final Response response = new RsGzip(new RsText(text)); MatcherAssert.assertThat( - new HeadPrint(response).asString(), + new RsHeadPrint(response).asString(), Matchers.containsString("Content-Encoding: gzip") ); MatcherAssert.assertThat( @@ -99,11 +99,11 @@ void reportsCorrectContentLength() throws IOException { final String text = "some text to encode"; final Response response = new RsGzip(new RsText(text)); MatcherAssert.assertThat( - new HeadPrint(response).asString(), + new RsHeadPrint(response).asString(), Matchers.containsString( String.format( "Content-Length: %d", - new BodyPrint(response).length() + new RsBodyPrint(response).asString().length() ) ) ); diff --git a/src/test/java/org/takes/rs/HeadPrintTest.java b/src/test/java/org/takes/rs/RsHeadPrintTest.java similarity index 93% rename from src/test/java/org/takes/rs/HeadPrintTest.java rename to src/test/java/org/takes/rs/RsHeadPrintTest.java index 2b51aad38..5936fa3fd 100644 --- a/src/test/java/org/takes/rs/HeadPrintTest.java +++ b/src/test/java/org/takes/rs/RsHeadPrintTest.java @@ -31,10 +31,10 @@ import org.llorllale.cactoos.matchers.Throws; /** - * Test case for {@link HeadPrint}. + * Test case for {@link RsHeadPrint}. * @since 1.19 */ -final class HeadPrintTest { +final class RsHeadPrintTest { /** * HeadPrint can fail on invalid chars. @@ -42,7 +42,7 @@ final class HeadPrintTest { void failsOnInvalidHeader() { MatcherAssert.assertThat( "Must catch invalid header exception", - () -> new HeadPrint( + () -> new RsHeadPrint( new RsWithHeader("name", "\n\n\n") ).asString(), new Throws<>(IllegalArgumentException.class) @@ -53,7 +53,7 @@ void failsOnInvalidHeader() { void simple() { MatcherAssert.assertThat( "must write head", - new HeadPrint( + new RsHeadPrint( new RsSimple(new IterableOf<>("HTTP/1.1 500 Internal Server Error"), "") ), new IsText("HTTP/1.1 500 Internal Server Error\r\n\r\n") @@ -67,7 +67,7 @@ void simple() { void simpleWithDash() { new Assertion<>( "must write head with dashes", - new HeadPrint( + new RsHeadPrint( new RsSimple(new IterableOf<>("HTTP/1.1 203 Non-Authoritative"), "") ), new IsText("HTTP/1.1 203 Non-Authoritative\r\n\r\n") diff --git a/src/test/java/org/takes/rs/RsPrettyJsonTest.java b/src/test/java/org/takes/rs/RsPrettyJsonTest.java index ed6bb5b75..2f2018fe0 100644 --- a/src/test/java/org/takes/rs/RsPrettyJsonTest.java +++ b/src/test/java/org/takes/rs/RsPrettyJsonTest.java @@ -49,7 +49,7 @@ final class RsPrettyJsonTest { @Test void formatsJsonBody() throws Exception { MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new RsPrettyJson( new RsWithBody("{\"widget\": {\"debug\": \"on\" }}") ) @@ -67,7 +67,7 @@ void formatsJsonBody() throws Exception { void rejectsNonJsonBody() { Assertions.assertThrows( IOException.class, - () -> new BodyPrint(new RsPrettyJson(new RsWithBody("foo"))).asString() + () -> new RsBodyPrint(new RsPrettyJson(new RsWithBody("foo"))).asString() ); } @@ -80,7 +80,7 @@ void reportsCorrectContentLength() throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (Writer w = new OutputStreamWriter(baos)) { w.write( - new BodyPrint( + new RsBodyPrint( new RsWithBody( "\n{\n \"test\": {\n \"test\": \"test\"\n }\n}" ) @@ -88,7 +88,7 @@ void reportsCorrectContentLength() throws Exception { ); } MatcherAssert.assertThat( - new HeadPrint( + new RsHeadPrint( new RsPrettyJson( new RsWithBody("{\"test\": {\"test\": \"test\" }}") ) diff --git a/src/test/java/org/takes/rs/RsPrettyXmlTest.java b/src/test/java/org/takes/rs/RsPrettyXmlTest.java index e3324ff73..b89640afa 100644 --- a/src/test/java/org/takes/rs/RsPrettyXmlTest.java +++ b/src/test/java/org/takes/rs/RsPrettyXmlTest.java @@ -49,7 +49,7 @@ final class RsPrettyXmlTest { @Test void formatsXmlBody() throws IOException { MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new RsPrettyXml( new RsWithBody("foo") ) @@ -65,7 +65,7 @@ void formatsXmlBody() throws IOException { // @checkstyle MethodNameCheck (1 line) void formatsHtml5DoctypeBody() throws IOException { MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new RsPrettyXml( new RsWithBody( "" @@ -85,7 +85,7 @@ void formatsHtml5DoctypeBody() throws IOException { void formatsHtml5ForLegacyBrowsersDoctypeBody() throws IOException { MatcherAssert.assertThat( new TextOf( - new BodyPrint( + new RsBodyPrint( new RsPrettyXml( new RsWithBody( new InputStreamOf( @@ -129,7 +129,7 @@ void formatsHtml4DoctypeBody() throws IOException { .concat("lang=\"en\">"); MatcherAssert.assertThat( new TextOf( - new BodyPrint( + new RsBodyPrint( new RsPrettyXml( new RsWithBody( new InputStreamOf( @@ -170,7 +170,7 @@ void formatsHtml4DoctypeBody() throws IOException { void formatsNonXmlBody() { Assertions.assertThrows( IOException.class, - () -> new BodyPrint(new RsPrettyXml(new RsWithBody("foo"))).asString() + () -> new RsBodyPrint(new RsPrettyXml(new RsWithBody("foo"))).asString() ); } @@ -180,13 +180,13 @@ void formatsNonXmlBody() { */ @Test void reportsCorrectContentLength() throws IOException { - final int clength = new BodyPrint( + final int clength = new RsBodyPrint( new RsWithBody( "\n test\n\n" ) - ).length(); + ).asString().length(); MatcherAssert.assertThat( - new HeadPrint( + new RsHeadPrint( new RsPrettyXml( new RsWithBody("test") ) diff --git a/src/test/java/org/takes/rs/RsPrintTest.java b/src/test/java/org/takes/rs/RsPrintTest.java index 5f5502859..f35e73c71 100644 --- a/src/test/java/org/takes/rs/RsPrintTest.java +++ b/src/test/java/org/takes/rs/RsPrintTest.java @@ -24,7 +24,6 @@ package org.takes.rs; import org.cactoos.Text; -import org.cactoos.bytes.BytesOf; import org.cactoos.iterable.IterableOf; import org.hamcrest.MatcherAssert; import org.hamcrest.core.IsEqual; @@ -41,6 +40,18 @@ @SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RsPrintTest { + /** + * RsPrint can fail on invalid chars. + */ + @Test + void printsBytesCorrectly() { + final Text response = new RsPrint(new RsWithHeader("name", "\n\n\n")); + Assertions.assertThrows( + IllegalArgumentException.class, + response::asString + ); + } + /** * RsPrint can fail on invalid chars. */ @@ -65,13 +76,6 @@ void simple() throws Exception { new IsEqual<>("HTTP/1.1 500 Internal Server Error\r\n\r\n") ) ); - MatcherAssert.assertThat( - "must write head as Bytes", - response.asBytes(), - new IsEqual<>( - new BytesOf("HTTP/1.1 500 Internal Server Error\r\n\r\n").asBytes() - ) - ); } /** diff --git a/src/test/java/org/takes/rs/RsTextTest.java b/src/test/java/org/takes/rs/RsTextTest.java index 1b04417cc..93348ce13 100644 --- a/src/test/java/org/takes/rs/RsTextTest.java +++ b/src/test/java/org/takes/rs/RsTextTest.java @@ -65,7 +65,7 @@ void makesPlainTextResponse() { @Test void makesTextResponseWithStatus() throws IOException { MatcherAssert.assertThat( - new HeadPrint( + new RsHeadPrint( new RsText( new RsWithStatus(HttpURLConnection.HTTP_NOT_FOUND), "something not found" diff --git a/src/test/java/org/takes/tk/TkClasspathTest.java b/src/test/java/org/takes/tk/TkClasspathTest.java index 9040baf20..2e4f52381 100644 --- a/src/test/java/org/takes/tk/TkClasspathTest.java +++ b/src/test/java/org/takes/tk/TkClasspathTest.java @@ -29,7 +29,7 @@ import org.llorllale.cactoos.matchers.StartsWith; import org.takes.HttpException; import org.takes.rq.RqFake; -import org.takes.rs.HeadPrint; +import org.takes.rs.RsHeadPrint; /** * Test case for {@link TkClasspath}. @@ -45,7 +45,7 @@ final class TkClasspathTest { @Test void dispatchesByResourceName() throws Exception { MatcherAssert.assertThat( - new HeadPrint( + new RsHeadPrint( new TkClasspath().act( new RqFake( "GET", "/org/takes/Take.class?a", "" diff --git a/src/test/java/org/takes/tk/TkCorsTest.java b/src/test/java/org/takes/tk/TkCorsTest.java index 9e3eb89b5..d2a6f6ac6 100644 --- a/src/test/java/org/takes/tk/TkCorsTest.java +++ b/src/test/java/org/takes/tk/TkCorsTest.java @@ -30,7 +30,7 @@ import org.takes.facets.hamcrest.HmRsStatus; import org.takes.rq.RqFake; import org.takes.rq.RqWithHeaders; -import org.takes.rs.HeadPrint; +import org.takes.rs.RsHeadPrint; import org.takes.rs.RsText; /** @@ -87,7 +87,7 @@ void cantHandleConnectionsWithWrongDomainOnOrigin() throws Exception { MatcherAssert.assertThat( "Wrong value on header.", - new HeadPrint( + new RsHeadPrint( new TkCors( new TkFixed(new RsText()), "http://www.teamed.io", diff --git a/src/test/java/org/takes/tk/TkFilesTest.java b/src/test/java/org/takes/tk/TkFilesTest.java index 7aa9a0a11..504211ff2 100644 --- a/src/test/java/org/takes/tk/TkFilesTest.java +++ b/src/test/java/org/takes/tk/TkFilesTest.java @@ -36,7 +36,7 @@ import org.llorllale.cactoos.matchers.StartsWith; import org.takes.HttpException; import org.takes.rq.RqFake; -import org.takes.rs.HeadPrint; +import org.takes.rs.RsHeadPrint; /** * Test case for {@link TkFiles}. @@ -58,7 +58,7 @@ void dispatchesByFileName(@TempDir final Path temp) throws Exception { StandardCharsets.UTF_8 ); MatcherAssert.assertThat( - new HeadPrint( + new RsHeadPrint( new TkFiles(temp.toFile()).act( new RqFake( "GET", "/a.txt?hash=a1b2c3", "" diff --git a/src/test/java/org/takes/tk/TkGzipTest.java b/src/test/java/org/takes/tk/TkGzipTest.java index 39c4f6672..227135b76 100644 --- a/src/test/java/org/takes/tk/TkGzipTest.java +++ b/src/test/java/org/takes/tk/TkGzipTest.java @@ -29,14 +29,16 @@ import java.net.HttpURLConnection; import java.util.Arrays; import java.util.zip.GZIPInputStream; +import org.cactoos.bytes.BytesOf; import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.StartsWith; import org.takes.http.FtRemote; import org.takes.rq.RqFake; +import org.takes.rq.RqWithHeader; +import org.takes.rs.RsGzip; import org.takes.rs.RsPrint; import org.takes.rs.RsText; @@ -45,8 +47,58 @@ * @since 0.17 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ +@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class TkGzipTest { + /** + * TkGzip can compress correctly. + * @throws Exception If some problem inside + */ + @Test + void compressesExactly() throws Exception { + final String body = "hello"; + MatcherAssert.assertThat( + new BytesOf( + new RsPrint( + new TkGzip(new TkText(body)).act( + new RqWithHeader( + new RqFake("GET", "/"), + "Accept-Encoding", "gzip" + ) + ) + ).body() + ).asBytes(), + Matchers.equalTo( + new BytesOf( + new RsGzip(new RsText(body)).body() + ).asBytes() + ) + ); + } + + /** + * TkGzip can compress correctly. + * @throws Exception If some problem inside + */ + @Test + void compressesCorrectly() throws Exception { + MatcherAssert.assertThat( + new TextOf( + new GZIPInputStream( + new RsPrint( + new TkGzip(new TkText("привет, world!")).act( + new RqWithHeader( + new RqFake("GET", "/"), + "Accept-Encoding", "gzip" + ) + ) + ).body() + ) + ), + new StartsWith("привет, ") + ); + } + /** * TkGzip can compress on demand only. * @throws Exception If some problem inside @@ -98,23 +150,21 @@ void doesntCompressIfNotRequired() throws Exception { */ @Test void returnsExactlyGzipBody() throws Exception { - final String body = "Halo, Siñor!" - new FtRemote(new TkGzip(req -> new RsText("Hi, dude!"))).exec( + final String body = "Halo, Siñor!"; + new FtRemote(new TkGzip(req -> new RsText(body))).exec( home -> MatcherAssert.assertThat( - new TextOf( - new GZIPInputStream( - new ByteArrayInputStream( - new JdkRequest(home) - .method("GET") - .header("Accept-Encoding", "gzip") - .fetch() - .as(RestResponse.class) - .assertStatus(HttpURLConnection.HTTP_OK) - .binary() - ) - ) - ).asString(), - Matchers.startsWith("Hi, ") + new JdkRequest(home) + .method("GET") + .header("Accept-Encoding", "gzip") + .fetch() + .as(RestResponse.class) + .assertStatus(HttpURLConnection.HTTP_OK) + .binary(), + Matchers.equalTo( + new BytesOf( + new RsPrint(new RsGzip(new RsText(body))).body() + ).asBytes() + ) ) ); } @@ -124,7 +174,6 @@ void returnsExactlyGzipBody() throws Exception { * @throws Exception If there are problems */ @Test - @Disabled void compressesOverHttp() throws Exception { new FtRemote(new TkGzip(req -> new RsText("Hi, dude!"))).exec( home -> MatcherAssert.assertThat( diff --git a/src/test/java/org/takes/tk/TkProxyTest.java b/src/test/java/org/takes/tk/TkProxyTest.java index 2367d282f..6324c6fe5 100644 --- a/src/test/java/org/takes/tk/TkProxyTest.java +++ b/src/test/java/org/takes/tk/TkProxyTest.java @@ -40,8 +40,8 @@ import org.takes.rq.RqHref; import org.takes.rq.RqMethod; import org.takes.rq.RqPrint; -import org.takes.rs.BodyPrint; -import org.takes.rs.HeadPrint; +import org.takes.rs.RsBodyPrint; +import org.takes.rs.RsHeadPrint; import org.takes.rs.RsPrint; import org.takes.rs.RsText; @@ -110,7 +110,7 @@ void correctlyMapsPathString(final String method, final String expected) ).exec( home -> MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new TkProxy(home).act( new RqFake( method, @@ -141,7 +141,7 @@ void modifiesHost( // @checkstyle AnonInnerLengthCheck (100 lines) home -> MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new TkProxy(home.toURL().toString()).act( new RqFake( Arrays.asList( @@ -181,7 +181,7 @@ void addsSpecificHeader( ).exec( home -> MatcherAssert.assertThat( - new HeadPrint( + new RsHeadPrint( new TkProxy( home.toURL().toString(), mark @@ -220,7 +220,7 @@ void addsAllInitialHeaders() throws Exception { ).exec( home -> MatcherAssert.assertThat( - new BodyPrint( + new RsBodyPrint( new TkProxy(home).act( new RqFake( Arrays.asList(