Skip to content

Commit 1d39456

Browse files
committed
Merge branch '__rultor'
2 parents 255261e + 7fe9917 commit 1d39456

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2018 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.cactoos.http;
25+
26+
import java.util.Iterator;
27+
import org.cactoos.Input;
28+
import org.cactoos.Text;
29+
import org.cactoos.iterable.LengthOf;
30+
import org.cactoos.list.Mapped;
31+
import org.cactoos.map.MapEntry;
32+
import org.cactoos.map.MapEnvelope;
33+
import org.cactoos.map.MapOf;
34+
import org.cactoos.text.SplitText;
35+
36+
/**
37+
* Cookies.
38+
*
39+
* @author Vseslav Sekorin (vssekorin@gmail.com)
40+
* @version $Id$
41+
* @since 0.1
42+
* @todo #8:30min The implementation of method stream() can handle only
43+
* one Set-Cookie in a response. Fix HtHeaders so that a single key
44+
* may be mapped to one or more values (it is legal to receive more than one
45+
* Set-Cookie in a response).
46+
* @todo #8:30min The implementation of method stream() will break on
47+
* "flag-type" directives (`Secure`, `HttpOnly`). Fix HtHeaders so that
48+
* these directives are handled correctly.
49+
*/
50+
public final class HtCookies extends MapEnvelope<String, String> {
51+
52+
/**
53+
* Ctor.
54+
* @param rsp Response
55+
*/
56+
public HtCookies(final Input rsp) {
57+
super(() -> new MapOf<>(
58+
new Mapped<>(
59+
entry -> {
60+
final Iterable<Text> parts = new SplitText(entry, "=");
61+
if (new LengthOf(parts).intValue() != 2) {
62+
throw new IllegalArgumentException(
63+
"Incorrect HTTP Response cookie"
64+
);
65+
}
66+
final Iterator<Text> iter = parts.iterator();
67+
return new MapEntry<>(
68+
iter.next().asString(),
69+
iter.next().asString()
70+
);
71+
},
72+
new SplitText(new HtHeaders(rsp).get("set-cookie"), ";\\s+")
73+
)
74+
));
75+
}
76+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2018 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.cactoos.http;
25+
26+
import org.cactoos.io.InputOf;
27+
import org.cactoos.text.JoinedText;
28+
import org.hamcrest.MatcherAssert;
29+
import org.hamcrest.core.IsEqual;
30+
import org.junit.Test;
31+
32+
/**
33+
* Test case for {@link HtCookies}.
34+
*
35+
* @author Vseslav Sekorin (vssekorin@gmail.com)
36+
* @version $Id$
37+
* @since 0.1
38+
* @checkstyle JavadocMethodCheck (500 lines)
39+
*/
40+
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
41+
public final class HtCookiesTest {
42+
43+
@Test
44+
public void takesCookiesOfHttpResponse() {
45+
MatcherAssert.assertThat(
46+
new HtCookies(
47+
new HtHead(
48+
new InputOf(
49+
new JoinedText(
50+
"\r\n",
51+
"HTTP/1.1 200 OK",
52+
"Content-type: text/plain",
53+
"Set-Cookie: path=/; domain=.google.com",
54+
"",
55+
"Hello, dude!",
56+
"How are you?"
57+
)
58+
)
59+
)
60+
).get("domain"),
61+
new IsEqual<>(".google.com")
62+
);
63+
}
64+
65+
@Test(expected = IllegalArgumentException.class)
66+
public void incorrectHttpResponseCookie() {
67+
MatcherAssert.assertThat(
68+
new HtCookies(
69+
new HtHead(
70+
new InputOf(
71+
new JoinedText(
72+
"\r\n",
73+
"HTTP/1.1 200 OK",
74+
"Content-type: text/plain",
75+
"Set-Cookie: path=/; 123; domain=.google.com",
76+
"",
77+
"Hello, dude!",
78+
"How are you?"
79+
)
80+
)
81+
)
82+
).get("domain"),
83+
new IsEqual<>(".google.com")
84+
);
85+
}
86+
}

0 commit comments

Comments
 (0)