Skip to content
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 130 additions & 52 deletions src/test/java/org/takes/tk/TkHtmlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,79 @@
*/
package org.takes.tk;

import java.io.InputStream;
import org.cactoos.io.InputStreamOf;
import org.cactoos.iterable.IterableOf;
import org.cactoos.text.Joined;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.llorllale.cactoos.matchers.HasString;
import org.llorllale.cactoos.matchers.IsText;
import org.takes.Take;
import org.takes.rq.RqFake;
import org.takes.rs.RsBodyPrint;
import org.takes.rs.RsPrint;

/**
* Test case for {@link TkHtml}.
* @since 0.10
*/
@SuppressWarnings("PMD.TooManyMethods")
final class TkHtmlTest {

@Test
void createsTextResponse() throws Exception {
final String body = "<html>hello, world!</html>";
/**
* Input Bodies for testing.
* @return The testing data
*/
static Iterable<Arguments> cases() {
return new IterableOf<>(
Arguments.arguments("<html>hello, world!</html>"),
Arguments.arguments("")
);
}

@ParameterizedTest
@MethodSource("cases")
void createsTextResponseFromInputString(final String body) throws Exception {
MatcherAssert.assertThat(
"TkHtml must create proper HTML response from string",
new RsPrint(new TkHtml(body).act(new RqFake())),
new IsText(
new Joined(
"\r\n",
"HTTP/1.1 200 OK",
String.format("Content-Length: %s", body.length()),
"Content-Type: text/html",
"",
body
)
)
this.textMatcher(body)
);
}

@Test
void createsTextResponseFromScalar() throws Exception {
final String body = "<html>hello, world!</html>";
@ParameterizedTest
@MethodSource("cases")
void createsTextResponseFromScalar(final String body) throws Exception {
MatcherAssert.assertThat(
"TkHtml must create proper HTML response from scalar supplier",
new RsPrint(new TkHtml(() -> body).act(new RqFake())),
new IsText(
new Joined(
"\r\n",
"HTTP/1.1 200 OK",
String.format("Content-Length: %s", body.length()),
"Content-Type: text/html",
"",
body
)
)
this.textMatcher(body)
);
}

@Test
void createsTextResponseFromByteArray() throws Exception {
final String body = "<html>hello, world!</html>";
@ParameterizedTest
@MethodSource("cases")
void createsTextResponseFromByteArray(final String body) throws Exception {
MatcherAssert.assertThat(
"TkHtml must create proper HTML response from byte array",
new RsPrint(new TkHtml(body.getBytes()).act(new RqFake())),
new IsText(
new Joined(
"\r\n",
"HTTP/1.1 200 OK",
String.format("Content-Length: %s", body.length()),
"Content-Type: text/html",
"",
body
)
)
this.textMatcher(body)
);
}

@Test
void createsTextResponseFromInputStream() throws Exception {
final String body = "<html>hello, world!</html>";
@ParameterizedTest
@MethodSource("cases")
void createsTextResponseFromInputStream(final String body) throws Exception {
MatcherAssert.assertThat(
"TkHtml must create proper HTML response from input stream",
new RsPrint(new TkHtml(new InputStreamOf(body)).act(new RqFake())),
new IsText(
new Joined(
"\r\n",
"HTTP/1.1 200 OK",
String.format("Content-Length: %s", body.length()),
"Content-Type: text/html",
"",
body
)
)
this.textMatcher(body)
);
}

Expand All @@ -112,4 +96,98 @@ void printsResourceMultipleTimes() throws Exception {
);
}

@Test
void startsTextResponseBodyWithHtmlTag() throws Exception {
final String body = "<html><body>Hello buddy!</body></html>";
MatcherAssert.assertThat(
"HTML response must start with <html> tag",
new RsBodyPrint(
new TkHtml(body).act(new RqFake())
).asString(),
Matchers.startsWith("<html>")
);
}

@Test
void endsTextResponseBodyWithHtmlTag() throws Exception {
final String body = "<html><body>Hello comrade!/body></html>";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MatcherAssert.assertThat(
"HTML response must end with </html> tag",
new RsBodyPrint(
new TkHtml(body).act(new RqFake())
).asString(),
Matchers.endsWith("</html>")
);
}

@Test
void failsOnNullInputString() {
final String body = null;
Assertions.assertThrows(
IllegalStateException.class,
() -> MatcherAssert.assertThat(
"Must reject null input string body",
new RsPrint(new TkHtml(body).act(new RqFake())),
this.textMatcher("Nothing to print")
)
);
}

@Test
void failsOnNullInputScalar() {
final String body = null;
Assertions.assertThrows(
IllegalStateException.class,
() -> MatcherAssert.assertThat(
"Must reject null input scalar body",
new RsPrint(new TkHtml(body).act(new RqFake())),
this.textMatcher("Unreachable text")
)
);
}

@Test
void failsOnNullInputByteArray() {
final byte[] body = null;
Assertions.assertThrows(
NullPointerException.class,
() -> MatcherAssert.assertThat(
"Must reject null input byte array body",
new RsPrint(new TkHtml(body).act(new RqFake())),
this.textMatcher("What should I print?")
)
);
}

@Test
void failsOnNullInputStream() throws Exception {
try (InputStream body = null) {
Assertions.assertThrows(
RuntimeException.class,
() -> MatcherAssert.assertThat(
"Must reject null input stream body",
new RsPrint(new TkHtml(body).act(new RqFake())),
this.textMatcher("Write your own version")
)
);
}
}

/**
* Creates text matcher for HTML response.
* @param body Response body
* @return Text matcher
*/
private IsText textMatcher(final String body) {
return new IsText(
new Joined(
"\r\n",
"HTTP/1.1 200 OK",
String.format("Content-Length: %s", body.getBytes().length),
"Content-Type: text/html",
"",
body
)
);
}
}
Loading