1
2
3
4
5 package com.jcabi.http.wire;
6
7 import com.jcabi.http.mock.MkAnswer;
8 import com.jcabi.http.mock.MkContainer;
9 import com.jcabi.http.mock.MkGrizzlyContainer;
10 import com.jcabi.http.mock.MkQuery;
11 import com.jcabi.http.request.JdkRequest;
12 import com.jcabi.http.response.RestResponse;
13 import jakarta.ws.rs.core.HttpHeaders;
14 import java.net.HttpURLConnection;
15 import org.hamcrest.MatcherAssert;
16 import org.hamcrest.Matchers;
17 import org.junit.jupiter.api.Test;
18
19
20
21
22
23 final class CookieOptimizingWireTest {
24
25
26
27
28
29 @Test
30 void transfersCookiesOnFollow() throws Exception {
31 final MkContainer container = new MkGrizzlyContainer().next(
32 new MkAnswer.Simple("")
33 .withHeader(HttpHeaders.SET_COOKIE, "beta=something; path=/")
34 .withHeader(HttpHeaders.SET_COOKIE, "alpha=boom1; path=/")
35 .withHeader(HttpHeaders.SET_COOKIE, "gamma=something; path=/")
36 .withHeader(HttpHeaders.LOCATION, "/")
37 ).next(new MkAnswer.Simple("")).start();
38 new JdkRequest(container.home())
39 .through(VerboseWire.class)
40 .through(CookieOptimizingWire.class)
41 .header(HttpHeaders.COOKIE, "alpha=boom5")
42 .fetch()
43 .as(RestResponse.class)
44 .follow()
45 .fetch().as(RestResponse.class)
46 .assertStatus(HttpURLConnection.HTTP_OK);
47 container.stop();
48 container.take();
49 final MkQuery query = container.take();
50 MatcherAssert.assertThat(
51 "should be size 1",
52 query.headers().get(HttpHeaders.COOKIE),
53 Matchers.hasSize(1)
54 );
55 MatcherAssert.assertThat(
56 "should contains 3 items",
57 query.headers(),
58 Matchers.hasEntry(
59 Matchers.equalTo(HttpHeaders.COOKIE),
60 Matchers.<String>everyItem(
61 Matchers.allOf(
62 Matchers.containsString("beta=something"),
63 Matchers.containsString("gamma=something"),
64 Matchers.containsString("alpha=boom1")
65 )
66 )
67 )
68 );
69 }
70
71
72
73
74
75 @Test
76 void avoidsTransferringOfEmptyCookiesOnFollow() throws Exception {
77 final MkContainer container = new MkGrizzlyContainer().next(
78 new MkAnswer.Simple("")
79 .withHeader(HttpHeaders.SET_COOKIE, "first=A; path=/")
80 .withHeader(HttpHeaders.SET_COOKIE, "second=; path=/")
81 .withHeader(HttpHeaders.SET_COOKIE, "third=B; path=/")
82 .withHeader(HttpHeaders.LOCATION, "/a")
83 ).next(new MkAnswer.Simple("")).start();
84 new JdkRequest(container.home())
85 .through(VerboseWire.class)
86 .through(CookieOptimizingWire.class)
87 .header(HttpHeaders.COOKIE, "second=initial-value")
88 .fetch()
89 .as(RestResponse.class)
90 .follow()
91 .fetch()
92 .as(RestResponse.class)
93 .assertStatus(HttpURLConnection.HTTP_OK);
94 container.stop();
95 container.take();
96 final MkQuery query = container.take();
97 MatcherAssert.assertThat(
98 "should be size 1",
99 query.headers().get(HttpHeaders.COOKIE),
100 Matchers.hasSize(1)
101 );
102 MatcherAssert.assertThat(
103 "should contains 2 items & not contains 1 item",
104 query.headers(),
105 Matchers.hasEntry(
106 Matchers.equalTo(HttpHeaders.COOKIE),
107 Matchers.hasItem(
108 Matchers.allOf(
109 Matchers.containsString("first=A"),
110 Matchers.containsString("third=B"),
111 Matchers.not(Matchers.containsString("second"))
112 )
113 )
114 )
115 );
116 }
117
118 }