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 package com.jcabi.http.wire;
31
32 import com.jcabi.http.mock.MkAnswer;
33 import com.jcabi.http.mock.MkContainer;
34 import com.jcabi.http.mock.MkGrizzlyContainer;
35 import com.jcabi.http.mock.MkQuery;
36 import com.jcabi.http.request.JdkRequest;
37 import com.jcabi.http.response.RestResponse;
38 import jakarta.ws.rs.core.HttpHeaders;
39 import java.net.HttpURLConnection;
40 import org.hamcrest.MatcherAssert;
41 import org.hamcrest.Matchers;
42 import org.junit.jupiter.api.Test;
43
44
45
46
47
48 final class CookieOptimizingWireTest {
49
50
51
52
53
54 @Test
55 void transfersCookiesOnFollow() throws Exception {
56 final MkContainer container = new MkGrizzlyContainer().next(
57 new MkAnswer.Simple("")
58 .withHeader(HttpHeaders.SET_COOKIE, "beta=something; path=/")
59 .withHeader(HttpHeaders.SET_COOKIE, "alpha=boom1; path=/")
60 .withHeader(HttpHeaders.SET_COOKIE, "gamma=something; path=/")
61 .withHeader(HttpHeaders.LOCATION, "/")
62 ).next(new MkAnswer.Simple("")).start();
63 new JdkRequest(container.home())
64 .through(VerboseWire.class)
65 .through(CookieOptimizingWire.class)
66 .header(HttpHeaders.COOKIE, "alpha=boom5")
67 .fetch()
68 .as(RestResponse.class)
69 .follow()
70 .fetch().as(RestResponse.class)
71 .assertStatus(HttpURLConnection.HTTP_OK);
72 container.stop();
73 container.take();
74 final MkQuery query = container.take();
75 MatcherAssert.assertThat(
76 query.headers().get(HttpHeaders.COOKIE),
77 Matchers.hasSize(1)
78 );
79 MatcherAssert.assertThat(
80 query.headers(),
81 Matchers.hasEntry(
82 Matchers.equalTo(HttpHeaders.COOKIE),
83 Matchers.<String>everyItem(
84 Matchers.allOf(
85 Matchers.containsString("beta=something"),
86 Matchers.containsString("gamma=something"),
87 Matchers.containsString("alpha=boom1")
88 )
89 )
90 )
91 );
92 }
93
94
95
96
97
98 @Test
99 void avoidsTransferringOfEmptyCookiesOnFollow() throws Exception {
100 final MkContainer container = new MkGrizzlyContainer().next(
101 new MkAnswer.Simple("")
102 .withHeader(HttpHeaders.SET_COOKIE, "first=A; path=/")
103 .withHeader(HttpHeaders.SET_COOKIE, "second=; path=/")
104 .withHeader(HttpHeaders.SET_COOKIE, "third=B; path=/")
105 .withHeader(HttpHeaders.LOCATION, "/a")
106 ).next(new MkAnswer.Simple("")).start();
107 new JdkRequest(container.home())
108 .through(VerboseWire.class)
109 .through(CookieOptimizingWire.class)
110 .header(HttpHeaders.COOKIE, "second=initial-value")
111 .fetch()
112 .as(RestResponse.class)
113 .follow()
114 .fetch()
115 .as(RestResponse.class)
116 .assertStatus(HttpURLConnection.HTTP_OK);
117 container.stop();
118 container.take();
119 final MkQuery query = container.take();
120 MatcherAssert.assertThat(
121 query.headers().get(HttpHeaders.COOKIE),
122 Matchers.hasSize(1)
123 );
124 MatcherAssert.assertThat(
125 query.headers(),
126 Matchers.hasEntry(
127 Matchers.equalTo(HttpHeaders.COOKIE),
128 Matchers.hasItem(
129 Matchers.allOf(
130 Matchers.containsString("first=A"),
131 Matchers.containsString("third=B"),
132 Matchers.not(Matchers.containsString("second"))
133 )
134 )
135 )
136 );
137 }
138
139 }