1
2
3
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 jakarta.ws.rs.core.HttpHeaders;
14 import java.io.IOException;
15 import java.net.HttpURLConnection;
16 import org.hamcrest.Matchers;
17 import org.junit.jupiter.api.Test;
18
19
20
21
22
23 final class ETagCachingWireTest {
24
25
26
27
28
29 @Test
30 void takesContentFromCache() throws IOException {
31 final String body = "sample content";
32 final MkContainer container = new MkGrizzlyContainer()
33 .next(
34 new MkAnswer.Simple(body)
35 .withHeader(HttpHeaders.ETAG, "3e25")
36 )
37 .next(
38 new MkAnswer.Simple("")
39 .withStatus(HttpURLConnection.HTTP_NOT_MODIFIED)
40 )
41 .start();
42 final Request req =
43 new JdkRequest(container.home()).through(ETagCachingWire.class);
44 req
45 .fetch()
46 .as(RestResponse.class)
47 .assertStatus(HttpURLConnection.HTTP_OK)
48 .assertBody(Matchers.equalTo(body));
49 req
50 .fetch()
51 .as(RestResponse.class)
52 .assertStatus(HttpURLConnection.HTTP_OK)
53 .assertBody(Matchers.equalTo(body));
54 container.stop();
55 }
56
57
58
59
60
61 @Test
62 void detectsContentModification() throws IOException {
63 final String before = "before change";
64 final String after = "after change";
65 final MkContainer container = new MkGrizzlyContainer()
66 .next(
67 new MkAnswer.Simple(before)
68 .withHeader(HttpHeaders.ETAG, "3e26")
69 )
70 .next(
71 new MkAnswer.Simple(after)
72 .withHeader(HttpHeaders.ETAG, "3e27")
73 )
74 .start();
75 final Request req =
76 new JdkRequest(container.home())
77 .through(ETagCachingWire.class);
78 req
79 .fetch()
80 .as(RestResponse.class)
81 .assertStatus(HttpURLConnection.HTTP_OK)
82 .assertBody(Matchers.equalTo(before));
83 req
84 .fetch()
85 .as(RestResponse.class)
86 .assertStatus(HttpURLConnection.HTTP_OK)
87 .assertBody(Matchers.equalTo(after));
88 container.stop();
89 }
90 }