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 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 * Test case for {@link RetryWire}.
50 *
51 * @since 1.2
52 */
53 final class RetryWireTest {
54
55 /**
56 * RetryWire can make a few requests before giving up.
57 *
58 * @throws Exception If something goes wrong inside
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 * RetryWire should strip user info when logging URL.
77 *
78 * @throws Exception If something goes wrong inside
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 }