1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
45
46
47 final class FcWireTest {
48
49
50
51
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
70
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
90
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 }