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.Request;
33  import com.jcabi.http.mock.MkAnswer;
34  import com.jcabi.http.mock.MkContainer;
35  import com.jcabi.http.mock.MkGrizzlyContainer;
36  import com.jcabi.http.request.JdkRequest;
37  import com.jcabi.http.response.RestResponse;
38  import jakarta.ws.rs.core.HttpHeaders;
39  import java.io.IOException;
40  import java.net.HttpURLConnection;
41  import org.hamcrest.Matchers;
42  import org.junit.jupiter.api.Test;
43  
44  /**
45   * Test case for {@link ETagCachingWire}.
46   * @since 2.0
47   */
48  final class ETagCachingWireTest {
49  
50      /**
51       * ETagCachingWire can take content from cache.
52       * @throws IOException If something goes wrong inside
53       */
54      @Test
55      void takesContentFromCache() throws IOException {
56          final String body = "sample content";
57          final MkContainer container = new MkGrizzlyContainer()
58              .next(
59                  new MkAnswer.Simple(body)
60                  .withHeader(HttpHeaders.ETAG, "3e25")
61              )
62              .next(
63                  new MkAnswer.Simple("")
64                  .withStatus(HttpURLConnection.HTTP_NOT_MODIFIED)
65              )
66              .start();
67          final Request req =
68              new JdkRequest(container.home()).through(ETagCachingWire.class);
69          req
70              .fetch()
71              .as(RestResponse.class)
72              .assertStatus(HttpURLConnection.HTTP_OK)
73              .assertBody(Matchers.equalTo(body));
74          req
75              .fetch()
76              .as(RestResponse.class)
77              .assertStatus(HttpURLConnection.HTTP_OK)
78              .assertBody(Matchers.equalTo(body));
79          container.stop();
80      }
81  
82      /**
83       * ETagCachingWire can detect content modification.
84       * @throws IOException If something goes wrong inside
85       */
86      @Test
87      void detectsContentModification() throws IOException {
88          final String before = "before change";
89          final String after = "after change";
90          final MkContainer container = new MkGrizzlyContainer()
91              .next(
92                  new MkAnswer.Simple(before)
93                      .withHeader(HttpHeaders.ETAG, "3e26")
94              )
95              .next(
96                  new MkAnswer.Simple(after)
97                      .withHeader(HttpHeaders.ETAG, "3e27")
98              )
99              .start();
100         final Request req =
101             new JdkRequest(container.home())
102                 .through(ETagCachingWire.class);
103         req
104             .fetch()
105             .as(RestResponse.class)
106             .assertStatus(HttpURLConnection.HTTP_OK)
107             .assertBody(Matchers.equalTo(before));
108         req
109             .fetch()
110             .as(RestResponse.class)
111             .assertStatus(HttpURLConnection.HTTP_OK)
112             .assertBody(Matchers.equalTo(after));
113         container.stop();
114     }
115 }