View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2025 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.jcabi.http.wire;
6   
7   import com.google.common.cache.CacheBuilder;
8   import com.google.common.cache.CacheLoader;
9   import com.google.common.cache.LoadingCache;
10  import com.jcabi.http.Request;
11  import com.jcabi.http.Response;
12  import com.jcabi.http.mock.MkAnswer;
13  import com.jcabi.http.mock.MkContainer;
14  import com.jcabi.http.mock.MkGrizzlyContainer;
15  import com.jcabi.http.request.JdkRequest;
16  import com.jcabi.http.response.RestResponse;
17  import java.net.HttpURLConnection;
18  import java.util.concurrent.Callable;
19  import org.hamcrest.MatcherAssert;
20  import org.hamcrest.Matchers;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Test case for {@link CachingWire}.
25   * @since 1.0
26   */
27  final class CachingWireTest {
28  
29      /**
30       * CachingWire can cache GET requests.
31       * @throws Exception If something goes wrong inside
32       */
33      @Test
34      void cachesGetRequest() throws Exception {
35          final MkContainer container = new MkGrizzlyContainer().next(
36              new MkAnswer.Simple("")
37          ).start();
38          final Request req = new JdkRequest(container.home())
39              .through(CachingWire.class);
40          for (int idx = 0; idx < 10; ++idx) {
41              req.fetch().as(RestResponse.class)
42                  .assertStatus(HttpURLConnection.HTTP_OK);
43          }
44          container.stop();
45          MatcherAssert.assertThat("should be equal 1", container.queries(), Matchers.equalTo(1));
46      }
47  
48      /**
49       * CachingWire can ignore PUT requests.
50       * @throws Exception If something goes wrong inside
51       */
52      @Test
53      void ignoresPutRequest() throws Exception {
54          final MkContainer container = new MkGrizzlyContainer()
55              .next(new MkAnswer.Simple(""))
56              .next(new MkAnswer.Simple(""))
57              .start();
58          final Request req = new JdkRequest(container.home())
59              .through(CachingWire.class).method(Request.PUT);
60          for (int idx = 0; idx < 2; ++idx) {
61              req.fetch().as(RestResponse.class)
62                  .assertStatus(HttpURLConnection.HTTP_OK);
63          }
64          container.stop();
65          MatcherAssert.assertThat("should be equal 1", container.queries(), Matchers.equalTo(2));
66      }
67  
68      /**
69       * CachingWire can flush on regular expression match.
70       * @throws Exception If something goes wrong inside
71       */
72      @Test
73      void flushesOnRegularExpressionMatch() throws Exception {
74          final MkContainer container = new MkGrizzlyContainer()
75              .next(new MkAnswer.Simple("first response"))
76              .next(new MkAnswer.Simple("second response"))
77              .next(new MkAnswer.Simple("third response"))
78              .start();
79          final Request req = new JdkRequest(container.home())
80              .through(CachingWire.class, "POST /flush\\?a=1");
81          req.fetch()
82              .as(RestResponse.class)
83              .assertBody(Matchers.containsString("first"));
84          req.fetch()
85              .as(RestResponse.class)
86              .assertBody(Matchers.containsString("first re"));
87          req.method(Request.POST).uri().path("flush")
88              .queryParam("a", "1").back().fetch();
89          req.fetch()
90              .as(RestResponse.class)
91              .assertBody(Matchers.containsString("third"));
92          container.stop();
93          MatcherAssert.assertThat(
94              "should be equal 3",
95              container.queries(),
96              Matchers.equalTo(3)
97          );
98      }
99  
100     /**
101      * CachingWire can use custom cache.
102      * @throws Exception If something goes wrong inside
103      */
104     @Test
105     void cachesGetRequestWithCustomCache() throws Exception {
106         final MkContainer container = new MkGrizzlyContainer().next(
107             new MkAnswer.Simple("")
108         ).next(
109             new MkAnswer.Simple(HttpURLConnection.HTTP_BAD_GATEWAY)
110         ).start();
111         final LoadingCache<Callable<Response>, Response> cache =
112             CacheBuilder
113                 .newBuilder()
114                 .build(
115                     new CacheLoader<Callable<Response>, Response>() {
116                         @Override
117                         public Response load(final Callable<Response> query)
118                             throws Exception {
119                             return query.call();
120                         }
121                     }
122                 );
123         final Request req = new JdkRequest(container.home())
124             .through(CachingWire.class, cache);
125         for (int idx = 0; idx < 10; ++idx) {
126             req.fetch().as(RestResponse.class)
127                 .assertStatus(HttpURLConnection.HTTP_OK);
128         }
129         container.stop();
130         MatcherAssert.assertThat("should be equal 1", container.queries(), Matchers.equalTo(1));
131     }
132 
133 }