Skip to content

Commit

Permalink
yegor256#37 change XeFlash to accept encoded cookie
Browse files Browse the repository at this point in the history
rewrite XeFlashTest  to test RsFlash and XeFlash together
  • Loading branch information
dmzaytsev committed Mar 29, 2015
1 parent d59b74f commit 379a1ff
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 32 deletions.
10 changes: 6 additions & 4 deletions src/main/java/org/takes/facets/flash/RsFlash.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
*/
package org.takes.facets.flash;

import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import javax.xml.bind.DatatypeConverter;
Expand Down Expand Up @@ -121,14 +122,15 @@ public RsFlash(final String msg, final Level level, final String cookie) {
new RsWithCookie(
cookie,
DatatypeConverter.printBase64Binary(
new StringBuilder(level.getName())
new StringBuilder(msg)
.append('/')
.append(msg)
.append(level.getName())
.toString()
.getBytes(StandardCharsets.UTF_8)
.getBytes(Charset.defaultCharset())
),
"Path=/",
String.format(
Locale.ENGLISH,
"Expires=%1$ta, %1$td %1$tb %1$tY %1$tT GMT",
new Date(
System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1L)
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/org/takes/facets/flash/XeFlash.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
package org.takes.facets.flash;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.DatatypeConverter;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.rq.RqCookies;
Expand All @@ -37,13 +40,18 @@
* Xembly source to show flash message in XML.
*
* <p>The class is immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.1
*/
@EqualsAndHashCode(of = { "req", "cookie" })
@EqualsAndHashCode(of = {"req", "cookie" })
public final class XeFlash implements XeSource {
/**
* Compiled RsFlash message regexp pattern.
*/
private static final Pattern RS_FLASH_MSG = Pattern.compile(
"^(.*?)/(.*?)$"
);

/**
* Request.
Expand Down Expand Up @@ -79,10 +87,17 @@ public Iterable<Directive> toXembly() throws IOException {
new RqCookies(this.req).cookie(this.cookie).iterator();
final Directives dirs = new Directives();
if (cookies.hasNext()) {
final String value = cookies.next();
dirs.add("flash")
.add("message").set(value).up()
.add("level").set(Level.INFO.toString());
final Matcher matcher = RS_FLASH_MSG.matcher(
new String(
DatatypeConverter.parseBase64Binary(cookies.next()),
Charset.defaultCharset()
)
);
if (matcher.find()) {
dirs.add("flash")
.add("message").set(matcher.group(1)).up()
.add("level").set(matcher.group(2));
}
}
return dirs;
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/takes/facets/flash/RsFlashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package org.takes.facets.flash;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.util.logging.Level;
import javax.xml.bind.DatatypeConverter;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -59,7 +59,7 @@ public void addsCookieToResponse() throws IOException {
.append('/')
.append(msg)
.toString()
.getBytes(StandardCharsets.UTF_8)
.getBytes(Charset.defaultCharset())
)
).toString()
)
Expand Down
54 changes: 35 additions & 19 deletions src/test/java/org/takes/facets/flash/XeFlashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

import com.jcabi.matchers.XhtmlMatchers;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
Expand All @@ -42,29 +46,41 @@
public final class XeFlashTest {

/**
* XeFlash can create a flash data.
* XeFlash can accept RsFlash cookie.
* @throws IOException If some problem inside
*/
@Test
public void generatesFlashData() throws IOException {
MatcherAssert.assertThat(
IOUtils.toString(
new RsXembly(
new XeAppend(
"root",
new XeFlash(
new RqWithHeader(
new RqFake(), "Cookie", "RsFlash=how are you"
public void acceptsRsFlashCookie() throws IOException {
final String msg = "how are you";
final Pattern pattern = Pattern.compile(
"^Set-Cookie: RsFlash=(.*?);Path.*"
);
final Iterator<String> itr = new RsFlash(msg, Level.FINE)
.head().iterator();
if (itr.hasNext()) {
final Matcher matcher = pattern.matcher(itr.next());
if (matcher.find()) {
MatcherAssert.assertThat(
IOUtils.toString(
new RsXembly(
new XeAppend(
"root",
new XeFlash(
new RqWithHeader(
new RqFake(),
"Cookie",
"RsFlash=".concat(matcher.group(1))
)
)
)
)
).body()
),
XhtmlMatchers.hasXPaths(
"/root/flash[message='how are you']",
"/root/flash[level='FINE']"
)
).body()
),
XhtmlMatchers.hasXPaths(
"/root/flash[message='how are you']",
"/root/flash[level='INFO']"
)
);
);
}
}
}

}

0 comments on commit 379a1ff

Please sign in to comment.