Skip to content

Commit eb8bac4

Browse files
committed
add a CookieSpec literal class and some tests for cookie parsing end-to-end
1 parent 36e7480 commit eb8bac4

9 files changed

Lines changed: 155 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* add a convenience method for setting the content type
33
* add a common reference to popular mime types
44
* cache methods on Config were not returning the config for the builder pattern.
5+
* add a CookieSpecs const class for reference
56

67
## 3.11.05
78
* issue #383 some problems with relocated packages.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@
315315
<!-- Only a test dependency. Waiting on
316316
https://github.com/perwendel/spark/pull/1201 -->
317317
<exclude>2f9b8690-2b13-48ad-adc9-af1489d0a2f3</exclude>
318+
<exclude>dbc80e9d-e821-4669-a4e1-6a537b6ad365</exclude>
318319
</excludeVulnerabilityIds>
319320
</banVulnerable>
320321
</rules>

unirest-mocks/src/test/java/kong/tests/AssertTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void assertHeader() {
8282
expect.assertHeader("monster", "grover");
8383

8484
assertException(() -> expect.assertHeader("monster", "oscar"),
85-
"No invocation found with header [monster: oscar]\nFound:\nmonster: grover\n");
85+
"No invocation found with header [monster: oscar]\nFound:\nmonster: grover");
8686
}
8787

8888
@Test
@@ -92,7 +92,7 @@ void canSetHeaderExpectationOnExpects() {
9292
assertException(() -> client.verifyAll(),
9393
"A expectation was never invoked! GET http://basic\n" +
9494
"Headers:\n" +
95-
"monster: grover\n");
95+
"monster: grover");
9696

9797
Unirest.get(path).header("monster", "grover").asEmpty();
9898

unirest-mocks/src/test/java/kong/tests/MultipleExpectsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void canDifferenciateBetweenExpectations() {
4444
"A expectation was never invoked! GET http://basic\n" +
4545
"Headers:\n" +
4646
"monster: oscar\n" +
47-
"fruit: apple\n");
47+
"fruit: apple");
4848
}
4949

5050
@Test
@@ -64,7 +64,7 @@ void willPickTheBestMatchingExpectation() {
6464
assertException(() -> client.verifyAll(),
6565
"A expectation was never invoked! GET http://basic\n" +
6666
"Headers:\n" +
67-
"monster: grover\n");
67+
"monster: grover");
6868
}
6969

7070
@Test
@@ -87,7 +87,7 @@ void willPickTheBestMatchingQueryExpectation() {
8787
assertException(() -> client.verifyAll(),
8888
"A expectation was never invoked! GET http://basic\n" +
8989
"Params:\n" +
90-
"monster: grover\n");
90+
"monster: grover");
9191
}
9292

9393
@Test
@@ -110,6 +110,6 @@ void whenDuplicateExpectsExistUseTheLastOne() {
110110
assertException(() -> client.verifyAll(),
111111
"A expectation was never invoked! GET http://basic\n" +
112112
"Params:\n" +
113-
"monster: grover\n");
113+
"monster: grover");
114114
}
115115
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* The MIT License
3+
*
4+
* Copyright for portions of unirest-java are held by Kong Inc (c) 2013.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
package kong.unirest;
27+
28+
29+
/**
30+
* Standard cookie specifications supported by Unirest.
31+
*/
32+
public final class CookieSpecs {
33+
34+
/**
35+
* The default policy. This policy provides a higher degree of compatibility
36+
* with common cookie management of popular HTTP agents for non-standard
37+
* (Netscape style) cookies.
38+
*/
39+
public static final String DEFAULT = "default";
40+
41+
/**
42+
* The Netscape cookie draft compliant policy.
43+
*/
44+
public static final String NETSCAPE = "netscape";
45+
46+
/**
47+
* The RFC 6265 compliant policy (interoprability profile).
48+
*/
49+
public static final String STANDARD = "standard";
50+
51+
/**
52+
* The RFC 6265 compliant policy (strict profile)
53+
*/
54+
public static final String STANDARD_STRICT = "standard-strict";
55+
56+
/**
57+
* The policy that ignores cookies.
58+
*/
59+
public static final String IGNORE_COOKIES = "ignoreCookies";
60+
}

unirest/src/main/java/kong/unirest/Headers.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525

2626
package kong.unirest;
2727

28-
import java.util.ArrayList;
29-
import java.util.Collection;
30-
import java.util.List;
31-
import java.util.Objects;
28+
import java.util.*;
3229
import java.util.function.Supplier;
3330

3431
import static java.util.stream.Collectors.toList;
@@ -164,8 +161,8 @@ void remove(String key, String value) {
164161
*/
165162
@Override
166163
public String toString() {
167-
final StringBuilder sb = new StringBuilder();
168-
headers.forEach(header -> sb.append(header.getName()).append(": ").append(header.getValue()).append(System.lineSeparator()));
164+
final StringJoiner sb = new StringJoiner(System.lineSeparator());
165+
headers.forEach(header -> sb.add(header.toString()));
169166
return sb.toString();
170167
}
171168

@@ -228,5 +225,10 @@ public boolean equals(Object o) {
228225
public int hashCode() {
229226
return Objects.hash(name, value.get());
230227
}
228+
229+
@Override
230+
public String toString() {
231+
return String.format("%s: %s",getName(), getValue());
232+
}
231233
}
232234
}

unirest/src/test/java/BehaviorTests/CookieTest.java

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@
3131
import org.apache.http.client.config.CookieSpecs;
3232
import org.junit.jupiter.api.Test;
3333

34+
import java.time.LocalDateTime;
35+
import java.time.Month;
36+
import java.time.ZoneId;
37+
import java.time.ZonedDateTime;
38+
import java.time.format.DateTimeFormatter;
3439
import java.util.Arrays;
3540

41+
import static java.time.LocalDateTime.of;
3642
import static org.junit.jupiter.api.Assertions.*;
3743

3844
class CookieTest extends BddTest {
@@ -114,13 +120,7 @@ void canGetValuesWithBadCharacters() {
114120

115121
@Test
116122
void complicatedCookies(){
117-
javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie("color", "blue");
118-
cookie.setDomain("localhost");
119-
cookie.setPath("/get");
120-
cookie.setHttpOnly(true);
121-
cookie.setSecure(false);
122-
cookie.setMaxAge(42);
123-
MockServer.expectCookie(cookie);
123+
expectCoookie(42);
124124

125125
HttpResponse response = Unirest.get(MockServer.GET).asEmpty();
126126

@@ -131,7 +131,34 @@ void complicatedCookies(){
131131
assertTrue(back.isHttpOnly());
132132
assertFalse(back.isSecure());
133133
assertEquals(42, back.getMaxAge());
134+
}
135+
136+
@Test
137+
void cookieLifeCycle() {
138+
expectCoookie(42);
139+
HttpResponse r1 = Unirest.get(MockServer.GET).asObject(RequestCapture.class);
140+
assertNotNull(r1.getCookies().getNamed("color"));
134141

142+
MockServer.clearCookies();
143+
expectCoookie(0);
144+
145+
HttpResponse<RequestCapture> r2 = Unirest.get(MockServer.GET).asObject(RequestCapture.class);
146+
assertNotNull(r1.getCookies().getNamed("color"));
147+
r2.getBody().assertCookie("color", "blue");
148+
149+
MockServer.clearCookies();
150+
HttpResponse<RequestCapture> r3 = Unirest.get(MockServer.GET).asObject(RequestCapture.class);
151+
r3.getBody().assertNoCookie("color");
152+
}
153+
154+
private void expectCoookie(int expiry) {
155+
javax.servlet.http.Cookie cookie = new javax.servlet.http.Cookie("color", "blue");
156+
cookie.setDomain("localhost");
157+
cookie.setPath("/get");
158+
cookie.setHttpOnly(true);
159+
cookie.setSecure(false);
160+
cookie.setMaxAge(expiry);
161+
MockServer.expectCookie(cookie);
135162
}
136163

137164
@Test
@@ -197,4 +224,40 @@ void canSetDefaultCookieAsFullCookieObj() {
197224
.assertCookie("flavor", "snickerdoodle")
198225
.assertCookie("size", "large");
199226
}
227+
228+
@Test
229+
void stringCookieParsing() {
230+
MockServer.addResponseHeader("Set-Cookie", getCookieValue(of(2140, Month.APRIL, 2, 4, 20, 0).atZone(ZoneId.of("UTC"))));
231+
232+
HttpResponse r1 = Unirest.get(MockServer.GET).asObject(RequestCapture.class);
233+
assertNotNull(r1.getCookies().getNamed("color"));
234+
235+
MockServer.clearHeaders();
236+
MockServer.addResponseHeader("Set-Cookie", getCookieValue(of(1985, Month.APRIL, 2, 4, 20, 0).atZone(ZoneId.of("UTC"))));
237+
238+
239+
HttpResponse<RequestCapture> r2 = Unirest.get(MockServer.GET).asObject(RequestCapture.class);
240+
assertNotNull(r1.getCookies().getNamed("color"));
241+
r2.getBody().assertCookie("color", "blue");
242+
243+
MockServer.clearHeaders();
244+
HttpResponse<RequestCapture> r3 = Unirest.get(MockServer.GET).asObject(RequestCapture.class);
245+
r3.getBody().assertNoCookie("color");
246+
}
247+
248+
@Test
249+
void newAgeCookies() {
250+
Unirest.config().cookieSpec("standard");
251+
MockServer.addResponseHeader("Set-Cookie", "cookie_name=blah;Max-Age=86400;Expires=Wed, 9 Dec 2220 20:26:05 GMT;Path=/;Domain=localhost;HTTPOnly");
252+
HttpResponse response = Unirest.get(MockServer.GET).asObject(RequestCapture.class);
253+
assertNotNull(response.getCookies().getNamed("cookie_name"));
254+
HttpResponse<RequestCapture> r2 = Unirest.get(MockServer.GET).asObject(RequestCapture.class);
255+
r2.getBody().assertCookie("cookie_name","blah");
256+
257+
}
258+
259+
private String getCookieValue(ZonedDateTime dt) {
260+
String date = dt.format(DateTimeFormatter.ofPattern("EEE, dd-MMM-yyyy HH:mm:ss zzz"));
261+
return String.format("color=blue; Path=/get; Domain=localhost; Expires=%s; HttpOnly", date);
262+
}
200263
}

unirest/src/test/java/BehaviorTests/MockServer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,19 @@ public static void expectedPages(int expected) {
257257
pages = expected;
258258
}
259259

260+
public static void clearCookies(){
261+
cookies.clear();
262+
}
263+
260264
public static void expectCookie(String name, String value) {
261265
cookies.add(new Cookie(name, UrlEncoded.encodeString(value)));
262266
}
263267

264268
public static void expectCookie(Cookie cookie) {
265269
cookies.add(cookie);
266270
}
271+
272+
public static void clearHeaders() {
273+
responseHeaders.clear();
274+
}
267275
}

unirest/src/test/java/kong/unirest/HeadersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void toStringOverride() {
6767
String toString = h.toString();
6868
assertEquals("a: 1" + ls +
6969
"c: 3" + ls +
70-
"d: null" + ls, toString);
70+
"d: null", toString);
7171
}
7272

7373
@Test

0 commit comments

Comments
 (0)