Skip to content

Commit

Permalink
#1144 FkHost fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Aug 23, 2022
1 parent 91d020c commit 24e086d
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 15 deletions.
15 changes: 7 additions & 8 deletions src/main/java/org/takes/facets/fork/FkHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Opt<Response>>(
new EqualsNullable(
new Lowered(host), new Lowered(hst)
),
new Opt.Single<>(take.act(req)),
new Opt.Empty<>()
).value();
final Opt<Response> 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;
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/fork/FkWrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* <p>The class is immutable and thread-safe.
*
* @since 0.13
* @see org.takes.facets.fork.RsFork
* @see RsFork
*/
@EqualsAndHashCode
public class FkWrap implements Fork {
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/org/takes/rq/RqEmpty.java
Original file line number Diff line number Diff line change
@@ -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).
*
* <p>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("")
)
);
}

}
44 changes: 38 additions & 6 deletions src/test/java/org/takes/facets/fork/FkHostTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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"))
);
}

}

0 comments on commit 24e086d

Please sign in to comment.