Skip to content

Commit

Permalink
yegor256#47 added logging level to cookie
Browse files Browse the repository at this point in the history
added unit test
  • Loading branch information
dmzaytsev committed Mar 26, 2015
1 parent 86026df commit e01f2b6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/main/java/org/takes/facets/flash/RsFlash.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
*/
package org.takes.facets.flash;

import java.nio.charset.Charset;
import java.util.logging.Level;
import javax.xml.bind.DatatypeConverter;
import lombok.EqualsAndHashCode;
import org.takes.rs.RsWithCookie;
import org.takes.rs.RsWrap;
Expand All @@ -32,7 +34,6 @@
* Forwarding response.
*
* <p>The class is immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.1
Expand Down Expand Up @@ -72,8 +73,17 @@ public RsFlash(final String msg, final Level level) {
* @param cookie Cookie name
*/
public RsFlash(final String msg, final Level level, final String cookie) {
super(new RsWithCookie(cookie, msg));
assert level != null;
super(
new RsWithCookie(
cookie,
DatatypeConverter.printBase64Binary(
new StringBuilder(level.getName())
.append('/')
.append(msg).toString()
.getBytes(Charset.defaultCharset())
)
)
);
}

}
74 changes: 74 additions & 0 deletions src/test/java/org/takes/facets/flash/RsFlashTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* 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.facets.flash;

import com.google.common.base.Joiner;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.logging.Level;
import javax.xml.bind.DatatypeConverter;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.takes.rs.RsPrint;

/**
* Test case for {@link RsFlash}.
* @author Dmitry Zaytsev (dmitry.zaytsev@gmail.com)
* @version $Id$
* @since 0.9.10
*/
public final class RsFlashTest {
/**
* RsFlash can make cookie with message.
* @throws IOException If some problem inside
*/
@Test
public void makeCookie() throws IOException {
final String msg = "message";
MatcherAssert.assertThat(
new RsPrint(
new RsFlash(msg, Level.INFO, RsFlashTest.class.getSimpleName())
).print(),
Matchers.equalTo(
Joiner.on("\r\n").join(
"HTTP/1.1 200 OK",
new StringBuilder("Set-Cookie: ")
.append(RsFlashTest.class.getSimpleName())
.append('=')
.append(
DatatypeConverter.printBase64Binary(
new StringBuilder(Level.INFO.getName())
.append('/')
.append(msg).toString()
.getBytes(Charset.defaultCharset())
)
),
"",
""
)
)
);
}
}

0 comments on commit e01f2b6

Please sign in to comment.