Skip to content

Commit

Permalink
#828 TkJoinedCookies
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Mar 12, 2018
1 parent e863bb6 commit 7ee9651
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/main/java/org/takes/facets/cookies/TkJoinedCookies.java
@@ -0,0 +1,102 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014-2018 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.facets.cookies;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.rs.RsWithHeader;
import org.takes.rs.RsWithoutHeader;
import org.takes.tk.TkWrap;

/**
* Set-Cookie headers will be joined.
*
* <p>The class is immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.11
*/
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public final class TkJoinedCookies extends TkWrap {

/**
* Pattern to find them.
*/
private static final Pattern PTN = Pattern.compile(
"set-cookie: (.+)", Pattern.CASE_INSENSITIVE
);

/**
* Ctor.
* @param take The take to wrap
* @checkstyle AnonInnerLengthCheck (100 lines)
*/
public TkJoinedCookies(final Take take) {
super(
new Take() {
@Override
public Response act(final Request req) throws IOException {
return TkJoinedCookies.join(take.act(req));
}
}
);
}

/**
* Join them.
* @param response The response
* @return New response
* @throws IOException If fails
*/
private static Response join(final Response response) throws IOException {
final StringBuilder cookies = new StringBuilder();
for (final String header : response.head()) {
final Matcher matcher =
TkJoinedCookies.PTN.matcher(header);
if (!matcher.matches()) {
continue;
}
cookies.append(matcher.group(1)).append(", ");
}
final Response out;
if (cookies.length() > 0) {
out = new RsWithHeader(
new RsWithoutHeader(response, "Set-cookie"),
"Set-Cookie", cookies.toString()
);
} else {
out = response;
}
return out;
}

}
67 changes: 67 additions & 0 deletions src/test/java/org/takes/facets/cookies/TkJoinedCookiesTest.java
@@ -0,0 +1,67 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014-2018 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.facets.cookies;

import java.io.IOException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.takes.rq.RqFake;
import org.takes.rs.RsPrint;
import org.takes.rs.RsText;
import org.takes.rs.RsWithHeaders;
import org.takes.tk.TkFixed;

/**
* Test case for {@link TkJoinedCookies}.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.11
*/
public final class TkJoinedCookiesTest {

/**
* TkJoinedCookies can join cookies.
* @throws IOException If some problem inside
*/
@Test
public void joinsCookies() throws IOException {
MatcherAssert.assertThat(
new RsPrint(
new TkJoinedCookies(
new TkFixed(
new RsWithHeaders(
new RsText(),
"Set-Cookie: a=1",
"Set-cookie: b=1; Path=/"
)
)
).act(new RqFake())
).print(),
Matchers.containsString("Set-Cookie: a=1, b=1; Path=/")
);
}

}

0 comments on commit 7ee9651

Please sign in to comment.