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 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
46
47
48 final class ETagCachingWireTest {
49
50
51
52
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
84
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 }