-
Notifications
You must be signed in to change notification settings - Fork 211
enhance TkHtml test coverage and readability #1487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eshabakhov
wants to merge
1
commit into
yegor256:master
Choose a base branch
from
eshabakhov:tkhtmltest-refactor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+235
−50
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2014-2025 Yegor Bugayenko | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| package org.takes.tk; | ||
|
|
||
| import org.cactoos.io.InputStreamOf; | ||
| import org.cactoos.text.Joined; | ||
| import org.hamcrest.MatcherAssert; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.llorllale.cactoos.matchers.IsText; | ||
| import org.takes.rq.RqFake; | ||
| import org.takes.rs.RsPrint; | ||
|
|
||
| /** | ||
| * Test case for {@link TkHtml with empty body}. | ||
| * @since 0.10 | ||
| */ | ||
| final class TkHtmlEmptyTest { | ||
|
|
||
| @Test | ||
| @DisplayName("Throw IllegalStateException when string body is null") | ||
| void failsOnNullInputString() { | ||
| final String body = null; | ||
| Assertions.assertThrows( | ||
| IllegalStateException.class, | ||
| () -> { | ||
| MatcherAssert.assertThat( | ||
| "Must reject null string body", | ||
| new RsPrint(new TkHtml(body).act(new RqFake())), | ||
| this.textMatcher(body) | ||
| ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Create valid HTTP response with empty body from empty string") | ||
| void createsEmptyTextResponseFromEmptyString() throws Exception { | ||
| final String body = ""; | ||
| MatcherAssert.assertThat( | ||
| "TkHtml must create valid HTTP response from empty string", | ||
| new RsPrint(new TkHtml(body).act(new RqFake())), | ||
| this.textMatcher(body) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Throw IllegalStateException when scalar supplier returns null") | ||
| void failsOnNullInputScalar() { | ||
| final String body = null; | ||
| Assertions.assertThrows( | ||
| IllegalStateException.class, | ||
| () -> { | ||
| MatcherAssert.assertThat( | ||
| "Must reject null scalar body", | ||
| new RsPrint(new TkHtml(() -> body).act(new RqFake())), | ||
| this.textMatcher(body) | ||
| ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Create valid HTTP response with empty body from empty scalar supplier") | ||
| void createsEmptyResponseFromEmptyScalar() throws Exception { | ||
| final String body = ""; | ||
| MatcherAssert.assertThat( | ||
| "TkHtml must create valid HTTP response from empty scalar", | ||
| new RsPrint(new TkHtml(() -> body).act(new RqFake())), | ||
| this.textMatcher(body) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Throw NullPointerException when byte array is null") | ||
| void failsOnNullInputByteArray() { | ||
| final String body = null; | ||
| Assertions.assertThrows( | ||
| NullPointerException.class, | ||
| () -> { | ||
| MatcherAssert.assertThat( | ||
| "Must reject null byte array body", | ||
| new RsPrint(new TkHtml(body.getBytes()).act(new RqFake())), | ||
| this.textMatcher(body) | ||
| ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Create valid HTTP response with empty body from empty byte array") | ||
| void createsEmptyResponseFromEmptyByteArray() throws Exception { | ||
| final String body = ""; | ||
| MatcherAssert.assertThat( | ||
| "TkHtml must handle empty byte arrays correctly", | ||
| new RsPrint(new TkHtml(body.getBytes()).act(new RqFake())), | ||
| this.textMatcher(body) | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Throw NullPointerException when input stream is null") | ||
| void failsOnNullInputStream() { | ||
| final String body = null; | ||
| Assertions.assertThrows( | ||
| NullPointerException.class, | ||
| () -> { | ||
| MatcherAssert.assertThat( | ||
| "Must reject null input stream body", | ||
| new RsPrint(new TkHtml(new InputStreamOf(body)).act(new RqFake())), | ||
| this.textMatcher(body) | ||
| ); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Create valid HTTP response with empty body from empty input stream") | ||
| void createsEmptyResponseFromEmptyInputStream() throws Exception { | ||
| final String body = ""; | ||
| MatcherAssert.assertThat( | ||
| "TkHtml must create valid HTTP response from empty input stream", | ||
| new RsPrint(new TkHtml(new InputStreamOf(body)).act(new RqFake())), | ||
| this.textMatcher(body) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * 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 | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.