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.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
46
47
48
49 final class AutoRedirectingWireTest {
50
51
52
53
54
55
56
57 @Test
58 void retriesForHttpRedirectStatus() throws Exception {
59 final MkContainer container = new MkGrizzlyContainer().next(
60 new MkAnswer.Simple(HttpStatus.SC_MOVED_TEMPORARILY, "")
61
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
83
84
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 }