Skip to content

Commit 24e086d

Browse files
committed
#1144 FkHost fixed
1 parent 91d020c commit 24e086d

File tree

4 files changed

+115
-15
lines changed

4 files changed

+115
-15
lines changed

src/main/java/org/takes/facets/fork/FkHost.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import lombok.EqualsAndHashCode;
2727
import org.cactoos.scalar.EqualsNullable;
28-
import org.cactoos.scalar.Ternary;
2928
import org.cactoos.text.Lowered;
3029
import org.takes.Response;
3130
import org.takes.Take;
@@ -69,13 +68,13 @@ public FkHost(final String host, final Take take) {
6968
private static Fork fork(final String host, final Take take) {
7069
return req -> {
7170
final String hst = new RqHeaders.Smart(req).single("host");
72-
return new Ternary<Opt<Response>>(
73-
new EqualsNullable(
74-
new Lowered(host), new Lowered(hst)
75-
),
76-
new Opt.Single<>(take.act(req)),
77-
new Opt.Empty<>()
78-
).value();
71+
final Opt<Response> ret;
72+
if (new EqualsNullable(new Lowered(host), new Lowered(hst)).value()) {
73+
ret = new Opt.Single<>(take.act(req));
74+
} else {
75+
ret = new Opt.Empty<>();
76+
}
77+
return ret;
7978
};
8079
}
8180

src/main/java/org/takes/facets/fork/FkWrap.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* <p>The class is immutable and thread-safe.
3535
*
3636
* @since 0.13
37-
* @see org.takes.facets.fork.RsFork
37+
* @see RsFork
3838
*/
3939
@EqualsAndHashCode
4040
public class FkWrap implements Fork {
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014-2022 Yegor Bugayenko
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.takes.rq;
25+
26+
import java.util.Arrays;
27+
import lombok.EqualsAndHashCode;
28+
import org.cactoos.io.InputStreamOf;
29+
30+
/**
31+
* Empty request, with only a head line (mostly for testing).
32+
*
33+
* <p>The class is immutable and thread-safe.
34+
*
35+
* @since 0.25
36+
*/
37+
@EqualsAndHashCode(callSuper = true)
38+
public final class RqEmpty extends RqWrap {
39+
40+
/**
41+
* Ctor.
42+
*/
43+
public RqEmpty() {
44+
this("GET");
45+
}
46+
47+
/**
48+
* Ctor.
49+
* @param method HTTP method
50+
*/
51+
public RqEmpty(final CharSequence method) {
52+
this(method, "/ HTTP/1.1");
53+
}
54+
55+
/**
56+
* Ctor.
57+
* @param method HTTP method
58+
* @param query HTTP query
59+
*/
60+
public RqEmpty(final CharSequence method, final CharSequence query) {
61+
super(
62+
new RequestOf(
63+
Arrays.asList(String.format("%s %s", method, query)),
64+
new InputStreamOf("")
65+
)
66+
);
67+
}
68+
69+
}

src/test/java/org/takes/facets/fork/FkHostTest.java

+38-6
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@
2323
*/
2424
package org.takes.facets.fork;
2525

26+
import java.util.Arrays;
27+
import java.util.concurrent.atomic.AtomicBoolean;
2628
import org.hamcrest.MatcherAssert;
2729
import org.hamcrest.Matchers;
30+
import org.junit.jupiter.api.Assertions;
2831
import org.junit.jupiter.api.Test;
32+
import org.takes.HttpException;
33+
import org.takes.rq.RqEmpty;
2934
import org.takes.rq.RqFake;
35+
import org.takes.rq.RqWithHeader;
36+
import org.takes.rs.RsEmpty;
3037
import org.takes.tk.TkEmpty;
38+
import org.takes.tk.TkText;
3139

3240
/**
3341
* Test case for {@link FkHost}.
@@ -38,17 +46,41 @@ final class FkHostTest {
3846
@Test
3947
void matchesByHost() throws Exception {
4048
MatcherAssert.assertThat(
41-
new FkHost("www.example.com", new TkEmpty()).route(
42-
new RqFake("GET", "/hel?a=1")
43-
).has(),
49+
new FkHost("www.foo.com", new TkText("boom"))
50+
.route(
51+
new RqWithHeader(
52+
new RqEmpty(),
53+
"Host: www.foo.com"
54+
)
55+
)
56+
.has(),
4457
Matchers.is(true)
4558
);
59+
}
60+
61+
@Test
62+
void doesntMatchByHost() throws Exception {
63+
final AtomicBoolean acted = new AtomicBoolean();
4664
MatcherAssert.assertThat(
47-
new FkHost("google.com", new TkEmpty()).route(
48-
new RqFake("PUT", "/?test")
49-
).has(),
65+
new FkHost(
66+
"google.com",
67+
req -> {
68+
acted.set(true);
69+
return new RsEmpty();
70+
}
71+
).route(new RqFake("PUT", "/?test")).has(),
5072
Matchers.is(false)
5173
);
74+
MatcherAssert.assertThat(acted.get(), Matchers.is(false));
75+
}
76+
77+
@Test
78+
void doesntMatchWithNoHost() {
79+
Assertions.assertThrows(
80+
HttpException.class,
81+
() -> new FkHost("google.com", new TkEmpty())
82+
.route(new RqFake(Arrays.asList("GET / HTTP/1.1"), "body"))
83+
);
5284
}
5385

5486
}

0 commit comments

Comments
 (0)