Skip to content

Commit

Permalink
Fix broken unit tests yegor256#958
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Sep 3, 2021
1 parent c92c1ec commit 898fe41
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
30 changes: 22 additions & 8 deletions src/main/java/org/takes/servlet/RqFrom.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
* @since 2.0
*/
final class RqFrom implements Request {

/**
* Host key.
*/
private static String HOST_KEY = "host";

/**
* Servlet request.
*/
Expand All @@ -58,17 +64,25 @@ public Iterable<String> head() {
final Collection<String> names = Collections.list(
this.sreq.getHeaderNames()
);
if (!names.stream().anyMatch("host"::equalsIgnoreCase)) {
if (names.stream().noneMatch(RqFrom.HOST_KEY::equalsIgnoreCase)) {
head.add(new HttpHost(this.sreq).toString());
}
names.forEach(
header -> head.add(
String.format(
"%s: %s",
header,
this.sreq.getHeader(header)
)
)
header -> {
final String cleanheader;
if (header.equals(RqFrom.HOST_KEY)) {
cleanheader = "Host";
} else {
cleanheader = header;
}
head.add(
String.format(
"%s: %s",
cleanheader,
this.sreq.getHeader(header)
)
);
}
);
head.add(
String.format(
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/takes/facets/hamcrest/HmHeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ void testMismatchMessage() {
MatcherAssert.assertThat(
description.toString(),
Matchers.equalTo(new StringBuilder()
.append("header was: equalToIgnoringCase")
.append("(\"content-type\") -> values: <[image/png]>")
.append("header was: a string equal to ")
.append("\"content-type\" ignoring case -> values: <[image/png]>")
.toString()
)
);
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/takes/servlet/RqFromTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,15 @@ void containsHostAndPortInHeader() throws Exception {

@Test
void containsContentInRequestBody() throws IOException {
final String method = "POST /content-test";
final String content = "My name is neo!";
MatcherAssert.assertThat(
"Can't add a body to servlet request",
new RqPrint(
new RqFrom(
new HttpServletRequestFake(
new RqFake(
new ListOf<>(RqFromTest.EOL),
new ListOf<>(method),
content
)
)
Expand Down

0 comments on commit 898fe41

Please sign in to comment.