Skip to content

Commit

Permalink
#1136 RsStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 5, 2022
1 parent b53b64d commit b6c88d2
Show file tree
Hide file tree
Showing 28 changed files with 146 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/main/java/org/takes/http/BkBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ private static Response failure(final Throwable err, final int code) {
* @param socket Socket
* @return Request with custom headers
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
private static Request addSocketHeaders(final Request req,
final Socket socket) {
return new RqWithHeaders(
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/org/takes/rs/RsStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2022 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 java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import org.takes.Response;

/**
* HTTP status of the Response.
*
* <p>All implementations of this interface must be immutable and thread-safe.
*
* @since 1.22
*/
public interface RsStatus extends Response {

/**
* Get status.
* @return Status
* @throws IOException If fails
*/
int status() throws IOException;

/**
* Request decorator, for HTTP URI query parsing.
*
* <p>The class is immutable and thread-safe.
*
* @since 1.22
*/
@EqualsAndHashCode(callSuper = true)
final class Base extends RsWrap implements RsStatus {

/**
* HTTP status line pattern.
*/
private static final Pattern LINE = Pattern.compile(
"HTTP/[0-9.]+\\s+([0-9]+)\\s+.*"
);

/**
* Ctor.
* @param res Original response
*/
public Base(final Response res) {
super(res);
}

@Override
public int status() throws IOException {
final String line = this.head().iterator().next();
final Matcher matcher = RsStatus.Base.LINE.matcher(line);
if (!matcher.matches()) {
throw new IllegalArgumentException(
String.format(
"Invalid status line: '%s'", line
)
);
}
return Integer.parseInt(matcher.group(1));
}
}
}

3 changes: 1 addition & 2 deletions src/main/java/org/takes/servlet/HttpServletRequestFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@
@SuppressWarnings({
"PMD.TooManyMethods",
"PMD.ExcessiveImports",
"PMD.ExcessivePublicCount",
"PMD.AvoidDuplicateLiterals"
"PMD.ExcessivePublicCount"
})
public final class HttpServletRequestFake implements HttpServletRequest {
/**
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/takes/tk/TkSslOnly.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public TkSslOnly(final Take take) {
}

@Override
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public Response act(final Request req) throws Exception {
final String href = new RqHref.Base(req).href().toString();
final String proto = new RqHeaders.Smart(
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/facets/auth/PsByFlagTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* Test case for {@link PsByFlag}.
* @since 0.10
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class PsByFlagTest {
final class PsByFlagTest {
/**
* PsByFlag can skip if nothing found.
* @throws Exception If some problem inside
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/facets/auth/codecs/CcAesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* Test case for {@link CcAes}.
* @since 0.13.8
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class CcAesTest {
final class CcAesTest {
/**
* CcAes can encrypt identity.
* @throws Exception If fails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* Test case for {@link SiHmac}.
* @since 1.3
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class SiHmacTest {
final class SiHmacTest {
/**
* SiHmac corrects wrong bit length.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/facets/fork/FkContentTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* Test case for {@link FkContentType}.
* @since 1.0
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class FkContentTypeTest {
final class FkContentTypeTest {

/**
* Content-Type header.
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/takes/facets/fork/FkRegexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
* Test case for {@link FkRegex}.
* @since 0.4
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class FkRegexTest {

/**
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/takes/facets/fork/TkConsumesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ void failsOnUnsupportedAcceptHeader() {
* Checks TkConsumes equals method.
*/
@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
void equalsAndHashCodeEqualTest() {
final Take take = new TkText("text");
final String type = "Content-Type: text/plain";
Expand Down
3 changes: 0 additions & 3 deletions src/test/java/org/takes/misc/HrefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
* Test case for {@link Href}.
* @since 0.7
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class HrefTest {

/**
Expand Down Expand Up @@ -147,7 +146,6 @@ void acceptsNonProperlyEncodedUrl() {
/**
* Href can build an URI with fragment.
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@Test
void buildsUriWithFragmentAndParams() {
MatcherAssert.assertThat(
Expand All @@ -159,7 +157,6 @@ void buildsUriWithFragmentAndParams() {
/**
* Href can build an URI with fragment and no params.
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@Test
void buildsUriWithFragmentAndNoParams() {
MatcherAssert.assertThat(
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/rq/RqFakeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* Test case for {@link RqFake}.
* @since 0.24
*/
@SuppressWarnings({"PMD.AvoidUsingHardCodedIP", "PMD.AvoidDuplicateLiterals"})
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
public final class RqFakeTest {

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/rq/RqHrefTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* Test case for {@link RqHref.Base}.
* @since 0.1
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RqHrefTest {
final class RqHrefTest {

/**
* RqHref.Base can parse a query.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/rq/RqSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* Test case for {@link RqSocket}.
* @since 1.0
*/
@SuppressWarnings({"PMD.AvoidUsingHardCodedIP", "PMD.AvoidDuplicateLiterals"})
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
final class RqSocketTest {

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/rq/RqWithHeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* Test case for {@link RqWithHeader}.
* @since 0.9
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RqWithHeaderTest {
final class RqWithHeaderTest {

/**
* RqWithHeader can add a header.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/rq/RqWithHeadersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* Test case for {@link RqWithHeaders}.
* @since 1.0
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RqWithHeadersTest {
final class RqWithHeadersTest {

/**
* RqWithHeaders can add headers.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/rq/multipart/RqMtFakeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* Test case for {@link RqMtFake}.
* @since 0.33
*/
@SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods"})
@SuppressWarnings("PMD.TooManyMethods")
final class RqMtFakeTest {
/**
* Format string for {@code Content-Length} header.
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/rs/RsBodyPrintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* Test case for {@link RsBodyPrint}.
* @since 1.19
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class RsBodyPrintTest {
final class RsBodyPrintTest {

@Test
void simple() throws IOException {
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/takes/rs/RsPrettyJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* Test case for {@link RsPrettyJson}.
* @since 1.0
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class RsPrettyJsonTest {

/**
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/takes/rs/RsPrintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* Test case for {@link RsPrint}.
* @since 1.19
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class RsPrintTest {

/**
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/takes/rs/RsStatusTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2022 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 java.io.IOException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link RsStatus}.
* @since 1.22
*/
final class RsStatusTest {

@Test
void readsStatusCode() throws IOException {
final int status = 200;
MatcherAssert.assertThat(
new RsStatus.Base(new RsWithStatus(new RsEmpty(), status)).status(),
Matchers.equalTo(status)
);
}
}
1 change: 0 additions & 1 deletion src/test/java/org/takes/rs/xe/XeWhenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* Test case for {@link XeWhen}.
* @since 0.13
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class XeWhenTest {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
*
* @since 1.14
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class HttpServletResponseFakeTest {
/**
* Set-Cookie header name.
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/takes/tk/TkGzipTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
* Test case for {@link TkGzip}.
* @since 0.17
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class TkGzipTest {

/**
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/takes/tk/TkHtmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
* Test case for {@link TkHtml}.
* @since 0.10
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class TkHtmlTest {

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/takes/tk/TkRedirectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* Test case for {@link TkRedirect}.
* @since 0.10
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals") final class TkRedirectTest {
final class TkRedirectTest {
/**
* Constant variable for HTTP header testing.
*/
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/takes/tk/TkSmartRedirectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
* Test case for {@link TkSmartRedirect}.
* @since 0.10
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class TkSmartRedirectTest {
/**
* TkRedirect should carry on the query and the fragment.
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/takes/tk/TkTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
* Test case for {@link TkText}.
* @since 0.4
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
final class TkTextTest {

/**
Expand Down

0 comments on commit b6c88d2

Please sign in to comment.