From 78a28c401527d992734f11aa6e3ab2e923348f33 Mon Sep 17 00:00:00 2001 From: Dmitry Zaystev Date: Mon, 11 May 2015 11:39:25 +0500 Subject: [PATCH] #276 RsWithStatus puzzle resolution --- src/main/java/org/takes/rs/RsWithStatus.java | 43 ++++++------ .../java/org/takes/rs/RsWithStatusTest.java | 65 +++++++++++++++++++ 2 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 src/test/java/org/takes/rs/RsWithStatusTest.java diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index 396d70523..5efdb90a1 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -26,14 +26,16 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; -import java.util.Collection; import java.util.Collections; -import java.util.LinkedList; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; +import java.util.regex.Pattern; import lombok.EqualsAndHashCode; import org.takes.Response; +import org.takes.misc.Concat; +import org.takes.misc.Condition; +import org.takes.misc.Select; /** * Response decorator, with status code. @@ -46,7 +48,12 @@ */ @EqualsAndHashCode(callSuper = true) public final class RsWithStatus extends RsWrap { - + /** + * Pattern for first header line. + */ + private static final Pattern FIRST = Pattern.compile( + "HTTP/1\\.1 \\d{3} [a-zA-Z ]+" + ); /** * Statuses and their reasons. */ @@ -94,11 +101,6 @@ public InputStream body() throws IOException { /** * Make head. - * @todo #160:30min/DEV To implement concatenation and selection with the - * conjunction Concat and Select class to get rid of List.add() - * between line 118 to line 127. That is, replacing the block using - * something like this: - * return new Concat(head,new Select(origin.head(),new FilterFirstCond())) * @param origin Original response * @param status Status * @param reason Reason @@ -117,17 +119,20 @@ private static Iterable head(final Response origin, ) ); } - final Collection head = new LinkedList(); - head.add(String.format("HTTP/1.1 %d %s", status, reason)); - boolean first = true; - for (final String hdr : origin.head()) { - if (first) { - first = false; - } else { - head.add(hdr); - } - } - return head; + return new Concat( + Collections.singletonList( + String.format("HTTP/1.1 %d %s", status, reason) + ), + new Select( + origin.head(), + new Condition() { + @Override + public boolean fits(final String element) { + return !RsWithStatus.FIRST.matcher(element).matches(); + } + } + ) + ); } /** diff --git a/src/test/java/org/takes/rs/RsWithStatusTest.java b/src/test/java/org/takes/rs/RsWithStatusTest.java new file mode 100644 index 000000000..80c8ae3b3 --- /dev/null +++ b/src/test/java/org/takes/rs/RsWithStatusTest.java @@ -0,0 +1,65 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.rs; + +import com.google.common.base.Joiner; +import java.io.IOException; +import java.net.HttpURLConnection; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link RsWithStatus}.. + * + *

The class is immutable and thread-safe. + * @author Dmitry Zaytsev (dmitry.zaytsev@gmail.com) + * @version $Id$ + * @since 0.16.9 + */ +public final class RsWithStatusTest { + /** + * RsWithStatus can add status. + * @throws IOException If some problem inside + */ + @Test + public void addsStatus() throws IOException { + MatcherAssert.assertThat( + new RsPrint( + new RsWithStatus( + new RsWithHeader("Host", "example.com"), + HttpURLConnection.HTTP_NOT_FOUND + ) + ).print(), + Matchers.equalTo( + Joiner.on("\r\n").join( + "HTTP/1.1 404 Not Found", + "Host: example.com", + "", + "" + ) + ) + ); + } +}