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.jcabi.http.Request;
8   import com.jcabi.http.mock.MkAnswer;
9   import com.jcabi.http.mock.MkContainer;
10  import com.jcabi.http.mock.MkGrizzlyContainer;
11  import com.jcabi.http.request.JdkRequest;
12  import com.jcabi.http.response.RestResponse;
13  import java.net.HttpURLConnection;
14  import org.hamcrest.MatcherAssert;
15  import org.hamcrest.Matchers;
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * Test case for {@link FcWire}.
20   * @since 1.0
21   */
22  final class FcWireTest {
23  
24      /**
25       * FileCachingWire can cache GET requests.
26       * @throws Exception If something goes wrong inside
27       */
28      @Test
29      void cachesGetRequest() throws Exception {
30          final MkContainer container = new MkGrizzlyContainer().next(
31              new MkAnswer.Simple("")
32          ).start();
33          final Request req = new JdkRequest(container.home())
34              .through(FcWire.class);
35          for (int idx = 0; idx < 10; ++idx) {
36              req.fetch().as(RestResponse.class)
37                  .assertStatus(HttpURLConnection.HTTP_OK);
38          }
39          container.stop();
40          MatcherAssert.assertThat("should be equal 1", container.queries(), Matchers.equalTo(1));
41      }
42  
43      /**
44       * CachingWire can ignore PUT requests.
45       * @throws Exception If something goes wrong inside
46       */
47      @Test
48      void ignoresPutRequest() throws Exception {
49          final MkContainer container = new MkGrizzlyContainer()
50              .next(new MkAnswer.Simple(""))
51              .next(new MkAnswer.Simple(""))
52              .start();
53          final Request req = new JdkRequest(container.home())
54              .through(FcWire.class).method(Request.PUT);
55          for (int idx = 0; idx < 2; ++idx) {
56              req.fetch().as(RestResponse.class)
57                  .assertStatus(HttpURLConnection.HTTP_OK);
58          }
59          container.stop();
60          MatcherAssert.assertThat("should be equal 2", container.queries(), Matchers.equalTo(2));
61      }
62  
63      /**
64       * CachingWire can flush on regular expression match.
65       * @throws Exception If something goes wrong inside
66       */
67      @Test
68      void flushesOnRegularExpressionMatch() throws Exception {
69          final MkContainer container = new MkGrizzlyContainer()
70              .next(new MkAnswer.Simple("first response"))
71              .next(new MkAnswer.Simple("second response"))
72              .next(new MkAnswer.Simple("third response"))
73              .start();
74          final Request req = new JdkRequest(container.home())
75              .through(FcWire.class, "POST /flush\\?a=1");
76          req.fetch()
77              .as(RestResponse.class)
78              .assertBody(Matchers.containsString("first"));
79          req.fetch()
80              .as(RestResponse.class)
81              .assertBody(Matchers.containsString("first re"));
82          req.method(Request.POST).uri().path("flush")
83              .queryParam("a", "1").back().fetch();
84          req.fetch()
85              .as(RestResponse.class)
86              .assertBody(Matchers.containsString("third"));
87          container.stop();
88          MatcherAssert.assertThat(
89              "should be equal 3",
90              container.queries(),
91              Matchers.equalTo(3)
92          );
93      }
94  
95  }