aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/test/java/com/yahoo/jdisc/http/CookieTestCase.java
blob: dab4f91f631b5fba47f90436ea5822c3a4a58c53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http;

import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;

/**
 * @author Simon Thoresen Hult
 * @author bjorncs
 */
public class CookieTestCase {

    @Test
    public void requireThatDefaultValuesAreSane() {
        Cookie cookie = new Cookie("foo", "bar");
        assertEquals("foo", cookie.getName());
        assertEquals("bar", cookie.getValue());
        assertEquals(null, cookie.getDomain());
        assertEquals(Integer.MIN_VALUE, cookie.getMaxAge(TimeUnit.SECONDS));
        assertEquals(null, cookie.getPath());
        assertEquals(false, cookie.isHttpOnly());
        assertEquals(false, cookie.isSecure());
    }

    @Test
    public void requireThatAccessorsWork() {
        final Cookie cookie = new Cookie();
        cookie.setName("foo");
        assertEquals("foo", cookie.getName());
        cookie.setName("bar");
        assertEquals("bar", cookie.getName());

        cookie.setValue("foo");
        assertEquals("foo", cookie.getValue());
        cookie.setValue("bar");
        assertEquals("bar", cookie.getValue());

        cookie.setDomain("foo");
        assertEquals("foo", cookie.getDomain());
        cookie.setDomain("bar");
        assertEquals("bar", cookie.getDomain());

        cookie.setPath("foo");
        assertEquals("foo", cookie.getPath());
        cookie.setPath("bar");
        assertEquals("bar", cookie.getPath());

        cookie.setMaxAge(69, TimeUnit.DAYS);
        assertEquals(69, cookie.getMaxAge(TimeUnit.DAYS));
        assertEquals(TimeUnit.DAYS.toHours(69), cookie.getMaxAge(TimeUnit.HOURS));

        cookie.setSecure(true);
        assertTrue(cookie.isSecure());
        cookie.setSecure(false);
        assertFalse(cookie.isSecure());

        cookie.setHttpOnly(true);
        assertTrue(cookie.isHttpOnly());
        cookie.setHttpOnly(false);
        assertFalse(cookie.isHttpOnly());
    }

    @Test
    public void requireThatCopyConstructorWorks() {
        final Cookie lhs = newSetCookie("foo");
        final Cookie rhs = new Cookie(lhs);
        assertEquals(rhs.getName(), rhs.getName());
        assertEquals(rhs.getValue(), rhs.getValue());
        assertEquals(rhs.getDomain(), rhs.getDomain());
        assertEquals(rhs.getPath(), rhs.getPath());
        assertEquals(rhs.getMaxAge(TimeUnit.MILLISECONDS), rhs.getMaxAge(TimeUnit.MILLISECONDS));
        assertEquals(rhs.isSecure(), rhs.isSecure());
        assertEquals(rhs.isHttpOnly(), rhs.isHttpOnly());
    }

    @Test
    public void requireThatHashCodeIsImplemented() {
        final Cookie cookie = newCookie("foo");
        assertFalse(cookie.hashCode() == new Cookie().hashCode());
        assertEquals(cookie.hashCode(), cookie.hashCode());
        assertEquals(cookie.hashCode(), new Cookie(cookie).hashCode());
    }

    @Test
    public void requireThatEqualsIsImplemented() {
        final Cookie cookie = newCookie("foo");
        assertFalse(cookie.equals(new Cookie()));
        assertEquals(cookie, cookie);
        assertEquals(cookie, new Cookie(cookie));
    }

    @Test
    public void requireThatCookieCanBeEncoded() {
        assertEncodeCookie(
                "foo.name=foo.value",
                Collections.singletonList(newCookie("foo")));
        assertEncodeCookie(
                "foo.name=foo.value;bar.name=bar.value",
                Arrays.asList(newCookie("foo"), newCookie("bar")));
    }

    @Test
    public void requireThatSetCookieCanBeEncoded() {
        assertEncodeSetCookie(
                Collections.singletonList("foo.name=foo.value;Path=path;Domain=domain;Secure;HttpOnly"),
                Collections.singletonList(newSetCookie("foo")));
    }

    @Test
    public void requireThatCookieCanBeDecoded() {
        final Cookie foo = new Cookie();
        foo.setName("foo.name");
        foo.setValue("foo.value");
        assertDecodeCookie(Collections.singletonList(newCookie("foo")), "foo.name=foo.value");

        final Cookie bar = new Cookie();
        bar.setName("bar.name");
        bar.setValue("bar.value");
        assertDecodeCookie(Arrays.asList(foo, bar),"foo.name=foo.value; bar.name=bar.value");
    }

    @Test
    @SuppressWarnings("deprecation")
    public void requireThatSetCookieCanBeDecoded() {
        final Cookie foo = new Cookie();
        foo.setName("foo.name");
        foo.setValue("foo.value");
        foo.setPath("path");
        foo.setDomain("domain");
        foo.setMaxAge(0, TimeUnit.SECONDS);
        foo.setSecure(true);
        foo.setHttpOnly(true);
        foo.setVersion(1);
        assertDecodeSetCookie(foo, "foo.name=foo.value;Max-Age=0;Path=path;Domain=domain;Secure;HTTPOnly;");

        final Cookie bar = new Cookie();
        bar.setName("bar.name");
        bar.setValue("bar.value");
        bar.setPath("path");
        bar.setDomain("domain");
        bar.setMaxAge(0, TimeUnit.SECONDS);
        bar.setVersion(1);
        assertDecodeSetCookie(bar, "bar.name=bar.value;Max-Age=0;Path=path;Domain=domain;");
    }

    @Test
    public void requireThatCookieDecoderWorksForGenericValidCookies() {
        Cookie.fromCookieHeader("Y=v=1&n=8es5opih9ljtk&l=og0_iedeh0qqvqqr/o&p=m2g2rs6012000000&r=pv&lg=en-US&intl=" +
                                   "us&np=1; T=z=h.nzPBhSP4PBVd5JqacVnIbNjU1NAY2TjYzNzVOTjYzNzM0Mj&a=YAE&sk=DAALShmNQ" +
                                   "vhoZV&ks=EAABsibvMK6ejwn0uUoS4rC9w--~E&d=c2wBTVRJeU13RXhPVEUwTURJNU9URTBNRFF6TlRJ" +
                                   "NU5nLS0BYQFZQUUBZwE1VkNHT0w3VUVDTklJVEdRR1FXT0pOSkhEQQFzY2lkAWNOUnZIbEc3ZHZoVHlWZ" +
                                   "0NoXzEwYkxhOVdzcy0Bb2sBWlcwLQF0aXABWUhwTmVDAXp6AWgubnpQQkE3RQ--");
    }

    @Test
    public void requireThatCookieDecoderWorksForYInvalidCookies() {
        Cookie.fromCookieHeader("Y=v=1&n=77nkr5t7o4nqn&l=og0_iedeh0qqvqqr/o&p=m2g2rs6012000000&r=pv&lg=en-US&intl=" +
                                   "us&np=1; T=z=05nzPB0NP4PBN/n0gwc1AWGNjU1NAY2TjYzNzVOTjYzNzM0Mj&a=QAE&sk=DAA4R2svo" +
                                   "osjIa&ks=EAAj3nBQFkN4ZmuhqFxJdNoaQ--~E&d=c2wBTVRJeU13RXhPVEUwTURJNU9URTBNRFF6TlRJ" +
                                   "NU5nLS0BYQFRQUUBZwE1VkNHT0w3VUVDTklJVEdRR1FXT0pOSkhEQQFzY2lkAUpPalRXOEVsUDZrR3RHT" +
                                   "VZkX29CWk53clJIQS0BdGlwAVlIcE5lQwF6egEwNW56UEJBN0U-");
    }

    @Test
    public void requireThatCookieDecoderWorksForYValidCookies() {
        Cookie.fromCookieHeader("Y=v=1&n=3767k6te5aj2s&l=1v4u3001uw2ys00q0rw0qrw34q0x5s3u/o&p=030vvit012000000&iz=" +
                                   "&r=pu&lg=en-US,it-IT,it&intl=it&np=1; T=z=m38yPBmLk3PBWvehTPBhBHYNU5OBjQ3NE5ONU5P" +
                                   "NDY0NzU0M0&a=IAE&sk=DAAAx5URYgbhQ6&ks=EAA4rTgdlAGeMQmdYeM_VehGg--~E&d=c2wBTWprNUF" +
                                   "UTXdNems1TWprNE16RXpNREl6TkRneAFhAUlBRQFnAUVJSlNMSzVRM1pWNVNLQVBNRkszQTRaWDZBAXNj" +
                                   "aWQBSUlyZW5paXp4NS4zTUZMMDVlSVhuMjZKYUcwLQFvawFaVzAtAWFsAW1hcmlvYXByZWFAeW1haWwuY" +
                                   "29tAXp6AW0zOHlQQkE3RQF0aXABaXRZOFRE");
    }

    @Test
    public void requireThatCookieDecoderWorksForGenericInvalidCookies() {
        Cookie.fromCookieHeader("Y=v=1&n=e92s5cq8qbs6h&l=3kdb0f.3@i126be10b.d4j/o&p=m1f2qgmb13000107&r=g5&lg=en-US" +
                                   "&intl=us; T=z=TXp3OBTrQ8OBFMcj3GBpFSyNk83TgY2MjMwN04zMDMw&a=YAE&sk=DAAVfaNwLeISrX" +
                                   "&ks=EAAOeNNgY8c5hV8YzPYmnrW7w--~E&d=c2wBTVRnd09RRXhOVFEzTURrME56UTMBYQFZQUUBZwFMQ" +
                                   "U5NT0Q2UjY2Q0I1STY0R0tKSUdVQVlRRQFvawFaVzAtAXRpcAFMTlRUdkMBenoBVFhwM09CQTdF&af=QU" +
                                   "FBQ0FDQURBd0FCMUNCOUFJQUJBQ0FEQU1IME1nTWhNbiZ0cz0xMzIzMjEwMTk1JnBzPVA1d3NYakh0aVk" +
                                   "2UDMuUGZ6WkdTT2ctLQ--");
    }

    private static void assertEncodeCookie(String expectedResult, List<Cookie> cookies) {
        assertThat(Cookie.toCookieHeader(cookies), equalTo(expectedResult));
    }

    private static void assertEncodeSetCookie(List<String> expectedResult, List<Cookie> cookies) {
        assertThat(Cookie.toSetCookieHeaderAll(cookies), containsInAnyOrder(expectedResult.toArray()));
    }

    private static void assertDecodeCookie(List<Cookie> expected, String toDecode) {
        assertThat(Cookie.fromCookieHeader(toDecode), containsInAnyOrder(expected.toArray()));
    }

    private static void assertDecodeSetCookie(final Cookie expected, String toDecode) {
        assertThat(Cookie.fromSetCookieHeader(toDecode), containsInAnyOrder(expected));
    }

    private static Cookie newCookie(final String name) {
        final Cookie cookie = new Cookie();
        cookie.setName(name + ".name");
        cookie.setValue(name + ".value");
        return cookie;
    }

    private static Cookie newSetCookie(String name) {
        final Cookie cookie = new Cookie();
        cookie.setName(name + ".name");
        cookie.setValue(name + ".value");
        cookie.setDomain("domain");
        cookie.setPath("path");
        cookie.setSecure(true);
        cookie.setHttpOnly(true);
        return cookie;
    }
}