View Javadoc
1   /*
2    * Copyright (c) 2011-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Test case for {@link CookieOptimizingWire}.
46   * @since 1.0
47   */
48  final class CookieOptimizingWireTest {
49  
50      /**
51       * CookieOptimizingWire can transfer cookies.
52       * @throws Exception If something goes wrong inside
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       * CookieOptimizingWire can avoid transferring of empty cookies.
96       * @throws Exception If something goes wrong inside
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 }