View Javadoc
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 com.jcabi.http.mock.MkAnswer;
33  import com.jcabi.http.mock.MkContainer;
34  import com.jcabi.http.mock.MkGrizzlyContainer;
35  import com.jcabi.http.mock.MkQuery;
36  import com.jcabi.http.request.JdkRequest;
37  import com.jcabi.http.response.RestResponse;
38  import jakarta.ws.rs.core.HttpHeaders;
39  import org.apache.http.HttpStatus;
40  import org.hamcrest.MatcherAssert;
41  import org.hamcrest.Matchers;
42  import org.junit.jupiter.api.Test;
43  
44  /**
45   * Test case for {@link AutoRedirectingWire}.
46   *
47   * @since 1.7
48   */
49  final class AutoRedirectingWireTest {
50  
51      /**
52       * AutoRedirectingWire retries up to the specified number of times for
53       * HTTP Status 3xx responses.
54       *
55       * @throws Exception If something goes wrong inside
56       */
57      @Test
58      void retriesForHttpRedirectStatus() throws Exception {
59          final MkContainer container = new MkGrizzlyContainer().next(
60              new MkAnswer.Simple(HttpStatus.SC_MOVED_TEMPORARILY, "")
61                  // @checkstyle MultipleStringLiteralsCheck (1 line)
62                  .withHeader(HttpHeaders.LOCATION, "/"),
63              Matchers.any(MkQuery.class),
64              Integer.MAX_VALUE
65          ).start();
66          try {
67              final int retries = 3;
68              new JdkRequest(container.home())
69                  .through(AutoRedirectingWire.class, retries)
70                  .fetch().as(RestResponse.class)
71                  .assertStatus(HttpStatus.SC_MOVED_TEMPORARILY);
72              MatcherAssert.assertThat(
73                  container.takeAll(Matchers.any(MkAnswer.class)),
74                  Matchers.<MkQuery>iterableWithSize(retries)
75              );
76          } finally {
77              container.stop();
78          }
79      }
80  
81      /**
82       * AutoRedirectingWire will retry a few times and immediately return if
83       * a valid response is obtained.
84       * @throws Exception If something goes wrong inside
85       */
86      @Test
87      void returnsValidResponseAfterRetry() throws Exception {
88          final String body = "success";
89          final MkContainer container = new MkGrizzlyContainer().next(
90              new MkAnswer.Simple(HttpStatus.SC_MOVED_TEMPORARILY, "")
91                  .withHeader(HttpHeaders.LOCATION, "/"),
92              Matchers.any(MkQuery.class),
93              2
94          ).next(new MkAnswer.Simple(body)).start();
95          try {
96              new JdkRequest(container.home())
97                  .through(AutoRedirectingWire.class)
98                  .fetch().as(RestResponse.class)
99                  .assertBody(Matchers.is(body))
100                 .assertStatus(HttpStatus.SC_OK);
101             MatcherAssert.assertThat(
102                 container.takeAll(Matchers.any(MkAnswer.class)),
103                 Matchers.<MkQuery>iterableWithSize(3)
104             );
105         } finally {
106             container.stop();
107         }
108     }
109 
110 }