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 ch.qos.logback.classic.Logger;
33 import ch.qos.logback.classic.spi.ILoggingEvent;
34 import ch.qos.logback.core.read.ListAppender;
35 import com.jcabi.http.mock.MkAnswer;
36 import com.jcabi.http.mock.MkContainer;
37 import com.jcabi.http.mock.MkGrizzlyContainer;
38 import com.jcabi.http.request.JdkRequest;
39 import com.jcabi.http.response.RestResponse;
40 import jakarta.ws.rs.core.UriBuilder;
41 import java.net.HttpURLConnection;
42 import java.net.URI;
43 import org.hamcrest.MatcherAssert;
44 import org.hamcrest.Matchers;
45 import org.junit.jupiter.api.Test;
46 import org.slf4j.LoggerFactory;
47
48
49
50
51
52
53 final class RetryWireTest {
54
55
56
57
58
59
60 @Test
61 void makesMultipleRequests() throws Exception {
62 final MkContainer container = new MkGrizzlyContainer()
63 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR))
64 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR))
65 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK))
66 .start();
67 new JdkRequest(container.home())
68 .through(RetryWire.class)
69 .fetch()
70 .as(RestResponse.class)
71 .assertStatus(HttpURLConnection.HTTP_OK);
72 container.stop();
73 }
74
75
76
77
78
79
80 @Test
81 void stripsUserInfoWhenLogging() throws Exception {
82 final Logger logger = (Logger) LoggerFactory.getLogger(RetryWire.class);
83 final ListAppender<ILoggingEvent> appender = new ListAppender<>();
84 appender.start();
85 logger.addAppender(appender);
86 try (MkContainer container = new MkGrizzlyContainer()
87 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_INTERNAL_ERROR))
88 .next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK))
89 .start()) {
90 final URI home = container.home();
91 new JdkRequest(UriBuilder.fromUri(home).userInfo("jeff:ffej").toString())
92 .through(RetryWire.class)
93 .fetch()
94 .as(RestResponse.class)
95 .assertStatus(HttpURLConnection.HTTP_OK);
96 MatcherAssert.assertThat(
97 appender.list,
98 Matchers.hasItem(
99 Matchers.hasProperty(
100 "message",
101 Matchers.containsString(
102 String.format("GET %s (auth: j***j)", home)
103 )
104 )
105 )
106 );
107 }
108 }
109
110 }