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 java.net.HttpURLConnection;
39 import org.hamcrest.MatcherAssert;
40 import org.hamcrest.Matchers;
41 import org.junit.jupiter.api.Test;
42
43 /**
44 * Test case for {@link FcWire}.
45 * @since 1.0
46 */
47 final class FcWireTest {
48
49 /**
50 * FileCachingWire can cache GET requests.
51 * @throws Exception If something goes wrong inside
52 */
53 @Test
54 void cachesGetRequest() throws Exception {
55 final MkContainer container = new MkGrizzlyContainer().next(
56 new MkAnswer.Simple("")
57 ).start();
58 final Request req = new JdkRequest(container.home())
59 .through(FcWire.class);
60 for (int idx = 0; idx < 10; ++idx) {
61 req.fetch().as(RestResponse.class)
62 .assertStatus(HttpURLConnection.HTTP_OK);
63 }
64 container.stop();
65 MatcherAssert.assertThat(container.queries(), Matchers.equalTo(1));
66 }
67
68 /**
69 * CachingWire can ignore PUT requests.
70 * @throws Exception If something goes wrong inside
71 */
72 @Test
73 void ignoresPutRequest() throws Exception {
74 final MkContainer container = new MkGrizzlyContainer()
75 .next(new MkAnswer.Simple(""))
76 .next(new MkAnswer.Simple(""))
77 .start();
78 final Request req = new JdkRequest(container.home())
79 .through(FcWire.class).method(Request.PUT);
80 for (int idx = 0; idx < 2; ++idx) {
81 req.fetch().as(RestResponse.class)
82 .assertStatus(HttpURLConnection.HTTP_OK);
83 }
84 container.stop();
85 MatcherAssert.assertThat(container.queries(), Matchers.equalTo(2));
86 }
87
88 /**
89 * CachingWire can flush on regular expression match.
90 * @throws Exception If something goes wrong inside
91 */
92 @Test
93 void flushesOnRegularExpressionMatch() throws Exception {
94 final MkContainer container = new MkGrizzlyContainer()
95 .next(new MkAnswer.Simple("first response"))
96 .next(new MkAnswer.Simple("second response"))
97 .next(new MkAnswer.Simple("third response"))
98 .start();
99 final Request req = new JdkRequest(container.home())
100 .through(FcWire.class, "POST /flush\\?a=1");
101 req.fetch()
102 .as(RestResponse.class)
103 .assertBody(Matchers.containsString("first"));
104 req.fetch()
105 .as(RestResponse.class)
106 .assertBody(Matchers.containsString("first re"));
107 req.method(Request.POST).uri().path("flush")
108 .queryParam("a", "1").back().fetch();
109 req.fetch()
110 .as(RestResponse.class)
111 .assertBody(Matchers.containsString("third"));
112 container.stop();
113 MatcherAssert.assertThat(
114 container.queries(),
115 Matchers.equalTo(3)
116 );
117 }
118
119 }