Skip to content

Commit

Permalink
yegor256#276 RsWithStatus puzzle resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
dmzaytsev committed May 11, 2015
1 parent 31dfedd commit 78a28c4
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/main/java/org/takes/rs/RsWithStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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
Expand All @@ -117,17 +119,20 @@ private static Iterable<String> head(final Response origin,
)
);
}
final Collection<String> head = new LinkedList<String>();
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<String>(
Collections.singletonList(
String.format("HTTP/1.1 %d %s", status, reason)
),
new Select<String>(
origin.head(),
new Condition<String>() {
@Override
public boolean fits(final String element) {
return !RsWithStatus.FIRST.matcher(element).matches();
}
}
)
);
}

/**
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/org/takes/rs/RsWithStatusTest.java
Original file line number Diff line number Diff line change
@@ -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}..
*
* <p>The class is immutable and thread-safe.
* @author Dmitry Zaytsev ([email protected])
* @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",
"",
""
)
)
);
}
}

0 comments on commit 78a28c4

Please sign in to comment.