From 24e086d66c470ecc1ecd78fdaf8967badfee3002 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Tue, 23 Aug 2022 08:02:55 +0300 Subject: [PATCH] #1144 FkHost fixed --- .../java/org/takes/facets/fork/FkHost.java | 15 ++-- .../java/org/takes/facets/fork/FkWrap.java | 2 +- src/main/java/org/takes/rq/RqEmpty.java | 69 +++++++++++++++++++ .../org/takes/facets/fork/FkHostTest.java | 44 ++++++++++-- 4 files changed, 115 insertions(+), 15 deletions(-) create mode 100644 src/main/java/org/takes/rq/RqEmpty.java diff --git a/src/main/java/org/takes/facets/fork/FkHost.java b/src/main/java/org/takes/facets/fork/FkHost.java index 82fbe022c..8e027dc73 100644 --- a/src/main/java/org/takes/facets/fork/FkHost.java +++ b/src/main/java/org/takes/facets/fork/FkHost.java @@ -25,7 +25,6 @@ import lombok.EqualsAndHashCode; import org.cactoos.scalar.EqualsNullable; -import org.cactoos.scalar.Ternary; import org.cactoos.text.Lowered; import org.takes.Response; import org.takes.Take; @@ -69,13 +68,13 @@ public FkHost(final String host, final Take take) { private static Fork fork(final String host, final Take take) { return req -> { final String hst = new RqHeaders.Smart(req).single("host"); - return new Ternary>( - new EqualsNullable( - new Lowered(host), new Lowered(hst) - ), - new Opt.Single<>(take.act(req)), - new Opt.Empty<>() - ).value(); + final Opt ret; + if (new EqualsNullable(new Lowered(host), new Lowered(hst)).value()) { + ret = new Opt.Single<>(take.act(req)); + } else { + ret = new Opt.Empty<>(); + } + return ret; }; } diff --git a/src/main/java/org/takes/facets/fork/FkWrap.java b/src/main/java/org/takes/facets/fork/FkWrap.java index d3a2d33b1..6a156bcb3 100644 --- a/src/main/java/org/takes/facets/fork/FkWrap.java +++ b/src/main/java/org/takes/facets/fork/FkWrap.java @@ -34,7 +34,7 @@ *

The class is immutable and thread-safe. * * @since 0.13 - * @see org.takes.facets.fork.RsFork + * @see RsFork */ @EqualsAndHashCode public class FkWrap implements Fork { diff --git a/src/main/java/org/takes/rq/RqEmpty.java b/src/main/java/org/takes/rq/RqEmpty.java new file mode 100644 index 000000000..6e24f3ca2 --- /dev/null +++ b/src/main/java/org/takes/rq/RqEmpty.java @@ -0,0 +1,69 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2022 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.rq; + +import java.util.Arrays; +import lombok.EqualsAndHashCode; +import org.cactoos.io.InputStreamOf; + +/** + * Empty request, with only a head line (mostly for testing). + * + *

The class is immutable and thread-safe. + * + * @since 0.25 + */ +@EqualsAndHashCode(callSuper = true) +public final class RqEmpty extends RqWrap { + + /** + * Ctor. + */ + public RqEmpty() { + this("GET"); + } + + /** + * Ctor. + * @param method HTTP method + */ + public RqEmpty(final CharSequence method) { + this(method, "/ HTTP/1.1"); + } + + /** + * Ctor. + * @param method HTTP method + * @param query HTTP query + */ + public RqEmpty(final CharSequence method, final CharSequence query) { + super( + new RequestOf( + Arrays.asList(String.format("%s %s", method, query)), + new InputStreamOf("") + ) + ); + } + +} diff --git a/src/test/java/org/takes/facets/fork/FkHostTest.java b/src/test/java/org/takes/facets/fork/FkHostTest.java index f57b518fc..149bcab79 100644 --- a/src/test/java/org/takes/facets/fork/FkHostTest.java +++ b/src/test/java/org/takes/facets/fork/FkHostTest.java @@ -23,11 +23,19 @@ */ package org.takes.facets.fork; +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicBoolean; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.takes.HttpException; +import org.takes.rq.RqEmpty; import org.takes.rq.RqFake; +import org.takes.rq.RqWithHeader; +import org.takes.rs.RsEmpty; import org.takes.tk.TkEmpty; +import org.takes.tk.TkText; /** * Test case for {@link FkHost}. @@ -38,17 +46,41 @@ final class FkHostTest { @Test void matchesByHost() throws Exception { MatcherAssert.assertThat( - new FkHost("www.example.com", new TkEmpty()).route( - new RqFake("GET", "/hel?a=1") - ).has(), + new FkHost("www.foo.com", new TkText("boom")) + .route( + new RqWithHeader( + new RqEmpty(), + "Host: www.foo.com" + ) + ) + .has(), Matchers.is(true) ); + } + + @Test + void doesntMatchByHost() throws Exception { + final AtomicBoolean acted = new AtomicBoolean(); MatcherAssert.assertThat( - new FkHost("google.com", new TkEmpty()).route( - new RqFake("PUT", "/?test") - ).has(), + new FkHost( + "google.com", + req -> { + acted.set(true); + return new RsEmpty(); + } + ).route(new RqFake("PUT", "/?test")).has(), Matchers.is(false) ); + MatcherAssert.assertThat(acted.get(), Matchers.is(false)); + } + + @Test + void doesntMatchWithNoHost() { + Assertions.assertThrows( + HttpException.class, + () -> new FkHost("google.com", new TkEmpty()) + .route(new RqFake(Arrays.asList("GET / HTTP/1.1"), "body")) + ); } }